Skip to content

Instantly share code, notes, and snippets.

@centralhardware
Created October 21, 2023 21:29
Show Gist options
  • Save centralhardware/04a5fb45c47dc7a22eeca7bd112ab92b to your computer and use it in GitHub Desktop.
Save centralhardware/04a5fb45c47dc7a22eeca7bd112ab92b to your computer and use it in GitHub Desktop.
public class InlineKeyboardBuilder {
private final List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
private String text;
private List<InlineKeyboardButton> row = null;
private InlineKeyboardBuilder() { }
public static InlineKeyboardBuilder create() {
return new InlineKeyboardBuilder();
}
public InlineKeyboardBuilder setText( String text) {
this.text = text;
return this;
}
public InlineKeyboardBuilder row() {
row = new ArrayList<>();
return this;
}
public InlineKeyboardBuilder button( String text, String callbackData) {
row.add(InlineKeyboardButton.
builder().
text(text).
callbackData(callbackData).
build());
return this;
}
public InlineKeyboardBuilder webApp(String url, String text){
row.add(InlineKeyboardButton
.builder()
.text(text)
.webApp(WebAppInfo
.builder()
.url(url)
.build())
.build());
return this;
}
public InlineKeyboardBuilder switchToInline(){
row.add(InlineKeyboardButton
.builder()
.text("inline")
.switchInlineQueryCurrentChat("")
.build());
return this;
}
public InlineKeyboardBuilder endRow() {
keyboard.add(row);
row = null;
return this;
}
public InlineKeyboardMarkup build(){
return InlineKeyboardMarkup.
builder().
keyboard(keyboard).
build();
}
public SendMessage build(Long chatId) {
return SendMessage.
builder().
chatId(chatId).
text(text).
replyMarkup(InlineKeyboardMarkup.
builder().
keyboard(keyboard).
build()).
build();
}
public EditMessageText build(CallbackQuery callbackQuery) {
return EditMessageText
.builder()
.chatId(callbackQuery.getMessage().getChatId())
.inlineMessageId(callbackQuery.getInlineMessageId())
.text(text)
.messageId(callbackQuery.getMessage().getMessageId())
.replyMarkup(InlineKeyboardMarkup
.builder()
.keyboard(keyboard)
.build())
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment