This blueprint listens to a Telegram Command and replies with a text message using telegram_bot.send_message action. It is compatible with private and group chats, Telegram markdown and Home Assistant templates.
Command: REQUIRED command to listen for. Must start with ‘/’. Example: /start
Bot: Bot username. It is used when there is more than one bot in a group chat. Example: my_bot_username_bot
Title: Optional message’s title. Accepts markdown and templates.
Message: REQUIRED message’s body. Accepts markdown and templates.
Reply in private?: Whether the answer should be sent in private or group chat. This option has no effect when talking directly to the bot in a private chat.
Delay: Optional delay, in seconds, before sending the message.
Inline keyboard: Optional keyboard buttons showed below the message, separated by commas. Format: ‘Text:/command’. Example: Help:/help, Status:/status
(advanced feature) Whitelist and message if rejected: You can optionally set up a list of user ID’s or chat ID’s as a whitelist. If you do so, the command WILL ONLY REPLY IF BOTH whitelists are matched. You can leave one or both whitelists to omit them.Additionally, you can set up an optional message to send when the user or the chat are rejected by the whitelist. Example: - 123456789
Available variables for templating
Event data received from telegram are parsed as variables so that they can be easily used in templates. These variables can be used within this automation:
telegram_args: List of words sent after command. You can get each word by its index: telegram_args[0], telegram_args[1], etc.
telegram_args_as_text: Single plain text string from arguments’ list. Useful if you need full text, not single words.
telegram_chat_id: Chat id.
telegram_sender_first_name: Sender’s first name.
telegram_sender_last_name: Sender’s last name.
telegram_sender_full_name: Sender’s full name, as first_name + " " + last_name
telegram_sender_user_id: Sender’s user id.
Template example
Use format {{ variable_name }} to indicate that it is a template, as usual in Home Assistant.
Title:Hi {{ telegram_sender_first_name }}! Message:You said {{ telegram_args_as_text }} after the command
Full example of use
Aim: With command /toggle_living_light I want to toggle the living room light and send back a message with its current state:
Using this other blueprint, configure command /toggle_living_light to execute the desired action (in this case, toggle a light).
In the current blueprint: set the same command, a message, an optional title, a delay of 1-2 seconds and optional inline keyboard buttons. Delay is necessary to allow the first blueprint to finish and send the correct state when processing the message’s template.
Copy-paste the content from this link to a new file inside your configuration’s blueprints/automation folder.
Changelog
2023-04-13: NEW FEATURE Whitelist and message when rejected.
2022-09-03: Telegram API was updated and variables telegram_sender_first_name, telegram_sender_last_name, telegram_sender_full_name and telegram_sender_user_id are now available always. “Reply in private” function is restored back.
2022-05-18: BREAKING CHANGE Variables telegram_sender_first_name, telegram_sender_last_name, telegram_sender_full_name and telegram_sender_user_id are now only available if you call a command (when you type /command in a telegram text message). If you click an inline keyboard button, these variables will not work. “Reply in private” function will only work with commands too.
2022-01-17: BREAKING CHANGE Replaced “Command @ Bot Username” field with a more simple “Bot” username field. Now it is not necessary to write down the command twice because it uses trigger_variables to concatenate command and bot username. Also added trigger ID.
2021-10-28: Add field “Command @ Bot Username” to ensure compatibility in group chats. Also add “Reply in private?” boolean field and Chat ID variable. Improved description.
2021-10-11: Added optional delay field.
2021-10-11: Parse telegram arguments and other event data received by Home Assistant and allow to use them as variables in templates. Also updated blueprint’s description and added more inline keyboard rows.
“Command @ Bot Username” field has been replaced with a more simple “Bot” username field. Now, it is not necessary to write down the command twice because it uses trigger_variables to concatenate command and bot username.
Bot username is used when there is more than one bot in a group chat.
It is a new field, so it is necessary to set it up if you update the blueprint.
It seems that telegram bot API has changed, and some information is now not working. I just updated the blueprint to avoid using data that is causing the error.
This means that variables telegram_sender_first_name, telegram_sender_last_name, telegram_sender_full_name and telegram_sender_user_id are now only available if you call a command (when you type /command in a telegram text message). If you click an inline keyboard button, these variables will not work. “Reply in private” function will only work with commands too.
The telegram bot API has updated and variables telegram_sender_first_name, telegram_sender_last_name, telegram_sender_full_name and telegram_sender_user_id are back available always, with no errors! Also, the “Reply in private” functionality of this blueprint was restored.
To benefit from these updates, redownload the blueprint code!
Hello! Tell me if someone knows the problem. Why there is an error when using Telegram Bat integration and using Telegram Bot in Node red. If I disable telegram in Node red, the error disappears.
BlockquoteLogger: homeassistant.components.telegram_bot.polling
Source: components/telegram_bot/polling.py:34
Integration: Telegram bot (documentation, issues)
First occurred: 15:02:04 (49 occurrences)
Last logged: 15:25:33
Update “None” caused error: “Conflict: terminated by other getUpdates request; make sure that only one bot instance is running”
I introduce you to a new feature: the whitelist. This allows us to restrict which users and/or chat IDs can activate the command. If they are rejected by the whitelist, it is also possible to set up a message to inform them.
The update is fully compatible with the previous configuration of the blueprint, so there should be no problem when updating.
Why would I need that?
I added this feature because I wanted to create a Telegram Bot to control all my house and share it with all the housemates, but I wanted to restrict the control of each bedroom to its owner. With this new feature, it’s possible to limit some commands to certain users by setting up their user ID or chat ID in the whitelist.
To benefit from this update, just redownload the blueprint code!
I can’t get the whitelist to work.
If it’s empty, I get the expected responses.
If I put anything in there, I get a response that indicates I was rejected. I tried my user id number, and just to be sure, tried putting my user name. I then used the reject message to spit back my sender user id, and the contents of the whitelist. They match, so I am certain I’m not making mistakes there.
No matter what I do, it’s acting like I’m rejected.
Thanks for your comment. With your help, I just realized that the documentation is not clear. To make it work, you have to define it as a list (with a hyphen at the beginning of each line, even if there is only one value):
- 123456789
- -123456789
This is to allow multiple users/chat to execute that action. In the example above, it would allow the chat ID 123456789 and the group ID -123456789.
I’ll try to explain it better or find a better way to define it.
While using the keyboard field, which works fine so far, i want to increase usability with organizing the buttons. How can i manage a “new line”, i tried several commands but without success.
Hi @RPhilbo , I’m glad you like the blueprint and find it useful!
Do you mean the inline_keyboard? In the blueprint, I prepared various fields for different lines of buttons. Within each line, you can define multiple buttons separated by commas.
Here there is an example I use in my telegram bot:
BTW, in case it is useful to anyone, to add emoji to messages and buttons I open Telegram Desktop, I send the emoji I need to a private message, and then I copy-paste it to Home Assistant. Some emojis are not supported by the web browser (such as the Fan emoji in the example above), but they will show up well in Telegram.
The command is not recognized (I text “/test” answer is “I dont understand “#test””). Even when I start the action directly I do not receive any message. My feeling is that the bot name might be an issue because when I use a simple automation “telegram_bot.send_message” it works with my configuration file.
I checked my Home Assistant instances and this blueprint is working fine for me. I also updated all the components to the last version, but there weren’t any recent changes to the Telegram integration that could affect this blueprint. Maybe you are having an issue with your telegram bot? I don’t know.
What I detect wrong in your screenshot is that the bot name shouldn’t start with @, but the name directly (such in “ioBroker”).
Also, I don’t understand why you get an answer saying “I dont understand “#test””… If an unknown command is received, the telegram bot wouldn’t respond at all because no automation would be triggered. Maybe you set up an automation to notify users when an unknown command is received?