Добавил TelegramService и сделал AttachmentServiceImpl асинхронным для кваркуса
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a05eb8a403
commit
72b08447c5
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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.FileContainer;
|
||||
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.IOUtils;
|
||||
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.bots.AbsSender;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -21,11 +24,13 @@ import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
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);
|
||||
|
||||
@ -56,73 +61,92 @@ public class AttachmentServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) {
|
||||
@Override
|
||||
public Uni<FileContainer> uploadFile(@NotNull DocumentAttachment documentAttachment) {
|
||||
isNotNull(documentAttachment);
|
||||
try {
|
||||
final File file = downloadFile(documentAttachment);
|
||||
return new FileContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), file);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return FileContainer.empty();
|
||||
return downloadFile(documentAttachment)
|
||||
.onItem().ifNotNull().transform(file -> new FileContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), file));
|
||||
}
|
||||
|
||||
public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) {
|
||||
@Override
|
||||
public Uni<ByteContainer> uploadBytes(@NotNull DocumentAttachment documentAttachment) {
|
||||
isNotNull(documentAttachment);
|
||||
try {
|
||||
final byte[] bytes = downloadBytes(documentAttachment);
|
||||
return new ByteContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), bytes);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return ByteContainer.empty();
|
||||
return downloadBytes(documentAttachment)
|
||||
.onItem().ifNotNull().transform(bytes -> new ByteContainer(documentAttachment.getFileName(), documentAttachment.getMimeType(), bytes));
|
||||
}
|
||||
|
||||
public ByteContainer uploadBytes(@NotNull Picture picture) {
|
||||
@Override
|
||||
public Uni<ByteContainer> uploadBytes(@NotNull Picture picture) {
|
||||
isNotNull(picture);
|
||||
try {
|
||||
final byte[] bytes = downloadBytes(picture);
|
||||
return new ByteContainer(null, "image/jpeg", bytes);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return ByteContainer.empty();
|
||||
return downloadBytes(picture)
|
||||
.onItem().ifNotNull().transform(bytes -> new ByteContainer(null, "image/jpeg", bytes));
|
||||
}
|
||||
|
||||
private byte[] downloadBytes(Picture picture) throws TelegramApiException, IOException {
|
||||
private Uni<byte[]> downloadBytes(Picture picture) {
|
||||
return telegramDownloadBytes(picture.getFileId());
|
||||
}
|
||||
|
||||
private byte[] downloadBytes(DocumentAttachment documentAttachment) throws TelegramApiException, IOException {
|
||||
private Uni<byte[]> downloadBytes(DocumentAttachment documentAttachment) {
|
||||
return telegramDownloadBytes(documentAttachment.getFileId());
|
||||
}
|
||||
|
||||
private byte[] telegramDownloadBytes(String fileId) throws TelegramApiException, IOException {
|
||||
final String fileUrl = getFileUrl(fileId);
|
||||
return IOUtils.toByteArray(new URL(fileUrl));
|
||||
private Uni<byte[]> telegramDownloadBytes(String fileId) {
|
||||
return getFileUrl(fileId)
|
||||
.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 {
|
||||
final String fileUrl = getFileUrl(documentAttachment.getFileId());
|
||||
private Uni<File> downloadFile(DocumentAttachment documentAttachment) {
|
||||
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();
|
||||
if (folderPathForFiles != null) {
|
||||
filePath.append(folderPathForFiles);
|
||||
private Uni<String> getFileUrl(String fileId) {
|
||||
return Uni.createFrom().completionStage(getFileCompletableFuture(fileId))
|
||||
.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());
|
||||
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);
|
||||
return completedFuture(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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.FileContainer;
|
||||
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.IOUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -25,7 +26,7 @@ import java.util.stream.Stream;
|
||||
|
||||
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);
|
||||
|
||||
@ -56,6 +57,7 @@ public class AttachmentServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileContainer uploadFile(@NotNull DocumentAttachment documentAttachment) {
|
||||
isNotNull(documentAttachment);
|
||||
try {
|
||||
@ -67,6 +69,7 @@ public class AttachmentServiceImpl {
|
||||
return FileContainer.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteContainer uploadBytes(@NotNull DocumentAttachment documentAttachment) {
|
||||
isNotNull(documentAttachment);
|
||||
try {
|
||||
@ -78,6 +81,7 @@ public class AttachmentServiceImpl {
|
||||
return ByteContainer.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteContainer uploadBytes(@NotNull Picture picture) {
|
||||
isNotNull(picture);
|
||||
try {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package dev.struchkov.godfather.telegram.domain;
|
||||
|
||||
public enum ChatAction {
|
||||
|
||||
TYPING,
|
||||
RECORDVIDEO,
|
||||
RECORDVIDEONOTE,
|
||||
RECORDVOICE,
|
||||
UPLOADPHOTO,
|
||||
UPLOADVIDEO,
|
||||
UPLOADVIDEONOTE,
|
||||
UPLOADVOICE,
|
||||
UPLOADDOCUMENT,
|
||||
FINDLOCATION
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user