python - CallbackQueryHandler() 不起作用

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

最佳答案

看起来你用过这个 inlinekeyboard.py例子。它对我很有用。
可能您在 TOKEN、互联网连接或其他方面有问题。代码没问题。
可以肯定的是,请将日志记录配置为 DEBUG级别,所以你会看到更详细的日志:

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.DEBUG)

关于python - CallbackQueryHandler() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530045/

相关文章:

python - 如何从 Telegram Bot 接收图像

python - 转换为 super 组时, Telegram 聊天 ID 是否保持不变?

python - 当消息包含多个消息时编译 avro 模式(python)

python - TensorFlow 异常退出,代码为 137

python - 如何测试求和树?

python - Telegram bot API 对于每个联系该机器人的用户来说,chat_id 是唯一的吗?

python-3.x - 如何使用 python-telegram-bot 仅回答最新的用户内联查询?

Python,selenium - 如何刷新页面直到找到项目?

python - 捕获 NameError 和错误处理

python - Telegram 机器人发起与用户的对话