D
D
deathmurder2020-02-06 02:13:36
Java
deathmurder, 2020-02-06 02:13:36

How to create multilevel menu in TelegramBots API JAVA?

I can’t figure out how to make a multi-level inlineKeybords menu in JAVA in Telegram. I tried to add conditions for both callbackQuery and getMessage. The result is the same, the check fails and the menu does not open. Google searched all over. There are such tutorials in Python, but I don't even know the syntax in Python.

import org.telegram.telegrambots.ApiContextInitializer;

import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.List;

public class Bot extends TelegramLongPollingBot {
    public static void main(String[] args) {
        System.out.println("бот стартанул");
        disableWarning();
        ApiContextInitializer.init();

        TelegramBotsApi botsApi = new TelegramBotsApi();
        try {
            botsApi.registerBot(new Bot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
    public String getBotUsername() {
        return "BotName";
        //возвращаем юзера
    }

    public void onUpdateReceived(Update update) {
        if (update.hasMessage()) {
//            Message msg = update.getMessage(); // Это нам понадобится
//            String txt = msg.getText();
            if (update.getMessage().hasText()) {
                if (update.getMessage().getText().equals("/start")) {
                    sendMsg(update.getMessage(), "Hello, world! This is simple bot!");
                    try {

                        execute(sendInlineKeyBoardMessage(update.getMessage().getChatId()));
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
                }
//                } else if (update.getMessage().getText().equals("Алтайский край")) {
//                    try {
//                        execute(sendCityOfStateMenuAltay(update.getMessage().getChatId()));
//                    } catch (TelegramApiException e) {
//                        e.printStackTrace();
//                    }
//                }

//                else if (update.getCallbackQuery().getMessage().getText().equals("/Алтайский край")) {
//                    try {
//                        execute(sendCityOfStateMenuAltay(update.getMessage().getChatId()));
//                    } catch (TelegramApiException e) {
//                        e.printStackTrace();
//                    }
//                }
            }
        } else if (update.hasCallbackQuery()) {
//            try {
//                execute(sendMsg(update.getMessage());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            if (update.getCallbackQuery().getData().equals("Алтайский край")) {
                try {
                    execute(new SendMessage().setText(
                            update.getCallbackQuery().getData())
                            .setChatId(update.getCallbackQuery().getMessage().getChatId()));
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
//            }
        }
    }


    @Override
    public String getBotToken() {
        return "";
    }
    private void sendMsg(Message msg, String text) {

        SendMessage s = new SendMessage();
        s.setChatId(msg.getChatId());
        s.setText(text);

        try { 
            execute(s);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }


    public static SendMessage sendInlineKeyBoardMessage(long chatId) {
        InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
        InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton();
        InlineKeyboardButton inlineKeyboardButton2 = new InlineKeyboardButton();
        inlineKeyboardButton1.setText("Алтайский край");
        inlineKeyboardButton1.setCallbackData("Алтайский край");
        inlineKeyboardButton2.setText("Амурская область");
        inlineKeyboardButton2.setCallbackData("Амурская область");
        List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>();
        List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>();
        keyboardButtonsRow1.add(inlineKeyboardButton1);
        keyboardButtonsRow2.add(inlineKeyboardButton2);
        List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
        rowList.add(keyboardButtonsRow1);
        rowList.add(keyboardButtonsRow2);
        inlineKeyboardMarkup.setKeyboard(rowList);
        return new SendMessage().setChatId(chatId).setText("Выберите область").setReplyMarkup(inlineKeyboardMarkup);
    }
    public static SendMessage sendCityOfStateMenuAltay(long chatId) {
        InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
        InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton();
        InlineKeyboardButton inlineKeyboardButton2 = new InlineKeyboardButton();
        inlineKeyboardButton1.setText("Барнаул");
        inlineKeyboardButton1.setCallbackData("Барнаул");
        inlineKeyboardButton2.setText("Бийск");
        inlineKeyboardButton2.setCallbackData("Бийск");
        List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>();
        List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>();
        keyboardButtonsRow1.add(inlineKeyboardButton1);
        keyboardButtonsRow2.add(inlineKeyboardButton2);
        List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
        rowList.add(keyboardButtonsRow1);
        rowList.add(keyboardButtonsRow2);
        inlineKeyboardMarkup.setKeyboard(rowList);
        return new SendMessage().setChatId(chatId).setText("Выберите город").setReplyMarkup(inlineKeyboardMarkup);
    }


    public static void disableWarning() {
        System.err.close();
        System.setErr(System.out);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
deathmurder, 2020-03-28
@deathmurder

in this piece of code

execute(new SendMessage().setText(
                            update.getCallbackQuery().getData())
                            .setChatId(update.getCallbackQuery().getMessage().getChatId()));

we need to pass our menu handler method to the arguments, to which it is necessary to pass the chatId and the string value that comes from the CallbackQuery by the getData () method, the string value itself is already processed directly in the method that displays the menu
execute(menu(update.getCallbackQuery().getMessage().getChatId(), update.getCallbackQuery().getData());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question