Python Telegram 机器人编辑自定义键盘
Python Telegram Bot Edit Custom Keyboard
我正在使用 Python Telegram BOT API 构建演示应用程序。我想要完成的任务是创建一个自定义键盘,然后在每次与用户交互时继续编辑键盘键。我尝试使用 "edit_message_reply_markup",但出现错误 "Message Can't be edited"。不能编辑自定义键盘吗?
这是我为我的任务编写的示例代码。
初始任务:
FirstKeyboard = [[KeyboardButton(text = "FRUITS"), KeyboardButton(text = "VEGITABLES"), KeyboardButton(text = "DRINKS")],[KeyboardButton(text = "SNACKS"), KeyboardButton(text = "CHIPS"), KeyboardButton(text = "NOTHING")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(FirstKeyboard)
KeyboardMessageID = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)
编辑任务:
SecondKeyBoard = [[KeyboardButton(text = "APPLE"), KeyboardButton(text = "BANANA"), KeyboardButton(text = "PUMPKIN")],[KeyboardButton(text = "ORANGES"), KeyboardButton(text = "GRAPES"), KeyboardButton(text = "WINE")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(SecondKeyBoard)
KeyboardMessageID = context.bot.edit_message_reply_markup(chat_id = chatID, message_id = KeyboardMessageID, reply_markup = menu)
我得到一个错误 "Message can't be edited"
https://core.telegram.org/bots/api#updating-messages
目前看来我们可以编辑内联键盘,但不能编辑回复键盘
您可以使用新的 KeyboardButton 发送新消息,而不是编辑以前的消息。新的 ReplyKeyboardMarkup 将自动替换为旧的 ReplyKeyboardMarkup。
使用:
context.bot.send_message(chat_id = chatID, text = "Select What you
Like", reply_markup = NEW_Menu)
或回复您的用户:
update.message.reply_text(text = "Select What you Like", reply_markup = NEW_Menu)
您可以在新的消息中更改您的文字或将其作为上一个。
作为解决方法,我删除了之前的消息并复制了 reply_markup 参数。在你的情况下是这样的:
context.user_data['last_message'] = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)
# changing reply markup
last_message = context.user_data['last_message']
context.bot.delete_message(chat_id=update.effective_chat.id, message_id=last_message.message_id)
# create similar message except new menu
context.user_data['last_message'] = context.bot.send_message(chat_id = last_message.chat_id, text = last_message.text, reply_markup = new_menu)
我正在使用 Python Telegram BOT API 构建演示应用程序。我想要完成的任务是创建一个自定义键盘,然后在每次与用户交互时继续编辑键盘键。我尝试使用 "edit_message_reply_markup",但出现错误 "Message Can't be edited"。不能编辑自定义键盘吗?
这是我为我的任务编写的示例代码。
初始任务:
FirstKeyboard = [[KeyboardButton(text = "FRUITS"), KeyboardButton(text = "VEGITABLES"), KeyboardButton(text = "DRINKS")],[KeyboardButton(text = "SNACKS"), KeyboardButton(text = "CHIPS"), KeyboardButton(text = "NOTHING")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(FirstKeyboard)
KeyboardMessageID = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)
编辑任务:
SecondKeyBoard = [[KeyboardButton(text = "APPLE"), KeyboardButton(text = "BANANA"), KeyboardButton(text = "PUMPKIN")],[KeyboardButton(text = "ORANGES"), KeyboardButton(text = "GRAPES"), KeyboardButton(text = "WINE")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(SecondKeyBoard)
KeyboardMessageID = context.bot.edit_message_reply_markup(chat_id = chatID, message_id = KeyboardMessageID, reply_markup = menu)
我得到一个错误 "Message can't be edited"
https://core.telegram.org/bots/api#updating-messages
目前看来我们可以编辑内联键盘,但不能编辑回复键盘
您可以使用新的 KeyboardButton 发送新消息,而不是编辑以前的消息。新的 ReplyKeyboardMarkup 将自动替换为旧的 ReplyKeyboardMarkup。 使用:
context.bot.send_message(chat_id = chatID, text = "Select What you
Like", reply_markup = NEW_Menu)
或回复您的用户:
update.message.reply_text(text = "Select What you Like", reply_markup = NEW_Menu)
您可以在新的消息中更改您的文字或将其作为上一个。
作为解决方法,我删除了之前的消息并复制了 reply_markup 参数。在你的情况下是这样的:
context.user_data['last_message'] = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)
# changing reply markup
last_message = context.user_data['last_message']
context.bot.delete_message(chat_id=update.effective_chat.id, message_id=last_message.message_id)
# create similar message except new menu
context.user_data['last_message'] = context.bot.send_message(chat_id = last_message.chat_id, text = last_message.text, reply_markup = new_menu)