All Downloads are FREE. Search and download functionalities are using the official Maven repository.

host.anzo.commons.socials.telegram.TelegramKeyboard Maven / Gradle / Ivy

There is a newer version: 1.29
Show newest version
package host.anzo.commons.socials.telegram;

import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardRow;

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

/**
 * @author Aristo
 */
public class TelegramKeyboard {
    private final InlineKeyboardMarkup markup = new InlineKeyboardMarkup(Collections.emptyList());
    private final List keyboard = new ArrayList<>();

    private TelegramKeyboard() {
    }

    public static TelegramKeyboard create() {
        return new TelegramKeyboard();
    }

    public TelegramKeyboard addButton(int row, String text) {
        try {
            while (keyboard.size() <= row) {
                keyboard.add(new InlineKeyboardRow());
            }
            keyboard.get(row).add(InlineKeyboardButton.builder().text(text).callbackData(text).build());
        } catch (Exception ignored) {
        }
        return this;
    }

    public SendMessage attach(@NotNull SendMessage message) {
        markup.setKeyboard(keyboard);
        message.setReplyMarkup(markup);
        return message;
    }

    public EditMessageText attachEdit(@NotNull EditMessageText message) {
        markup.setKeyboard(keyboard);
        message.setReplyMarkup(markup);
        return message;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy