Search code examples
python-3.xtelegram-botpython-telegram-bot

How to handle callbackquery in python telegram bot


In my code I'm facing an issue with callbackquery handler, when I hit /start command the "Next" button appears and when I hit on that button it gives me reply as "hi", till here the output is correct. Then when I hit another command "/help" then "help" button appears, when I hit that help button then it gives me previous reply which was "hi", where the output should have been "help"

 from telegram import InlineKeyboardButton, InlineKeyboardMarkup
 from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler

 TELEGRAM_HTTP_API_TOKEN = 'token'

 FIRST, SECOND, HELP = range(3)

 def start(bot, update):
        keyboard = [
            [InlineKeyboardButton(u"Next", callback_data=str(FIRST))]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text(
            u"Start handler, Press next",
            reply_markup=reply_markup
        )
        return FIRST

 def first(bot, update):
        query = update.callback_query
        #reply_markup = InlineKeyboardMarkup(keyboard)
        bot.send_message(chat_id=query.message.chat_id,
                         text='hi')

 def help(bot,update):
        keyboard = [
            [InlineKeyboardButton(u"HELP", callback_data=str(HELP))]
        ]
        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text(
            u"Help handler, Press button",
            reply_markup=reply_markup
        )

        return HELP

 def myhelp(bot,update):
        query = update.callback_query
        bot.send_message(chat_id=query.message.chat_id,
                         text='help')

 updater = Updater(TELEGRAM_HTTP_API_TOKEN)

 conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [CallbackQueryHandler(first)]
        },
        fallbacks=[CommandHandler('start', start)]
    )
 conv_handler1=ConversationHandler(
        entry_points=[CommandHandler('help',help)],
        states={
            HELP: [CallbackQueryHandler(myhelp)]
        },
        fallbacks=[CommandHandler('help',help)]
    )

 updater.dispatcher.add_handler(conv_handler)
 updater.dispatcher.add_handler(conv_handler1)

 updater.start_polling()

 updater.idle()

Expected Output


Solution

  • You're first conversationhandler is still in state FIRST en thus is still waiting for callbackqueries. Since it's the first handler added and they're in the same group, the first will respond, and the second won't. You can look at the pattern argument for CallbackQueryHandler to solve your problem.