Добавил файлы для отправки
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-03-30 19:42:22 +03:00
parent 2713cd8246
commit f916f7aa09
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
14 changed files with 274 additions and 50 deletions

View File

@ -38,7 +38,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.55</godfather.core.ver>
<godfather.core.ver>0.0.56-SNAPSHOT</godfather.core.ver>
<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
<telegrambots.ver>6.3.0</telegrambots.ver>

View File

@ -5,10 +5,10 @@ import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.quarkus.context.service.EventDispatching;
import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import dev.struchkov.godfather.telegram.main.consumer.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.main.context.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.context.convert.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.context.convert.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import io.smallrye.mutiny.Uni;

View File

@ -6,11 +6,11 @@ import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.simple.context.service.EventDispatching;
import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import dev.struchkov.godfather.telegram.main.consumer.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.main.consumer.MessageChatMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.main.context.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.main.context.convert.MessageChatMailConvert;
import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.context.convert.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.context.convert.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import org.jetbrains.annotations.NotNull;

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.consumer;
package dev.struchkov.godfather.telegram.main.context.convert;
import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment;

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.consumer;
package dev.struchkov.godfather.telegram.main.context.convert;
import dev.struchkov.godfather.main.domain.content.Attachment;
import dev.struchkov.godfather.main.domain.content.ChatMail;

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.consumer;
package dev.struchkov.godfather.telegram.main.context.convert;
import dev.struchkov.godfather.main.domain.content.Attachment;
import dev.struchkov.godfather.main.domain.content.Mail;

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.consumer;
package dev.struchkov.godfather.telegram.main.context.convert;
import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.consumer;
package dev.struchkov.godfather.telegram.main.context.convert;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;

View File

@ -24,6 +24,10 @@
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-context-main</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-domain-simple</artifactId>
</dependency>
<dependency>
<groupId>org.telegram</groupId>

View File

@ -1,13 +1,14 @@
package dev.struchkov.godfather.telegram.quarkus.core.util;
import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.quarkus.domain.unit.func.UniPredicate;
import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.CommandAttachment;
import dev.struchkov.godfather.telegram.main.core.util.Attachments;
import java.util.Optional;
import java.util.function.Predicate;
import static dev.struchkov.godfather.quarkus.domain.unit.func.UniPredicate.predicate;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
public class UnitTrigger {
@ -16,40 +17,44 @@ public class UnitTrigger {
utilityClass();
}
public static Predicate<Mail> clickButtonRaw(String rawCallBackData) {
return mail -> {
final Optional<ButtonClickAttachment> optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments());
if (optButtonClick.isPresent()) {
final ButtonClickAttachment buttonClick = optButtonClick.get();
final String rawData = buttonClick.getRawCallBackData();
return rawData.equals(rawCallBackData);
}
return false;
};
public static UniPredicate<Mail> clickButtonRaw(String rawCallBackData) {
return predicate(
mail -> {
final Optional<ButtonClickAttachment> optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments());
if (optButtonClick.isPresent()) {
final ButtonClickAttachment buttonClick = optButtonClick.get();
final String rawData = buttonClick.getRawCallBackData();
return rawData.equals(rawCallBackData);
}
return false;
}
);
}
public static Predicate<Mail> isCommandByType(String commandType) {
return mail -> {
final Optional<CommandAttachment> optCommand = Attachments.findFirstCommand(mail.getAttachments());
if (optCommand.isPresent()) {
final CommandAttachment command = optCommand.get();
final String type = command.getCommandType();
return type.equals(commandType);
}
return false;
};
public static UniPredicate<Mail> isCommandByType(String commandType) {
return predicate(
mail -> {
final Optional<CommandAttachment> optCommand = Attachments.findFirstCommand(mail.getAttachments());
if (optCommand.isPresent()) {
final CommandAttachment command = optCommand.get();
final String type = command.getCommandType();
return type.equals(commandType);
}
return false;
}
);
}
public static Predicate<Mail> isCommand() {
return mail -> Attachments.findFirstCommand(mail.getAttachments()).isPresent();
public static UniPredicate<Mail> isCommand() {
return predicate(mail -> Attachments.findFirstCommand(mail.getAttachments()).isPresent());
}
public static Predicate<Mail> isClickButton() {
return mail -> Attachments.findFirstButtonClick(mail.getAttachments()).isPresent();
public static UniPredicate<Mail> isClickButton() {
return predicate(mail -> Attachments.findFirstButtonClick(mail.getAttachments()).isPresent());
}
public static Predicate<Mail> isLinks() {
return mail -> Attachments.findFirstLink(mail.getAttachments()).isPresent();
public static UniPredicate<Mail> isLinks() {
return predicate(mail -> Attachments.findFirstLink(mail.getAttachments()).isPresent());
}
}

View File

@ -0,0 +1,28 @@
package dev.struchkov.godfather.telegram.simple.domain.attachment.send;
import dev.struchkov.godfather.simple.domain.content.send.SendAttachment;
import dev.struchkov.godfather.simple.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;
}
}

View File

@ -0,0 +1,28 @@
package dev.struchkov.godfather.telegram.simple.domain.attachment.send;
import dev.struchkov.godfather.simple.domain.content.send.SendAttachment;
import dev.struchkov.godfather.simple.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;
}
}

View File

@ -6,6 +6,7 @@ import dev.struchkov.godfather.quarkus.domain.SentBox;
import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
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.service.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
@ -179,7 +180,13 @@ public class TelegramSender implements TelegramSending {
});
return Uni.createFrom().completionStage(executeAsync(editMessageText))
.onItem().ifNotNull().transformToUni(t -> Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer)))
.onItem().ifNotNull().transform(t -> {
final SentBox sentBox = new SentBox();
sentBox.setSentAnswer(boxAnswer);
sentBox.setOriginalAnswer(boxAnswer);
sentBox.setMessageId(telegramId);
return sentBox;
})
.onFailure(TelegramApiRequestException.class).recoverWithUni(
ex -> {
final TelegramApiRequestException exception = (TelegramApiRequestException) ex;
@ -209,8 +216,17 @@ public class TelegramSender implements TelegramSending {
}
return Uni.createFrom().nullItem();
})
.onItem().ifNotNull().transformToUni(
answerMessages -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessages.get(answerMessages.size() - 1).getMessageId().toString(), boxAnswer, boxAnswer))
.onItem().ifNotNull().transform(
answerMessages -> {
final Message lastMessage = answerMessages.get(answerMessages.size() - 1);
final SentBox sentBox = new SentBox();
sentBox.setMessageId(lastMessage.getMessageId().toString());
sentBox.setOriginalAnswer(boxAnswer);
sentBox.setSentAnswer(boxAnswer);
sentBox.setSentMail(MessageMailConvert.apply(lastMessage));
return sentBox;
}
);
}

View File

@ -4,23 +4,31 @@ import dev.struchkov.godfather.main.domain.SendType;
import dev.struchkov.godfather.simple.domain.BoxAnswer;
import dev.struchkov.godfather.simple.domain.SentBox;
import dev.struchkov.godfather.simple.domain.action.PreSendProcessing;
import dev.struchkov.godfather.simple.domain.content.send.SendAttachment;
import dev.struchkov.godfather.simple.domain.content.send.SendFile;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
import dev.struchkov.godfather.telegram.main.context.convert.MessageMailConvert;
import dev.struchkov.godfather.telegram.simple.context.repository.SenderRepository;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending;
import dev.struchkov.godfather.telegram.simple.domain.attachment.send.PhotoSendAttachment;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
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.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -30,6 +38,7 @@ import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DIS
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.convertKeyBoard;
import static dev.struchkov.haiti.utils.Checker.checkNotBlank;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
import static java.lang.Boolean.TRUE;
@ -148,6 +157,16 @@ public class TelegramSender implements TelegramSending {
}
}
}
final SendAttachment sendAttachment = boxAnswer.getAttachment();
if (checkNotNull(sendAttachment)) {
switch (sendAttachment.getType()) {
case "PHOTO":
return sendPhoto(boxAnswer, preparedAnswer);
case "DOCUMENT":
return sendDocument(boxAnswer, preparedAnswer);
}
}
return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
}
@ -165,7 +184,14 @@ public class TelegramSender implements TelegramSending {
try {
absSender.execute(editMessageText);
return SentBox.optional(telegramId, replaceMessageId, preparedAnswer, boxAnswer);
return Optional.of(
SentBox.builder()
.personId(telegramId)
.messageId(replaceMessageId)
.originalAnswer(boxAnswer)
.sentAnswer(boxAnswer)
.build()
);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
@ -179,6 +205,7 @@ public class TelegramSender implements TelegramSending {
private Optional<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
final List<SendMessage> sendMessages = splitBoxAnswerByMessageLength(boxAnswer, 4000);
Message execute = null;
for (SendMessage sendMessage : sendMessages) {
try {
@ -193,11 +220,127 @@ public class TelegramSender implements TelegramSending {
if (checkNotNull(senderRepository) && saveMessageId) {
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId().toString());
}
return SentBox.optional(telegramId, execute.getMessageId().toString(), preparedAnswer, boxAnswer);
return Optional.of(
SentBox.builder()
.personId(telegramId)
.messageId(execute.getMessageId().toString())
.sentAnswer(boxAnswer)
.originalAnswer(boxAnswer)
.sentMail(MessageMailConvert.apply(execute))
.build()
);
}
return Optional.empty();
}
private Optional<SentBox> sendPhoto(BoxAnswer boxAnswer, BoxAnswer preparedAnswer) {
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("Markdown");
});
Message execute = null;
try {
execute = absSender.execute(sendPhoto);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
if (checkNotNull(execute)) {
if (checkNotNull(senderRepository)) {
senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), execute.getMessageId().toString());
}
return Optional.of(
SentBox.builder()
.personId(boxAnswer.getRecipientPersonId())
.messageId(execute.getMessageId().toString())
.sentAnswer(boxAnswer)
.originalAnswer(boxAnswer)
.sentMail(MessageMailConvert.apply(execute))
.build()
);
}
return Optional.empty();
}
private Optional<SentBox> sendDocument(BoxAnswer boxAnswer, BoxAnswer preparedAnswer) {
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("Markdown");
});
Message execute = null;
try {
execute = absSender.execute(sendDocument);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
if (checkNotNull(execute)) {
if (checkNotNull(senderRepository)) {
senderRepository.saveLastSendMessage(boxAnswer.getRecipientPersonId(), execute.getMessageId().toString());
}
return Optional.of(
SentBox.builder()
.personId(boxAnswer.getRecipientPersonId())
.messageId(execute.getMessageId().toString())
.sentAnswer(boxAnswer)
.originalAnswer(boxAnswer)
.sentMail(MessageMailConvert.apply(execute))
.build()
);
}
return Optional.empty();
}
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 List<SendMessage> splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) {
final List<SendMessage> split = new ArrayList<>();
String message = boxAnswer.getMessage();
@ -205,15 +348,15 @@ public class TelegramSender implements TelegramSending {
while (message.length() > maxMessageLength) {
String subMessage = message.substring(0, maxMessageLength);
message = message.substring(maxMessageLength);
split.add(createNewBoxAnswer(boxAnswer, subMessage));
split.add(createNewMessage(boxAnswer, subMessage));
}
split.add(createNewBoxAnswer(boxAnswer, message));
split.add(createNewMessage(boxAnswer, message));
return split;
}
private SendMessage createNewBoxAnswer(BoxAnswer boxAnswer, String subMessage) {
private SendMessage createNewMessage(BoxAnswer boxAnswer, String subMessage) {
final SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(boxAnswer.getRecipientPersonId());
sendMessage.setText(subMessage);