From 79f99ae7c1e6b5d559c68afd7d2601ce058c3b04 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 24 Apr 2023 03:03:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../telegram-context-quarkus/pom.xml | 4 + .../send/DocumentSendAttachment.java | 28 +++ .../attachment/send/PhotoSendAttachment.java | 28 +++ .../TelegramAttachmentSendDeserializer.java | 34 ++++ .../quarkus/sender/TelegramSender.java | 172 +++++++++++++++--- 5 files changed, 245 insertions(+), 21 deletions(-) create mode 100644 telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/DocumentSendAttachment.java create mode 100644 telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/PhotoSendAttachment.java create mode 100644 telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/deser/TelegramAttachmentSendDeserializer.java diff --git a/telegram-context/telegram-context-quarkus/pom.xml b/telegram-context/telegram-context-quarkus/pom.xml index 02533c6..10b9099 100644 --- a/telegram-context/telegram-context-quarkus/pom.xml +++ b/telegram-context/telegram-context-quarkus/pom.xml @@ -24,6 +24,10 @@ dev.struchkov.godfather.telegram telegram-context-main + + dev.struchkov.godfather.telegram + telegram-domain-quarkus + org.telegram diff --git a/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/DocumentSendAttachment.java b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/DocumentSendAttachment.java new file mode 100644 index 0000000..22ccee7 --- /dev/null +++ b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/DocumentSendAttachment.java @@ -0,0 +1,28 @@ +package dev.struchkov.godfather.telegram.quarkus.domain.attachment.send; + +import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment; +import dev.struchkov.godfather.quarkus.domain.content.send.SendFile; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class DocumentSendAttachment implements SendAttachment { + + public static final String TYPE = "DOCUMENT"; + + private SendFile sendFile; + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/PhotoSendAttachment.java b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/PhotoSendAttachment.java new file mode 100644 index 0000000..f39d816 --- /dev/null +++ b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/attachment/send/PhotoSendAttachment.java @@ -0,0 +1,28 @@ +package dev.struchkov.godfather.telegram.quarkus.domain.attachment.send; + +import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment; +import dev.struchkov.godfather.quarkus.domain.content.send.SendFile; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class PhotoSendAttachment implements SendAttachment { + + public static final String TYPE = "PHOTO"; + + private SendFile sendFile; + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/deser/TelegramAttachmentSendDeserializer.java b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/deser/TelegramAttachmentSendDeserializer.java new file mode 100644 index 0000000..f04b648 --- /dev/null +++ b/telegram-domain/telegram-domain-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/domain/deser/TelegramAttachmentSendDeserializer.java @@ -0,0 +1,34 @@ +package dev.struchkov.godfather.telegram.quarkus.domain.deser; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment; +import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.DocumentSendAttachment; +import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.PhotoSendAttachment; + +import java.io.IOException; + +public class TelegramAttachmentSendDeserializer extends StdDeserializer { + + public TelegramAttachmentSendDeserializer() { + this(null); + } + + public TelegramAttachmentSendDeserializer(Class vc) { + super(vc); + } + + @Override + public SendAttachment deserialize(JsonParser parser, DeserializationContext context) throws IOException { + final JsonNode node = parser.getCodec().readTree(parser); + final String attachmentType = node.get("type").asText(); + return switch (attachmentType) { + case "PHOTO" -> parser.getCodec().treeToValue(node, PhotoSendAttachment.class); + case "DOCUMENT" -> parser.getCodec().treeToValue(node, DocumentSendAttachment.class); + default -> throw new IllegalStateException("Unexpected value: " + attachmentType); + }; + } + +} 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 226f619..6a3ae1f 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 @@ -4,26 +4,34 @@ import dev.struchkov.godfather.main.domain.SendType; import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.SentBox; import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing; +import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment; +import dev.struchkov.godfather.quarkus.domain.content.send.SendFile; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload; import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert; import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderRepository; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending; +import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.PhotoSendAttachment; import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.telegram.telegrambots.meta.api.methods.ParseMode; import org.telegram.telegrambots.meta.api.methods.invoices.SendInvoice; +import org.telegram.telegrambots.meta.api.methods.send.SendDocument; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.methods.send.SendPhoto; import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; +import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; +import java.io.File; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -36,6 +44,7 @@ import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENA import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN; import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertInlineKeyBoard; import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertKeyBoard; +import static dev.struchkov.haiti.utils.Checker.checkNotBlank; import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static java.lang.Boolean.TRUE; import static java.util.concurrent.CompletableFuture.completedFuture; @@ -135,28 +144,39 @@ public class TelegramSender implements TelegramSending { final SendInvoice sendInvoice = optInvoice.get(); return Uni.createFrom().completionStage(executeAsync(sendInvoice)) .onItem().transform(ignore -> null); - } else { - if (boxAnswer.isReplace()) { - final String replaceMessageId = boxAnswer.getReplaceMessageId(); - if (checkNotNull(replaceMessageId)) { - return replace(recipientTelegramId, replaceMessageId, boxAnswer, saveMessageId); - } else { - if (checkNotNull(senderRepository)) { - return senderRepository.getLastSendMessage(recipientTelegramId) - .onItem().transformToUni( - lastId -> { - if (checkNotNull(lastId)) { - return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId); - } else { - return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); - } + } + + if (boxAnswer.isReplace()) { + final String replaceMessageId = boxAnswer.getReplaceMessageId(); + if (checkNotNull(replaceMessageId)) { + return replace(recipientTelegramId, replaceMessageId, boxAnswer, saveMessageId); + } else { + if (checkNotNull(senderRepository)) { + return senderRepository.getLastSendMessage(recipientTelegramId) + .onItem().transformToUni( + lastId -> { + if (checkNotNull(lastId)) { + return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId); + } else { + return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); } - ); - } + } + ); } } - return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); } + + final SendAttachment sendAttachment = boxAnswer.getAttachment(); + if (checkNotNull(sendAttachment)) { + switch (sendAttachment.getType()) { + case "PHOTO": + return sendPhoto(boxAnswer); + case "DOCUMENT": + return sendDocument(boxAnswer); + } + } + + return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); } ); } @@ -230,6 +250,108 @@ public class TelegramSender implements TelegramSending { ); } + private Uni sendPhoto(BoxAnswer boxAnswer) { + final PhotoSendAttachment photoSendAttachment = (PhotoSendAttachment) boxAnswer.getAttachment(); + final SendFile sendFile = photoSendAttachment.getSendFile(); + + final SendPhoto sendPhoto = new SendPhoto(); + sendPhoto.setCaption(boxAnswer.getMessage()); + sendPhoto.setChatId(boxAnswer.getRecipientPersonId()); + sendPhoto.setPhoto(convertInputFile(sendFile)); + sendPhoto.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); + + boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> { + if (TRUE.equals(isDisable)) sendPhoto.disableNotification(); + }); + boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendPhoto.setParseMode(ParseMode.MARKDOWN); + }); + boxAnswer.getPayLoad(ENABLE_HTML).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendPhoto.setParseMode(ParseMode.HTML); + }); + + return Uni.createFrom().completionStage(executeAsync(sendPhoto)) + .onItem().ifNotNull().call( + message -> { + if (checkNotNull(senderRepository)) { + return senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), message.getMessageId().toString()); + } + return Uni.createFrom().voidItem(); + } + ).onItem().ifNotNull().transform( + message -> SentBox.builder() + .personId(boxAnswer.getRecipientPersonId()) + .messageId(message.getMessageId().toString()) + .sentAnswer(boxAnswer) + .originalAnswer(boxAnswer) + .sentMail(MessageMailConvert.apply(message)) + .build() + ); + } + + private Uni sendDocument(BoxAnswer boxAnswer) { + final SendDocument sendDocument = new SendDocument(); + sendDocument.setCaption(boxAnswer.getMessage()); + sendDocument.setChatId(boxAnswer.getRecipientPersonId()); + sendDocument.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); + sendDocument.setDocument(convertInputFile(boxAnswer.getAttachment().getSendFile())); + boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> { + if (TRUE.equals(isDisable)) sendDocument.disableNotification(); + }); + boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendDocument.setParseMode(ParseMode.MARKDOWN); + }); + boxAnswer.getPayLoad(ENABLE_HTML).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendDocument.setParseMode(ParseMode.HTML); + }); + + return Uni.createFrom().completionStage(executeAsync(sendDocument)) + .onItem().ifNotNull().call( + message -> { + if (checkNotNull(senderRepository)) { + return senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), message.getMessageId().toString()); + } + return Uni.createFrom().voidItem(); + } + ).onItem().ifNotNull().transform( + message -> SentBox.builder() + .personId(boxAnswer.getRecipientPersonId()) + .messageId(message.getMessageId().toString()) + .sentAnswer(boxAnswer) + .originalAnswer(boxAnswer) + .sentMail(MessageMailConvert.apply(message)) + .build() + ); + } + + private InputFile convertInputFile(SendFile sendFile) { + final File fileData = sendFile.getData(); + final String fileName = sendFile.getFileName(); + + if (checkNotBlank(sendFile.getFileId())) { + return new InputFile(sendFile.getFileId()); + } + + if (checkNotBlank(sendFile.getUrl())) { + return new InputFile(sendFile.getUrl()); + } + + if (checkNotNull(fileData)) { + if (checkNotBlank(fileName)) { + return new InputFile(fileData, fileName); + } else { + return new InputFile(fileData); + } + } + + if (checkNotNull(sendFile.getFileStream())) { + return new InputFile(sendFile.getFileStream(), fileName); + } else { + return new InputFile(fileName); + } + + } + public Multi splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) { final List split = new ArrayList<>(); String message = boxAnswer.getMessage(); @@ -237,15 +359,15 @@ public class TelegramSender implements TelegramSending { while (message.length() > maxMessageLength) { String subMessage = message.substring(0, maxMessageLength); message = message.substring(maxMessageLength); - split.add(createNewBoxAnswer(boxAnswer, subMessage)); + split.add(createNewTextAnswer(boxAnswer, subMessage)); } - split.add(createNewBoxAnswer(boxAnswer, message)); + split.add(createNewTextAnswer(boxAnswer, message)); return Multi.createFrom().iterable(split); } - private SendMessage createNewBoxAnswer(BoxAnswer boxAnswer, String subMessage) { + private SendMessage createNewTextAnswer(BoxAnswer boxAnswer, String subMessage) { final SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(boxAnswer.getRecipientPersonId()); sendMessage.setText(subMessage); @@ -289,6 +411,14 @@ public class TelegramSender implements TelegramSending { return completedFuture(null); } + private CompletableFuture executeAsync(SendPhoto sendPhoto) { + return absSender.executeAsync(sendPhoto); + } + + private CompletableFuture executeAsync(SendDocument sendDocument) { + return absSender.executeAsync(sendDocument); + } + @Override public SendType getType() { return SendType.PRIVATE;