简体   繁体   中英

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 aa message, but that crashes (and does not update the last message I sent as a bot).

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM