Built-in buttons in Telegram Bot API - pyTelegramBotAPI
Good afternoon, dear readers, let's look at the main types of built-in buttons that telegram chat bots offer and what are their features. The article will be useful to anyone who wants to understand the possibilities of interacting with telegram users in the version of bot API 2.0.
To review the possibilities, we need to install 3 point 2 tenths of Python and a couple of spoons pyTelegramBotAPI . We will not consider the features of setting up and registering a chat bot, because There are many articles on this topic.
And so, what exactly are the built-in buttons (keyboard) in the Telegram messenger? These are buttons that appear in the internal area of the chat and are attached to a specific message. They are tightly connected with the message (if you delete the message, the internal buttons are also deleted along with it.). They make it possible to dynamically modify it.
There are currently three types of built-in buttons:
To create the button, the InlineKeyboardMarkup type is used , let's create the "Our site" button:
Here the name speaks for itself, this type of button is designed to redirect the user to the link, with the appropriate warning. The button has a corresponding label in the upper right corner to make it clear to the user that this is a link.
This type of button is designed to redirect the user to a chat, with the subsequent activation of the (built-in) inline-mode of communication with the bot. This mode can be activated manually: in the chat, enter: "@ bot name", but the switch-buttons allow this to be done automatically (helping newcomers get acquainted with the inline-mode).
In order to create such a switch, you must specify the switch_inline_query argument either empty or with some text.
Now, if we click on the button and select the chat, this is what happens:
Step 1:
Click on the button.
Step 2 :
Choose a chat.
Step 3:
The built-in inline mode is activated.
And finally, the most interesting thing is the feedback buttons: they allow you to dynamically update the message / built-in buttons (without clogging the feed at the same time), as well as display a notification at the top of the chat bot or modal window.
For example, they can be used to view a long message, similar to pagination of pages on sites, or for example to make a calendar. I will not reinvent the wheel, but through a search on GitHub, I will find a ready-made calendar-telegram library . By following these instructions, we get a finished calendar that can be dynamically changed by clicking on the appropriate buttons:
You can also add a notification by clicking on a date, for this it is enough to indicate the message in the response:
(Example in the desktop version)
(Example in the mobile version)
If we change show_alert to True , we get a modal window:
According to the latest data, more than 600k users are registered daily in the acclaimed Telegram messenger. That is why it is important to pick up a trend and deal with its main features, as various methods of interacting with bots greatly simplifies the lives of developers and users.
Thank you for your interest in this topic.
To review the possibilities, we need to install 3 point 2 tenths of Python and a couple of spoons pyTelegramBotAPI . We will not consider the features of setting up and registering a chat bot, because There are many articles on this topic.
And so, what exactly are the built-in buttons (keyboard) in the Telegram messenger? These are buttons that appear in the internal area of the chat and are attached to a specific message. They are tightly connected with the message (if you delete the message, the internal buttons are also deleted along with it.). They make it possible to dynamically modify it.
There are currently three types of built-in buttons:
URL buttons
To create the button, the InlineKeyboardMarkup type is used , let's create the "Our site" button:
@bot.message_handler(commands = ['url'])
def url(message):
markup = types.InlineKeyboardMarkup()
btn_my_site= types.InlineKeyboardButton(text='Наш сайт', url='https://habrahabr.ru')
markup.add(btn_my_site)
bot.send_message(message.chat.id, "Нажми на кнопку и перейди на наш сайт.", reply_markup = markup)
Here the name speaks for itself, this type of button is designed to redirect the user to the link, with the appropriate warning. The button has a corresponding label in the upper right corner to make it clear to the user that this is a link.
Switch buttons
This type of button is designed to redirect the user to a chat, with the subsequent activation of the (built-in) inline-mode of communication with the bot. This mode can be activated manually: in the chat, enter: "@ bot name", but the switch-buttons allow this to be done automatically (helping newcomers get acquainted with the inline-mode).
In order to create such a switch, you must specify the switch_inline_query argument either empty or with some text.
@bot.message_handler(commands = ['switch'])
def switch(message):
markup = types.InlineKeyboardMarkup()
switch_button = types.InlineKeyboardButton(text='Try', switch_inline_query="Telegram")
markup.add(switch_button)
bot.send_message(message.chat.id, "Выбрать чат", reply_markup = markup)
Now, if we click on the button and select the chat, this is what happens:
Step 1:
Click on the button.
Step 2 :
Choose a chat.
Step 3:
The built-in inline mode is activated.
Callback buttons
And finally, the most interesting thing is the feedback buttons: they allow you to dynamically update the message / built-in buttons (without clogging the feed at the same time), as well as display a notification at the top of the chat bot or modal window.
For example, they can be used to view a long message, similar to pagination of pages on sites, or for example to make a calendar. I will not reinvent the wheel, but through a search on GitHub, I will find a ready-made calendar-telegram library . By following these instructions, we get a finished calendar that can be dynamically changed by clicking on the appropriate buttons:
@bot.message_handler(commands=['calendar'])
def get_calendar(message):
now = datetime.datetime.now() #Текущая дата
chat_id = message.chat.id
date = (now.year,now.month)
current_shown_dates[chat_id] = date #Сохраним текущую дату в словарь
markup = create_calendar(now.year,now.month)
bot.send_message(message.chat.id, "Пожалйста, выберите дату", reply_markup=markup)
You can also add a notification by clicking on a date, for this it is enough to indicate the message in the response:
bot.answer_callback_query(call.id, text="Дата выбрана")
(Example in the desktop version)
(Example in the mobile version)
If we change show_alert to True , we get a modal window:
bot.answer_callback_query(call.id, show_alert=True, text="Дата выбрана")
Conclusion
According to the latest data, more than 600k users are registered daily in the acclaimed Telegram messenger. That is why it is important to pick up a trend and deal with its main features, as various methods of interacting with bots greatly simplifies the lives of developers and users.
Thank you for your interest in this topic.