diff --git a/pom.xml b/pom.xml index b39d84f..5be605b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-bot - 0.0.34 + 0.0.38 pom @@ -38,8 +38,8 @@ UTF-8 UTF-8 - 0.0.27 - 6.1.0 + 0.0.29 + 6.3.0 1.4.0 diff --git a/telegram-consumer/pom.xml b/telegram-consumer/pom.xml index 0987280..245016b 100644 --- a/telegram-consumer/pom.xml +++ b/telegram-consumer/pom.xml @@ -5,7 +5,7 @@ telegram-bot dev.struchkov.godfather.telegram - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-consumer/telegram-consumer-main/pom.xml b/telegram-consumer/telegram-consumer-main/pom.xml index b6da63d..ad0adbc 100644 --- a/telegram-consumer/telegram-consumer-main/pom.xml +++ b/telegram-consumer/telegram-consumer-main/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-consumer - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/CallbackQueryConvert.java b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/CallbackQueryConvert.java index b7e511f..302ed79 100644 --- a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/CallbackQueryConvert.java +++ b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/CallbackQueryConvert.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.main.consumer; import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.User; @@ -14,10 +15,15 @@ import java.time.LocalDateTime; public class CallbackQueryConvert { public static Mail apply(CallbackQuery callbackQuery) { + final String callbackData = callbackQuery.getData(); + final Mail mail = new Mail(); mail.setCreateDate(LocalDateTime.now()); - mail.setText(callbackQuery.getData()); - mail.setPersonId(callbackQuery.getMessage().getChatId()); + mail.setText(callbackData); + mail.addAttachment(convertToButtonClick(callbackData)); + + final Long chatId = callbackQuery.getMessage().getChatId(); + mail.setPersonId(chatId != null ? chatId.toString() : null); final User user = callbackQuery.getFrom(); mail.setFirstName(user.getFirstName()); @@ -25,4 +31,19 @@ public class CallbackQueryConvert { return mail; } + private static ButtonClickAttachment convertToButtonClick(String callbackData) { + final ButtonClickAttachment buttonClickAttachment = new ButtonClickAttachment(); + buttonClickAttachment.setRawCallBackData(callbackData); + if (callbackData.charAt(0) == '[' && callbackData.charAt(callbackData.length() - 1) == ']') { + final String[] args = callbackData.substring(1, callbackData.length() - 1).split(";"); + for (String arg : args) { + final String[] oneArg = arg.split(":"); + final String key = oneArg[0]; + final String value = oneArg[1]; + buttonClickAttachment.addClickArg(key, value); + } + } + return buttonClickAttachment; + } + } diff --git a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/MessageMailConvert.java b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/MessageMailConvert.java index 2eee279..dbcc34e 100644 --- a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/MessageMailConvert.java +++ b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/MessageMailConvert.java @@ -38,7 +38,8 @@ public final class MessageMailConvert { public static Mail apply(Message message) { final Mail mail = new Mail(); - mail.setPersonId(message.getChatId()); + final Long chatId = message.getChatId(); + mail.setPersonId(chatId != null ? chatId.toString() : null); mail.setText(message.getText()); mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault())); mail.setFirstName(message.getChat().getFirstName()); diff --git a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/SubscribeConvert.java b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/SubscribeConvert.java index 3d358c6..a8b0546 100644 --- a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/SubscribeConvert.java +++ b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/SubscribeConvert.java @@ -1,13 +1,11 @@ package dev.struchkov.godfather.telegram.main.consumer; import dev.struchkov.godfather.telegram.domain.event.Subscribe; -import dev.struchkov.haiti.utils.Exceptions; import org.telegram.telegrambots.meta.api.objects.Chat; import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; import java.time.LocalDateTime; -import static dev.struchkov.haiti.utils.Exceptions.*; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; public final class SubscribeConvert { @@ -20,7 +18,7 @@ public final class SubscribeConvert { final Chat chat = updated.getChat(); final Subscribe subscribe = new Subscribe(); - subscribe.setTelegramId(chat.getId()); + subscribe.setTelegramId(chat.getId().toString()); subscribe.setLastName(chat.getLastName()); subscribe.setFirstName(chat.getFirstName()); subscribe.setSubscriptionDate(LocalDateTime.now()); diff --git a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/UnsubscribeConvert.java b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/UnsubscribeConvert.java index 3a43fb3..d5f8ee3 100644 --- a/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/UnsubscribeConvert.java +++ b/telegram-consumer/telegram-consumer-main/src/main/java/dev/struchkov/godfather/telegram/main/consumer/UnsubscribeConvert.java @@ -18,7 +18,7 @@ public final class UnsubscribeConvert { final Chat chat = updated.getChat(); final Unsubscribe unsubscribe = new Unsubscribe(); - unsubscribe.setTelegramId(chat.getId()); + unsubscribe.setTelegramId(chat.getId().toString()); unsubscribe.setLastName(chat.getLastName()); unsubscribe.setFirstName(chat.getFirstName()); unsubscribe.setSubscriptionDate(LocalDateTime.now()); diff --git a/telegram-consumer/telegram-consumer-quarkus/pom.xml b/telegram-consumer/telegram-consumer-quarkus/pom.xml index c1a93c0..97c53e5 100644 --- a/telegram-consumer/telegram-consumer-quarkus/pom.xml +++ b/telegram-consumer/telegram-consumer-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-consumer - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-consumer/telegram-consumer-simple/pom.xml b/telegram-consumer/telegram-consumer-simple/pom.xml index 6aa2aea..6bd364c 100644 --- a/telegram-consumer/telegram-consumer-simple/pom.xml +++ b/telegram-consumer/telegram-consumer-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-consumer - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-context/pom.xml b/telegram-context/pom.xml index 32164dd..869fb26 100644 --- a/telegram-context/pom.xml +++ b/telegram-context/pom.xml @@ -5,7 +5,7 @@ telegram-bot dev.struchkov.godfather.telegram - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-context/telegram-context-main/pom.xml b/telegram-context/telegram-context-main/pom.xml index 3a11b29..5529c9f 100644 --- a/telegram-context/telegram-context-main/pom.xml +++ b/telegram-context/telegram-context-main/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-context - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-context/telegram-context-quarkus/pom.xml b/telegram-context/telegram-context-quarkus/pom.xml index 585b332..e5bea5f 100644 --- a/telegram-context/telegram-context-quarkus/pom.xml +++ b/telegram-context/telegram-context-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-context - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java index 9b70661..aae48c6 100644 --- a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java +++ b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java @@ -7,6 +7,6 @@ import org.jetbrains.annotations.NotNull; public interface TelegramSending extends Sending { - Uni sendNotSave(@NotNull Long personId, @NotNull BoxAnswer boxAnswer); + Uni sendNotSave(@NotNull String personId, @NotNull BoxAnswer boxAnswer); } diff --git a/telegram-context/telegram-context-simple/pom.xml b/telegram-context/telegram-context-simple/pom.xml index 4dd4ee7..6fa07b1 100644 --- a/telegram-context/telegram-context-simple/pom.xml +++ b/telegram-context/telegram-context-simple/pom.xml @@ -5,7 +5,7 @@ telegram-context dev.struchkov.godfather.telegram - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/SenderStorageService.java b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/SenderStorageService.java index aa7a87a..9cd8102 100644 --- a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/SenderStorageService.java +++ b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/SenderStorageService.java @@ -4,10 +4,10 @@ import java.util.Optional; public interface SenderStorageService { - Optional getLastSendMessage(Long telegramId); + Optional getLastSendMessage(String telegramId); - void saveLastSendMessage(Long telegramId, Integer messageId); + void saveLastSendMessage(String telegramId, Integer messageId); - void removeLastSendMessage(Long telegramId); + void removeLastSendMessage(String telegramId); } diff --git a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java index 04e29e8..1dbc554 100644 --- a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java +++ b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java @@ -6,6 +6,6 @@ import org.jetbrains.annotations.NotNull; public interface TelegramSending extends Sending { - void sendNotSave(@NotNull Long personId, @NotNull BoxAnswer boxAnswer); + void sendNotSave(@NotNull String personId, @NotNull BoxAnswer boxAnswer); } diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 9c92fd8..4a77900 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-bot - 0.0.34 + 0.0.38 telegram-core-main diff --git a/telegram-core/telegram-core-main/pom.xml b/telegram-core/telegram-core-main/pom.xml index 6857b5e..bcf871b 100644 --- a/telegram-core/telegram-core-main/pom.xml +++ b/telegram-core/telegram-core-main/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-core - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/TelegramDefaultConnect.java b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/TelegramDefaultConnect.java index 6734f4a..18abd17 100644 --- a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/TelegramDefaultConnect.java +++ b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/TelegramDefaultConnect.java @@ -1,19 +1,56 @@ package dev.struchkov.godfather.telegram.main.core; +import dev.struchkov.godfather.telegram.domain.config.ProxyConfig; import dev.struchkov.godfather.telegram.domain.config.TelegramConnectConfig; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; +import org.jetbrains.annotations.NotNull; import org.telegram.telegrambots.bots.DefaultAbsSender; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.meta.bots.AbsSender; +import java.net.Authenticator; +import java.net.PasswordAuthentication; + public class TelegramDefaultConnect implements TelegramConnect { private final String botToken; private final AbsSender absSender; - public TelegramDefaultConnect(TelegramConnectConfig telegramConnectConfig) { - this.botToken = telegramConnectConfig.getBotToken(); - this.absSender = new DefaultAbsSender(new DefaultBotOptions()) { + public TelegramDefaultConnect(TelegramConnectConfig connectConfig) { + this.botToken = connectConfig.getBotToken(); + this.absSender = createAbsSender(connectConfig); + } + + @NotNull + private DefaultAbsSender createAbsSender(TelegramConnectConfig connectConfig) { + final DefaultBotOptions botOptions = new DefaultBotOptions(); + + final ProxyConfig proxyConfig = connectConfig.getProxyConfig(); + if (proxyConfig != null && proxyConfig.getPassword() != null) { + try { + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication( + proxyConfig.getUser(), + proxyConfig.getPassword().toCharArray() + ); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + + if (proxyConfig != null && proxyConfig.getHost() != null) { + System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); + System.setProperty("javax.net.debug", "all"); + botOptions.setProxyHost(proxyConfig.getHost()); + botOptions.setProxyPort(proxyConfig.getPort()); + botOptions.setProxyType(convertProxyType(proxyConfig.getType())); + } + + return new DefaultAbsSender(botOptions) { @Override public String getBotToken() { return botToken; @@ -21,6 +58,19 @@ public class TelegramDefaultConnect implements TelegramConnect { }; } + private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) { + switch (type) { + case SOCKS5: + return DefaultBotOptions.ProxyType.SOCKS5; + case SOCKS4: + return DefaultBotOptions.ProxyType.SOCKS4; + case HTTP: + return DefaultBotOptions.ProxyType.HTTP; + default: + return DefaultBotOptions.ProxyType.NO_PROXY; + } + } + @Override public AbsSender getAbsSender() { return absSender; diff --git a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/Attachments.java b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/Attachments.java index 03ec6a7..d16126a 100644 --- a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/Attachments.java +++ b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/Attachments.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.main.core.util; import dev.struchkov.godfather.main.domain.content.Attachment; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; import dev.struchkov.godfather.telegram.domain.attachment.CommandAttachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; @@ -45,6 +46,17 @@ public final class Attachments { return Optional.empty(); } + public static Optional findFirstButtonClick(Collection attachments) { + if (checkNotEmpty(attachments)) { + for (Attachment attachment : attachments) { + if (isButtonClick(attachment)) { + return Optional.of((ButtonClickAttachment) attachment); + } + } + } + return Optional.empty(); + } + public static Optional findFirstPictureGroup(Collection attachments) { if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { @@ -136,4 +148,9 @@ public final class Attachments { return TelegramAttachmentType.LINK.name().equals(attachment.getType()); } + public static boolean isButtonClick(Attachment attachment) { + isNotNull(attachment); + return TelegramAttachmentType.BUTTON_CLICK.name().equals(attachment.getType()); + } + } diff --git a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/InlineKeyBoards.java b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/InlineKeyBoards.java index b89dca5..748f83b 100644 --- a/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/InlineKeyBoards.java +++ b/telegram-core/telegram-core-main/src/main/java/dev/struchkov/godfather/telegram/main/core/util/InlineKeyBoards.java @@ -93,27 +93,35 @@ public final class InlineKeyBoards { return keyBoard.build(); } - public static InlineKeyBoard verticalDuoMenu(KeyBoardButton... buttons) { - final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + public static void verticalDuoMenu(InlineKeyBoard.Builder builder, List buttons) { boolean flag = true; SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); - for (int i = 0; i <= buttons.length - 1; i++) { - keyBoardLine.button(buttons[i]); + for (int i = 0; i <= buttons.size() - 1; i++) { + keyBoardLine.button(buttons.get(i)); if (flag) { - if (i == buttons.length - 1) { - keyBoard.line(keyBoardLine.build()); + if (i == buttons.size() - 1) { + builder.line(keyBoardLine.build()); } else { flag = false; } } else { - keyBoard.line(keyBoardLine.build()); + builder.line(keyBoardLine.build()); keyBoardLine = SimpleKeyBoardLine.builder(); flag = true; } } + } + + public static InlineKeyBoard verticalDuoMenu(List buttons) { + final InlineKeyBoard.Builder keyBoard = InlineKeyBoard.builder(); + verticalDuoMenu(keyBoard, buttons); return keyBoard.build(); } + public static InlineKeyBoard verticalDuoMenu(KeyBoardButton... buttons) { + return verticalDuoMenu(Arrays.stream(buttons).toList()); + } + /** * Возвращает клавиатуру формата 1xN сформированную из списка кнопок, где N - количество кнопок в списке * diff --git a/telegram-core/telegram-core-quarkus/pom.xml b/telegram-core/telegram-core-quarkus/pom.xml index 3f3eb10..e55f7ff 100644 --- a/telegram-core/telegram-core-quarkus/pom.xml +++ b/telegram-core/telegram-core-quarkus/pom.xml @@ -5,7 +5,7 @@ telegram-core dev.struchkov.godfather.telegram - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-core/telegram-core-simple/pom.xml b/telegram-core/telegram-core-simple/pom.xml index 61aae18..72a036c 100644 --- a/telegram-core/telegram-core-simple/pom.xml +++ b/telegram-core/telegram-core-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-core - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramConnectBot.java b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramConnectBot.java index 5bb0c83..e2dca5b 100644 --- a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramConnectBot.java +++ b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/TelegramConnectBot.java @@ -34,21 +34,6 @@ public class TelegramConnectBot implements TelegramConnect { initLongPolling(telegramConnectConfig); } -// public TelegramConnect(TelegramWebHookConfig telegramWebHookConfig) { -// initWebHook(telegramWebHookConfig); -// } -// -// private void initWebHook(TelegramWebHookConfig telegramWebHookConfig) { -// TelegramBotsApi botapi = new TelegramBotsApi(); -// final TelegramWebhookBot telegramWebhookBot = new TelegramHookBot(telegramWebHookConfig); -// try { -// botapi.registerBot(telegramWebhookBot); -// this.telegramBot = (TelegramBot) telegramWebhookBot; -// } catch (TelegramApiRequestException e) { -// e.printStackTrace(); -// } -// } - private void initLongPolling(TelegramConnectConfig telegramConnectConfig) { final ProxyConfig proxyConfig = telegramConnectConfig.getProxyConfig(); @@ -79,7 +64,6 @@ public class TelegramConnectBot implements TelegramConnect { botOptions.setProxyPort(proxyConfig.getPort()); botOptions.setProxyType(convertProxyType(proxyConfig.getType())); - final TelegramPollingBot bot = new TelegramPollingBot(telegramConnectConfig, botOptions); botapi = new TelegramBotsApi(DefaultBotSession.class); diff --git a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/SenderMapStorageService.java b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/SenderMapStorageService.java index efc810b..8541b9e 100644 --- a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/SenderMapStorageService.java +++ b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/SenderMapStorageService.java @@ -11,21 +11,21 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; public class SenderMapStorageService implements SenderStorageService { - private final Map lastMessageId = new HashMap<>(); + private final Map lastMessageId = new HashMap<>(); @Override - public Optional getLastSendMessage(Long telegramId) { + public Optional getLastSendMessage(String telegramId) { return Optional.ofNullable(lastMessageId.get(telegramId)); } @Override - public void saveLastSendMessage(@NotNull Long telegramId, @NotNull Integer messageId) { + public void saveLastSendMessage(@NotNull String telegramId, @NotNull Integer messageId) { isNotNull(telegramId); lastMessageId.put(telegramId, messageId); } @Override - public void removeLastSendMessage(Long telegramId) { + public void removeLastSendMessage(String telegramId) { lastMessageId.remove(telegramId); } diff --git a/telegram-domain/pom.xml b/telegram-domain/pom.xml index b8a1242..29d0f52 100644 --- a/telegram-domain/pom.xml +++ b/telegram-domain/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-bot - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPage.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPage.java new file mode 100644 index 0000000..441b072 --- /dev/null +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPage.java @@ -0,0 +1,147 @@ +package dev.struchkov.godfather.telegram.domain; + +import dev.struchkov.godfather.main.domain.BoxAnswer; +import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton; +import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine; +import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; +import dev.struchkov.haiti.utils.Inspector; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; +import static dev.struchkov.godfather.telegram.domain.UnitPaginationUtil.navigableLine; + +public class UnitPage { + + /** + * Дополнительные линии клавиатуры. Выводятся после кнопок навигации. + */ + private final List additionalLines = new ArrayList<>(); + + /** + * Элементы, которые будут выводиться. + */ + private List elements; + + /** + * Номер текущей страницы. + */ + private Integer currentOffset; + + /** + * Общее количество элементов на всех страницах. + */ + private Integer countAllElements; + + /** + * Функция преобразования элементов с линии. + */ + private Function function; + + /** + * Сообщение, которое выводится над результатами + */ + private String message; + + /** + * Флаг, определяющий нужно ли заменить старое сообщение или вывести новое (по умолчанию заменять) + */ + private boolean replace; + + /** + * Флаг, исключающий добавление строки с навигацией + */ + private boolean removeDefaultNavigableLine; + + /** + * Сообщение, которое будет отправлено, если коллекция элементов пустая. + */ + private BoxAnswer emptyElements; + + public static UnitPage builder() { + return new UnitPage<>(); + } + + public UnitPage elements(List elements) { + this.elements = elements; + return this; + } + + public UnitPage countAllElements(Integer countAllElements) { + this.countAllElements = countAllElements; + return this; + } + + public UnitPage currentOffset(Integer currentOffset) { + this.currentOffset = currentOffset; + return this; + } + + public UnitPage additionLine(KeyBoardLine line) { + additionalLines.add(line); + return this; + } + + public UnitPage additionLine(KeyBoardButton button) { + additionalLines.add(simpleLine(button)); + return this; + } + + public UnitPage mapper(Function function) { + this.function = function; + return this; + } + + public UnitPage message(String message) { + this.message = message; + return this; + } + + public UnitPage replace(boolean replace) { + this.replace = replace; + return this; + } + + public UnitPage emptyElements(BoxAnswer boxAnswer) { + this.emptyElements = boxAnswer; + return this; + } + + public UnitPage removeDefaultNavigableLine() { + this.removeDefaultNavigableLine = true; + return this; + } + + public BoxAnswer build() { + Inspector.isNotNull(currentOffset, countAllElements, function); + if (elements.isEmpty()) { + return emptyElements != null ? emptyElements : BoxAnswer.boxAnswer("Данные не найдены."); + } else { + final List lines = elements.stream() + .map(function) + .toList(); + final InlineKeyBoard.Builder builder = InlineKeyBoard.builder(); + + lines.forEach(builder::line); + if (!removeDefaultNavigableLine) { + navigableLine(currentOffset, countAllElements).ifPresent(builder::line); + } + additionalLines.forEach(builder::line); + + final InlineKeyBoard keyBoard = builder.build(); + + final BoxAnswer.Builder boxAnswer = BoxAnswer.builder() + .keyBoard(keyBoard) + .replace(true); + if (message != null) { + boxAnswer.message(message); + } + + boxAnswer.replace(replace); + return boxAnswer.build(); + } + } + +} diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPaginationUtil.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPaginationUtil.java new file mode 100644 index 0000000..e39763d --- /dev/null +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/UnitPaginationUtil.java @@ -0,0 +1,88 @@ +package dev.struchkov.godfather.telegram.domain; + +import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton; +import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine; +import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine; + +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; + + +public final class UnitPaginationUtil { + + public static final String PAGE_REGEX = "^page\\s\\d*$"; + + private static final String PAGINATION_SCHEMA = "page %s"; + private static final Pattern PATTERN_FOR_SEARCH_OFFSET = Pattern.compile("\\d++$"); + + /** + * Создание строки пагинации для передачи ее в payload кнопки + */ + public static String getStringForOffset(Integer offset) { + return PAGINATION_SCHEMA.formatted(offset); + } + + /** + * Разбор передаваемой строки пагинации + */ + public static Optional getOffsetFromString(String offsetString) { + if (!Pattern.matches(PAGE_REGEX, offsetString)) { + return Optional.empty(); + } + final Matcher matcher = PATTERN_FOR_SEARCH_OFFSET.matcher(offsetString); + if (matcher.find()) { + final String offsetNumber = offsetString.substring(matcher.start(), matcher.end()); + return Optional.of(Integer.valueOf(offsetNumber)); + } else { + return Optional.empty(); + } + } + + /** + * Получение строки для клавиатуры, в которой содержатся кнопки навигации. + * + * @param currentOffset - текущее смещение, используемое для запроса + * @param countElements - общее количество элементов которое может вернуть запрос + */ + public static Optional navigableLine(Integer currentOffset, Integer countElements) { + final SimpleKeyBoardLine.Builder lineBuilder = SimpleKeyBoardLine.builder(); + + final Optional optPrevButton = getPrevPageButton(currentOffset); + final Optional optNextButton = getNextPageButton(currentOffset, countElements); + + if (optPrevButton.isPresent() || optNextButton.isPresent()) { + optPrevButton.ifPresent(lineBuilder::button); + optNextButton.ifPresent(lineBuilder::button); + return Optional.of(lineBuilder.build()); + } + return Optional.empty(); + } + + /** + * Получение кнопки навигации на прошлую страницу, т.к. пользователь находится на самой первой странице, то + * кнопки перехода на предыдущую страницу может и не быть + */ + private static Optional getPrevPageButton(Integer currentOffset) { + if (!currentOffset.equals(0)) { + int prevOffset = currentOffset < 5 ? 0 : currentOffset - 5; + return Optional.of(simpleButton("❮❮❮", getStringForOffset(prevOffset))); + } + return Optional.empty(); + } + + /** + * Получение кнопки навигации на следующую страницу, т.к. пользователь может находиться на последней странице, + * то кнопки навигации вперед может и не быть + */ + private static Optional getNextPageButton(Integer currentOffset, Integer countElements) { + final Integer nextOffset = currentOffset + 5; + if (nextOffset.compareTo(countElements) < 0) { + return Optional.of(simpleButton("❯❯❯", getStringForOffset(nextOffset))); + } + return Optional.empty(); + } + +} \ No newline at end of file diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/ButtonClickAttachment.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/ButtonClickAttachment.java new file mode 100644 index 0000000..c7d3eac --- /dev/null +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/ButtonClickAttachment.java @@ -0,0 +1,69 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import dev.struchkov.godfather.main.domain.content.Attachment; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException; +import static dev.struchkov.haiti.utils.Inspector.isNotNull; + +public class ButtonClickAttachment extends Attachment { + + private String rawCallBackData; + private final Map args = new HashMap<>(); + + public String getRawCallBackData() { + return rawCallBackData; + } + + public void setRawCallBackData(String rawCallBackData) { + this.rawCallBackData = rawCallBackData; + } + + public void addClickArg(String type, String value) { + isNotNull(type, value); + args.put(type, new Arg(type, value)); + } + + public Optional getArgByType(String type) { + isNotNull(type); + return Optional.ofNullable(args.get(type)); + } + + public Arg getArgByTypeOrThrow(String type) { + isNotNull(type); + return Optional.of(args.get(type)).orElseThrow(notFoundException("Аргумент типа {0} не найден.", type)); + } + + public Collection getClickArgs() { + return args.values(); + } + + @Override + public String getType() { + return TelegramAttachmentType.BUTTON_CLICK.name(); + } + + public static class Arg { + private final String type; + private final String value; + + private Arg(String type, String value) { + this.type = type; + this.value = value; + } + + public String getType() { + return type; + } + + public String getValue() { + return value; + } + + } + +} diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/CommandAttachment.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/CommandAttachment.java index a3fa856..e7a9d95 100644 --- a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/CommandAttachment.java +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/CommandAttachment.java @@ -4,6 +4,8 @@ import dev.struchkov.godfather.main.domain.content.Attachment; import java.util.Optional; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; + public class CommandAttachment extends Attachment { private String value; @@ -39,6 +41,13 @@ public class CommandAttachment extends Attachment { return commandType; } + public boolean isCommandType(String type) { + if (checkNotNull(type)) { + return type.equals(commandType); + } + return false; + } + public String getRawValue() { return rawValue; } diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java index 4a43aac..6482916 100644 --- a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java @@ -6,6 +6,7 @@ public enum TelegramAttachmentType { CONTACT, PICTURE, LINK, - COMMAND + COMMAND, + BUTTON_CLICK } diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java index e2d4e96..9f67f60 100644 --- a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java @@ -8,16 +8,16 @@ public class Subscribe implements Event { public static final String TYPE = "SUBSCRIBE"; - private Long telegramId; + private String telegramId; private String firstName; private String lastName; private LocalDateTime subscriptionDate; - public Long getTelegramId() { + public String getTelegramId() { return telegramId; } - public void setTelegramId(Long telegramId) { + public void setTelegramId(String telegramId) { this.telegramId = telegramId; } diff --git a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java index d0e3cbd..4137d7e 100644 --- a/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java +++ b/telegram-domain/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java @@ -8,16 +8,16 @@ public class Unsubscribe implements Event { public static final String TYPE = "UNSUBSCRIBE"; - private Long telegramId; + private String telegramId; private String firstName; private String lastName; private LocalDateTime subscriptionDate; - public Long getTelegramId() { + public String getTelegramId() { return telegramId; } - public void setTelegramId(Long telegramId) { + public void setTelegramId(String telegramId) { this.telegramId = telegramId; } diff --git a/telegram-sender/pom.xml b/telegram-sender/pom.xml index 30745e9..0f40b04 100644 --- a/telegram-sender/pom.xml +++ b/telegram-sender/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-bot - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-sender/telegram-sender-main/pom.xml b/telegram-sender/telegram-sender-main/pom.xml index 8e421d7..93653bd 100644 --- a/telegram-sender/telegram-sender-main/pom.xml +++ b/telegram-sender/telegram-sender-main/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-sender - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-sender/telegram-sender-quarkus/pom.xml b/telegram-sender/telegram-sender-quarkus/pom.xml index 47a990a..b21c0d5 100644 --- a/telegram-sender/telegram-sender-quarkus/pom.xml +++ b/telegram-sender/telegram-sender-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-sender - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SenderStorageService.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SenderStorageService.java index 31dcd54..56f05ad 100644 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SenderStorageService.java +++ b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SenderStorageService.java @@ -4,10 +4,10 @@ import io.smallrye.mutiny.Uni; public interface SenderStorageService { - Uni getLastSendMessage(Long telegramId); + Uni getLastSendMessage(String telegramId); - Uni saveLastSendMessage(Long telegramId, Integer messageId); + Uni saveLastSendMessage(String telegramId, Integer messageId); - Uni removeLastSendMessage(Long telegramId); + Uni removeLastSendMessage(String telegramId); } diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java index 545e081..8cd6b32 100644 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java @@ -50,16 +50,16 @@ public class TelegramSender implements TelegramSending { } @Override - public Uni send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { + public Uni send(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { return sendBoxAnswer(telegramId, boxAnswer, true); } @Override - public Uni sendNotSave(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { + public Uni sendNotSave(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { return sendBoxAnswer(telegramId, boxAnswer, false); } - private Uni sendBoxAnswer(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private Uni sendBoxAnswer(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { @@ -83,7 +83,7 @@ public class TelegramSender implements TelegramSending { ); } - private Uni replaceMessage(@NotNull Long telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) { + private Uni replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { @@ -103,13 +103,13 @@ public class TelegramSender implements TelegramSending { ); } - private Uni sendMessage(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private Uni sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); - sendMessage.setChatId(String.valueOf(telegramId)); + sendMessage.setChatId(telegramId); sendMessage.setText( sendPreProcessing != null ? sendPreProcessing.pretreatment(boxAnswer.getMessage()) diff --git a/telegram-sender/telegram-sender-simple/pom.xml b/telegram-sender/telegram-sender-simple/pom.xml index ca1eb84..2bf0302 100644 --- a/telegram-sender/telegram-sender-simple/pom.xml +++ b/telegram-sender/telegram-sender-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-sender - 0.0.34 + 0.0.38 4.0.0 diff --git a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java index b93f993..f588ec6 100644 --- a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java @@ -51,16 +51,16 @@ public class TelegramSender implements TelegramSending { } @Override - public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { + public void send(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { sendBoxAnswer(telegramId, boxAnswer, true); } @Override - public void sendNotSave(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { + public void sendNotSave(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { sendBoxAnswer(telegramId, boxAnswer, false); } - private void sendBoxAnswer(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private void sendBoxAnswer(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { isNotNull(telegramId, boxAnswer); try { if (boxAnswer.isReplace() && checkNotNull(senderStorageService)) { @@ -83,9 +83,9 @@ public class TelegramSender implements TelegramSending { } } - private void replaceMessage(@NotNull Long telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException { + private void replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException { final EditMessageText editMessageText = new EditMessageText(); - editMessageText.setChatId(String.valueOf(telegramId)); + editMessageText.setChatId(telegramId); editMessageText.setMessageId(lastMessageId); editMessageText.enableMarkdown(true); editMessageText.setText(boxAnswer.getMessage()); @@ -93,10 +93,10 @@ public class TelegramSender implements TelegramSending { absSender.execute(editMessageText); } - private void sendMessage(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private void sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); - sendMessage.setChatId(String.valueOf(telegramId)); + sendMessage.setChatId(telegramId); sendMessage.setText( sendPreProcessing != null ? sendPreProcessing.pretreatment(boxAnswer.getMessage()) diff --git a/telegram-simple/pom.xml b/telegram-simple/pom.xml index 7e6e5ac..e687ca6 100644 --- a/telegram-simple/pom.xml +++ b/telegram-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather.telegram telegram-bot - 0.0.34 + 0.0.38 4.0.0