3

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)

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
B A C H A S H
  • 126
  • 1
  • 9

1 Answers1

1

Answer

You should use FSM for that case. This way the same text command may be routed to different actions and levels

Also

  1. Don't concatenate strings, use f-strings instead.
  2. Use different handlers for different text:
@dp.message_handler(text=" Random String")
async def handler_random_string(message: types.Message):
   ...
  1. Use shortcuts instead of full bot methods. This will make your code cleaner and more readable.
await message.answer("Hello world")
Oleg
  • 523
  • 2
  • 10