2017-09-14 103 views
0

使用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幫助另一個函數來處理的選擇,但我不知道如何做到這一點。

回答

0

您正在使用內聯按鈕,返回的查詢僅爲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以獲得完整的示例。