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

标签 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)

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

最佳答案

我相信您在 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 .

关于Python Telegram Bot - 如何更新我的机器人发送的最后一条消息的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49836428/

相关文章:

python - 电报机器人在命令 Python 后等待用户回复

python - 根据另一列获取一列中最常见的值/将 pandas 系列的系列转换为列表的字典

javascript - 从 Facebook 上的 JavaScript 文件 (melonJS) 游戏调用 Python 函数 (Flask)

python - 关于 python-telegram-bot 中异步处理的问题

python - 在某些消息电报机器人之后保存用户输入

telegram - 如何让 Telegram Bot 将消息发布到我的 channel 而不是它自己的 channel

python - 如何在python中加载m4a文件

python - 转换 dd-mm-yyyy hh :mm to MySQL TIMESTAMP

python - 将变量 append 到列表时,为什么会出现“"' str' object is not callable”?

使用 ptb : Same line logged twice but only one handler added 进行 Python 日志记录