# markups

#### Introduction

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

If you didn't open the [previous lesson](../lessonsmd-3/sendmessages), 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.

```java
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

```java
public class HandlerToLessonTwo  extends TelegramLongPollingBot {

    @Override
    public String getBotUsername() {
        return Bot.USERNAME;
    }

    @Override
    public String getBotToken() {
        return Bot.TOKEN;
    }

    @Override
    public void onUpdateReceived(Update update) {
        
    }
}
```

Next, let's _Override_ `onUpdateReceived` method

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

```java
public class HandlerToLessonTwo  extends TelegramLongPollingBot {
    
    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()) {
            // Initialization Keyboard Markup
            ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
            // Create the keyboard (list of keyboard rows)
            List<KeyboardRow> keyboard = new ArrayList<>();
            // Create a keyboard row
            KeyboardRow row = new KeyboardRow();
            // Set button, Row 1 Button 1
            row.add("want cute message");
            // Set button, Row 1 Button 2
            row.add("want picture");
            // Add the first row to the keyboard
            keyboard.add(row);
            // Create another keyboard row
            row = new KeyboardRow();
            // Set button, Row 2 Button 1
            row.add("want a song");
            // Add the second row to the keyboard
            keyboard.add(row);
            // Then you can add the number of buttons you need by analogy
            // Set keyboards to the markup
            keyboardMarkup.setKeyboard(keyboard);
            keyboardMarkup.setResizeKeyboard(true);
        }
    }
}
```

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

```java
public class HandlerToLessonTwo  extends TelegramLongPollingBot {
    
    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()){
            // Previous methods
            
            // Initialization received message
            String message_text = update.getMessage().getText();
            // Initialization sendMessage
            SendMessage message = new SendMessage();
            // In advance set chat id to sendMessage
            message.setChatId(update.getMessage().getChatId().toString());

            switch(message_text) {
                case "/start" -> {
                    message.setReplyMarkup(keyboardMarkup);
                    message.setChatId(update.getMessage().getChatId().toString());
                }
                case "want picture" -> message.setText("It`s working!");
                // The following commands are added by analogy
                default -> {
                    message.setReplyToMessageId(update.getMessage().getMessageId());
                    message.setText("no such command");
                }
            }
        }
    }
}
```

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

```java
public class HandlerToLessonTwo  extends TelegramLongPollingBot {

    @Override
    public String getBotUsername() {
        return Bot.USERNAME;
    }

    @Override
    public String getBotToken() {
        return Bot.TOKEN;
    }

    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()){
            // Initialization Keyboard Markup
            ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
            // Create the keyboard (list of keyboard rows)
            List<KeyboardRow> keyboard = new ArrayList<>();
            // Create a keyboard row
            KeyboardRow row = new KeyboardRow();
            // Set button, Row 1 Button 1
            row.add("want cute message");
            // Set button, Row 1 Button 2
            row.add("want picture");
            // Add the first row to the keyboard
            keyboard.add(row);
            // Create another keyboard row
            row = new KeyboardRow();
            // Set button, Row 2 Button 1
            row.add("want a song");
            // Add the second row to the keyboard
            keyboard.add(row);
            // Set keyboard to the markup
            keyboardMarkup.setKeyboard(keyboard);
            keyboardMarkup.setResizeKeyboard(true);
            // Initialization received message
            String message_text = update.getMessage().getText();
            // Initialization sendMessage
            SendMessage message = new SendMessage();
            // In advance set chat id to sendMessage
            message.setChatId(update.getMessage().getChatId().toString());

            switch(message_text) {
                case "/start" -> {
                    message.setReplyMarkup(keyboardMarkup);
                    message.setChatId(update.getMessage().getChatId().toString());
                }
                case "want picture" -> message.setText("It`s working!");
                // The following commands are added by analogy
                default -> {
                    message.setReplyToMessageId(update.getMessage().getMessageId());
                    message.setText("no such command");
                }
            }
            try {
                execute(message); // Sending message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }
}
```

The pictures show the result of executing this code:

> Buttons instead of a keyboard

![Show Work](https://1967353769-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDXJtS5AIvP2jP328wKtj%2Fuploads%2Fgit-blob-a2f9f177a932469271528d2fba7ad62d3bff5a9a%2Fmarkup.png?alt=media)

> Sending a command with the help of buttons

![Show Work](https://1967353769-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDXJtS5AIvP2jP328wKtj%2Fuploads%2Fgit-blob-8e7db841e381759810e72bff3f7f330d2e445a4d%2Fcomands.png?alt=media)

Back to [Lesson 1](../lessonsmd-3/sendmessages) of sending messages to user\
Go to [Lesson 3](../lessonsmd-5/sendphoto) of sending photos to user from local directory
