TELEGRAM - Retrive the message id after send

Hi, I would like to delete a message that I sent via a script, on my Telegram bot after a set time.

To do this I need to have the message_id parameter of the message sent on Telegram. I’ve been looking around here, and the only way I’ve found to retrieve the message_id is to have an automation whose trigger has this code:

message_id: '{{ trigger.event.data.message.message_id }}'

which is activated when a key is pressed in the message already sent on telegram via inline_keyboard and therefore activates the event_type: telegram_callback event.

I would like to be able to recover the message_id to delete commands sent on Telegram after a minute, when no response has been given.
It is not good to delete the last message, because in the meantime other messages may have been sent and the one with the commands may not be the last.
Is there a way to get the message_id, possibly while running the script that sends it?

1 Like

Did you ever find a solution to this issue? I have the exact same need.

Yes, i solved it by putting an input number that stores the message id every time a message is sent

This is the automation:

  • id: registra_numero_messaggio_g
    alias: Registra numero msg telegram_g
    trigger:
    platform: event
    event_type: telegram_sent
    event_data:
    message_tag: comandi_cancello_g
    action:
    • service: input_number.set_value
      data_template:
      entity_id: input_number.msg_giuliano
      value: ‘{{ trigger.event.data.message_id }}’

after a certain time (30 seconds for me) the message is deleted by the telegram bot.
this is the action in the automation:

  - delay: 00:00:30
  - service: telegram_bot.delete_message
    data:
      chat_id: YOUR_CHAT_ID
      message_id: '{{ states(''input_number.msg_giuliano'') }}'