Oooohhhhhh…Just read this:
Oh, I see!!! Thank you very much! I knew I was missing something. I did it, but It did not change anything… I still can not send commands from my bot to my hassio… I do not even understand the architecture of this process… I issue manually command to my bot in telegram, how does the bot know where to send this command?
The bot knows where to send the command specifically because of that webhook URL. I believe the polling method, well, polls to see if there are any pending messages, but otherwise telegram will just hit that URL with the messages as they arrive.
ok… I have got the following stuff now… I guess that is why it does not work:
curl https://api.telegram.org/bot<MY_BOT_API_KEY>/getWebhookInfo | jq
{
"ok": true,
"result": {
"url": "https://<MY_DOMAIN>/api/telegram_webhooks",
"has_custom_certificate": false,
"pending_update_count": 12,
"last_error_date": 1587664784,
"last_error_message": "Bad Request: zlib error -3",
"max_connections": 40
}
}
and now I am totally lost (((
At least you can see that your button presses were registered – there are 12 pending messages that are still queued.
Found this:
https://www.gitmemory.com/issue/home-assistant/home-assistant/13993/483011580
Seems like it’s still an issue in 108.6
but then, how was @SteveDinn able to send /commands? Mystique…
Good news everyone.
I managed to get this working. Just change automation with this. I use pooling for telegram so you don’t need webhook.
- alias: "Acknowledge Telegram Alert"
initial_state: "on"
hide_entity: true
trigger:
- platform: event
event_type: telegram_callback
event_data:
data: "/bathroom_leak_ack"
action:
- service: telegram_bot.answer_callback_query
data_template:
callback_query_id: "{{ trigger.event.data.id }}"
message: "Alert stop"
- service: alert.turn_off
entity_id: alert.bathroom_leakage
To summarize, the difference is in the trigger, it’s not command
, but rather data
that needs to be under event_data
. This makes total sense, and I didn’t see the difference at all, even after looking at my configuration!
I think this
callback_query_id: "{{ trigger.event.data.id }}"
is important part here. If i remember correctly you need to initialize connection to bot first to be able to receive messages.
That part is actually only necessary to let Telegram know that the callback has been answered. It’s not necessary to allow Home Assistant to act on that callback. Even if you didn’t send that telegram_bot.answer_callback_query
, telegram would time it out after a while.
I don’t know. I first just changed command to data and that didn’t work. It started to work when i add telegram_bot.answer_callback_query.
But at the point where you’re about to send answer_callback_query, the event has already been received by home assistant. You can do whatever you want then. Literally anything can be in the actions part of that automation.
Anyway, regardless, as long as it’s working, that’s the main thing.
Cheers!
Ok, finally I fixed it!
@vladosam, thanks for your help, but when I tried your solution it still did not work for me… But now I know why…
I switched to polling… and after that, when I tried to acknowlege the alert from my telegram group again, using inline keyboard, hoping that @vladosam solution works, but it still didn’t…
However, now I could see some error in my home-assistant.log:
2020-04-24 00:39:15 ERROR (dispatcher) [homeassistant.components.telegram_bot] Incoming message is not allowed ({'id': 'XXXXXXXXXXXXXXXXXXX', 'chat_instance': '-xxxxxxxxxxxxxxxxxxx', 'message': {'message_id': xxxx, 'date': 1587677931, 'chat': {'id': -xxxxxxxxxxxxx, 'type': 'supergroup', 'title': 'HomeAssistant'}, 'text': '*ALERT:* BATHROOM LEAKAGE DETECTED!', 'entities': [], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': xxxxxxxxx, 'first_name': 'hassio', 'is_bot': True, 'username': 'my_ha_bot'}}, 'data': '/bathroom_leak_ack', 'from': {'id': <MY_USER_ID>, 'first_name': 'drew', 'is_bot': False, 'username': 'drew-kun', 'language_code':home-assistant.log
Ok, that’s something…
Fast google-fu led me here…
So I added the id from the home-assistant.log
error message to the list of allowed_chat_ids
of telegram_bot:telegram_bot:
- platform: polling
parse_mode: html
api_key: !secret telegram_hassio_token
allowed_chat_ids:
- !secret telegram_hassio_chatid
- <USER_CHAT_ID FROM ERROR IN THE LOG>
restarted HA and voila!
Now I can send bot commands from my telegram group to my hassio instance.
It seems that hassio indeed needs the user_id of a person in the telegram group in order to receive commands… The group’s id (the negative number) is obviously enough for sending messages, but not enough for receiving commands (maybe because multiple people in the group can send those commands, but not all of them necessary need to be allowed to do this - so it makes sense).
And speaking about webhook platform people say they fix same kind of problem adding url:
and trusted_networks
to telegram_bot: in configuration.yaml. Here is the reference
I will play with it a bit more, also will try adding the bot’s ID… and see if it is enough for all the users in the group to be able to send comands to hassio. Will leave the answers here for reference.
Thank you guys for your help!
UPDATE:
- It seems that adding bot’s ID to allowed_chat_ids does not make any difference, so there must be a non-bot user’s ID added (meaning the ID of the user within that Telegram group which will press the inline keyboard button to send command to hassio).
- now my working automation simply looks like this:
- alias: "Acknowledge Telegram Alert"
initial_state: "on"
trigger:
- platform: event
event_type: telegram_callback
event_data:
data: "/bathroom_leak_ack"
action:
- service: alert.turn_off
entity_id: alert.bathroom_leakage
It’s not the bot I’d that you need there, it’s the chat instance. Is that what you meant? I use multiple chat groups in my config for different kinds of notifications. That way I can configure telegram to use different sounds.
No, I mean this bot’s chat id (a piece from the log above):
Bot - not allowed to send messages to hassio (must use user’s chat_id instead for that purpose):
'from': {'id': xxxxxxxxx, 'first_name': 'hassio', 'is_bot': True, 'username': 'my_ha_bot'}},
Group (note minus sign before id) - only enough to send messages to telegram:
'chat': {'id': -xxxxxxxxxxxxx, 'type': 'supergroup', 'title': 'HomeAssistant'},
User in that group:
User in that group - this is what you want to whitelist in allowed_chat_ids
for each user to allow send commands with inline keyboard (it is not the bot sends command - it is a user in group, who pressed the inline keyboard button; that’s the logic…):
'from': {'id': <MY_USER_ID>, 'first_name': 'drew', 'is_bot': False, 'username': 'drew-kun', 'language_code':home-assistant.log}
Scott8586 confrms this here:
I hope it helps…
Hi Steven,
it has been a while. Did you find a solution for “inline_keyboard” buttons? I’m using an android device.
/commands work fine, but the buttons are not functional.
Thank you!
Best regards,
Axel
No, I didn’t because I stopped trying When the Home Assistant Android app came out, and I could use its actionable notifications, I started using that instead of trying to wedge my square requirements into Telegram’s round hole — er…so to speak.
I still use Telegram for some things, but no longer for any actionable notifications.
Thank you. I don’t want to have may HA accessible from outside. That’s why I like the Telegram approach.