how to create telegram main menu using java
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);
}
}
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);
}
}
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?
It is better to use Bot API to control bots.
In TDLib you can use the method setMenuButton to change the bot's menu button in private chats if this is what you call "main menu".