简体   繁体   English

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

[英]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 我正在使用python-telegram-bot(python-telegram-bot.org)与Python3的Telegram通信

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. 当前,下面的代码发送该消息,然后在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.")

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. 我相信您在edit_message_text()中的参数顺序是错误的。 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 . 快捷方式message.edit_text()的文档在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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