I'm building a tele bot which requires nested menus (and visually I want them to be inline) like the solution mentioned in this 2020 link, but unfortunately after reading through the Jan 2023 documentation, I realised Updater and Dispatcher have been changed/removed, so I can't quite use this solution.
Short of downgrading my python-telegram-bot to an older version, would anyone be so kind as to help me with an updated version of nested tele bot menus? Thanks! :)
I tried combining asyncio with the solution linked above, but it doesn't work. No new nested telegram menu appeared on my bot and I'm a newbie coder so I'm really quite lost after trying to fix this for a whole day :/
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
############################### Bot ############################################
def start(bot, update):
bot.message.reply_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def main_menu(bot, update):
bot.callback_query.message.edit_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def first_menu(bot, update):
bot.callback_query.message.edit_text(first_menu_message(),
reply_markup=first_menu_keyboard())
def second_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())
def first_submenu(bot, update):
pass
def second_submenu(bot, update):
pass
def error(update, context):
print(f'Update {update} caused error {context.error}')
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Menu 1', callback_data='m1')],
[InlineKeyboardButton('Menu 2', callback_data='m2')],
[InlineKeyboardButton('Menu 3', callback_data='m3')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 1-1', callback_data='m1_1')],
[InlineKeyboardButton('Submenu 1-2', callback_data='m1_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 2-1', callback_data='m2_1')],
[InlineKeyboardButton('Submenu 2-2', callback_data='m2_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
############################# Messages #########################################
def main_menu_message():
return 'Choose the option in main menu:'
def first_menu_message():
return 'Choose the submenu in first menu:'
def second_menu_message():
return 'Choose the submenu in second menu:'
###Attempt at replacing Updater/Dispatcher function###
def __init__(
self,
bot: "Bot",
update_queue: "asyncio.Queue[object]",
):
self.bot: Bot = bot
self.update_queue: "asyncio.Queue[object]" = update_queue
self._last_update_id = 0
self._running = False
self._initialized = False
self._httpd: Optional[WebhookServer] = None
self.__lock = asyncio.Lock()
self.__polling_task: Optional[asyncio.Task] = None
self._logger = logging.getLogger(__name__)
Any help would be greatly appreciated, thanks so much! :)