GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — InlineKeyboard Example InlineKeyboard Example
Mahmut Uzunpostalci edited this page
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 application = Application.builder().token("TOKEN").build() The first line in the main function builds an application instance from the Application class. The function calls lined up after another means that the calls happen on the return of the previous call. So application.add_handler(CommandHandler("start", start)) application.add_handler(CallbackQueryHandler(button)) application.add_handler(CommandHandler("help", help_command)) Line 60 to 62 registers our three handlers. The first handler is a CommandHandler. Whenever a user sends a /start command to the bot, the function The second handler is a CallbackQueryHandler. A Callbackquery is what Telegram sends to our bot when a user presses an InlineButton. Every press of a button gets sent to the application.run_polling() Line 65 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. Let's start our way through the handlers in the same way we would expect a user to go through it. This means we begin with the start handler: async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: In line 24 we define a function called start. It is an async function and it takes the two arguments update (instance of an Update) and context (instance of a CallbackContext). The context is the default context type, since we didn't change anything with it. If you want to see how that works, checkout the ContextType bot example. The keyboard = [ [ InlineKeyboardButton("Option 1", callback_data='1'), InlineKeyboardButton("Option 2", callback_data='2'), ], [InlineKeyboardButton("Option 3", callback_data='3')], ] Line 26 to 32 a variable called keyboard is defined. It is a list of lists, representing a 2D-Matrix. Every "parent" list is a row in the actual inline keyboard (so reply_markup = InlineKeyboardMarkup(keyboard) Line 34 turns our list into an actual Inline Keyboard that we can pass along with our message. await update.message.reply_text('Please choose:', reply_markup=reply_markup) In line 36 we reply to the update message with a text (hence reply_text) and pass the keyboard along in the Now we expect people to press one of the provided buttons, so let's jump to the button callback async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: Line 39 defines a function called button. It takes the two arguments update and context and returns nothing. Basically the same as query = update.callback_query Line 41 defines query 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. await query.answer() Line 45 here we answer the await query.edit_message_text(text=f"Selected option: {query.data}") Line 47 edits the message where async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Use /start to test this bot.") Line 50 to 52 is a simple callback. Here we reply to the /help command with the provided text: 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 18 to 21 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
Clone this wiki locally |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |