576i
576i

Reputation: 8372

Python Telegram Bot - How to update the text of the last message my bot has sent

I'm using python-telegram-bot (python-telegram-bot.org) to communicate with Telegram from Python3

I would like to update the last reply I sent. Currently, the code below sends the message and then sends another message 5 seconds later.

def echo(bot, update):
    update.message.reply_text("Sorry, you're on your own, kiddo.")
    time.sleep(5)
    update.message.reply_text("Seriously, you're on your own, kiddo.")

I'd like to update the last message instead.

I tried

bot.editMessageText("Seriously, you're on your own, kiddo.",
                   chat_id=update.message.chat_id,
                   message_id=update.message.message_id)

which works in the examples to update replace an inline keyboard with a a message, but that crashes (and does not update the last message I sent as a bot).

Upvotes: 7

Views: 22411

Answers (2)

Andrei Alekseev
Andrei Alekseev

Reputation: 480

@jeffffc's solution is good if you need to edit message inside handler.

But if you need to edit message in another handler, you should store message_id somewhere. It's because in other handler you'll don't have access to previous bot message, only to user message/command it receives.

For example, if you try to do something like this:

def echo(bot, update):
    update.message.reply_text("Sorry, you're on your own, kiddo.")

def echo2(bot, update):
    bot.edit_message_text(chat_id=update.message.chat_id, 
                          message_id=.message_id,
                          text="Seriously, you're on your own, kiddo.")

You will receive an error "Message can't be edited". It's because you try to edit user message, not bot message.

With latest 13.x version of python-telegram-bot you can use CallbackContext to store data between handlers. Instead of handlers above you can make it this:

def echo(update, context):
    msg = update.message.reply_text("Sorry, you're on your own, kiddo.")
    context.user_data['bot_last_message_id'] = msg.message_id  # remember to edit later

def echo2(update, context):
    context.bot.edit_message_text(
        text="Seriously, you're on your own, kiddo.",
        chat_id=update.message.chat_id,
        message_id=context.user_data['bot_last_message_id'],  # get it from context
    )

Upvotes: 1

jeffffc
jeffffc

Reputation: 780

I believe the order of your arguments in edit_message_text() is wrong. Check out the docs for that:

def echo(bot, update):
    # Any send_* methods return the sent message object
    msg = update.message.reply_text("Sorry, you're on your own, kiddo.")
    time.sleep(5)
    # you can explicitly enter the details
    bot.edit_message_text(chat_id=update.message.chat_id, 
                          message_id=msg.message_id,
                          text="Seriously, you're on your own, kiddo.")
    # or use the shortcut (which pre-enters the chat_id and message_id behind)
    msg.edit_text("Seriously, you're on your own, kiddo.")

The docs for the shortcut message.edit_text() is here.

Upvotes: 14

Related Questions