5

In my code I'm facing an issue with callbackquery handler, when I'm hit /start command Next button appear and when I'm hitting on that button it gives me reply as hi, till this output is correct. Then when I'm hitting another command /help then help button appears, when I hit that help button then it gives me same reply for next button is hi.

Conclusion: Is there is way to kill old callbackquery handler. I found way is return Conversationhandler.END from callbackquery handler function but it limits my functionality had googled for it but no found expected output.

Here is my code:

 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()

This is code screenshot output for more detail

Any kind of help is welcome.

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
VISHAL LIMGIRE
  • 529
  • 1
  • 5
  • 21

1 Answers1

0

You need to make your bot persistent (so state don't get lost), add error handler (to know if something failed) and fallback route (if there is not route matching).

In that case you'll know what's going wrong and where.

wowkin2
  • 5,895
  • 5
  • 23
  • 66