From dc7f1cf767b156732646225262e4cfa172a044f2 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 26 Jun 2022 23:16:54 +0300 Subject: [PATCH] v.0.0.9 --- pom.xml | 2 +- .../godfather/telegram/TelegramBot.java | 4 +-- .../telegram/TelegramPollingBot.java | 10 +++--- .../MessageAutoresponderTelegram.java | 16 ++++----- .../domain/keyboard/InlineKeyBoard.java | 14 +++++--- .../domain/keyboard/button/ButtonUrl.java | 2 +- .../domain/keyboard/button/ButtonWebApp.java | 34 +++++++++++++++++++ ...Impl.java => EventDistributorService.java} | 17 ++++++---- .../telegram/listen/TelegramConnect.java | 2 +- .../telegram/listen/TelegramSender.java | 18 +++++++--- 10 files changed, 84 insertions(+), 35 deletions(-) create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonWebApp.java rename telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/{EventDistributorImpl.java => EventDistributorService.java} (57%) diff --git a/pom.xml b/pom.xml index 46736c1..f71600b 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ UTF-8 UTF-8 - 0.0.6 + 0.0.7 6.0.1 3.10.1 diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramBot.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramBot.java index ae098f6..c8c0c5f 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramBot.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramBot.java @@ -1,6 +1,6 @@ package dev.struchkov.godfather.telegram; -import dev.struchkov.godfather.telegram.listen.EventDistributorImpl; +import dev.struchkov.godfather.telegram.listen.EventDistributorService; import org.jetbrains.annotations.NotNull; import org.telegram.telegrambots.meta.bots.AbsSender; @@ -13,6 +13,6 @@ public interface TelegramBot { AbsSender getAdsSender(); - void initEventDistributor(@NotNull EventDistributorImpl eventDistributor); + void initEventDistributor(@NotNull EventDistributorService eventDistributor); } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramPollingBot.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramPollingBot.java index a4eb11b..d567962 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramPollingBot.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramPollingBot.java @@ -2,15 +2,13 @@ package dev.struchkov.godfather.telegram; import dev.struchkov.godfather.telegram.config.TelegramPollingConfig; import dev.struchkov.godfather.telegram.listen.EventDistributor; -import dev.struchkov.godfather.telegram.listen.EventDistributorImpl; +import dev.struchkov.godfather.telegram.listen.EventDistributorService; import org.jetbrains.annotations.NotNull; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.bots.AbsSender; -import java.util.Optional; - /** * TODO: Добавить описание класса. * @@ -32,8 +30,8 @@ public class TelegramPollingBot extends TelegramLongPollingBot implements Telegr @Override public void onUpdateReceived(Update update) { - if (eventDistributor != null) { - Optional.ofNullable(update).ifPresent(newUpdate -> eventDistributor.processing(update)); + if (update != null && eventDistributor != null) { + eventDistributor.processing(update); } } @@ -53,7 +51,7 @@ public class TelegramPollingBot extends TelegramLongPollingBot implements Telegr } @Override - public void initEventDistributor(@NotNull EventDistributorImpl eventDistributor) { + public void initEventDistributor(@NotNull EventDistributorService eventDistributor) { this.eventDistributor = eventDistributor; } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MessageAutoresponderTelegram.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MessageAutoresponderTelegram.java index 545f39f..cc05be1 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MessageAutoresponderTelegram.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MessageAutoresponderTelegram.java @@ -1,13 +1,12 @@ package dev.struchkov.godfather.telegram.autoresponder; -import dev.struchkov.autoresponder.repository.UnitPointerRepository; import dev.struchkov.godfather.context.domain.content.Mail; -import dev.struchkov.godfather.context.service.MessageService; +import dev.struchkov.godfather.context.service.PersonSettingService; +import dev.struchkov.godfather.context.service.UnitPointerService; import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.core.GeneralAutoResponder; -import dev.struchkov.godfather.core.domain.unit.MainUnit; -import java.util.Set; +import java.util.List; /** * TODO: Добавить описание класса. @@ -17,11 +16,12 @@ import java.util.Set; public class MessageAutoresponderTelegram extends GeneralAutoResponder { public MessageAutoresponderTelegram( - Set menuUnit, Sending sending, - MessageService messageService, - UnitPointerRepository unitPointerRepository + Sending sending, + PersonSettingService personSettingService, + UnitPointerService unitPointerService, + List unitConfigurations ) { - super(menuUnit, sending, messageService, unitPointerRepository); + super(sending, personSettingService, unitPointerService, unitConfigurations); } } 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 d8f38e1..c89b9db 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 @@ -1,21 +1,22 @@ package dev.struchkov.godfather.telegram.domain.keyboard; +import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine; -import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard; import java.util.ArrayList; import java.util.List; -public class InlineKeyBoard extends SimpleKeyBoard { +public class InlineKeyBoard implements KeyBoard { public static final String TYPE = "INLINE"; + protected List lines = new ArrayList<>(); public InlineKeyBoard(List lines) { - super(lines); + this.lines = lines; } private InlineKeyBoard(Builder builder) { - super(builder.lines); + this.lines = builder.lines; } public static Builder builder() { @@ -26,7 +27,10 @@ public class InlineKeyBoard extends SimpleKeyBoard { return builder().line(keyBoardLine).build(); } - @Override + public List getLines() { + return lines; + } + public String getType() { return TYPE; } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonUrl.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonUrl.java index a6e501f..28f815a 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonUrl.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonUrl.java @@ -14,7 +14,7 @@ public class ButtonUrl implements KeyBoardButton { this.url = url; } - public static ButtonUrl link(String label, String url) { + public static ButtonUrl buttonUrl(String label, String url) { return new ButtonUrl(label, url); } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonWebApp.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonWebApp.java new file mode 100644 index 0000000..1a24a8c --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/keyboard/button/ButtonWebApp.java @@ -0,0 +1,34 @@ +package dev.struchkov.godfather.telegram.domain.keyboard.button; + +import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton; + +public class ButtonWebApp implements KeyBoardButton { + + public static final String TYPE = "WEB_APP"; + + private final String label; + private final String url; + + public ButtonWebApp(String label, String url) { + this.label = label; + this.url = url; + } + + public static ButtonWebApp buttonWebApp(String label, String url) { + return new ButtonWebApp(label, url); + } + + public String getUrl() { + return url; + } + + public String getLabel() { + return label; + } + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorImpl.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java similarity index 57% rename from telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorImpl.java rename to telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java index 8fc2a1a..2fce0f9 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorImpl.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.listen; -import dev.struchkov.godfather.context.service.MailService; +import dev.struchkov.godfather.context.domain.content.Mail; +import dev.struchkov.godfather.context.service.EventProvider; import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert; import dev.struchkov.godfather.telegram.convert.MessageMailConvert; import org.jetbrains.annotations.NotNull; @@ -8,17 +9,19 @@ import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Update; +import java.util.List; + /** * TODO: Добавить описание класса. * * @author upagge [30.01.2020] */ -public class EventDistributorImpl implements EventDistributor { +public class EventDistributorService implements EventDistributor { - private final MailService mailService; + private final List> eventProviders; - public EventDistributorImpl(TelegramConnect telegramConnect, MailService mailService) { - this.mailService = mailService; + public EventDistributorService(TelegramConnect telegramConnect, List> eventProviders) { + this.eventProviders = eventProviders; telegramConnect.initEventDistributor(this); } @@ -27,10 +30,10 @@ public class EventDistributorImpl implements EventDistributor { final Message message = update.getMessage(); final CallbackQuery callbackQuery = update.getCallbackQuery(); if (message != null) { - mailService.add(MessageMailConvert.apply(message)); + eventProviders.forEach(provider -> provider.sendEvent(MessageMailConvert.apply(message))); } if (callbackQuery != null) { - mailService.add(CallbackQueryConvert.apply(callbackQuery)); + eventProviders.forEach(provider -> provider.sendEvent(CallbackQueryConvert.apply(callbackQuery))); } } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java index c7a0f5b..321613b 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java @@ -109,7 +109,7 @@ public class TelegramConnect { return telegramBot.getAdsSender(); } - void initEventDistributor(EventDistributorImpl eventDistributor) { + void initEventDistributor(EventDistributorService eventDistributor) { telegramBot.initEventDistributor(eventDistributor); } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java index 878fe0b..4ca91f1 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java @@ -11,6 +11,7 @@ import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.button.ButtonUrl; +import dev.struchkov.godfather.telegram.domain.keyboard.button.ButtonWebApp; import dev.struchkov.godfather.telegram.service.SendPreProcessing; import dev.struchkov.haiti.context.exception.ConvertException; import dev.struchkov.haiti.utils.Inspector; @@ -26,6 +27,7 @@ import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMar import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow; +import org.telegram.telegrambots.meta.api.objects.webapp.WebAppInfo; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; @@ -186,6 +188,12 @@ public class TelegramSender implements Sending { button.setUrl(buttonUrl.getUrl()); button.setText(buttonUrl.getLabel()); } + case ButtonWebApp.TYPE -> { + final ButtonWebApp buttonWebApp = (ButtonWebApp) keyBoardButton; + final WebAppInfo webAppInfo = WebAppInfo.builder().url(buttonWebApp.getUrl()).build(); + button.setWebApp(webAppInfo); + button.setText(buttonWebApp.getLabel()); + } default -> throw new ConvertException("Ошибка преобразования кнопки"); } return button; @@ -199,15 +207,17 @@ public class TelegramSender implements Sending { button.setText(simpleButton.getLabel()); Inspector.isNull(simpleButton.getCallbackData(), ConvertException.supplier("CallbackData поддерживает только Inline клавитаура")); } + case ButtonWebApp.TYPE -> { + final ButtonWebApp buttonWebApp = (ButtonWebApp) keyBoardButton; + final WebAppInfo webAppInfo = WebAppInfo.builder().url(buttonWebApp.getUrl()).build(); + button.setText(buttonWebApp.getLabel()); + button.setWebApp(webAppInfo); + } default -> throw new ConvertException("Ошибка преобразования кнопки"); } return button; } - public void send(Long integer, Long integer1, BoxAnswer boxAnswer) { - - } - public SendType getType() { return SendType.PRIVATE; }