markups

Introduction

In this lesson we will learn how to make buttons in the keyboard area

If you didn't open the previous lesson, don't forget to make a Bot class with a token and name of the bot, as well as connect libraries.

Process

The main function remains unchanged, so just copy it.

public class Main {

    public static void main(String[] args) {
        // Creating a bot
        TelegramBotsApi telegramBotsApi;
        try {
            telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            // Registration of our bot
            // new Handler() is our class, so there is no need to import the proposed library
            telegramBotsApi.registerBot(new HandlerToLessonTwo());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }

    }
}

Then we'll move on to the HandlerToLessonTwo class.

The initial parameters that we did in the last lesson

Next, let's Override onUpdateReceived method

To begin with, let's create the buttons themselves using ReplyKeyboardMarkup class

Next, we will add processing of commands entered using the buttons

Do not forget that when sending a message, it must have chat id and text

After that, the addition and operation of the buttons is completely done, you should have the following code:

You can add the number of buttons you need by analogy

The pictures show the result of executing this code:

Buttons instead of a keyboard

Show Work

Sending a command with the help of buttons

Show Work

Back to Lesson 1 of sending messages to user Go to Lesson 3 of sending photos to user from local directory

Last updated