Разделение большого сообщения на несколько сообщений
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-03-26 22:08:32 +03:00
parent 40fd76590f
commit efe3e042e2
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
3 changed files with 98 additions and 44 deletions

View File

@ -8,6 +8,7 @@ public final class BoxAnswerPayload {
public static final ContextKey<Boolean> DISABLE_WEB_PAGE_PREVIEW = ContextKey.of("DISABLE_WEB_PAGE_PREVIEW", Boolean.class);
public static final ContextKey<Boolean> DISABLE_NOTIFICATION = ContextKey.of("DISABLE_NOTIFICATION", Boolean.class);
public static final ContextKey<Boolean> ENABLE_MARKDOWN = ContextKey.of("DISABLE_MARKDOWN", Boolean.class);
public static final ContextKey<SendInvoice> INVOICE = ContextKey.of("INVOICE", SendInvoice.class);
private BoxAnswerPayload() {

View File

@ -6,10 +6,10 @@ 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.sender.util.KeyBoardConvert;
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;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@ -24,13 +24,16 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_NOTIFICATION;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_WEB_PAGE_PREVIEW;
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.checkNotNull;
import static java.lang.Boolean.TRUE;
import static java.util.concurrent.CompletableFuture.completedFuture;
@ -100,10 +103,16 @@ public class TelegramSender implements TelegramSending {
v -> {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setInlineMessageId(inlineMessageId);
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(editMessageText::enableMarkdown);
boxAnswer.getPayLoad(DISABLE_WEB_PAGE_PREVIEW).ifPresent(
isDisable -> {
if (TRUE.equals(isDisable)) editMessageText.disableWebPagePreview();
}
);
return Uni.createFrom().completionStage(executeAsync(editMessageText))
.onFailure(TelegramApiRequestException.class).call(
ex -> {
@ -184,40 +193,54 @@ public class TelegramSender implements TelegramSending {
private Uni<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem()
.onItem().transformToMulti(v -> splitBoxAnswerByMessageLength(boxAnswer, 4000))
.onItem().transformToUni(
v -> {
final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(telegramId);
sendMessage.setText(boxAnswer.getMessage());
sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard()));
boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(
isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableNotification();
}
);
boxAnswer.getPayLoad(DISABLE_WEB_PAGE_PREVIEW).ifPresent(
isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview();
}
);
return Uni.createFrom().completionStage(executeAsync(sendMessage))
.onFailure(TelegramApiRequestException.class).invoke(t -> log.error(((TelegramApiRequestException) t).getApiResponse()))
.onFailure().invoke(t -> log.error(t.getMessage(), t));
}
).onItem().ifNotNull().call(answerMessage -> {
sendMessage -> Uni.createFrom().completionStage(executeAsync(sendMessage))
.onFailure(TelegramApiRequestException.class).invoke(t -> log.error(((TelegramApiRequestException) t).getApiResponse()))
.onFailure().invoke(t -> log.error(t.getMessage(), t))
).concatenate().collect().asList()
.call(answerMessages -> {
if (checkNotNull(senderRepository) && saveMessageId) {
return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId().toString());
return senderRepository.saveLastSendMessage(telegramId, answerMessages.get(answerMessages.size() - 1).getMessageId().toString());
}
return Uni.createFrom().nullItem();
})
.onItem().ifNotNull().transformToUni(
answerMessage -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessage.getMessageId().toString(), boxAnswer, boxAnswer))
answerMessages -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessages.get(answerMessages.size() - 1).getMessageId().toString(), boxAnswer, boxAnswer))
);
}
public Multi<SendMessage> splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) {
final List<SendMessage> split = new ArrayList<>();
String message = boxAnswer.getMessage();
while (message.length() > maxMessageLength) {
String subMessage = message.substring(0, maxMessageLength);
message = message.substring(maxMessageLength);
split.add(createNewBoxAnswer(boxAnswer, subMessage));
}
split.add(createNewBoxAnswer(boxAnswer, message));
return Multi.createFrom().iterable(split);
}
private SendMessage createNewBoxAnswer(BoxAnswer boxAnswer, String subMessage) {
final SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(boxAnswer.getRecipientPersonId());
sendMessage.setText(subMessage);
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(sendMessage::enableMarkdown);
boxAnswer.getPayLoad(DISABLE_WEB_PAGE_PREVIEW).ifPresent(isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview();
});
boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableNotification();
});
return sendMessage;
}
private CompletableFuture<Message> executeAsync(SendMessage sendMessage) {
try {
return absSender.executeAsync(sendMessage);

View File

@ -27,6 +27,7 @@ import java.util.Optional;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_NOTIFICATION;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_WEB_PAGE_PREVIEW;
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.checkNotNull;
@ -149,9 +150,14 @@ public class TelegramSender implements TelegramSending {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(telegramId);
editMessageText.setMessageId(parseInt(replaceMessageId));
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(editMessageText::enableMarkdown);
boxAnswer.getPayLoad(DISABLE_WEB_PAGE_PREVIEW).ifPresent(isDisable -> {
if (TRUE.equals(isDisable)) editMessageText.disableWebPagePreview();
});
try {
absSender.execute(editMessageText);
return SentBox.optional(telegramId, replaceMessageId, preparedAnswer, boxAnswer);
@ -167,31 +173,55 @@ 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 {
execute = absSender.execute(sendMessage);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
}
if (checkNotNull(execute)) {
if (checkNotNull(senderRepository) && saveMessageId) {
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId().toString());
}
return SentBox.optional(telegramId, execute.getMessageId().toString(), preparedAnswer, boxAnswer);
}
return Optional.empty();
}
public List<SendMessage> splitBoxAnswerByMessageLength(BoxAnswer boxAnswer, int maxMessageLength) {
final List<SendMessage> split = new ArrayList<>();
String message = boxAnswer.getMessage();
while (message.length() > maxMessageLength) {
String subMessage = message.substring(0, maxMessageLength);
message = message.substring(maxMessageLength);
split.add(createNewBoxAnswer(boxAnswer, subMessage));
}
split.add(createNewBoxAnswer(boxAnswer, message));
return split;
}
private SendMessage createNewBoxAnswer(BoxAnswer boxAnswer, String subMessage) {
final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(telegramId);
sendMessage.setText(boxAnswer.getMessage());
sendMessage.setChatId(boxAnswer.getRecipientPersonId());
sendMessage.setText(subMessage);
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
boxAnswer.getPayLoad(ENABLE_MARKDOWN).ifPresent(sendMessage::enableMarkdown);
boxAnswer.getPayLoad(DISABLE_WEB_PAGE_PREVIEW).ifPresent(isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview();
});
boxAnswer.getPayLoad(DISABLE_NOTIFICATION).ifPresent(isDisable -> {
if (TRUE.equals(isDisable)) sendMessage.disableNotification();
});
try {
final Message execute = absSender.execute(sendMessage);
if (checkNotNull(senderRepository) && saveMessageId) {
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId().toString());
}
return SentBox.optional(telegramId, execute.getMessageId().toString(), preparedAnswer, boxAnswer);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return Optional.empty();
return sendMessage;
}
@Override