Python Telegram Bot - 如何更新我的机器人发送的最后一条消息的文本

576*_*76i 7 python python-telegram-bot

我正在使用 python-telegram-bot (python-telegram-bot.org) 从 Python3 与 Telegram 进行通信

我想更新我发送的最后一条回复。目前,下面的代码发送消息,然后 5 秒后发送另一条消息。

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.")
Run Code Online (Sandbox Code Playgroud)

我想更新最后一条消息。

我试过

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

在示例中,它可以更新用消息替换内联键盘,但是会崩溃(并且不会更新我作为机器人发送的最后一条消息)。

jef*_*ffc 14

我相信你的论点顺序edit_message_text()是错误的。查看文档

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.")
Run Code Online (Sandbox Code Playgroud)

快捷方式的文档message.edit_text()这里

  • 你成功了吗?@576i (2认同)