Inline keyboard builder for Telegram Bots API

5682

February 20, 2018

One of the most popular Java Library implementing Telegram Bots API is rubenlagus/TelegramBots.

It supports inline buttons since April 9, 2016 as Bot API 2.0 had been released.

Full toturial how to make inline keyboards you can find on GitHub.

But it seems so verbose.

So I create InlineKeyboardBuilder which can help you to build your telegram menues.

Inline keyboard builder

Just add following class into your project:


public class InlineKeyboardBuilder {

    private Long chatId;
    private String text;

    private List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
    private List<InlineKeyboardButton> row = null;

    private InlineKeyboardBuilder() {}

    public static InlineKeyboardBuilder create() {
        InlineKeyboardBuilder builder = new InlineKeyboardBuilder();
        return builder;
    }

    public static InlineKeyboardBuilder create(Long chatId) {
        InlineKeyboardBuilder builder = new InlineKeyboardBuilder();
        builder.setChatId(chatId);
        return builder;
    }

    public InlineKeyboardBuilder setText(String text) {
        this.text = text;
        return this;
    }

    public InlineKeyboardBuilder setChatId(Long chatId) {
        this.chatId = chatId;
        return this;
    }

    public InlineKeyboardBuilder row() {
        this.row = new ArrayList<>();
        return this;
    }

    public InlineKeyboardBuilder button(String text, String callbackData) {
        row.add(new InlineKeyboardButton().setText(text).setCallbackData(callbackData));
        return this;
    }

    public InlineKeyboardBuilder endRow() {
        this.keyboard.add(this.row);
        this.row = null;
        return this;
    }


    public SendMessage build() {
        SendMessage message = new SendMessage();

        message.setChatId(chatId);
        message.setText(text);

        InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup();

        keyboardMarkup.setKeyboard(keyboard);
        message.setReplyMarkup(keyboardMarkup);

        return message;
    }

}

Usage

And you will be able to create your own menu:


SendMessage message = InlineKeyboardBuilder.create(ctx.chatId())
        .setText("Menu:")
        .row()
        .button("Action 1", "action-1")
        .button("Action 2", "action-2")
        .endRow()
        .row()
        .button("Action 3", "action-3")
        .endRow()
        .build();

try {
    // Send the message
    sendApiMethod(message);
    //execute(message);
} catch (TelegramApiException e) {
    e.printStackTrace();
}

like this one:

Enhansed menu:

You actually can build your menu more dynamically (this is the instance from our production):


InlineKeyboardBuilder builder = InlineKeyboardBuilder.create(chatId)
        .setText("Locked Routes:")
        .row();
for (RouteWithDeadlock route : routes) {
    builder.row()
            .button(String.format("%s от %s", route.getNumber(), df.format(route.getDate().toGregorianCalendar().getTime())), "fix-route-with-deadlock:" + route.getUUID())
            .endRow();
}

builder.row().button("Cancel", "cancel").endRow();

SendMessage message = builder.build();

try {
    // Send the message
    sendApiMethod(message);
    //execute(message);
} catch (TelegramApiException e) {
    e.printStackTrace();
}