【问题标题】:Callbackquery handler issue in python telegram botpython电报机器人中的回调查询处理程序问题
【发布时间】:2018-02-13 11:54:29
【问题描述】:

在我的代码中,我遇到了callbackquery 处理程序的问题,当我点击/start 命令时出现下一步 按钮,当我点击该按钮时它会给我回复作为hi,直到这个输出是正确的。然后当我点击另一个命令/help 然后出现 help 按钮,当我点击那个 help 按钮然后它给我相同的答复下一个按钮是 hi

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

这是我的代码:

 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

欢迎任何形式的帮助。

【问题讨论】:

    标签: python python-telegram-bot


    【解决方案1】:

    你需要make your bot persistent(这样状态不会迷路),add error handler(知道是否失败)和回退路由(如果没有路由匹配)。

    这样你就会知道哪里出了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多