Telegram bot notifications and communication

Hey all,

awesome idea and setup. Everything’s working like a charm except one thing: removing the inline-keyboard doesn’t work. By clicking on a reminder- or the remove-the-keyboard-button I’d expect the keyboard to go away. But everything I get is the notification “Ok, I’ll notify you in 30 minutes”. The keyboard is still there.

Did I do something wrong? Here is my automation.yaml:

 - id: 'telegramremoveinline'
  alias: 'Telegram callback to remove keyboard'
  hide_entity: true
  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'
  - service: telegram_bot.edit_replymarkup
    data_template:
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: '{{ trigger.event.data.user_id }}'
      inline_keyboard: []

Any ideas?

Solved it by myself: I had to definite exact chat_id:

- service: telegram_bot.edit_replymarkup
    data_template:
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: HERE GOES THE CHATID WITHOUT QUOTATION MARKS
      inline_keyboard: []

instead of:

- service: telegram_bot.edit_replymarkup
    data_template:
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: '{{ trigger.event.data.user_id }}'
      inline_keyboard: []

Otherwise the wrong chat-id has been addressed.
Hopefully this helps someone some day. :slight_smile:

Cheers

1 Like

Hey! Glad it could be of use!

Note that the telegramremoveinline function in my automations is only used when you press the 'No reminder:/removekeyboard' button. The same actions have to be replicated onto the automations that handle all the timed buttons as well:

- id: 'telegramrepeat1h'
  alias: 'Telegram callback to repeat message in 1 hour'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/1h'
  action:
  CHOPPED
  - service: telegram_bot.edit_replymarkup
    data_template:
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: '{{ trigger.event.data.user_id }}'
      inline_keyboard: []
  CHOPPED

See that the function handling the '/1h' command also sends inline_keyboard: [] to destroy the keyboard.

That all seems to work at my end. Can you confirm your timed button handlers: telegramrepeat30m, telegramrepeat1h, etc… all contain the action to remove the keyboard?

EDIT: I just saw your second post. I’ll leave the above in case it clears anything up for other people.
However, I’m not sure why you need to place the chatid there, it seems to work on my end. Is it a different kind of chat? Group chat?

Is it a different kind of chat? Group chat?

Indeed, I use a group chat. Maybe that was the reason. :slight_smile:

Do inline commands work within a group chat context?

Do I need to open a port and make HA outside network accesible for two-way communication with Telegram bot?

It will need to be connected to the Internet, but you don’t need any open ports.

When I set the reminders (30 Minutes, 1h, 3h) and click on the button in the telegram chat, I get the following error message:

“Incoming message is not allowed”

Maybe there must be something configured in the telegram configuration?

I want to know if a can send a URL to mapas with a data template that contains the latitude and longitude off one of my device tracker, does telegram have option?

Probably you used the broadcast config instead of polling or webhooks.

Check this component page for more info.

Do you mean this?

@Domoticon
I am using polling. Is there anything which I have to add to the config?

Current config:

telegram_bot:
  - platform: polling
    api_key: <API_KEY>
    allowed_chat_ids:
     - <CHAT_ID>

I am also using duckdns.

@taddeuz

I only use HA Telegram broadcast. For Telegram interaction I use Node-Red (link), Your polling config looks good to me, as far as I can tell. :thinking:

I do not have experience setting up interaction with HA, but I read the component docs, linked in my previous answer.

So, if you use polling and a Telegram answer message starts with

/

then you probably need an event triggering to, ehh, trigger an event…:smirk:

Also, as far as the none Home Assistant part, you don’t need extra config to communicate with Telegram, no need for port forwarding, dynamic DNS config, etcetera… If sending a message works, then sending back commands should be working also.

I don’t know if sending a message without / will work, I don’t think so, but I’m not sure about that…

Can anyone, with more experience, please jump in?

Thank you for your help!

Its still not working. I am using the exact code like in the initial post from @Kallb123. I double checked my code and so far, I didn’t make a mistake.

I assume that there is something which blocks the message from telegram because of the error “Incoming message is not allowed”. So, telegram sends the message back to my HA and then it got lost…

2019-01-27 12:45:34 ERROR (dispatcher) [homeassistant.components.telegram_bot] Incoming message is not allowed ({‘id’: ‘’, ‘chat_instance’: ‘<CHAT_ID>’, ‘message’: {‘message_id’: 335, ‘date’: 1547658445, ‘chat’: {‘id’: , ‘type’: ‘group’, ‘title’: ‘’, ‘all_members_are_administrators’: True}, ‘text’: ‘’, ‘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’: , ‘first_name’: ‘’, ‘is_bot’: True, ‘username’: ‘’}}, ‘data’: ‘/removekeyboard’, ‘from’: {‘id’: , ‘first_name’: ‘<FIRST_NAME>’, ‘is_bot’: False, ‘language_code’: ‘de’}})

I found the following information: https://www.home-assistant.io/components/telegram_bot.webhooks/

“To get your chat_id and api_key follow the instructions here. As well as authorizing the chat, if you have added your bot to a group you will also need to authorize any user that will be interacting with the webhook. When an unauthorized user tries to interact with the webhook Home Assistant will raise an error (“Incoming message is not allowed”), you can easily obtain the users id by looking in the “from” section of this error message.”

But I dont know what to do with this information…

No one an idea how to solve the problem?

1 Like

@syssi Would you share your code? I’m interessted :smiley:

@taddeuz Did you send your bot a message first? Cause you have to get in contact with him the first time. Bots are not allowed to send message to users at first.

@azrael783 Yes, I send a message to the bot.