I am currently working on a new project which makes use of the Telegram bot. Yet, I am new to the Telegram bot, and I'm trying hard to learn new things. The project includes creating a multi-level list with displaying buttons that perform a specific function. However, each level has a back button that returns one step backwards.
The current menus are as follow:
Main Menu has three buttons: 1- Random Number 2- Random String 3- Other
Other Menu has three buttons: 1- Information 2- Exchange Rate 3- Main Menu
Information Menu has one button: 1- Back
Random String Menu has one button: 1- Back
Now I managed to return one step from the Information Menu back to the Other Menu using the below code; however, I couldn't do the same thing with Random String Menu as once I press the back button will take me back to the Other Menu instead of the Main Menu, most likely because of the duplicate KeyboardButton('⬅ Back') in markups.py file, which results in navigating to one of the existing menus (Other Menu).
Any idea how to overcome this issue?
# main.py
import logging
from aiogram import Bot, Dispatcher, executor, types
import markups as nav
import random
TOKEN = ''
# logging.basicConfig(level=logging.INFO)
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def command_start(message: types.Message):
await bot.send_message(message.from_user.id, f'Hello {message.from_user.first_name}', reply_markup = nav.mainMenu)
@dp.message_handler()
async def bot_message(message: types.Message):
# await bot.send_message(message.from_user.id, message.text)
if message.text == '♦ Random Number':
await bot.send_message(message.from_user.id, "" + str(random.randint(1000,9999)))
elif message.text == ' Random String':
await bot.send_message(message.from_user.id, ' Random String', reply_markup = nav.subFoodMenu)
elif message.text == '⬅ Main Menu':
await bot.send_message(message.from_user.id, "⬅ Main Menu", reply_markup = nav.mainMenu)
elif message.text == '➡ Other':
await bot.send_message(message.from_user.id, "➡ Other", reply_markup = nav.otherMenu)
elif message.text == '⬅ Back':
await bot.send_message(message.from_user.id, '⬅ Back', reply_markup = nav.otherMenu)
elif message.text == ' Information':
await bot.send_message(message.from_user.id, " Information", reply_markup = nav.subOtherMenu)
elif message.text == ' Exchange Rate':
await bot.send_message(message.from_user.id, " Exchange Rate")
else:
await message.reply('No data')
if __name__ == '__main__':
executor.start_polling(dp, skip_updates = True)
# markups.py
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
btnMain = KeyboardButton('⬅ Main Menu')
btnOtherMain = KeyboardButton('⬅ Back')
btnFoodMain = KeyboardButton('⬅ Back')
# Main Menu :
btnRandom = KeyboardButton('♦ Random Number')
btnFood = KeyboardButton(' Random String')
btnOther = KeyboardButton('➡ Other')
mainMenu = ReplyKeyboardMarkup(resize_keyboard = True).add(btnRandom, btnFood, btnOther)
# Other Menu :
btnInfo = KeyboardButton(' Information')
btnMoney = KeyboardButton(' Exchange Rate')
otherMenu = ReplyKeyboardMarkup(resize_keyboard = True).add(btnInfo, btnMoney, btnMain)
# Sub Other Menu:
subOtherMenu = ReplyKeyboardMarkup(resize_keyboard = True).add(btnOtherMain)
# Food Menu:
subFoodMenu = ReplyKeyboardMarkup(resize_keyboard = True).add(btnFoodMain)