Skip to content

InlineKeyboard Example

Fox Larsson edited this page Mar 28, 2020 · 14 revisions

Introduction

Hey, this wiki page will walk you through the inline keyboard example found here. We will start with how python starts with the example, then follow through the code in the same way we expect updates from the user would go through it. Let's do it.

Disclaimer: We will conveniently ignore the imports.

Startup

if __name__ == '__main__':
    main()

Lines 63 to 64 tell python that after starting the script, it's supposed to call the main function

main

updater = Updater("TOKEN", use_context=True)

First line in the main function, it creates an updater instance from the Updater class. The "TOKEN" part is where you put the bot token, use_context means that we use context based handlers.

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 registers our four handlers. The first handler is a CommandHandler. Whenever an user sends a /start command to the bot, the function start is called. Same situation with the third handler: Whenever an user sends the /help command, help gets called.

The second handler is a CallbackQueryHandler. A Callbackquery is what an user sends after he presses an InlineButton. Every press of a button gets send to the button handler.

The last handler is an error handler. Every error which was raised after an update, wherever it happens in this code, will get send to the error handler and can be dealt with there.

updater.start_polling()

Line 56 tells the PTB library to start the bot using polling, which means that the library will continuously make a request to the telegram servers and get new updates from there, if they exists.

updater.idle()

Line 60 actually runs the bot until a termination signal is send.

Let's start our way through the handlers in the same way we would expect an user to go through it: With the start handler:

start

def start(update, context):

Line 18 we define a function called start. It takes the two arguments update (instance of an Update) and context (instance of a CallbackContext).

keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
             InlineKeyboardButton("Option 2", callback_data='2')],

          [InlineKeyboardButton("Option 3", callback_data='3')]]

Line 19 to 22 a variable called keyboard is defined. It is a double list. Every entry in the first list is a row in the actual inline keyboard, every entry in the list entry is a column. So this keyboard will have two rows, Option 1 and Option 2 will be in the first; Option 3 in the second one.

reply_markup = InlineKeyboardMarkup(keyboard)

Line 24 turns our list into an actual Inline Keyboard that we can pass along with our message.

update.message.reply_text('Please choose:', reply_markup=reply_markup)

Line 26 we reply to the update message with a text (hence reply_text) and pass the keyboard along in the reply_markup argument.

Now we expect people to press one of the provided buttons, so let's jump to the button callback

button

def button(update, context):

Line 29 we define a function called button. It takes the two arguments update and context.

query = update.callback_query

query.edit_message_text(text="Selected option: {}".format(query.data))

Line 30 to 32 query is defined as a shortcut to access the provided callbackquery. Then we edit the message where to callbackquery originates from with the text where we tell the user which option we picked. We insert query.data into the string, which is the data we defined in the keyboard, so the number 1, 2 or 3. Since we don't pass the inline keyboard along again, it will disappear.

help

def help(update, context):
    update.message.reply_text("Use /start to test this bot.")

Line 35 to 36 in this simple callback, we reply to the /help command with the provided text, that they should use /start to use this bot.

error

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

Line 39 to 41 we simply log in the logger that the provided update raised the provided error.


This section of the wiki is currently in development, feedback is greatly appreciated. Ping one of the admins in our telegram group to anything you want to tell us.

Clone this wiki locally