Добавил поддержку получения и отправки видео
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-04-22 17:23:06 +03:00
parent 455a8e1316
commit 266aa852e2
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
8 changed files with 145 additions and 1 deletions

View File

@ -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<MessageEntity> entities = message.getEntities();
if (entities != null) {
@ -121,6 +124,17 @@ public final class MessageChatMailConvert {
return Optional.empty();
}
private static Optional<VideoAttachment> 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<Attachment> convertAttachments(Message message) {
final List<MessageEntity> entities = message.getEntities();
if (Checker.checkNotEmpty(entities)) {

View File

@ -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<MessageEntity> entities = message.getEntities();
if (entities != null) {
@ -120,6 +123,17 @@ public final class MessageMailConvert {
return Optional.empty();
}
private static Optional<VideoAttachment> 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<Attachment> convertAttachments(Message message) {
final List<MessageEntity> entities = message.getEntities();
if (Checker.checkNotEmpty(entities)) {

View File

@ -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<VideoAttachment> findFirstVideo(Collection<Attachment> attachments) {
if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) {
if (isVideo(attachment)) {
return Optional.of((VideoAttachment) attachment);
}
}
}
return Optional.empty();
}
public static Optional<PictureGroupAttachment> findFirstPictureGroup(Collection<Attachment> 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());
}
}

View File

@ -7,6 +7,7 @@ public enum TelegramAttachmentType {
PICTURE,
LINK,
COMMAND,
BUTTON_CLICK
BUTTON_CLICK,
VIDEO
}

View File

@ -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());
}
}

View File

@ -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<Attachment>
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);
};
}

View File

@ -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;
}
}

View File

@ -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<SentBox> 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<SentBox> sendDocument(BoxAnswer boxAnswer, BoxAnswer preparedAnswer) {
final SendDocument sendDocument = new SendDocument();
sendDocument.setCaption(boxAnswer.getMessage());