如何在Python电报博特
问题描述:
使用ChosenInlineResultHandler我尝试使用python-telegram-bot如何在Python电报博特
我不知道如何正确处理InlineKeyboardButton。
def start(bot, update):
currencies = [currency for currency in API().get_currencies()]
keyboard = [[InlineKeyboardButton("{}".format(c), callback_data='{}'.format(c))] for c in currencies]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Select the currency you want to exchange:', reply_markup=reply_markup)
updater.dispatcher.add_handler(CommandHandler('start', start))
现在,我需要将它传递给与ChosenInlineResultHandler
帮助另一个函数来处理的选择,但我不知道如何做到这一点。
答
您正在使用内联按钮,返回的查询仅为CallbackQuery
,但不是InlineQuery
,是的,这些名称有点让Telegram Bot API感到困惑。
您可以使用telegram.ext.CallbackQueryHandler
来捕获按下按钮时的查询。
def button_callback(bot, update):
# data is the callback_data where you declared in the buttons
query = update.callback_query.data
if query == "something":
# do something here
updater.dispatcher.add_handler(CallbackQueryHandler(button_callback))
这是如何捕捉按钮数据的一个最简单的例子。您可以查看here以获得完整的示例。