I wrote a bot, created a menu and a back button, but this button does not work because I don’t know how to set a link to the previous menu.

@bot.message_handler(commands=['Сантехника']) def handle_start(message): user_markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True) user_markup.row('/Назад') <-------------- Вот эта кнопка user_markup.row('Раковина', 'Унитаз') user_markup.row('Биде') bot.send_message(message.from_user.id, 'Выберите коллекцию', reply_markup=user_markup) 

This is my submenu. If you click on the command "Sink" then the bot will send pictures. But if I want to return to the previous menu, I should click on the "Back" button.

So the question is how to write code or give a link to the previous menu?

  • 3
    Add a code snippet where you are having trouble. - 0xdb
  • This is my submenu. If you click on the command "Sink" then the bot send pictures. But if I want to return to the previous menu, I should click on the "BACK" button, so the question is how to write code or give a link to the previous menu? - Bobur
  • @ bot.message_handler (commands = ['Plumbing']) def handle_start (message): user_markup = telebot.types.ReplyKeyboardMarkup (resize_keyboard = True) user_markup.row ('/ Back') <--------- ----- This user_markup.row button ('Sink', 'Toilet') user_markup.row ('Bidet') bot.send_message (message.from_user.id, 'Select a collection', reply_markup = user_markup) - Bobur
  • Use the edit button to transfer comments to the question. - 0xdb

2 answers 2

Here you can add a logical variable to understand which menu the user is in. When entering the "sanitary"

 @bot.message_handler(commands=['Сантехника']) def handle_start(message): menu_plumbing = True #остальной код\ 

and add the command "back" which will return you to the menu back depending on the menu in which you are

 @bot.message_handler(commands=['Назад']) def handle_start(message): if menu_plumbing: menu_plumbing = False #тут можно уже или как то так handle_start('Другое меню') #или просто создать и отправить новую клаву юзеру user_markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True) user_markup.row('Кнопка') bot.send_message(message.from_user.id, 'Новая клавиатура', reply_markup=user_markup) 
     @bot.message_handler(commands=['Сантехника']) def handle_start(message): user_markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True) user_markup.row('/Назад') <----- Вот эта кнопка user_markup.row('Раковина', 'Унитаз') user_markup.row('Биде') bot.send_message(message.from_user.id, 'Выберите коллекцию', reply_markup=user_markup) 
    • Do you really want to answer your question? - 0xdb