I’m building a navigation system for my Telegram bot using inline keyboards. The bot should allow users to move between different menu screens by clicking buttons.
Here’s my current implementation:
STATE_MAIN, STATE_INFO = range(2)
def welcome_handler(bot, update):
buttons = [
[InlineKeyboardButton('Info', callback_data='info')]
]
welcome_text = 'Hello there!'
update.message.reply_text(welcome_text, reply_markup=InlineKeyboardMarkup(buttons))
def info_handler(bot, update):
buttons = [
[InlineKeyboardButton('Back', callback_data='back')]
]
update.callback_query.edit_message_reply_markup('Information section...', reply_markup=InlineKeyboardMarkup(buttons))
def back_handler(bot, update):
update.message.reply_text('Goodbye!', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
updater = Updater(token=my_bot_token)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', welcome_handler))
dispatcher.add_handler(CallbackQueryHandler(info_handler, pattern='info'))
dispatcher.add_handler(CallbackQueryHandler(back_handler, pattern='back'))
updater.start_polling()
updater.idle()
The problem is that when I check update.callback_query.inline_message_id, it returns None instead of the expected message ID. I thought this value was needed to update inline keyboards properly. Why is this happening and how can I fix the keyboard updates?
your info_handler has a bug - edit_message_reply_markup doesn’t take text as first param, so it’s probably failing. try update.callback_query.edit_message_text(text='Information section...', reply_markup=InlineKeyboardMarkup(buttons)) instead. also, inline_message_id is None cuz you’re not using inline mode.
The inline_message_id being None just means you’re working with regular bot messages, not inline query results. This ID only gets populated for inline bots that send messages directly into chats without the traditional chat interface. In your info_handler function, use edit_message_text instead of edit_message_reply_markup to modify both the text and keyboard at once: ```python
def info_handler(bot, update):
buttons = [[
InlineKeyboardButton(‘Back’, callback_data=‘back’)
]]
update.callback_query.edit_message_text(
text='Information section...',
reply_markup=InlineKeyboardMarkup(buttons)
)
update.callback_query.answer() # Acknowledge the callback to prevent loading spinner from hanging
```, Also, use edit_message_text in your back handler too - keeps everything consistent with the same message object.
Yeah, inline_message_id trips people up all the time. You’re mixing up two different things here. That field is only for inline queries—when users type @yourbotname in any chat. It’s got nothing to do with callback queries on inline keyboards. For regular bot conversations, you’ll always get None there.
Apart from the API method issue others mentioned, your back_handler has another bug. You’re trying to use update.message, but callback queries don’t have that attribute. You need update.callback_query.message, or just edit the existing message:
def back_handler(bot, update):
update.callback_query.edit_message_text('Goodbye!')
update.callback_query.answer()
return ConversationHandler.END
This way you’re editing the same message instead of creating new ones.