javatelegramtelegram-api

create/update main menu based on user's preferred language


I would like to customize the main menu texts based on the customer language preference, but I have not seen any document about how to create / update the main menu using java.

I can create Inline-Menu using the InlineKeyboardButton and InlineKeyboardMarkup classes.

I can create Keyboard-Buttons using the KeyboardRow, KeyboardButton and ReplyKeyboardMarkup classes.

But I can not see any documentation or example hot to create and configure the main Telegram menu button using Java.

This is how I create the inline-menu:

private void buildInlineMenu(SendMessage.SendMessageBuilder builder, ChatResponse chatResponse) {
    if (!chatResponse.getInlineMenus().isEmpty()) {
        List<InlineKeyboardButton> inlineButtons = new ArrayList<>();
        chatResponse.getInlineMenus().forEach(menu -> {
            var next = InlineKeyboardButton.builder()
                    .text(menu.getCaption())
                    .callbackData(menu.getCommand())
                    .build();
            inlineButtons.add(next);
        });

        InlineKeyboardMarkup markup = InlineKeyboardMarkup
                .builder()
                .keyboardRow(inlineButtons)
                .build();

        builder.replyMarkup(markup);
    }
}

link to image

My code that creates the keyboard:

private void buildKeyboard(SendMessage.SendMessageBuilder builder, ChatResponse chatResponse) {
    if (!chatResponse.getKeyboards().isEmpty()) {
        List<KeyboardRow> keyboard = new ArrayList<>();
        chatResponse.getKeyboards().forEach(b -> {
            String[] buttons = b.getCaption().split(";");
            KeyboardRow keyboardRow = new KeyboardRow();
            Arrays.stream(buttons).forEach(button -> {
                keyboardRow.add(new KeyboardButton(button));
            });
            keyboard.add(keyboardRow);
        });

        ReplyKeyboardMarkup markup = new ReplyKeyboardMarkup();
        markup.setSelective(true);
        markup.setResizeKeyboard(true);
        markup.setOneTimeKeyboard(false);
        markup.setKeyboard(keyboard);

        builder.replyMarkup(markup);
    }
}

link to image

How I call them:

(1)

ChatResponse.builder()
    .textMessage("Select a fruit! (inline menu)")
    .inlineMenus(List.of(
            ChatMenu.builder().caption("apple").command("apple").build(),
            ChatMenu.builder().caption("kiwi").command("kiwi").build(),
            ChatMenu.builder().caption("orange").command("orange").build()))
    .build();

(2)

ChatResponse.builder()
    .textMessage("What is your favourite color? (keyboard)")
    .keyboards(List.of(
            ChatMenu.builder().caption("Red;Blue").build(),
            ChatMenu.builder().caption("Green;Purple;White").build()))
    .build();

Calling:

SendMessage.SendMessageBuilder sendMessageBuilder = SendMessage
        .builder()
        .chatId(telegramId)
        .parseMode("HTML")
        .text(chatResponse.getTextMessage());

buildInlineMenu(sendMessageBuilder, chatResponse);
buildKeyboard(sendMessageBuilder, chatResponse);

execute(sendMessageBuilder.build());

The code above works like a charm but I want to create and configure the main menu as well from Java. I know that I can add the main menu using the BotFather menu from the Telegram chat, but I would like to use Java for this functionality as well.

Is that possible somehow? Or I am wrong and there is no way to configure the main menu using Java?


Solution

  • You're looking for the Telegram Bot API setMyCommands method:

    Use this method to change the list of the bot's commands. See this manual for more details about bot commands.
    Returns True on success.


    However, your question starts with "... customize the main menu texts based on the customer language preference ...";

    The command list send to Telegram isn't user specific, it's the same for all people using your bot, so to 'change' them based on language might conflict with other users.


    Assuming you're using rubenlagus/TelegramBots, there is a setMyCommands class with a test file that shows the following example:

    SetMyCommands setMyCommands = SetMyCommands
        .builder()
        .command(BotCommand.builder().command("test").description("Test description").build())
        .languageCode("en")
        .scope(BotCommandScopeDefault.builder().build())
        .build();