Setting up HASS to receive commands form Telegram

By following tutorials I have been able to work out how to push messages to Telegram but how do I get HASS to respond to messages from Telegram? Is it possible?

Yes, this is very much possible.

There are two main ways that HA can have bidirectional actions with telegram.

The first is directly responding to unsolicited commands. IE you type a command into telegram and HA acts on it.
The second is with HA sending a TG message that has built in (in-line) buttons in the message, that you can click in the telegram client, to tell HA to do something.

For the first way, To send an unsolicited command from telegram, you send a message starting with a slash, such as /command. When received by HA it will fire a telegram_command event on the event bus with event_data.

This is an example automation I have that reacts to the command “/testme”:

- id: 'telegramtestcallback'
  alias: 'Telegram callback to test notifications'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/testme'
  action:
  - event: test_telegram_bot_notify

This type of event is further explained in the “event-triggering” section of the docs:
https://www.home-assistant.io/integrations/telegram_bot/#event-triggering

The second method, of having in-line buttons that can be clicked is a bit more complicated.
When you send a message from HA, it has the normal title and message fields, but you can also specify a data component of the message. From here, you can specify the inline buttons you want along with the commands they will trigger when pushed.

In my previous example, if you send the “testme” command, it will then use the automation below to send a message back with 3 buttons:

- id: 'telegramtesttrigger'
  alias: 'Telegram bot test sequence'
  trigger:
    - platform: event
      event_type: test_telegram_bot_notify
  condition: []
  action:
  - service: notify.tg_notification_chat
    data_template:
      title: "From Home-Assistant:"
      message: "The is a test of the laundry notification system.\nChoose a notification to test:"
      data:
          inline_keyboard:
          - 'Dryer:/testdryer, Washer:/testwasher'
          - 'Cancel:/removekeyboard'

That looks like this, inside telegram:

When you click one of those buttons, it will fire an event with the command you specified. However this event will be a “telegram_callback” event. In this case, there’s another part of the event data that you may want to pay attention to, and that’s the id, since that will be unique to the specific message you responded to.
This is important because you can edit previous messages, and the id is used to identify which message you responded to. For example, if you click the “cancel” button in my example above, it will edit the message to remove the inline buttons. There’s a lot more you can do, but this is a basic example.
The automation below is what get’s called when you click the “cancel” button from above:

- id: 'telegramremoveinline'
  alias: 'Telegram callback to remove keyboard'
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/removekeyboard'
  action:
  - service: telegram_bot.answer_callback_query
    data_template:
      callback_query_id: '{{ trigger.event.data.id }}'
      message: 'OK, dismissing notification'
  - service: telegram_bot.edit_replymarkup
    data_template:
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: '{{ trigger.event.data.chat_id }}'
      inline_keyboard: []

This code may not be the best or most optimized since I hobbled it together from the docs and a few examples on this forum, but it works fine for my needs.

For more of the second type of commands (inline buttons and such) search this forum for “telegram actionable buttons” to get more topics with possibly better examples.

4 Likes

Thanks @JWilson for your help, I am however still missing something.

My Telegram is set up in the configuration.yaml file as follows:

telegram_bot:
  - platform: polling
    api_key: !secret telegram_api_key
    allowed_chat_ids:
      - !secret telegram_chat_id1

notify:
  - platform: telegram
    name: hcourt
    chat_id: !secret telegram_chat_id1

From the Developer Tools I can go to services, select “Telegram bot: Send message” , enter a message and Telegram recieves it.

In automations.yaml I have:

- id: telegramtestcallback
  alias: Telegram callback to test notifications
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /testme
  action:
  - service: telegram_bot.send_message
    data:
      message: Test message received.
  mode: single

But when I type /testme into Telegram, I do not get the “Test message received” message.

I think I am getting closer. I changed the automation to the following

- id: telegramtestcallback
  alias: Telegram callback to test notifications
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /testme
  action:
  - service: notify.hcourt
    data:
      message: Hcourt test message
  mode: single

And now when I run the automation (via the developer tools) I get the test message, “Hcourt test message”.

However, when I enter /testme into the group chat, nothing happens.

Where is test_telegram_bot_notify defined?

Got it to work but had to use a Node-Red implementation: A guide to using Telegram with Node-red and Home-Assistant | The Smarthome Book

Hello guys. Is it posible to send a command from telegram to start a service in homeassistant ?
My problem is: i use cloudflare add-on to acces HA from outside the network, and from time to time because of power shortages the cloudflare add-on is not starting and im locked out of HA.
I am trying to send a msg command with the telegram bot to start the cloudflare add-on.
Is this possible ?
Thank you

Hi, I am guessing that you are running cloudflare in a docker container. If so, add the following run parameter:

docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token 2348yvrehshflsdhfhpihfwp48p --restart unless-stopped

You can find more details here: https://docs.docker.com/config/containers/start-containers-automatically/

In short this will mean unless you explicitly stopped the container beforehand, that it will automatically restart when docker starts ap again (after say a powerfailure).