title: Managing Back Buttons In Multi Level Menu - Telegram Bot (Python)
body: <p>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.</p>
  <p>The current menus are as follow:</p>
  <p><strong>Main Menu</strong> has three buttons: 1- Random Number 2- Random String 3- Other</p>
  <p><strong>Other Menu</strong> has three buttons: 1- Information 2- Exchange Rate 3- Main Menu</p>
  <p><strong>Information Menu</strong> has one button: 1- Back</p>
  <p><strong>Random String Menu</strong> has one button: 1- Back</p>
  <p>Now I managed to return one step from the <strong>Information Menu</strong> back to the <strong>Other Menu</strong> using the below code; however, I couldn't do the same thing with <strong>Random String Menu</strong> as once I press the back button will take me back to the <strong>Other Menu</strong> instead of the <strong>Main Menu</strong>, most likely because of the duplicate KeyboardButton('⬅ Back') in markups.py file, which results in navigating to one of the existing menus (<strong>Other Menu</strong>).</p>
  <p>Any idea how to overcome this issue?</p>
  <pre><code># 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, &quot;&quot; + 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, &quot;⬅ Main Menu&quot;, reply_markup = nav.mainMenu) 
  
      elif message.text == '➡ Other':
          await bot.send_message(message.from_user.id, &quot;➡ Other&quot;, 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, &quot; Information&quot;, reply_markup = nav.subOtherMenu)
          
      elif message.text == ' Exchange Rate':
          await bot.send_message(message.from_user.id, &quot; Exchange Rate&quot;) 
          
      else:
          await message.reply('No data')
      
      
  if __name__ == '__main__':
      executor.start_polling(dp, skip_updates = True)
  
  </code></pre>
  <pre><code># 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)
  
  </code></pre>