-
Notifications
You must be signed in to change notification settings - Fork 0
InlineKeyboard Example
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.
if __name__ == '__main__':
main()
Lines 68 to 69 tell python that after starting the script, it's supposed to call the main function
updater = Updater("TOKEN")
The 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.
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(CommandHandler('help', help_command))
Line 56 to 58 registers our three 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_command
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.
updater.start_polling()
Line 61 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 65 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:
def start(update: Update, context: CallbackContext) -> None:
Line 20 we define a function called start. It takes the two arguments update (instance of an Update) and context (instance of a CallbackContext). The ->
indicates to a type checker that this function returns nothing.
keyboard = [
[
InlineKeyboardButton("Option 1", callback_data='1'),
InlineKeyboardButton("Option 2", callback_data='2'),
],
[InlineKeyboardButton("Option 3", callback_data='3')],
]
Line 22 to 28 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 30 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 32 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
def button(update: Update, context: CallbackContext) -> None:
Line 35 we define a function called button. It takes the two arguments update and context, basically the same as start
.
query = update.callback_query
Line 37 query is defined as a shortcut to access the provided CallbackQuery. This is the part of the update which has all the information in it, remember, it gets generated/send to the bot once a user presses a button.
query.answer()
Line 41 here we answer the CallbackQuery
. We use a convenient shortcut PTB provides. It takes care of calling the actual function and passing all the required parameters to it. If you check out the function, you see that you can pass a text
argument to it, which will be displayed in a little pop-up on the client end, and if you pass show_alert
on top of it, the user has to dismiss the pop-up. Not useful for this example, so we just pass it without these optional arguments.
query.edit_message_text(text=f"Selected option: {query.data}")
Line 43 then we edit the message where 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.
def help_command(update: Update, _: CallbackContext) -> None:
update.message.reply_text("Use /start to test this bot.")
Line 46 to 48 in this simple callback, we reply to the /help command with the provided text, that they should use /start to use this bot.
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
Line 14 to 17 are the only lines of the code we haven't covered yet. Here we set up the logging module to have the format we want, and we define logger in case we want to use it later. More docs regarding logging can be found here
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.
- Wiki of
python-telegram-bot
© Copyright 2015-2025 – Licensed by Creative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests