Good day. There is a code telegram bot, which displays 4 buttons and when you click on each of them displays a message.

from telebot import types import constants, os, re bot = telebot.TeleBot(constants.token) @bot.message_handler(commands=["start"]) def inline(message): key = types.InlineKeyboardMarkup() but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne") but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo") but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree") but_4 = types.InlineKeyboardButton(text="Number4", callback_data="Number4") key.add(but_1, but_2, but_3, but_4) bot.send_message(message.chat.id, "Π’Π«Π‘Π•Π Π˜Π’Π• КНОПКУ", reply_markup=key) @bot.callback_query_handler(func=lambda c:True) def inlin(c): if c.data == 'NumberOne': bot.send_message(c.message.chat.id, 'Π­Ρ‚ΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ° 1') if c.data == 'NumberTwo': bot.send_message(c.message.chat.id, 'Π­Ρ‚ΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ° 2') if c.data == 'NumberTree': bot.send_message(c.message.chat.id, 'Π­Ρ‚ΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ° 3') if c.data == 'Number4': bot.send_message(c.message.chat.id, 'Π­Ρ‚ΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ° 4') if __name__ == "__main__": bot.polling(none_stop=True) 

How to do that when you click on a button - a new menu of several buttons opens, with the possibility of returning to the main menu?

  • Also interested in this menu! if there is a solution, share the code, I will be grateful - Hoasker

1 answer 1

 #НапримСр, ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ reply_markup Π² edit_message_text def inline_key(num): """Ѐункция для Π²Ρ‹Π²ΠΎΠ΄Π° ΠΊΠ½ΠΎΠΏΠΎΠΊ """ i=1 btns = [] while i<=num: btns.append(types.InlineKeyboardButton(text='Кнопка '+str(i+10), callback_data='butt'+str(i+10))) i=i+1 btns.append(types.InlineKeyboardButton(text='Π½Π°Π·Π°Π΄', callback_data='nazad')) keyboard = types.InlineKeyboardMarkup() keyboard.add(*btns) return keyboard @bot.message_handler(commands=["start"]) #Π³Π»Π°Π²Π½ΠΎΠ΅ мСню def start(m): key = types.InlineKeyboardMarkup() key.add(types.InlineKeyboardButton(text='ΠΊΠ½ΠΎΠΏΠΊΠ°1', callback_data="butt1")) key.add(types.InlineKeyboardButton(text='ΠΊΠ½ΠΎΠΏΠΊΠ°2', callback_data="butt2")) msg=bot.send_message(m.chat.id, 'НаТми ΠΊΠ½ΠΎΠΏΠΊΡƒ', reply_markup=inline_main()) logging.info(m.chat.id) @bot.callback_query_handler(func=lambda call: True) def inline(c): if c.data=='butt1': bot.edit_message_text( chat_id=c.message.chat.id, message_id=c.message.message_id, text="Π½Π°ΠΆΠ°Ρ‚Π° *ΠΊΠ½ΠΎΠΏΠΊΠ° 1*", parse_mode="markdown") elif c.data=='butt2': bot.edit_message_text( chat_id=c.message.chat.id, message_id=c.message.message_id, text="Π½Π°ΠΆΠ°Ρ‚Π° *ΠΊΠ½ΠΎΠΏΠΊΠ° 2*", parse_mode="markdown", reply_markup=inline_key(5)) elif c.data=='nazad': bot.edit_message_text( chat_id=c.message.chat.id, message_id=c.message.message_id, text="Π½Π°ΠΆΠ°Ρ‚Π° *ΠΊΠ½ΠΎΠΏΠΊΠ° 2*", parse_mode="markdown", reply_markup=inline_key(2))