Добавил TelegramService и сделал AttachmentServiceImpl асинхронным для кваркуса
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-02-17 21:18:53 +03:00
parent a05eb8a403
commit 72b08447c5
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
9 changed files with 238 additions and 50 deletions

View File

@ -0,0 +1,18 @@
package dev.struchkov.godfather.telegram.quarkus.context.service;
import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
public interface AttachmentService {
Uni<FileContainer> uploadFile(@NotNull DocumentAttachment documentAttachment);
Uni<ByteContainer> uploadBytes(@NotNull DocumentAttachment documentAttachment);
Uni<ByteContainer> uploadBytes(@NotNull Picture picture);
}

View File

@ -0,0 +1,11 @@
package dev.struchkov.godfather.telegram.quarkus.context.service;
import dev.struchkov.godfather.telegram.domain.ChatAction;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
public interface TelegramService {
Uni<Void> executeAction(@NotNull String personId, ChatAction chatAction);
}

View File

@ -0,0 +1,17 @@
package dev.struchkov.godfather.telegram.simple.context.service;
import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import org.jetbrains.annotations.NotNull;
public interface AttachmentService {
FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment);
ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment);
ByteContainer uploadBytes(@NotNull Picture picture);
}

View File

@ -0,0 +1,10 @@
package dev.struchkov.godfather.telegram.simple.context.service;
import dev.struchkov.godfather.telegram.domain.ChatAction;
import org.jetbrains.annotations.NotNull;
public interface TelegramService {
void executeAction(@NotNull String personId, ChatAction chatAction);
}

View File

@ -5,6 +5,8 @@ import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.AttachmentService;
import io.smallrye.mutiny.Uni;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,6 +15,7 @@ import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.GetFile; import org.telegram.telegrambots.meta.api.methods.GetFile;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -21,11 +24,13 @@ import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream; import java.util.stream.Stream;
import static dev.struchkov.haiti.utils.Inspector.isNotNull; import static dev.struchkov.haiti.utils.Inspector.isNotNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
public class AttachmentServiceImpl { public class AttachmentServiceImpl implements AttachmentService {
private static final Logger log = LoggerFactory.getLogger(AttachmentServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(AttachmentServiceImpl.class);
@ -56,73 +61,92 @@ public class AttachmentServiceImpl {
} }
} }
public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) { @Override
public Uni<FileContainer> uploadFile(@NotNull DocumentAttachment documentAttachment) {
isNotNull(documentAttachment); isNotNull(documentAttachment);
try { return downloadFile(documentAttachment)
final File file = downloadFile(documentAttachment); .onItem().ifNotNull().transform(file -> new FileContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), file));
return new FileContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), file);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return FileContainer.empty();
} }
public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) { @Override
public Uni<ByteContainer> uploadBytes(@NotNull DocumentAttachment documentAttachment) {
isNotNull(documentAttachment); isNotNull(documentAttachment);
try { return downloadBytes(documentAttachment)
final byte[] bytes = downloadBytes(documentAttachment); .onItem().ifNotNull().transform(bytes -> new ByteContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), bytes));
return new ByteContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), bytes);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return ByteContainer.empty();
} }
public ByteContainer uploadBytes(@NotNull Picture picture) { @Override
public Uni<ByteContainer> uploadBytes(@NotNull Picture picture) {
isNotNull(picture); isNotNull(picture);
try { return downloadBytes(picture)
final byte[] bytes = downloadBytes(picture); .onItem().ifNotNull().transform(bytes -> new ByteContainer(null, "image/jpeg", bytes));
return new ByteContainer(null, "image/jpeg", bytes);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return ByteContainer.empty();
} }
private byte[] downloadBytes(Picture picture) throws TelegramApiException, IOException { private Uni<byte[]> downloadBytes(Picture picture) {
return telegramDownloadBytes(picture.getFileId()); return telegramDownloadBytes(picture.getFileId());
} }
private byte[] downloadBytes(DocumentAttachment documentAttachment) throws TelegramApiException, IOException { private Uni<byte[]> downloadBytes(DocumentAttachment documentAttachment) {
return telegramDownloadBytes(documentAttachment.getFileId()); return telegramDownloadBytes(documentAttachment.getFileId());
} }
private byte[] telegramDownloadBytes(String fileId) throws TelegramApiException, IOException { private Uni<byte[]> telegramDownloadBytes(String fileId) {
final String fileUrl = getFileUrl(fileId); return getFileUrl(fileId)
return IOUtils.toByteArray(new URL(fileUrl)); .onItem().ifNotNull().transformToUni(
fileUrl -> Uni.createFrom().completionStage(
CompletableFuture.supplyAsync(
() -> {
final URL url;
try {
url = new URL(fileUrl);
return IOUtils.toByteArray(url);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return null;
}
)
)
);
} }
private File downloadFile(DocumentAttachment documentAttachment) throws IOException, TelegramApiException { private Uni<File> downloadFile(DocumentAttachment documentAttachment) {
final String fileUrl = getFileUrl(documentAttachment.getFileId()); return getFileUrl(documentAttachment.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());
final File localFile = new File(filePath.toString());
final InputStream is;
try {
is = new URL(fileUrl).openStream();
FileUtils.copyInputStreamToFile(is, localFile);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return localFile;
}
)
));
}
final StringBuilder filePath = new StringBuilder(); private Uni<String> getFileUrl(String fileId) {
if (folderPathForFiles != null) { return Uni.createFrom().completionStage(getFileCompletableFuture(fileId))
filePath.append(folderPathForFiles); .onItem().ifNotNull().transform(file -> file.getFileUrl(botToken));
}
private CompletableFuture<org.telegram.telegrambots.meta.api.objects.File> getFileCompletableFuture(String fileId) {
try {
return absSender.executeAsync(GetFile.builder().fileId(fileId).build());
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
} }
filePath.append(UUID.randomUUID()); return completedFuture(null);
filePath.append("_");
filePath.append(documentAttachment.getFileName());
final File localFile = new File(filePath.toString());
final InputStream is = new URL(fileUrl).openStream();
FileUtils.copyInputStreamToFile(is, localFile);
return localFile;
}
private String getFileUrl(String fileId) throws TelegramApiException {
final GetFile getFile = new GetFile();
getFile.setFileId(fileId);
return absSender.execute(getFile).getFileUrl(botToken);
} }
} }

View File

@ -0,0 +1,49 @@
package dev.struchkov.godfather.telegram.quarkus.core.service;
import dev.struchkov.godfather.telegram.domain.ChatAction;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramService;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.ActionType;
import org.telegram.telegrambots.meta.api.methods.send.SendChatAction;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.util.concurrent.CompletableFuture;
public class TelegramServiceImpl implements TelegramService {
private static final Logger log = LoggerFactory.getLogger(TelegramServiceImpl.class);
private final AbsSender absSender;
public TelegramServiceImpl(TelegramConnect telegramConnect) {
this.absSender = telegramConnect.getAbsSender();
}
@Override
public Uni<Void> executeAction(@NotNull String personId, ChatAction chatAction) {
final SendChatAction sendChatAction = new SendChatAction();
sendChatAction.setChatId(personId);
sendChatAction.setAction(ActionType.valueOf(chatAction.name()));
return Uni.createFrom().completionStage(getExecuteAsync(sendChatAction))
.replaceWithVoid();
}
private CompletableFuture<Boolean> getExecuteAsync(SendChatAction sendChatAction) {
try {
return absSender.executeAsync(sendChatAction);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return CompletableFuture.completedFuture(null);
}
}

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.AttachmentService;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -25,7 +26,7 @@ import java.util.stream.Stream;
import static dev.struchkov.haiti.utils.Inspector.isNotNull; import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class AttachmentServiceImpl { public class AttachmentServiceImpl implements AttachmentService {
private static final Logger log = LoggerFactory.getLogger(AttachmentServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(AttachmentServiceImpl.class);
@ -56,6 +57,7 @@ public class AttachmentServiceImpl {
} }
} }
@Override
public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) { public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) {
isNotNull(documentAttachment); isNotNull(documentAttachment);
try { try {
@ -67,6 +69,7 @@ public class AttachmentServiceImpl {
return FileContainer.empty(); return FileContainer.empty();
} }
@Override
public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) { public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) {
isNotNull(documentAttachment); isNotNull(documentAttachment);
try { try {
@ -78,6 +81,7 @@ public class AttachmentServiceImpl {
return ByteContainer.empty(); return ByteContainer.empty();
} }
@Override
public ByteContainer uploadBytes(@NotNull Picture picture) { public ByteContainer uploadBytes(@NotNull Picture picture) {
isNotNull(picture); isNotNull(picture);
try { try {

View File

@ -0,0 +1,39 @@
package dev.struchkov.godfather.telegram.simple.core.service;
import dev.struchkov.godfather.telegram.domain.ChatAction;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramService;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.ActionType;
import org.telegram.telegrambots.meta.api.methods.send.SendChatAction;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
public class TelegramServiceImpl implements TelegramService {
private static final Logger log = LoggerFactory.getLogger(TelegramServiceImpl.class);
private final AbsSender absSender;
public TelegramServiceImpl(TelegramConnect telegramConnect) {
this.absSender = telegramConnect.getAbsSender();
}
@Override
public void executeAction(@NotNull String personId, ChatAction chatAction) {
final SendChatAction sendChatAction = new SendChatAction();
sendChatAction.setChatId(personId);
sendChatAction.setAction(ActionType.valueOf(chatAction.name()));
try {
absSender.execute(sendChatAction);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
}
}

View File

@ -0,0 +1,16 @@
package dev.struchkov.godfather.telegram.domain;
public enum ChatAction {
TYPING,
RECORDVIDEO,
RECORDVIDEONOTE,
RECORDVOICE,
UPLOADPHOTO,
UPLOADVIDEO,
UPLOADVIDEONOTE,
UPLOADVOICE,
UPLOADDOCUMENT,
FINDLOCATION
}