diff --git a/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageChatMailConvert.java b/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageChatMailConvert.java index 260fe52..dd5fdc3 100644 --- a/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageChatMailConvert.java +++ b/telegram-context/telegram-context-main/src/main/java/dev/struchkov/godfather/telegram/main/context/convert/MessageChatMailConvert.java @@ -8,6 +8,7 @@ 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.VideoAttachment; import dev.struchkov.godfather.telegram.main.context.MailPayload; import dev.struchkov.haiti.utils.Checker; import dev.struchkov.haiti.utils.Strings; @@ -17,6 +18,7 @@ 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.User; +import org.telegram.telegrambots.meta.api.objects.Video; import java.time.Instant; import java.time.LocalDateTime; @@ -56,6 +58,7 @@ public final class MessageChatMailConvert { convertDocument(message.getDocument()).ifPresent(mail::addAttachment); convertContact(message.getContact()).ifPresent(mail::addAttachment); convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment); + convertVideo(message.getVideo()).ifPresent(mail::addAttachment); final List entities = message.getEntities(); if (entities != null) { @@ -121,6 +124,17 @@ public final class MessageChatMailConvert { return Optional.empty(); } + private static Optional convertVideo(Video video) { + if (video != null) { + final VideoAttachment attachment = new VideoAttachment(); + attachment.setFileId(video.getFileId()); + attachment.setFileSize(video.getFileSize()); + attachment.setFileName(video.getFileName()); + return Optional.of(attachment); + } + return Optional.empty(); + } + private static List convertAttachments(Message message) { final List entities = message.getEntities(); if (Checker.checkNotEmpty(entities)) { 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 076c9b0..ac1bc66 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 @@ -8,6 +8,7 @@ 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.VideoAttachment; import dev.struchkov.godfather.telegram.main.context.MailPayload; import dev.struchkov.haiti.utils.Checker; import dev.struchkov.haiti.utils.Strings; @@ -17,6 +18,7 @@ import org.telegram.telegrambots.meta.api.objects.Document; 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 java.time.Instant; import java.time.LocalDateTime; @@ -55,6 +57,7 @@ public final class MessageMailConvert { convertDocument(message.getDocument()).ifPresent(mail::addAttachment); convertContact(message.getContact()).ifPresent(mail::addAttachment); convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment); + convertVideo(message.getVideo()).ifPresent(mail::addAttachment); final List entities = message.getEntities(); if (entities != null) { @@ -120,6 +123,17 @@ public final class MessageMailConvert { return Optional.empty(); } + private static Optional convertVideo(Video video) { + if (video != null) { + final VideoAttachment attachment = new VideoAttachment(); + attachment.setFileId(video.getFileId()); + attachment.setFileSize(video.getFileSize()); + attachment.setFileName(video.getFileName()); + return Optional.of(attachment); + } + return Optional.empty(); + } + private static List convertAttachments(Message message) { final List entities = message.getEntities(); if (Checker.checkNotEmpty(entities)) { 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 d16126a..5c6a187 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 @@ -9,6 +9,7 @@ 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.TelegramAttachmentType; +import dev.struchkov.godfather.telegram.domain.attachment.VideoAttachment; import java.util.Collection; import java.util.Collections; @@ -57,6 +58,17 @@ public final class Attachments { return Optional.empty(); } + public static Optional findFirstVideo(Collection attachments) { + if (checkNotEmpty(attachments)) { + for (Attachment attachment : attachments) { + if (isVideo(attachment)) { + return Optional.of((VideoAttachment) attachment); + } + } + } + return Optional.empty(); + } + public static Optional findFirstPictureGroup(Collection attachments) { if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { @@ -153,4 +165,9 @@ public final class Attachments { return TelegramAttachmentType.BUTTON_CLICK.name().equals(attachment.getType()); } + public static boolean isVideo(Attachment attachment) { + isNotNull(attachment); + return TelegramAttachmentType.VIDEO.name().equals(attachment.getType()); + } + } 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 6482916..05f95e6 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 @@ -7,6 +7,7 @@ public enum TelegramAttachmentType { PICTURE, LINK, COMMAND, - BUTTON_CLICK + BUTTON_CLICK, + VIDEO } diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VideoAttachment.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VideoAttachment.java new file mode 100644 index 0000000..477f656 --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/VideoAttachment.java @@ -0,0 +1,19 @@ +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 VideoAttachment extends Attachment { + + private String fileId; + private Long fileSize; + private String fileName; + + public VideoAttachment() { + super(TelegramAttachmentType.VIDEO.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 fa750d8..a162461 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 @@ -12,6 +12,7 @@ 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.TelegramAttachmentType; +import dev.struchkov.godfather.telegram.domain.attachment.VideoAttachment; import dev.struchkov.haiti.utils.ObjectUtils; import java.io.IOException; @@ -39,6 +40,7 @@ public class TelegramAttachmentDeserializer extends StdDeserializer case PICTURE -> parser.getCodec().treeToValue(node, PictureGroupAttachment.class); case LINK -> parser.getCodec().treeToValue(node, LinkAttachment.class); case COMMAND -> parser.getCodec().treeToValue(node, CommandAttachment.class); + case VIDEO -> parser.getCodec().treeToValue(node, VideoAttachment.class); }; } diff --git a/telegram-domain/telegram-domain-simple/src/main/java/dev/struchkov/godfather/telegram/simple/domain/attachment/send/VideoSendAttachment.java b/telegram-domain/telegram-domain-simple/src/main/java/dev/struchkov/godfather/telegram/simple/domain/attachment/send/VideoSendAttachment.java new file mode 100644 index 0000000..8877318 --- /dev/null +++ b/telegram-domain/telegram-domain-simple/src/main/java/dev/struchkov/godfather/telegram/simple/domain/attachment/send/VideoSendAttachment.java @@ -0,0 +1,28 @@ +package dev.struchkov.godfather.telegram.simple.domain.attachment.send; + +import dev.struchkov.godfather.simple.domain.content.send.SendAttachment; +import dev.struchkov.godfather.simple.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 VideoSendAttachment implements SendAttachment { + + public static final String TYPE = "VIDEO"; + + private SendFile sendFile; + + @Override + public String getType() { + return TYPE; + } + +} 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 60d7edb..8f02f15 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 @@ -14,6 +14,7 @@ import dev.struchkov.godfather.telegram.simple.context.repository.SenderReposito import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import dev.struchkov.godfather.telegram.simple.domain.attachment.send.PhotoSendAttachment; +import dev.struchkov.godfather.telegram.simple.domain.attachment.send.VideoSendAttachment; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,6 +23,7 @@ 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.send.SendVideo; 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; @@ -163,6 +165,8 @@ public class TelegramSender implements TelegramSending { return sendPhoto(boxAnswer, preparedAnswer); case "DOCUMENT": return sendDocument(boxAnswer, preparedAnswer); + case "VIDEO": + return sendVideo(boxAnswer); } } return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId); @@ -277,6 +281,51 @@ public class TelegramSender implements TelegramSending { return Optional.empty(); } + private Optional sendVideo(BoxAnswer boxAnswer) { + final VideoSendAttachment video = (VideoSendAttachment) boxAnswer.getAttachment(); + final SendFile sendFile = video.getSendFile(); + + final SendVideo sendVideo = new SendVideo(); + sendVideo.setCaption(boxAnswer.getMessage()); + sendVideo.setChatId(boxAnswer.getRecipientPersonId()); + sendVideo.setVideo(convertInputFile(sendFile)); + sendVideo.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); + + boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> { + if (TRUE.equals(isDisable)) sendVideo.disableNotification(); + }); + boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendVideo.setParseMode(ParseMode.MARKDOWN); + }); + boxAnswer.getPayLoad(ENABLE_HTML).ifPresent(isEnable -> { + if (TRUE.equals(isEnable)) sendVideo.setParseMode(ParseMode.HTML); + }); + + Message execute = null; + try { + execute = absSender.execute(sendVideo); + } catch (TelegramApiRequestException e) { + throw new TelegramSenderException(e.getApiResponse(), e); + } catch (TelegramApiException e) { + throw new TelegramSenderException(e.getMessage(), e); + } + if (checkNotNull(execute)) { + if (checkNotNull(senderRepository)) { + senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), execute.getMessageId().toString()); + } + return Optional.of( + SentBox.builder() + .personId(boxAnswer.getRecipientPersonId()) + .messageId(execute.getMessageId().toString()) + .sentAnswer(boxAnswer) + .originalAnswer(boxAnswer) + .sentMail(MessageMailConvert.apply(execute)) + .build() + ); + } + return Optional.empty(); + } + private Optional sendDocument(BoxAnswer boxAnswer, BoxAnswer preparedAnswer) { final SendDocument sendDocument = new SendDocument(); sendDocument.setCaption(boxAnswer.getMessage());