如何多次编辑电报消息?

How do I edit a telegram message multiple times?

你好,我正在尝试制作一个电报机器人,它可以像 BotFather 一样多次编辑同一消息,但每次我尝试它都会给我这个错误:

telegram.error.BadRequest: Message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message

这是代码,我尽量把它说清楚了。

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import *
from API import API_KEY
from bot_messages import *


updater = Updater(API_KEY, use_context=True)
dispatcher = updater.dispatcher


def start(update, context):
    update.message.reply_text(WELCOME, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Ciao", callback_data="ciao")]]))

def ciao(update, context):
    update.callback_query.edit_message_text("Ciao", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Dona!", callback_data="donate")]]))

def donate(update, context):
    update.callback_query.edit_message_text("Dona", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Dona", callback_data="dona")]]))


dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("restart", start))

dispatcher.add_handler(CallbackQueryHandler(ciao))
dispatcher.add_handler(CallbackQueryHandler(donate))


updater.start_polling()
updater.idle()

您还能告诉我制作内联查询处理程序的正确方法吗?因为如果它给我错误,那可能意味着我做得不太对。提前致谢。

在您的代码段中,只有第一个 CallbackQueryHandler 会处理更新 - 请参阅 Dispatcher.add_handler 的文档以了解有关调度程序如何决定哪个处理程序处理更新的详细信息。 这就是为什么您的代码试图用未更改的文本更新消息而您收到该错误的原因。

要解决这个问题,您可以例如使用 CallbackQueryhandler.

pattern 参数

免责声明:我目前是 python-telegram-bot.

的维护者