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

时间:2018-04-14 21:37:17

标签: 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.")

我想更新最后一条消息。

我试过

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

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

1 个答案:

答案 0 :(得分:0)

我相信edit_message_text()中论据的顺序是错误的。请查看the docs

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.")

快捷方式message.edit_text()的文档是here

相关问题