Remove 'inline_keyboard' from telegram notification

Hi,

I have an automation that turns off my AC and sends me a Telegram notification whenever I leave my apartment.
Everything works smoothly, and my conf looks like this:

  action:
    - service: homeassistant.turn_off
      entity_id: switch.ac
    - service: notify.yeralin_bot
      data:
        title: "AC was turned off"
        message: "Seems like you left your apartment with AC on, I gladly turned it off for you :)"
        data:
          inline_keyboard:
            - "I'm still home:/turnOn AC"
            - "All good:/dismiss"

When I receive the notification, I have 2 inline_keyboard buttons, if I press “I’m still home”, it will turn my AC back on, If I press “All good”, it will remove the inline_keyboard buttons.

What I want here is to automatically remove these inline_keyboard buttons after a minute (if there is no response).
So, I came up with following:

  action:
    - service: homeassistant.turn_off
      entity_id: switch.ac
    - service: notify.yeralin_bot
      data:
        title: "AC was turned off"
        message: "Seems like you left your apartment with AC on, I gladly turned it off for you :)"
        data:
          inline_keyboard:
            - "I'm still home:/turnOn AC"
            - "All good:/dismiss"
    - delay: 0:01
    - service: telegram_bot.edit_replymarkup
      data_template:
        message_id: '{{ action.data.message.message_id }}'
        chat_id: '{{ notify.yeralin_bot.data.user_id }}'
        inline_keyboard: [] 

The problem here, that it cannot resolve {{action.data.message.message_id}}. I could have used {{last}}, but then I have other messages that might come from Telegram bot, and I don’t want to remove inline_keyboard buttons from them by accident.

Perhaps you can store the last id inside an input_text? With this you can use a different entity for every message type.

I wanted to see a cleaner solution. It looks like an easy task, I just don’t know the specifics. And there is no good way of debugging this

Bumping it up!

Came here cause I’m stuck on the exact same thing
I don’t want to use {{last}} too.

I see the suggestion from @syphernl to use an input_text
But how to you even retrieve the chat_id in the first place ?

Yeah, I tried to bring people from Discord, but no one seemed interested.

You could try as well: https://discord.gg/c5DvZ4e

I use Python and python-telegram-bot implementation.

Normally you would send Inline keyboard message like this:

update.message.reply_text('Some inline menu', reply_markup=reply_markup)

But you can assign the same command to a variable:

sent = update.message.reply_text('Some inline menu', reply_markup=reply_markup)

Which then sends the message as usual, but also stores all the messge details as a dictionary in the sent variable, so you can get all the details from it, like message id, chat id and time sent.

I haven’t figured out yet how to use this to remove Inline keyboard after a period of time, but it certainly helps.

Hi, I know this is a year later, but I struggled with the issue to get the message_id, so i wanted to post incase others struggle and come across this post.

@daniyar94, I am not sure what is in your trigger for the above code, but the {{action.data.message.message_id}} code only works off of a event_type: telegram_callback (from my limited understanding).

I needed to create a cancel inline keyboard so that i can cancel and the massage would be deleted, below is how I achieved this:

- alias: 'Telegram Cancel Message'
  hide_entity: true
  trigger:
  - platform: event
    event_type: telegram_callback
    event_data:
      data: '/cancel'
  action: 
  - service: telegram_bot.delete_message
    data_template:
      #title: Menu
      message_id: '{{ trigger.event.data.message.message_id }}'
      chat_id: '{{ trigger.event.data.chat_id }}'
2 Likes