dtostes
dtostes

Reputation: 113

Message after action on submenu telegram bot python

I am using python-telegram-bot lib dto create a bot and i am trying to send a message to chat after press a button on submenu.

from telegram.ext import Updater from telegram.ext import CommandHandler, CallbackQueryHandler

from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update

from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
############################### Aux Functions ##################################



############################### 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):
  query = update.callback_query
  query.answer()
  query.edit_message_text(text="hi")

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('Temp', callback_data='m1')],
          [InlineKeyboardButton('Menu 2', callback_data='m2')],
          [InlineKeyboardButton('Menu 3', callback_data='m3')]]
  return InlineKeyboardMarkup(keyboard)

def first_menu_keyboard():
  keyboard = [[InlineKeyboardButton('New York', 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 'Escolha uma opção'

def first_menu_message():
  return 'Choose the submenu in first menu:'

def second_menu_message():
  return 'Choose the submenu in second menu:'

############################# Handlers #########################################
updater = Updater('token', use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='m1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='m2'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_submenu, pattern='m1_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_submenu, pattern='m2_1'))
updater.dispatcher.add_error_handler(error)

updater.start_polling()
################################################################################

After choose New York on my first menu, i am trying to send a message to chat but i am getting this error:

caused error Message is not modified: specified new message content and reply markup are 
exactly the same as a current content and reply markup of the message

How can I fix this?

Upvotes: 0

Views: 811

Answers (1)

goalie1998
goalie1998

Reputation: 1442

After looking at your code again, I noticed that you are using variations on m1, and m2 in your callbacks. CallbackQueryHandler() uses regex as it's pattern matching, which is what's causing your problems. You can use non-intersecting strings for callbacks, map them to other formats (i.e. using chr() as is done in many of the examples, use a more specific regex pattern in CallbackQueryHandler() - such as pattern='^m1_1$' for all of your patterns - or any combination of the three.

Upvotes: 1

Related Questions