Search code examples
pythontelegramtelegram-botpy-telegram-bot-api

How to create nested menu with pyTelegramBotAPI?


I am creating my own telegram bot with pyTelegramBotAPI. And I wonder how to create nested menu with inline buttons. Here is good example of what I want to create, this is BotFather, bot, that allows you to create tokes for your bots. As you can see, you work in only one "message", because this is menu.

enter image description here

So, could you please provide me a minimal working example of that menu? Thanks in advance!


Solution

  • It could be done with edit_message_text method. Here is example:

    ...
    keyboard = [[types.InlineKeyboardButton("< Go back", callback_data='smt')]]
    markup = types.InlineKeyboardMarkup(keyboard)
    ...
    
    @bot.callback_query_handler(func=lambda call: True)
    def commandshandlebtn(call):
        userMessage = call.data
        if userMessage == "smt":
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Some text, for example')