⬆️ ⬇️

Built-in buttons in Telegram Bot API

Good afternoon, dear readers, let's consider what the main types of built-in buttons telegram chat bots offer and what are their features. The article will be useful to anyone who wants to understand the possibilities of interaction with telegram users in version bot API 2.0.



For an overview of the possibilities, we will need to install 3 point 2 Pythonʻa and a couple of spoons of pyTelegramBotAPI . We will not consider the features of setting up and registering a chat bot, since There are many articles on this topic.



And so, what is the built-in buttons (keyboard) in the Telegram messenger? These are buttons that are displayed in the internal chat area and are tied to a specific message. They are rigidly 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 a button, use the InlineKeyboardMarkup type, 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 buttons is intended to redirect the user by reference, with a corresponding warning. The button has a corresponding tab in the upper right corner to let the user know that this is a link.



Switch buttons



This type of buttons is designed to redirect the user to any 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 will automatically do this (helping newcomers to get acquainted with the inline mode).



In order to create such a switch, you must specify the argument switch_inline_query, 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 press a button and select a chat, this is what happens:

Step 1:



Push the button.



Step 2 :



Choose a chat.



Step 3:



The built-in inline mode has been activated.



Callback buttons



Finally, the most interesting thing is the feedback buttons: they allow you to dynamically update the message / built-in buttons (without clogging the tape), as well as display a notification at the top of the chat bot or a modal window.



For example, they can be used to view a long message, similarly to the pagination of pages on websites, or for example to make a calendar. I will not reinvent the wheel, but through a search on GitHub, I will find the calendar-telegram library. Following these instructions, we get a ready-made calendar, which 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 the date, for this it is enough to specify a message in the answer:



 bot.answer_callback_query(call.id, text=" ") 




(Example in the desktop version)





(Example in the mobile version)



If we change show_alert to True , then 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 sensational Telegram messenger. That is why it is important to pick up the trend and deal with its main features, because Various methods of interacting with bots make life easier for developers and users.







Thank you for your interest in this topic.

Source: https://habr.com/ru/post/335886/



All Articles