Hi! I've noticed that a lot of dudes starting to understand telegram bot development (in aiogram for sure) have problems with understanding the logic of inline buttons. Now I will try to explain by example how these buttons and their handlers are organized in Aiogram v.3. At the end of the article there will be a link to the whole code.
First, you need to understand that an inline button is a button assigned to message.
Message handlers are used for the bot to respond to a normal message/button press from the bottom menu. But there is no message when the inline button is pressed, so how to handle them?
Telegram provides the 'CallbackQuery' class, which inherits from the 'TelegramObject' class. The objects of this class are the inline button calls that need to be processed.
*Тыкню чтобы посмотреть картинку*
Holy shit, we found the class. What does that mean? It means that we also have all the necessary methods to work with objects of this class. And we can write a handler.
But first of all, let's define what we're doing. To make it more interesting, I set the following task: to create an inline keyboard with the text of buttons from some list of string.
My list:
*Тыкню чтобы посмотреть картинку*
Okay, we have a list. Now we just need to create a keyboard. Well, there is a class 'InlineKeyboardMarkup' for that. Keyboards are objects of this class.
What is an inline button? It is an object of class 'InlineKeyboardButton' that has text and callback data.
*Тыкню чтобы посмотреть картинку*
What is a keyboard? A keyboard is a list of lists of keys. Where nested lists are keyboard rows (buttons in one row), and the main list is actually a list of such horizontal sets of buttons.
Keyboard creation:
*Тыкню чтобы посмотреть картинку*
Fucking great, we have a keyboard with the buttons on the list. Now we've got to get it linked to messages. Here's how we do it:
*Тыкню чтобы посмотреть картинку*
Great, it's time for the handler. Let our bot just write which button was pressed in response to its pressing.
*Тыкню чтобы посмотреть картинку*
That's it, the whole code looks like this:
*Тыкню чтобы посмотреть картинку*
An example of how the bot works:
*Тыкню чтобы посмотреть картинку*
First, you need to understand that an inline button is a button assigned to message.
Message handlers are used for the bot to respond to a normal message/button press from the bottom menu. But there is no message when the inline button is pressed, so how to handle them?
Telegram provides the 'CallbackQuery' class, which inherits from the 'TelegramObject' class. The objects of this class are the inline button calls that need to be processed.
*Тыкню чтобы посмотреть картинку*
Holy shit, we found the class. What does that mean? It means that we also have all the necessary methods to work with objects of this class. And we can write a handler.
But first of all, let's define what we're doing. To make it more interesting, I set the following task: to create an inline keyboard with the text of buttons from some list of string.
My list:
*Тыкню чтобы посмотреть картинку*
Okay, we have a list. Now we just need to create a keyboard. Well, there is a class 'InlineKeyboardMarkup' for that. Keyboards are objects of this class.
What is an inline button? It is an object of class 'InlineKeyboardButton' that has text and callback data.
*Тыкню чтобы посмотреть картинку*
What is a keyboard? A keyboard is a list of lists of keys. Where nested lists are keyboard rows (buttons in one row), and the main list is actually a list of such horizontal sets of buttons.
Keyboard creation:
*Тыкню чтобы посмотреть картинку*
Fucking great, we have a keyboard with the buttons on the list. Now we've got to get it linked to messages. Here's how we do it:
*Тыкню чтобы посмотреть картинку*
Great, it's time for the handler. Let our bot just write which button was pressed in response to its pressing.
*Тыкню чтобы посмотреть картинку*
That's it, the whole code looks like this:
*Тыкню чтобы посмотреть картинку*
An example of how the bot works:
*Тыкню чтобы посмотреть картинку*
READ OTHER POSTS