# InlineKeyboard Example(翻譯) ###### tags: `telegram` `bot` >[name=shaoe.chen][time=Fri, Mar 13, 2020] :::danger [官方文件](https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example) ::: ## Introduction Hey,這個wiki page會引導你順一次[這邊](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py)的inline keyboard的範例。我們會用python切入這個範例,然後依循這個程式碼,我們期待使用者可以透過它來做屬於自己的變化。我們開始吧。 聲明:我們會忽略掉import package的部份。 ### Startup ```python if __name__ == '__main__': main() ``` [Line 63 to 64](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L63-L64),告知python在啟動腳本之後,它應該呼叫這個mail function。 ### main ```python updater = Updater("TOKEN", use_context=True) ``` [First line](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L48),main function的第一行,從[Updater class](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.updater.html)建立一個updater instance。"TOKEN"的部份是放置你的bot的token,use_context意味著我們使用基於上下文的handlers。 ```python 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) ``` [Line 50 to 53](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L50-53)註冊四個handlers。第一個handler是[CommandHandler](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.commandhandler.html)。每當用戶發送命令`/start`給bot的時候,function `start`就會被呼叫。第三個handler也是相同的情況:每當用戶發送命令`/help`的時候,function `help`就會被呼叫。 第二個handler是[CallbackQueryHandler](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.callbackqueryhandler.html)。Callbackquery就是用戶點擊[InlineButton](https://python-telegram-bot.readthedocs.io/en/stable/telegram.callbackquery.html)之後發送的內容。每次點擊按鈕都會發送到`button` handler。 最後一個hanlder是[error handler](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.dispatcher.html#telegram.ext.Dispatcher.add_error_handler)。在update之後引起的每一個異常,不論它是在那邊觸發的,都會送到`error` handler並在那邊處理。 ```python updater.start_polling() ``` [Line 56](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L56)告訴PTB library使用polling啟動bot,這意味著libary會持續的發送request到telegram servers並從telegram servers取得新的update(如果有)。 ```python updater.idle() ``` [Line 60](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L60)實際執行bot,一直到發送終止信號。 ### start ```python def start(update, context): ``` [Line 18](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L18)我們定義一個function,命名為start。該function帶有兩個參數,update([Update](https://python-telegram-bot.readthedocs.io/en/stable/telegram.update.html)的instance)與context([CallbackContext](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.callbackcontext.html)的instance)。 ```python keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'), InlineKeyboardButton("Option 2", callback_data='2')], [InlineKeyboardButton("Option 3", callback_data='3')]] ``` Line 19 to 22定義一個變數,命名為keyboard。內含兩個list。第一個list定義的是row,list內的list定義的是column~(大概這個意思)~。因此這個keyboard會有兩個rows,Option 1與Option 2會是第一個row,而Option 3是第二個row。 ```python reply_markup = InlineKeyboardMarkup(keyboard) ``` Line 24將我們的list轉為實際的Inline Keyboard,我們可以隨著訊息一起傳遞。 ```python update.message.reply_text('Please choose:', reply_markup=reply_markup) ``` Line26我們以文本回覆update message(因此為[reply_text](https://python-telegram-bot.readthedocs.io/en/stable/telegram.message.html#telegram.Message.reply_text)),並在參數`reply_markup`中傳遞keyboard。 現在我們期待人們點擊其中一個按鈕,讓我們跳到button callback吧。 ### button ```python def button(update, context): ``` Line 29我們定義一個function,命名為button。帶兩個參數,`update`與`context`。 ```python query = update.callback_query query.edit_message_text(text="Selected option: {}".format(query.data)) ``` Line 30 to 32的query定義為訪問所提供的callbackquery的捷徑。然後我們用`text`來編輯callbackquery的來源訊息,我們告知用戶我們選擇了那一個選項~(就是點擊那一個按鈕)~。我們插入`query.data`到字串,這個`query.data`是我們定義在keyboard的資料,也就是數字1, 2, 或3~(可以回頭看上面button的callback_data)~。因為我們並不會再次的傳遞inline keyboard, 因此inline keyboard將會消失。 ### help ```python def help(update, context): update.message.reply_text("Use /start to test this bot.") ``` [Line 35 to 36](def error(update, context): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, context.error)),在這個簡單的callback中,我們用文本來回應命令`/help`,告知用戶應該用`/start`來使用這個bot。 ### error ```python def error(update, context): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, context.error) ``` [Line 39 to 41,](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py#L39-41)我們單純的在logger中記錄update所引起的異常。 ___