In my bot, the user is supposed to interact mostly through Inlinekeyboard
messages.
So, if the user writes some 'regular' message or sends something, I must somehow change the last Inlinekeyboard
message to continue the process or give him some message.
See the picture below:
The user wrote some message but I had to create a new Inlinekeyboard
button with the new message because I couldn't find a way to get the message_id
of the previous 'Welcome' button and change it.
My code:
HELP = range(1)
def start(bot, update):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
# Create initial message:
message = 'Welcome.'
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
def help(bot, update):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
bot.edit_message_text(
text='Help ... help..',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard)
)
bot.answer_callback_query(update.callback_query.id, text='')
def unknown(bot, update):
message = 'Please press the Help button for more instructions.'
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
# Create the EventHandler and pass it your bot's token.
updater = Updater(token=config.TELEGRAM_API_TOKEN)
# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(MessageHandler(Filters.all, unknown))
updater.start_polling()
updater.idle()
Best Regards. Kleyson Rios.
If you want to edit the previous messages that the bot has sent, you need to store their message_id
(which you already know).
So you can simply do this:
sent_message = update.message.reply_text(
message,
reply_markup=InlineKeyboardMarkup(keyboard)
)
And then you can access it like sent_message.message_id
Now for the variable sent_message
, you can make it global or create a separate file to handle these variables, or at larger scale, you can store it in a database.