我正在尝试将 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/