diff --git a/telegram-consumer/telegram-consumer-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/consumer/EventDistributorService.java b/telegram-consumer/telegram-consumer-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/consumer/EventDistributorService.java index 216a2cf..d721dd9 100644 --- a/telegram-consumer/telegram-consumer-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/consumer/EventDistributorService.java +++ b/telegram-consumer/telegram-consumer-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/consumer/EventDistributorService.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.quarkus.consumer; import dev.struchkov.godfather.main.domain.EventContainer; +import dev.struchkov.godfather.main.domain.content.EditedMail; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.quarkus.context.service.EventDispatching; import dev.struchkov.godfather.telegram.domain.event.Subscribe; @@ -44,6 +45,7 @@ public class EventDistributorService implements EventDistributor { .onItem().transformToUni( v -> { final Message message = update.getMessage(); + final Message editedMessage = update.getEditedMessage(); final CallbackQuery callbackQuery = update.getCallbackQuery(); final PreCheckoutQuery preCheckoutQuery = update.getPreCheckoutQuery(); final InlineQuery inlineQuery = update.getInlineQuery(); @@ -66,6 +68,10 @@ public class EventDistributorService implements EventDistributor { return Uni.createFrom().item(new EventContainer<>(Mail.class, MessageMailConvert.apply(message))); } + if (update.hasEditedMessage()) { + return Uni.createFrom().item(new EventContainer<>(EditedMail.class, MessageMailConvert.applyEdited(editedMessage))); + } + if (update.hasCallbackQuery()) { return Uni.createFrom().item(new EventContainer<>(Mail.class, CallbackQueryConvert.apply(callbackQuery))); } diff --git a/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageMailConvert.java b/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageMailConvert.java index 0056800..1760c86 100644 --- a/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageMailConvert.java +++ b/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageMailConvert.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.telegram.main.context.convert; import dev.struchkov.godfather.main.domain.content.Attachment; +import dev.struchkov.godfather.main.domain.content.EditedMail; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.telegram.domain.attachment.CommandAttachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; @@ -8,7 +9,9 @@ import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.StickerAttachment; import dev.struchkov.godfather.telegram.domain.attachment.VideoAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.VoiceAttachment; import dev.struchkov.godfather.telegram.main.context.MailPayload; import dev.struchkov.haiti.utils.Checker; import dev.struchkov.haiti.utils.Strings; @@ -19,13 +22,17 @@ import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.PhotoSize; import org.telegram.telegrambots.meta.api.objects.Video; +import org.telegram.telegrambots.meta.api.objects.Voice; +import org.telegram.telegrambots.meta.api.objects.stickers.Sticker; +import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.UUID; import static dev.struchkov.haiti.utils.Checker.checkNotBlank; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; @@ -41,6 +48,13 @@ public final class MessageMailConvert { utilityClass(); } + public static EditedMail applyEdited(Message message) { + return EditedMail.builder() + .editDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getEditDate()), ZoneId.systemDefault())) + .newMail(apply(message)) + .build(); + } + public static Mail apply(Message message) { final Mail mail = new Mail(); @@ -59,6 +73,7 @@ public final class MessageMailConvert { convertContact(message.getContact()).ifPresent(mail::addAttachment); convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment); convertVideo(message.getVideo()).ifPresent(mail::addAttachment); + convertVoice(message.getVoice()).ifPresent(mail::addAttachment); final List entities = message.getEntities(); if (entities != null) { @@ -134,6 +149,32 @@ public final class MessageMailConvert { return Optional.empty(); } + private static Optional convertVoice(Voice voice) { + if (voice != null) { + final VoiceAttachment attachment = new VoiceAttachment(); + attachment.setFileId(voice.getFileId()); + attachment.setFileSize(voice.getFileSize()); + attachment.setMimeType(voice.getMimeType()); + attachment.setDuration(Duration.ofSeconds(voice.getDuration())); + attachment.setFileName(UUID.randomUUID().toString()); + return Optional.of(attachment); + } + return Optional.empty(); + } + + private static Optional convertVoice(Sticker sticker) { + if (sticker != null) { + final StickerAttachment attachment = new StickerAttachment(); + attachment.setFileId(sticker.getFileId()); + attachment.setFileSize(sticker.getFileSize().longValue()); + attachment.setAnimated(sticker.getIsAnimated()); + attachment.setVideo(sticker.getIsVideo()); + attachment.setFileName(UUID.randomUUID().toString()); + return Optional.of(attachment); + } + return Optional.empty(); + } + private static Optional convertVideo(Video video) { if (video != null) { final VideoAttachment attachment = new VideoAttachment(); diff --git a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/AttachmentService.java b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/AttachmentService.java index 9ef9a8d..768701c 100644 --- a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/AttachmentService.java +++ b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/AttachmentService.java @@ -1,6 +1,6 @@ package dev.struchkov.godfather.telegram.quarkus.context.service; -import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.FileAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer; @@ -9,9 +9,9 @@ import org.jetbrains.annotations.NotNull; public interface AttachmentService { - Uni uploadFile(@NotNull DocumentAttachment documentAttachment); + Uni uploadFile(@NotNull FileAttachment documentAttachment); - Uni uploadBytes(@NotNull DocumentAttachment documentAttachment); + Uni uploadBytes(@NotNull FileAttachment fileAttachment); Uni uploadBytes(@NotNull Picture picture); 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 5c6a187..4394af4 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 @@ -8,8 +8,10 @@ import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.StickerAttachment; import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; import dev.struchkov.godfather.telegram.domain.attachment.VideoAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.VoiceAttachment; import java.util.Collection; import java.util.Collections; @@ -47,6 +49,28 @@ public final class Attachments { return Optional.empty(); } + public static Optional findFirstVoice(Collection attachments) { + if (checkNotEmpty(attachments)) { + for (Attachment attachment : attachments) { + if (isVoice(attachment)) { + return Optional.of((VoiceAttachment) attachment); + } + } + } + return Optional.empty(); + } + + public static Optional findFirstSticker(Collection attachments) { + if (checkNotEmpty(attachments)) { + for (Attachment attachment : attachments) { + if (isSticker(attachment)) { + return Optional.of((StickerAttachment) attachment); + } + } + } + return Optional.empty(); + } + public static Optional findFirstButtonClick(Collection attachments) { if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { @@ -145,17 +169,17 @@ public final class Attachments { return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType()); } - private static boolean isContact(Attachment attachment) { + public static boolean isContact(Attachment attachment) { isNotNull(attachment); return TelegramAttachmentType.CONTACT.name().equals(attachment.getType()); } - private static boolean isPictureGroup(Attachment attachment) { + public static boolean isPictureGroup(Attachment attachment) { isNotNull(attachment); return TelegramAttachmentType.PICTURE.name().equals(attachment.getType()); } - private static boolean isLink(Attachment attachment) { + public static boolean isLink(Attachment attachment) { isNotNull(attachment); return TelegramAttachmentType.LINK.name().equals(attachment.getType()); } @@ -170,4 +194,14 @@ public final class Attachments { return TelegramAttachmentType.VIDEO.name().equals(attachment.getType()); } + public static boolean isVoice(Attachment attachment) { + isNotNull(attachment); + return TelegramAttachmentType.VOICE.name().equals(attachment.getType()); + } + + public static boolean isSticker(Attachment attachment) { + isNotNull(attachment); + return TelegramAttachmentType.STICKER.name().equals(attachment.getType()); + } + } diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/AttachmentServiceImpl.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/AttachmentServiceImpl.java index 2bc3124..7631f04 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/AttachmentServiceImpl.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/AttachmentServiceImpl.java @@ -1,7 +1,9 @@ package dev.struchkov.godfather.telegram.quarkus.core.service; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.FileAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; +import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.quarkus.context.service.AttachmentService; @@ -62,17 +64,17 @@ public class AttachmentServiceImpl implements AttachmentService { } @Override - public Uni uploadFile(@NotNull DocumentAttachment documentAttachment) { - isNotNull(documentAttachment); - return downloadFile(documentAttachment) - .onItem().ifNotNull().transform(file -> new FileContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), file)); + public Uni uploadFile(@NotNull FileAttachment fileAttachment) { + isNotNull(fileAttachment); + return downloadFile(fileAttachment) + .onItem().ifNotNull().transform(file -> new FileContainer(fileAttachment.getFileName(), fileAttachment.getMimeType(), file)); } @Override - public Uni uploadBytes(@NotNull DocumentAttachment documentAttachment) { - isNotNull(documentAttachment); - return downloadBytes(documentAttachment) - .onItem().ifNotNull().transform(bytes -> new ByteContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), bytes)); + public Uni uploadBytes(@NotNull FileAttachment fileAttachment) { + isNotNull(fileAttachment); + return downloadBytes(fileAttachment) + .onItem().ifNotNull().transform(bytes -> new ByteContainer(fileAttachment.getFileName(), fileAttachment.getMimeType(), bytes)); } @Override @@ -86,7 +88,7 @@ public class AttachmentServiceImpl implements AttachmentService { return telegramDownloadBytes(picture.getFileId()); } - private Uni downloadBytes(DocumentAttachment documentAttachment) { + private Uni downloadBytes(FileAttachment documentAttachment) { return telegramDownloadBytes(documentAttachment.getFileId()); } @@ -110,15 +112,15 @@ public class AttachmentServiceImpl implements AttachmentService { ); } - private Uni downloadFile(DocumentAttachment documentAttachment) { - return getFileUrl(documentAttachment.getFileId()) + private Uni downloadFile(FileAttachment fileAttachment) { + return getFileUrl(fileAttachment.getFileId()) .onItem().ifNotNull().transformToUni(fileUrl -> Uni.createFrom().completionStage( CompletableFuture.supplyAsync(() -> { final StringBuilder filePath = new StringBuilder(); if (folderPathForFiles != null) { filePath.append(folderPathForFiles); } - filePath.append(UUID.randomUUID()).append("_").append(documentAttachment.getFileName()); + filePath.append(UUID.randomUUID()).append("_").append(fileAttachment.getFileName()); final File localFile = new File(filePath.toString()); final InputStream is; try { diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java index ae76c22..a5c36af 100644 --- a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java @@ -1,17 +1,11 @@ package dev.struchkov.godfather.telegram.domain.attachment; -import dev.struchkov.godfather.main.domain.content.Attachment; import lombok.Getter; import lombok.Setter; @Getter @Setter -public class DocumentAttachment extends Attachment { - - private String fileId; - private Long fileSize; - private String fileName; - private String mimeType; +public class DocumentAttachment extends FileAttachment { public DocumentAttachment() { super(TelegramAttachmentType.DOCUMENT.name()); diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/FileAttachment.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/FileAttachment.java new file mode 100644 index 0000000..7190758 --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/FileAttachment.java @@ -0,0 +1,20 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import dev.struchkov.godfather.main.domain.content.Attachment; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class FileAttachment extends Attachment { + + protected String fileId; + protected Long fileSize; + protected String mimeType; + private String fileName; + + protected FileAttachment(String type) { + super(type); + } + +} diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/StickerAttachment.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/StickerAttachment.java new file mode 100644 index 0000000..c19e2e9 --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/StickerAttachment.java @@ -0,0 +1,18 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class StickerAttachment extends FileAttachment { + + private String emoji; + private boolean video; + private boolean animated; + + public StickerAttachment() { + super(TelegramAttachmentType.STICKER.name()); + } + +} diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java index 05f95e6..be038af 100644 --- a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java @@ -8,6 +8,8 @@ public enum TelegramAttachmentType { LINK, COMMAND, BUTTON_CLICK, - VIDEO + VIDEO, + VOICE, + STICKER } diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VoiceAttachment.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VoiceAttachment.java new file mode 100644 index 0000000..59c2bda --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VoiceAttachment.java @@ -0,0 +1,18 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import lombok.Getter; +import lombok.Setter; + +import java.time.Duration; + +@Getter +@Setter +public class VoiceAttachment extends FileAttachment { + + private Duration duration; + + public VoiceAttachment() { + super(TelegramAttachmentType.VOICE.name()); + } + +} diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/deser/TelegramAttachmentDeserializer.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/deser/TelegramAttachmentDeserializer.java index a162461..1f63df9 100644 --- a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/deser/TelegramAttachmentDeserializer.java +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/deser/TelegramAttachmentDeserializer.java @@ -11,8 +11,10 @@ import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.StickerAttachment; import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; import dev.struchkov.godfather.telegram.domain.attachment.VideoAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.VoiceAttachment; import dev.struchkov.haiti.utils.ObjectUtils; import java.io.IOException; @@ -41,6 +43,8 @@ public class TelegramAttachmentDeserializer extends StdDeserializer case LINK -> parser.getCodec().treeToValue(node, LinkAttachment.class); case COMMAND -> parser.getCodec().treeToValue(node, CommandAttachment.class); case VIDEO -> parser.getCodec().treeToValue(node, VideoAttachment.class); + case VOICE -> parser.getCodec().treeToValue(node, VoiceAttachment.class); + case STICKER -> parser.getCodec().treeToValue(node, StickerAttachment.class); }; }