Python电报Bot编辑自定义键盘

时间:2019-11-05 01:40:17

标签: telegram telegram-bot python-telegram-bot

我正在使用Python Telegram BOT API来构建演示应用程序。我要实现的任务是创建一个自定义键盘,然后在每次与用户交互时继续编辑键盘键。我尝试使用“ edit_message_reply_markup”,但收到错误消息“消息无法编辑”。不能编辑自定义键盘吗?

这是我为任务编写的示例代码。

初始任务:

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)

我收到错误消息“消息无法编辑”

3 个答案:

答案 0 :(得分:1)

https://core.telegram.org/bots/api#updating-messages

目前看来我们可以编辑嵌入式键盘,而不能编辑回复键盘

答案 1 :(得分:0)

您可以使用新的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)

您可以在新的消息中更改您的文字,也可以将其作为上一个。

答案 2 :(得分:0)

作为一种解决方法,我删除了先前的消息并复制了它,但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)
相关问题