Telegram - Get_Last_Message_Id Service (easy extension?)

Hello,
As I know Telegram Bot allows to edit/delete just last message in a chat by using magic “last” constant for message_id. But… sometimes HA should be able to edit/delete ANY (not just last) important message in chat. For this purpose, a special service call could be implemented - returning last_message_id as input_number.

The good example of such a scenario (know as “singleton-notification”) could be:

  1. HA sends Telegram Notification is that one of “PCs is not used” (e.g. by detecting low CPU usage for last 30 minutes) also… giving the options to “turn PC off” or decide to “leave it running” by selecting one of the options on Telegram Keyboard. Any User action should delete this message and… this is OK, this can be realized right now done right now, as it’s a straightforward reaction to the callback (callback provides message_id)

  2. Now, imagine that nobody takes an action on this Telegram message and after a few minutes new notifications are generated by HA (not related to PC) in the same chat. The message giving the option to “turn off PC” is no longer the last one.

  3. Now, in case of someone goes back to PC and its CPU goes up again (or PC is turned off manually) the HA should automatically delete the message in a chat - the old message allowing to “turn PC off” remotely. Otherwise users still see the option to “turn PC off” even if conditions have changed.

For this purpose, I think it’s worth to implement service getting the value of the last message_id for given chat_id. The value could be stored (using special input_number entity) by triggering secondary action of telegram_bot.get_last_message_id executing it immediately after notification is sent.

Service Proposal

service telegram_bot.get_last_message_id

attributes:

  • chat_id: the chat id for which we want to extract the last message_id
  • message_id_variable: the return variable, the name of input_number entity, which will be set to the value of the last message_id for given chat_id

NOTE: As I can see in GitHub the method _get_last_message_id(…) is already implemented in telegram_bot. I guess it should be quite easy to expose it as a service call.

At the moment it is ridiculous that you send the message via HA service and don’t know its id. So, in case when you want to change it later, you can’t do it, since there could be a lot of messages after it.

In general, I encountered a small incident, an important message was deleted after the launch of automation with sending duplicate messages, respectively, this automation deletes my previous messages. I began to look for advice … And the possibility was found in the official documentation:

### Sending message in script/automation etc. ###
  - service: notify.telegram_sergeyfilzner
    data:
       title: 'My title'
       message: 'Text of my message...'
       data:
         message_tag: 'msg_start'


- alias: 'Notifications about messages sent by Telegram bot'
  trigger:
    platform: event
    event_type: telegram_sent
    event_data:
      message_tag: "msg_start"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.chat_id
        value: "{{ trigger.event.data.chat_id }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.message_id
        value: "{{ trigger.event.data.message_id }}"

I have not yet found the time to thoroughly understand this, but as I understand it, we give our messages a special message_tag, and catch the event, saving the message_id somewhere, which allows us to use it later.

1 Like

Anybody got this to work? I can successfully add the message tag and that tag then can trigger the automation but it just wont set the input number from trigger event data. Is the value maybe not registring as a number and one has to convert it via float or int?

I tried so many things but so far without success.

When I manually catch the message id and chat id and add them to the automation, then the auto updating message works

It works perfectly, I can delete the message I sent after the time I set
I use this code in the automation:


      - service: telegram_bot.delete_message
        data:
          chat_id: "{{ states('input_number.chat_id') }}"
          message_id:  "{{ states('input_number.message_id') }}"