Telegram inline keyboard does not send callback for acknowledging alert

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 ?

Gah! I cracked this issue. I am not sure why it stopped working, but here’s what got it working for me:

I’m using commands and callbacks with the same data/command to reduce the number of automations.
The documentation on HomeAssistant use to say use

target: '{{ trigger.event.data.user_id }}'

but switching it to this after looking at logs did the trick (cause looking for a common ‘target’ to send the message to that’s present in both callback and command json).

target: '{{ trigger.event.data.chat_id }}'

Sooo I guess someone changed something in one of the releases and we missed it on breaking changes? I certainly did.

Here’s an example of how I’ve written one of many of my automations for my telegram bot:

  - alias: Telegram bot light menu command
    id: telegram-light-menu-cmd
    trigger:
    - event_type: telegram_callback
      platform: event
      event_data:
        data: /lights
    - event_type: telegram_command
      platform: event
      event_data:
        command: /lights
    action:
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.chat_id }}'
        callback_query_id: '{{ trigger.event.data.id }}'
        message: AZIZ! Your lighting options
        disable_notification: true
        inline_keyboard:
        - 🛋️ Lounge lamp:/lamplounge, 🪔 Entrance hall lamp:/lamphall
        - 🔪 Kitchen main light:/lightkitchenmain, 🗄️ Kitchen counter light:/lightkitchencounter
        - 🚪 Front door light:/lightfrontdoor, 🏡 Garden light:/lightgarden
        - 📶 Loft stairs:/lightstairs, 🌈 Landing LED:/lightled
        - 🔙 Back to options:/options
2 Likes

Fantastic! Thanks! Answers this

yer-welcome! :wink: