From ffc5b63508f8455c5710f08d4d0223e77237c9d2 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 26 May 2022 02:22:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20In?= =?UTF-8?q?lineKeyBoards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 12 +- telegram-core/pom.xml | 2 +- .../config/TelegramPollingConfig.java | 15 +- .../domain/keyboard/InlineKeyBoard.java | 4 + .../telegram/utils/InlineKeyBoards.java | 133 ++++++++++++++++++ 5 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/InlineKeyBoards.java diff --git a/pom.xml b/pom.xml index 29dc98e..6525331 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.6 + 0.0.7 pom @@ -33,7 +33,7 @@ UTF-8 UTF-8 - 0.0.5 + 0.0.6 6.0.1 3.10.1 @@ -142,10 +142,10 @@ org.apache.maven.plugins maven-compiler-plugin - - org.apache.maven.plugins - maven-javadoc-plugin - + + + + org.apache.maven.plugins maven-source-plugin diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 34332d2..34f6522 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.6 + 0.0.7 telegram-core diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java index 7dda61f..2a352f6 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java @@ -9,8 +9,8 @@ import dev.struchkov.godfather.telegram.ProxyConfig; */ public class TelegramPollingConfig { - private final String botUsername; - private final String botToken; + private String botUsername; + private String botToken; private ProxyConfig proxyConfig; @@ -19,6 +19,17 @@ public class TelegramPollingConfig { this.botToken = botToken; } + public TelegramPollingConfig() { + } + + public void setBotUsername(String botUsername) { + this.botUsername = botUsername; + } + + public void setBotToken(String botToken) { + this.botToken = botToken; + } + public String getBotUsername() { return botUsername; } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/InlineKeyBoard.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/InlineKeyBoard.java index 24ed8bc..d8f38e1 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/InlineKeyBoard.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/InlineKeyBoard.java @@ -22,6 +22,10 @@ public class InlineKeyBoard extends SimpleKeyBoard { return new Builder(); } + public static InlineKeyBoard inlineKeyBoard(KeyBoardLine keyBoardLine) { + return builder().line(keyBoardLine).build(); + } + @Override public String getType() { return TYPE; diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/InlineKeyBoards.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/InlineKeyBoards.java new file mode 100644 index 0000000..c314e37 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/InlineKeyBoards.java @@ -0,0 +1,133 @@ +package dev.struchkov.godfather.telegram.utils; + +import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton; +import dev.struchkov.godfather.context.domain.keyboard.button.SimpleButton; +import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard; +import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoardLine; +import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; + +import java.util.Arrays; +import java.util.List; + +import static dev.struchkov.godfather.context.domain.keyboard.button.SimpleButton.simpleButton; +import static dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; + +public final class InlineKeyBoards { + + public static final SimpleButton YES_BUTTON = simpleButton("Да", "{\"button\": \"yes\"}"); + public static final SimpleButton NO_BUTTON = simpleButton("Нет", "{\"button\": \"no\"}"); + + public InlineKeyBoards() { + utilityClass(); + } + + /** + * Возвращает клавиатуру формата 1х2, с кнопками "Да | Нет" + * + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard keyBoardYesNo() { + return InlineKeyBoard.inlineKeyBoard( + SimpleKeyBoardLine.builder().button(YES_BUTTON).button(NO_BUTTON).build() + ); + } + + /** + * Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке + * + * @param labelButtons Список названий для кнопок + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard verticalMenuString(List labelButtons) { + final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + for (String labelButton : labelButtons) { + keyBoard.line(simpleLine(simpleButton(labelButton, labelButton))); + } + return keyBoard.build(); + } + + /** + * Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке + * + * @param labelButton Список названий для кнопок + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard verticalMenuString(String... labelButton) { + return verticalMenuString(Arrays.asList(labelButton)); + } + + /** + * Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке + * + * @param labelButton Список названий для кнопок + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard verticalDuoMenuString(String... labelButton) { + return verticalDuoMenuString(Arrays.asList(labelButton)); + } + + /** + * Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке + * + * @param labelButton Список названий для кнопок + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard verticalDuoMenuString(List labelButton) { + final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + boolean flag = true; + SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); + for (int i = 0; i <= labelButton.size() - 1; i++) { + String label = labelButton.get(i); + keyBoardLine.button(simpleButton(label)); + if (flag) { + if (i == labelButton.size() - 1) { + keyBoard.line(keyBoardLine.build()); + } else { + flag = false; + } + } else { + keyBoard.line(keyBoardLine.build()); + keyBoardLine = SimpleKeyBoardLine.builder(); + flag = true; + } + } + return keyBoard.build(); + } + + public static InlineKeyBoard verticalDuoMenu(KeyBoardButton... buttons) { + final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + boolean flag = true; + SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); + for (int i = 0; i <= buttons.length - 1; i++) { + keyBoardLine.button(buttons[i]); + if (flag) { + if (i == buttons.length - 1) { + keyBoard.line(keyBoardLine.build()); + } else { + flag = false; + } + } else { + keyBoard.line(keyBoardLine.build()); + keyBoardLine = SimpleKeyBoardLine.builder(); + flag = true; + } + } + return keyBoard.build(); + } + + /** + * Возвращает клавиатуру формата 1xN сформированную из списка кнопок, где N - количество кнопок в списке + * + * @param simpleButtons Список кнопок + * @return {@link SimpleKeyBoard} + */ + public static InlineKeyBoard verticalMenuButton(List buttons) { + final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + for (KeyBoardButton simpleButton : buttons) { + keyBoard.line(simpleLine(simpleButton)); + } + return keyBoard.build(); + } + +}