0

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?

2
  • It could be that you aren't using the same updated format for all your functions: (update: Update, context: CallbackContext) -> None: Try rewriting your functions so that they all follow the same template as first_submenu Commented Feb 6, 2021 at 21:26
  • thanks goalie1988.... but same error. Commented Feb 7, 2021 at 0:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank goalie1998, replace m1_1 for sub1_1, and work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.