Telegram inline keyboard does not send callback for acknowledging alert

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!

1 Like

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 :slight_smile: 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.

1 Like

Thank you. I don’t want to have may HA accessible from outside. That’s why I like the Telegram approach.

Doesn’t each telegram callback have to…er…call back into your HA instance? Does this not necessitate it being accessible from outside anyway?

I meant there is no accesible HA URL from outside. VPN is necessary. HA can access the internet.

Yeah, but a telegram callback requires a way to get back to your HA system somehow. Maybe I don’t understand your use case.

There is definetly no need to expose HA for telegram callback. It works without.

https://www.home-assistant.io/integrations/telegram_polling/

Right…I was using the webhook method…I completely forgot that it could work with polling as well!

Hey Axel - I have been playing around and think I have worked out the inline_keyboard problem. I found another post saying that inline keyboard responses need to be treated as callback queries not commands, where all other inputs are direct commands.

Anyway I then used this event node - Which when I dragged it on the sheet was a Callback query
image


I then paired this to a switch node that was listening for the /command (/disarm in my case) which then went to a home assistant call service node to turn off the alarm.
Works perfectly (except the button doesnt show on the lock screen until I have unlocked the phone). Hope that helps?

1 Like

Hi Neil, works like a charm. Thank you very much! :partying_face: :pray:

1 Like

hey @brix29

whats your modified code after implementing the changes from @neilma ?