This commit is contained in:
parent
c7595ff2e4
commit
79f99ae7c1
@ -24,6 +24,10 @@
|
|||||||
<groupId>dev.struchkov.godfather.telegram</groupId>
|
<groupId>dev.struchkov.godfather.telegram</groupId>
|
||||||
<artifactId>telegram-context-main</artifactId>
|
<artifactId>telegram-context-main</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.struchkov.godfather.telegram</groupId>
|
||||||
|
<artifactId>telegram-domain-quarkus</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package dev.struchkov.godfather.telegram.quarkus.domain.attachment.send;
|
||||||
|
|
||||||
|
import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment;
|
||||||
|
import dev.struchkov.godfather.quarkus.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 DocumentSendAttachment implements SendAttachment {
|
||||||
|
|
||||||
|
public static final String TYPE = "DOCUMENT";
|
||||||
|
|
||||||
|
private SendFile sendFile;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package dev.struchkov.godfather.telegram.quarkus.domain.attachment.send;
|
||||||
|
|
||||||
|
import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment;
|
||||||
|
import dev.struchkov.godfather.quarkus.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 PhotoSendAttachment implements SendAttachment {
|
||||||
|
|
||||||
|
public static final String TYPE = "PHOTO";
|
||||||
|
|
||||||
|
private SendFile sendFile;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package dev.struchkov.godfather.telegram.quarkus.domain.deser;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment;
|
||||||
|
import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.DocumentSendAttachment;
|
||||||
|
import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.PhotoSendAttachment;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class TelegramAttachmentSendDeserializer extends StdDeserializer<SendAttachment> {
|
||||||
|
|
||||||
|
public TelegramAttachmentSendDeserializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TelegramAttachmentSendDeserializer(Class<?> vc) {
|
||||||
|
super(vc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SendAttachment deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
||||||
|
final JsonNode node = parser.getCodec().readTree(parser);
|
||||||
|
final String attachmentType = node.get("type").asText();
|
||||||
|
return switch (attachmentType) {
|
||||||
|
case "PHOTO" -> parser.getCodec().treeToValue(node, PhotoSendAttachment.class);
|
||||||
|
case "DOCUMENT" -> parser.getCodec().treeToValue(node, DocumentSendAttachment.class);
|
||||||
|
default -> throw new IllegalStateException("Unexpected value: " + attachmentType);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,26 +4,34 @@ import dev.struchkov.godfather.main.domain.SendType;
|
|||||||
import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
|
import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
|
||||||
import dev.struchkov.godfather.quarkus.domain.SentBox;
|
import dev.struchkov.godfather.quarkus.domain.SentBox;
|
||||||
import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing;
|
import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing;
|
||||||
|
import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment;
|
||||||
|
import dev.struchkov.godfather.quarkus.domain.content.send.SendFile;
|
||||||
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
|
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
|
||||||
import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
|
import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
|
||||||
import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert;
|
import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert;
|
||||||
import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderRepository;
|
import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderRepository;
|
||||||
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
|
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
|
||||||
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
|
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
|
||||||
|
import dev.struchkov.godfather.telegram.quarkus.domain.attachment.send.PhotoSendAttachment;
|
||||||
import io.smallrye.mutiny.Multi;
|
import io.smallrye.mutiny.Multi;
|
||||||
import io.smallrye.mutiny.Uni;
|
import io.smallrye.mutiny.Uni;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.ParseMode;
|
||||||
import org.telegram.telegrambots.meta.api.methods.invoices.SendInvoice;
|
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.SendMessage;
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
|
||||||
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
|
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
|
||||||
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
|
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
|
||||||
|
import org.telegram.telegrambots.meta.api.objects.InputFile;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||||
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 org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -36,6 +44,7 @@ import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENA
|
|||||||
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN;
|
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN;
|
||||||
import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertInlineKeyBoard;
|
import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertInlineKeyBoard;
|
||||||
import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertKeyBoard;
|
import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertKeyBoard;
|
||||||
|
import static dev.struchkov.haiti.utils.Checker.checkNotBlank;
|
||||||
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||||
import static java.lang.Boolean.TRUE;
|
import static java.lang.Boolean.TRUE;
|
||||||
import static java.util.concurrent.CompletableFuture.completedFuture;
|
import static java.util.concurrent.CompletableFuture.completedFuture;
|
||||||
@ -135,28 +144,39 @@ public class TelegramSender implements TelegramSending {
|
|||||||
final SendInvoice sendInvoice = optInvoice.get();
|
final SendInvoice sendInvoice = optInvoice.get();
|
||||||
return Uni.createFrom().completionStage(executeAsync(sendInvoice))
|
return Uni.createFrom().completionStage(executeAsync(sendInvoice))
|
||||||
.onItem().transform(ignore -> null);
|
.onItem().transform(ignore -> null);
|
||||||
} else {
|
}
|
||||||
if (boxAnswer.isReplace()) {
|
|
||||||
final String replaceMessageId = boxAnswer.getReplaceMessageId();
|
if (boxAnswer.isReplace()) {
|
||||||
if (checkNotNull(replaceMessageId)) {
|
final String replaceMessageId = boxAnswer.getReplaceMessageId();
|
||||||
return replace(recipientTelegramId, replaceMessageId, boxAnswer, saveMessageId);
|
if (checkNotNull(replaceMessageId)) {
|
||||||
} else {
|
return replace(recipientTelegramId, replaceMessageId, boxAnswer, saveMessageId);
|
||||||
if (checkNotNull(senderRepository)) {
|
} else {
|
||||||
return senderRepository.getLastSendMessage(recipientTelegramId)
|
if (checkNotNull(senderRepository)) {
|
||||||
.onItem().transformToUni(
|
return senderRepository.getLastSendMessage(recipientTelegramId)
|
||||||
lastId -> {
|
.onItem().transformToUni(
|
||||||
if (checkNotNull(lastId)) {
|
lastId -> {
|
||||||
return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId);
|
if (checkNotNull(lastId)) {
|
||||||
} else {
|
return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId);
|
||||||
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
|
} else {
|
||||||
}
|
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final SendAttachment sendAttachment = boxAnswer.getAttachment();
|
||||||
|
if (checkNotNull(sendAttachment)) {
|
||||||
|
switch (sendAttachment.getType()) {
|
||||||
|
case "PHOTO":
|
||||||
|
return sendPhoto(boxAnswer);
|
||||||
|
case "DOCUMENT":
|
||||||
|
return sendDocument(boxAnswer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -230,6 +250,108 @@ public class TelegramSender implements TelegramSending {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Uni<SentBox> sendPhoto(BoxAnswer boxAnswer) {
|
||||||
|
final PhotoSendAttachment photoSendAttachment = (PhotoSendAttachment) boxAnswer.getAttachment();
|
||||||
|
final SendFile sendFile = photoSendAttachment.getSendFile();
|
||||||
|
|
||||||
|
final SendPhoto sendPhoto = new SendPhoto();
|
||||||
|
sendPhoto.setCaption(boxAnswer.getMessage());
|
||||||
|
sendPhoto.setChatId(boxAnswer.getRecipientPersonId());
|
||||||
|
sendPhoto.setPhoto(convertInputFile(sendFile));
|
||||||
|
sendPhoto.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
|
||||||
|
|
||||||
|
boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> {
|
||||||
|
if (TRUE.equals(isDisable)) sendPhoto.disableNotification();
|
||||||
|
});
|
||||||
|
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(isEnable -> {
|
||||||
|
if (TRUE.equals(isEnable)) sendPhoto.setParseMode(ParseMode.MARKDOWN);
|
||||||
|
});
|
||||||
|
boxAnswer.getPayLoad(ENABLE_HTML).ifPresent(isEnable -> {
|
||||||
|
if (TRUE.equals(isEnable)) sendPhoto.setParseMode(ParseMode.HTML);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Uni.createFrom().completionStage(executeAsync(sendPhoto))
|
||||||
|
.onItem().ifNotNull().call(
|
||||||
|
message -> {
|
||||||
|
if (checkNotNull(senderRepository)) {
|
||||||
|
return senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), message.getMessageId().toString());
|
||||||
|
}
|
||||||
|
return Uni.createFrom().voidItem();
|
||||||
|
}
|
||||||
|
).onItem().ifNotNull().transform(
|
||||||
|
message -> SentBox.builder()
|
||||||
|
.personId(boxAnswer.getRecipientPersonId())
|
||||||
|
.messageId(message.getMessageId().toString())
|
||||||
|
.sentAnswer(boxAnswer)
|
||||||
|
.originalAnswer(boxAnswer)
|
||||||
|
.sentMail(MessageMailConvert.apply(message))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Uni<SentBox> sendDocument(BoxAnswer boxAnswer) {
|
||||||
|
final SendDocument sendDocument = new SendDocument();
|
||||||
|
sendDocument.setCaption(boxAnswer.getMessage());
|
||||||
|
sendDocument.setChatId(boxAnswer.getRecipientPersonId());
|
||||||
|
sendDocument.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
|
||||||
|
sendDocument.setDocument(convertInputFile(boxAnswer.getAttachment().getSendFile()));
|
||||||
|
boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> {
|
||||||
|
if (TRUE.equals(isDisable)) sendDocument.disableNotification();
|
||||||
|
});
|
||||||
|
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(isEnable -> {
|
||||||
|
if (TRUE.equals(isEnable)) sendDocument.setParseMode(ParseMode.MARKDOWN);
|
||||||
|
});
|
||||||
|
boxAnswer.getPayLoad(ENABLE_HTML).ifPresent(isEnable -> {
|
||||||
|
if (TRUE.equals(isEnable)) sendDocument.setParseMode(ParseMode.HTML);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Uni.createFrom().completionStage(executeAsync(sendDocument))
|
||||||
|
.onItem().ifNotNull().call(
|
||||||
|
message -> {
|
||||||
|
if (checkNotNull(senderRepository)) {
|
||||||
|
return senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), message.getMessageId().toString());
|
||||||
|
}
|
||||||
|
return Uni.createFrom().voidItem();
|
||||||
|
}
|
||||||
|
).onItem().ifNotNull().transform(
|
||||||
|
message -> SentBox.builder()
|
||||||
|
.personId(boxAnswer.getRecipientPersonId())
|
||||||
|
.messageId(message.getMessageId().toString())
|
||||||
|
.sentAnswer(boxAnswer)
|
||||||
|
.originalAnswer(boxAnswer)
|
||||||
|
.sentMail(MessageMailConvert.apply(message))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputFile convertInputFile(SendFile sendFile) {
|
||||||
|
final File fileData = sendFile.getData();
|
||||||
|
final String fileName = sendFile.getFileName();
|
||||||
|
|
||||||
|
if (checkNotBlank(sendFile.getFileId())) {
|
||||||
|
return new InputFile(sendFile.getFileId());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkNotBlank(sendFile.getUrl())) {
|
||||||
|
return new InputFile(sendFile.getUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkNotNull(fileData)) {
|
||||||
|
if (checkNotBlank(fileName)) {
|
||||||
|
return new InputFile(fileData, fileName);
|
||||||
|
} else {
|
||||||
|
return new InputFile(fileData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkNotNull(sendFile.getFileStream())) {
|
||||||
|
return new InputFile(sendFile.getFileStream(), fileName);
|
||||||
|
} else {
|
||||||
|
return new InputFile(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Multi<SendMessage> splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) {
|
public Multi<SendMessage> splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) {
|
||||||
final List<SendMessage> split = new ArrayList<>();
|
final List<SendMessage> split = new ArrayList<>();
|
||||||
String message = boxAnswer.getMessage();
|
String message = boxAnswer.getMessage();
|
||||||
@ -237,15 +359,15 @@ public class TelegramSender implements TelegramSending {
|
|||||||
while (message.length() > maxMessageLength) {
|
while (message.length() > maxMessageLength) {
|
||||||
String subMessage = message.substring(0, maxMessageLength);
|
String subMessage = message.substring(0, maxMessageLength);
|
||||||
message = message.substring(maxMessageLength);
|
message = message.substring(maxMessageLength);
|
||||||
split.add(createNewBoxAnswer(boxAnswer, subMessage));
|
split.add(createNewTextAnswer(boxAnswer, subMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
split.add(createNewBoxAnswer(boxAnswer, message));
|
split.add(createNewTextAnswer(boxAnswer, message));
|
||||||
|
|
||||||
return Multi.createFrom().iterable(split);
|
return Multi.createFrom().iterable(split);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SendMessage createNewBoxAnswer(BoxAnswer boxAnswer, String subMessage) {
|
private SendMessage createNewTextAnswer(BoxAnswer boxAnswer, String subMessage) {
|
||||||
final SendMessage sendMessage = new SendMessage();
|
final SendMessage sendMessage = new SendMessage();
|
||||||
sendMessage.setChatId(boxAnswer.getRecipientPersonId());
|
sendMessage.setChatId(boxAnswer.getRecipientPersonId());
|
||||||
sendMessage.setText(subMessage);
|
sendMessage.setText(subMessage);
|
||||||
@ -289,6 +411,14 @@ public class TelegramSender implements TelegramSending {
|
|||||||
return completedFuture(null);
|
return completedFuture(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CompletableFuture<Message> executeAsync(SendPhoto sendPhoto) {
|
||||||
|
return absSender.executeAsync(sendPhoto);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CompletableFuture<Message> executeAsync(SendDocument sendDocument) {
|
||||||
|
return absSender.executeAsync(sendDocument);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SendType getType() {
|
public SendType getType() {
|
||||||
return SendType.PRIVATE;
|
return SendType.PRIVATE;
|
||||||
|
Loading…
Reference in New Issue
Block a user