Актуализировал с godfatherbot

This commit is contained in:
Struchkov Mark 2022-07-10 00:31:29 +03:00
parent af627cd7b7
commit 1a6c801e9c
14 changed files with 290 additions and 30 deletions

View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId>
<version>0.0.10</version>
<version>0.0.11</version>
<packaging>pom</packaging>
<modules>
@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.8</godfather.core.ver>
<godfather.core.ver>0.0.9</godfather.core.ver>
<telegrambots.ver>6.1.0</telegrambots.ver>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId>
<version>0.0.10</version>
<version>0.0.11</version>
</parent>
<artifactId>telegram-core</artifactId>

View File

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

View File

@ -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<Mail> {
public class MailAutoresponderTelegram extends GeneralAutoResponder<Mail> {
public MessageAutoresponderTelegram(
public MailAutoresponderTelegram(
Sending sending,
PersonSettingService personSettingService,
UnitPointerService unitPointerService,
List<Object> unitConfigurations
StorylineService<Mail> storyLineService
) {
super(sending, personSettingService, unitPointerService, unitConfigurations);
super(sending, personSettingService, storyLineService);
}
}

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram;
package dev.struchkov.godfather.telegram.config;
/**
* TODO: Добавить описание класса.

View File

@ -1,7 +1,5 @@
package dev.struchkov.godfather.telegram.config;
import dev.struchkov.godfather.telegram.ProxyConfig;
/**
* TODO: Добавить описание класса.
*

View File

@ -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<MessageEntity> 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<DocumentAttachment> 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<Attachment> convertAttachments(List<MessageEntity> entities) {
final List<Attachment> 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;
}

View File

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

View File

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

View File

@ -0,0 +1,7 @@
package dev.struchkov.godfather.telegram.domain.attachment;
public enum TelegramAttachmentType {
DOCUMENT
}

View File

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

View File

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

View File

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

View File

@ -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<DocumentAttachment> findFirstDocument(Collection<Attachment> attachments) {
if (attachments != null) {
for (Attachment attachment : attachments) {
if (isDocument(attachment)) {
return Optional.ofNullable((DocumentAttachment) attachment);
}
}
}
return Optional.empty();
}
public static boolean hasDocument(Collection<Attachment> 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());
}
}