I am using the Python Telegram BOT API for building a demo application. The task I am trying to achieve is to create a custom keyboard and then keep editing the keyboard keys upon each interaction with the user. I tried using “edit_message_reply_markup” but I get an error “Message Can’t be edited”. Is it not possible to edit a custom keyboard?
Here is the sample code that I have written for my task.
Initial Task:
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)
Edit Task:
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)
I get an Error “Message can’t be edited”
3
Answers
https://core.telegram.org/bots/api#updating-messages
Looks like at the moment we can edit inline keyboards, and can't edit reply keyboards
You can send a new message with new KeyboardButton instead of editing previous message. New ReplyKeyboardMarkup will be replaced with the old ReplyKeyboardMarkup automatically.
using:
or reply to your user with:
You can change your text in the new massage or put it as the previous one.
As a workaround I delete previous message and dublicate it except reply_markup parameter. In your case something like: