CallbackQueryHandler() 不起作用

ata*_*eao 6 python python-telegram-bot

我正在尝试将 CallbackQueryHandler() 与我的机器人一起使用,但我无法使其适用于我的情况。我尝试的机器人代码与此示例相同:

def start(bot, update):
    keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
                 InlineKeyboardButton("Option 2", callback_data='2')],
                [InlineKeyboardButton("Option 3", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('Please choose:', reply_markup=reply_markup)

def button(bot, update):
    query = update.callback_query
    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)
def help(bot, update):
    update.message.reply_text("Use /start to test this bot.")

def error(bot, update, error):
    logging.warning('Update "%s" caused error "%s"' % (update, error))

# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
Run Code Online (Sandbox Code Playgroud)

wow*_*in2 0

看起来您使用了这个inlinekeyboard.py示例。这对我来说很有用。

可能您遇到了令牌、互联网连接或其他问题。代码没问题。

可以肯定的是,请将日志记录配置为DEBUG级别,以便您会看到更详细的日志:

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.DEBUG)
Run Code Online (Sandbox Code Playgroud)