python电报机器人中的回调查询处理程序问题

VIS*_*IRE 5 python python-telegram-bot

在我的代码中,我面临callbackquery处理程序的问题,当我点击/start命令Next按钮出现时,当我点击该按钮时,它给我回复hi,直到此输出正确为止。然后当我点击另一个命令/help然后帮助按钮出现时,当我点击那个帮助按钮然后它给我相同的答复 next button is hi

结论:有没有办法杀死旧的callbackquery处理程序。我发现方法是Conversationhandler.ENDcallbackquery处理程序函数返回,但它限制了我在谷歌上搜索的功能,但没有找到预期的输出。

这是我的代码:

 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()
Run Code Online (Sandbox Code Playgroud)

这是代码截图输出的更多细节

欢迎任何形式的帮助。

wow*_*in2 0

您需要使您的机器人持久化(这样状态就不会丢失),添加错误处理程序(以了解是否失败)和后备路由(如果没有路由匹配)。

在这种情况下,您就会知道出了什么问题以及出在哪里。