Code

Creating Telegram Bot Buttons in Python: 5 Steps to a Menu

Creating Telegram Bot Buttons in Python: 5 Steps to a Menu

Python for 30 Days: Free Quick Start Course

Learn More

In the first part of our Telegram chatbot tutorial, we developed a simple echo bot using Python and the Aiogram library. The echo bot performs the main task of repeating text sent by the user. If you haven't completed the first part of the tutorial, we highly recommend starting with it to learn the important fundamentals and gain the knowledge you need to continue developing chatbots.

Creating a Chatbot Menu

In the second part of our tutorial, we will focus on improving the user experience by adding a quick response keyboard and inline buttons. These elements will help users easily navigate the Skillbox website. Before we begin creating the menu, it's important to understand the different keyboard types available in the Aiogram library. This knowledge will allow us to effectively customize the interface and improve the user experience when interacting with the bot.

Keyboard Types in Aiogram

The Aiogram library offers developers two main types of keyboards in Python, which differ in functionality and button layout. These keyboards include inline and embedded keyboards. Inline keyboards appear directly below the message input field, allowing users to quickly interact with the bot. Inline keyboards, on the other hand, can be placed anywhere in the message and enable more complex user interactions, such as navigating to other screens or executing commands without having to type. Choosing the right keyboard type can improve the user experience and increase the effectiveness of interaction with the bot.

  • Reply Buttons: These buttons are designed to provide canned responses and can be pinned to the screen in place of the main keyboard. They are often used in chatbots as the main menu and are created using the ReplyKeyboardMarkup method.
  • Inline Buttons: These buttons are placed next to chat messages, allowing the user to see the main keyboard. They are created using the InlineKeyboardMarkup method and provide a more interactive experience.

Telegram Bot Menu Optimization

Our Telegram bot, developed on the Aiogram platform, offers users the ability to send text messages and receive replies. To improve the quality of interaction with users, we will implement the functionality of buttons with preset phrases. These reply buttons will make communication more convenient and intuitive, allowing users to quickly select the desired responses and minimize manual text entry. The implementation of this feature will significantly improve the user experience and increase the effectiveness of communication with the bot.

To implement this functionality, you need to use the ReplyKeyboardMarkup class, as well as a number of additional classes. Let's start by importing them.

The ReplyKeyboardRemove and ReplyKeyboardMarkup classes provide the ability to not only create custom keyboards but also remove them if needed. In this context, the KeyboardButton class is used to create individual buttons on the keyboard. These tools allow you to effectively manage user interactions, providing flexibility in customizing the chatbot interface. Proper use of these classes improves the user experience and helps create an intuitive interface for communication.

Creating buttons with predefined phrases is an important aspect of web development. In this process, we will write code that will allow users to quickly send predefined messages. Next, we will go through each part of the code in detail so that you can understand its structure and functionality.

In our main decorator, we will start by creating a kb list, which will serve to store the buttons. Each button in Aiogram is created using the types.KeyboardButton(text=»«) constructor, where the text parameter specifies the text displayed on the button. This allows us to create an interactive interface for users, simplifying interaction with the bot. Proper use of buttons will significantly improve the user experience and increase the bot's efficiency.

To create a keyboard with buttons in a Telegram bot, you need to use the types.ReplyKeyboardMarkup(keyboard=list) method. Instead of list, you specify the name of the list with buttons. In this example, we are using the kb list. This method allows you to quickly and easily create a user interface, ensuring intuitive interaction with the bot and improving the user experience.

To display the keyboard in a Telegram chat, you need to use the reply_markup=keyboard parameter in the response to the /start command. This will display the keyboard with the buttons that we previously defined in the kb list. After launching the bot, users will see both buttons directly in the chat, which will simplify interaction and make it more convenient.

Screenshot: Aiogram / Skillbox Media

When you click on any of buttons, the text contained on the button will be sent to the chat. After that, the Telegram bot will respond by repeating the same phrase.

Screenshot: Aiogram / Skillbox Media

Create an unlimited number of templates and link each button With new actions. In the next section, we'll take a closer look at how to use inline keyboards to enhance communication interactivity. Using inline keyboards improves the user experience by allowing users to quickly respond to messages and perform actions without typing. This significantly simplifies interaction and makes communication more dynamic.

Creating Inline Buttons Using Aiogram

Inline buttons are an effective means of interaction in Telegram. Unlike standard keyboards, they are directly linked to chat messages. A striking example of their use can be seen in the @BotFather channel, where inline buttons simplify the process of creating a token for accessing the Telegram API. In this context, the inline menu demonstrates the convenience of working with existing bots, making interaction more intuitive and faster. Inline buttons significantly improve the user experience, allowing users to quickly perform actions and get the information they need.

Screenshot: Aiogram / Skillbox Media

Let's create inline buttons for Python bot that will direct users to Skillbox Media pages and programming courses. Let's start by adding the necessary classes in the imports section of our code. This will allow us to integrate buttons into the bot interface and improve user interaction by providing them with direct access to relevant resources and materials.

The InlineKeyboardMarkup class is used to initialize inline buttons, and the InlineKeyboardButton class is used to create them. Now let's look at the process of creating inline buttons in more detail.

To initialize the keyboard, we use the InlineKeyboardMarkup class with the row_width parameter set to 1. This allows us to specify the number of buttons placed on a single line. Given that the text on the buttons is quite long, a vertical arrangement of the buttons is the optimal solution. This approach improves perception and ease of interaction with the interface.

Let's create separate variables for each button and initialize the Button class, passing the necessary parameters. This will allow us to easily manage the properties and behavior of each button in our project. Using separate variables for buttons will improve the readability of the code and simplify its further modification. It will also ensure better organization of the logic of interaction with interface elements, which will positively impact the user experience.

  • text - the text that will be displayed on the button in the messenger;
  • url - the link the user will be redirected to when clicking the button.

After adding buttons to the keyboard using the add method, we can move on to creating message_handler, which will be responsible for displaying inline buttons in Telegram. This process is similar to working with regular buttons, but links are specified as the command. In the reply_markup parameter, we pass the name of our keyboard - urlkb. This allows users to interact with the bot more conveniently and effectively, providing them with access to the necessary information in just one click.

After launching our bot, we can evaluate its effectiveness and functionality. This will allow us to see how it handles assigned tasks and responds to user commands. During testing, we'll be able to identify the bot's strengths and weaknesses, as well as areas for further improvement and optimization.

Screenshot: Aiogram / Skillbox Media

The result exceeded all expectations. Now the inline keyboard appears automatically when sending a command / link to the bot. This significantly improves user interaction with the bot and allows for providing additional information more conveniently and effectively. The inline keyboard simplifies access to essential functions, making communication with the bot more intuitive. Improving the bot's functionality contributes to its popularity and ease of use.

Final thoughts on echo bot functionality

The echo bot for Telegram, built using Python, now offers two types of menus: response buttons for quick interaction and inline buttons that provide access to the Skillbox blog and website. These innovations significantly improve the user experience, allowing important information to be quickly and conveniently obtained. The optimized interface and functionality of the bot make interaction with it more efficient and intuitive.

If you are looking to create more complex bots, for example, for online stores, we strongly recommend studying the official documentation of the Aiogram library. This will help you master new classes, methods, and objects, which in turn will significantly expand the functionality of your bots. Understanding Aiogram's capabilities will open up new development horizons, allowing you to create more interactive and useful applications for users. Additional learning resources include a variety of online platforms, learning materials, and specialized courses. These resources will help you deepen your knowledge on topics of interest and improve your skills. We recommend using courses on platforms such as Coursera, Udemy, and edX, where you can find high-quality lectures and practical exercises. You should also consider library resources and research articles that provide up-to-date information and research in your chosen field. Don't forget about forums and communities where you can ask questions and share experiences with like-minded people. Using these resources will significantly improve the effectiveness of your learning and help you master new concepts.

  • C# for Beginners: Debunking Myths and Creating a Simple Chatbot
  • Test: Which Programming Language Will You Choose — Java or Python?
  • What Can and Cannot Be Done in JavaScript

Python Developer: 7 Steps to a Successful Career

Want to become a Python developer? Learn 7 key steps to a successful career!

Learn more