【问题标题】:Python Telegram Bot - How to update the text of the last message my bot has sentPython Telegram Bot - 如何更新我的机器人发送的最后一条消息的文本
【发布时间】:2018-09-24 23:03:11
【问题描述】:

我正在使用 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)

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

【问题讨论】:

    标签: python python-telegram-bot


    【解决方案1】:

    我认为您在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

    【讨论】:

    • 谢谢。我会试试这个并评论成功。
    • 你成功了吗? @576i
    猜你喜欢
    • 2018-05-17
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多