From 1a6c801e9cecc2505105936dee8af897ac6df582 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 10 Jul 2022 00:31:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=BA=D1=82=D1=83=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D1=81=20godfather?= =?UTF-8?q?bot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- telegram-core/pom.xml | 2 +- .../{listen => }/TelegramConnect.java | 18 ++-- ...am.java => MailAutoresponderTelegram.java} | 13 +-- .../telegram/{ => config}/ProxyConfig.java | 2 +- .../config/TelegramPollingConfig.java | 2 - .../telegram/convert/MessageMailConvert.java | 36 ++++++-- .../telegram/domain/FileContainer.java | 48 ++++++++++ .../domain/attachment/DocumentAttachment.java | 49 ++++++++++ .../attachment/TelegramAttachmentType.java | 7 ++ .../listen/EventDistributorService.java | 1 + .../telegram/listen/TelegramSender.java | 1 + .../service/AttachmentServiceImpl.java | 92 +++++++++++++++++++ .../godfather/telegram/utils/Attachments.java | 45 +++++++++ 14 files changed, 290 insertions(+), 30 deletions(-) rename telegram-core/src/main/java/dev/struchkov/godfather/telegram/{listen => }/TelegramConnect.java (89%) rename telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/{MessageAutoresponderTelegram.java => MailAutoresponderTelegram.java} (56%) rename telegram-core/src/main/java/dev/struchkov/godfather/telegram/{ => config}/ProxyConfig.java (95%) create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java diff --git a/pom.xml b/pom.xml index 7f12fd7..da59a3a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.10 + 0.0.11 pom @@ -33,7 +33,7 @@ UTF-8 UTF-8 - 0.0.8 + 0.0.9 6.1.0 3.10.1 diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index b13e2f5..4d1d95f 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.10 + 0.0.11 telegram-core 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/TelegramConnect.java similarity index 89% rename from telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java rename to telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramConnect.java index 321613b..8cc3f91 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramConnect.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/TelegramConnect.java @@ -1,9 +1,8 @@ -package dev.struchkov.godfather.telegram.listen; +package dev.struchkov.godfather.telegram; -import dev.struchkov.godfather.telegram.ProxyConfig; -import dev.struchkov.godfather.telegram.TelegramBot; -import dev.struchkov.godfather.telegram.TelegramPollingBot; +import dev.struchkov.godfather.telegram.config.ProxyConfig; import dev.struchkov.godfather.telegram.config.TelegramPollingConfig; +import dev.struchkov.godfather.telegram.listen.EventDistributorService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.telegram.telegrambots.bots.DefaultBotOptions; @@ -25,8 +24,10 @@ public class TelegramConnect { private static final Logger log = LoggerFactory.getLogger(TelegramConnect.class); private TelegramBot telegramBot; + private final TelegramPollingConfig telegramPollingConfig; public TelegramConnect(TelegramPollingConfig telegramPollingConfig) { + this.telegramPollingConfig = telegramPollingConfig; initLongPolling(telegramPollingConfig); } @@ -46,6 +47,7 @@ public class TelegramConnect { // } private void initLongPolling(TelegramPollingConfig telegramPollingConfig) { + final ProxyConfig proxyConfig = telegramPollingConfig.getProxyConfig(); if (proxyConfig != null && proxyConfig.getPassword() != null) { try { @@ -105,12 +107,16 @@ public class TelegramConnect { } } - AbsSender getAdsSender() { + public AbsSender getAdsSender() { return telegramBot.getAdsSender(); } - void initEventDistributor(EventDistributorService eventDistributor) { + public void initEventDistributor(EventDistributorService eventDistributor) { telegramBot.initEventDistributor(eventDistributor); } + public String getToken() { + return telegramPollingConfig.getBotToken(); + } + } 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/MailAutoresponderTelegram.java similarity index 56% rename from telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MessageAutoresponderTelegram.java rename to telegram-core/src/main/java/dev/struchkov/godfather/telegram/autoresponder/MailAutoresponderTelegram.java index cc05be1..3331c17 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/MailAutoresponderTelegram.java @@ -2,26 +2,23 @@ package dev.struchkov.godfather.telegram.autoresponder; import dev.struchkov.godfather.context.domain.content.Mail; import dev.struchkov.godfather.context.service.PersonSettingService; -import dev.struchkov.godfather.context.service.UnitPointerService; +import dev.struchkov.godfather.context.service.StorylineService; import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.core.GeneralAutoResponder; -import java.util.List; - /** * TODO: Добавить описание класса. * * @author upagge [18.08.2019] */ -public class MessageAutoresponderTelegram extends GeneralAutoResponder { +public class MailAutoresponderTelegram extends GeneralAutoResponder { - public MessageAutoresponderTelegram( + public MailAutoresponderTelegram( Sending sending, PersonSettingService personSettingService, - UnitPointerService unitPointerService, - List unitConfigurations + StorylineService storyLineService ) { - super(sending, personSettingService, unitPointerService, unitConfigurations); + super(sending, personSettingService, storyLineService); } } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/ProxyConfig.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/ProxyConfig.java similarity index 95% rename from telegram-core/src/main/java/dev/struchkov/godfather/telegram/ProxyConfig.java rename to telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/ProxyConfig.java index 07355eb..61ac458 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/ProxyConfig.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/ProxyConfig.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.telegram; +package dev.struchkov.godfather.telegram.config; /** * TODO: Добавить описание класса. diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java index 2a352f6..fc1e24e 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/config/TelegramPollingConfig.java @@ -1,7 +1,5 @@ package dev.struchkov.godfather.telegram.config; -import dev.struchkov.godfather.telegram.ProxyConfig; - /** * TODO: Добавить описание класса. * diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java index d225c8e..05a74bd 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java @@ -2,7 +2,8 @@ package dev.struchkov.godfather.telegram.convert; import dev.struchkov.godfather.context.domain.content.Mail; import dev.struchkov.godfather.context.domain.content.attachment.Attachment; -import dev.struchkov.godfather.context.domain.content.attachment.Link; +import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import org.telegram.telegrambots.meta.api.objects.Document; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.MessageEntity; @@ -12,6 +13,7 @@ import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; @@ -35,9 +37,11 @@ public final class MessageMailConvert { mail.setFirstName(message.getChat().getFirstName()); mail.setLastName(message.getChat().getLastName()); + convertDocument(message.getDocument()).ifPresent(mail::addAttachment); + final List entities = message.getEntities(); if (entities != null) { - mail.setAttachments(convertAttachments(entities)); + mail.addAttachments(convertAttachments(entities)); } if (message.getReplyToMessage() != null) { @@ -47,16 +51,28 @@ public final class MessageMailConvert { return mail; } + private static Optional convertDocument(Document document) { + if (document != null) { + final DocumentAttachment attachment = new DocumentAttachment(); + attachment.setFileId(document.getFileId()); + attachment.setFileSize(document.getFileSize()); + attachment.setFileName(document.getFileName()); + attachment.setFileType(document.getMimeType()); + return Optional.of(attachment); + } + return Optional.empty(); + } + private static List convertAttachments(List entities) { final List attachments = new ArrayList<>(); - for (MessageEntity entity : entities) { - String type = entity.getType(); - if ("text_link".equals(type)) { - Link link = new Link(); - link.setUrl(entity.getUrl()); - attachments.add(link); - } - } +// for (MessageEntity entity : entities) { +// String type = entity.getType(); +// if ("text_link".equals(type)) { +// Link link = new Link(); +// link.setUrl(entity.getUrl()); +// attachments.add(link); +// } +// } return attachments; } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java new file mode 100644 index 0000000..ebc91bc --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/FileContainer.java @@ -0,0 +1,48 @@ +package dev.struchkov.godfather.telegram.domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class FileContainer { + + public static final FileContainer EMPTY = new FileContainer(null, null); + + private static final Logger log = LoggerFactory.getLogger(FileContainer.class); + + private final String fileName; + private final File file; + + public FileContainer(String fileName, File file) { + this.fileName = fileName; + this.file = file; + } + + public String getFileName() { + return fileName; + } + + public File getFile() { + return file; + } + + public static FileContainer empty() { + return EMPTY; + } + + public boolean isNotEmpty() { + return file != null; + } + + public void delete() { + try { + Files.deleteIfExists(file.toPath()); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java new file mode 100644 index 0000000..617786d --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/DocumentAttachment.java @@ -0,0 +1,49 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import dev.struchkov.godfather.context.domain.content.attachment.Attachment; + +public class DocumentAttachment extends Attachment { + + private String fileId; + private Long fileSize; + private String fileName; + private String fileType; + + public String getFileId() { + return fileId; + } + + public void setFileId(String fileId) { + this.fileId = fileId; + } + + public Long getFileSize() { + return fileSize; + } + + public void setFileSize(Long fileSize) { + this.fileSize = fileSize; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } + + @Override + public String getType() { + return TelegramAttachmentType.DOCUMENT.name(); + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java new file mode 100644 index 0000000..0789133 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java @@ -0,0 +1,7 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +public enum TelegramAttachmentType { + + DOCUMENT + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java index 2fce0f9..46e8f93 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.listen; import dev.struchkov.godfather.context.domain.content.Mail; import dev.struchkov.godfather.context.service.EventProvider; +import dev.struchkov.godfather.telegram.TelegramConnect; import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert; import dev.struchkov.godfather.telegram.convert.MessageMailConvert; import org.jetbrains.annotations.NotNull; 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 4ca91f1..e1e17a2 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 @@ -8,6 +8,7 @@ import dev.struchkov.godfather.context.domain.keyboard.button.SimpleButton; import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard; import dev.struchkov.godfather.context.service.sender.SendType; import dev.struchkov.godfather.context.service.sender.Sending; +import dev.struchkov.godfather.telegram.TelegramConnect; 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; diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java new file mode 100644 index 0000000..9552f48 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/AttachmentServiceImpl.java @@ -0,0 +1,92 @@ +package dev.struchkov.godfather.telegram.service; + +import dev.struchkov.godfather.telegram.TelegramConnect; +import dev.struchkov.godfather.telegram.domain.FileContainer; +import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import org.apache.commons.io.FileUtils; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.telegram.telegrambots.meta.api.methods.GetFile; +import org.telegram.telegrambots.meta.bots.AbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Stream; + +import static dev.struchkov.haiti.utils.Inspector.isNotNull; + +public class AttachmentServiceImpl { + + private static final Logger log = LoggerFactory.getLogger(AttachmentServiceImpl.class); + + private final AbsSender absSender; + private final String botToken; + + private String folderPathForFiles; + + public AttachmentServiceImpl(TelegramConnect telegramConnect) { + this.absSender = telegramConnect.getAdsSender(); + this.botToken = telegramConnect.getToken(); + } + + public void setFolderPathForFiles(String folderPathForFiles) { + if (folderPathForFiles != null) { + this.folderPathForFiles = folderPathForFiles + "/"; + try (final Stream pathStream = Files.list(Path.of(folderPathForFiles))) { + pathStream.forEach(path -> { + try { + Files.delete(path); + } catch (IOException e) { + log.error(e.getMessage()); + } + }); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + } + + public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) { + isNotNull(documentAttachment); + try { + final File file = downloadFile(documentAttachment); + return new FileContainer(documentAttachment.getFileName(), file); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return FileContainer.empty(); + } + + private File downloadFile(DocumentAttachment documentAttachment) throws IOException, TelegramApiException { + final org.telegram.telegrambots.meta.api.objects.File file = getFilePath(documentAttachment); + + final StringBuilder filePath = new StringBuilder(); + if (folderPathForFiles != null) { + filePath.append(folderPathForFiles); + } + filePath.append(UUID.randomUUID()); + filePath.append("_"); + filePath.append(documentAttachment.getFileName()); + + final java.io.File localFile = new java.io.File(filePath.toString()); + final InputStream is = new URL(file.getFileUrl(botToken)).openStream(); + FileUtils.copyInputStreamToFile(is, localFile); + return localFile; + } + + private org.telegram.telegrambots.meta.api.objects.File getFilePath(DocumentAttachment documentAttachment) throws TelegramApiException { + final GetFile getFile = new GetFile(); + getFile.setFileId(documentAttachment.getFileId()); + return absSender.execute(getFile); + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java new file mode 100644 index 0000000..e4e0706 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java @@ -0,0 +1,45 @@ +package dev.struchkov.godfather.telegram.utils; + +import dev.struchkov.godfather.context.domain.content.attachment.Attachment; +import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; +import dev.struchkov.haiti.utils.Inspector; + +import java.util.Collection; +import java.util.Optional; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; + +public final class Attachments { + + private Attachments() { + utilityClass(); + } + + public static Optional findFirstDocument(Collection attachments) { + if (attachments != null) { + for (Attachment attachment : attachments) { + if (isDocument(attachment)) { + return Optional.ofNullable((DocumentAttachment) attachment); + } + } + } + return Optional.empty(); + } + + public static boolean hasDocument(Collection attachments) { + Inspector.isNotNull(attachments); + for (Attachment attachment : attachments) { + if (isDocument(attachment)) { + return true; + } + } + return false; + } + + public static boolean isDocument(Attachment attachment) { + Inspector.isNotNull(attachment); + return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType()); + } + +}