From a103f20dd0c6ec9df7c6e8f0a396727ba0d78538 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Fri, 17 Feb 2023 16:43:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B0=D1=81=D0=B8=D0=BD=D1=85=D1=80=D0=BE=D0=BD?= =?UTF-8?q?=D0=BD=D1=83=D1=8E=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D1=83=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D0=B5=D0=B3=D1=80=D0=B0=D0=BC=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20quarkus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quarkus/sender/TelegramSender.java | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java index 4496750..adf58ab 100644 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java @@ -16,17 +16,21 @@ import org.slf4j.LoggerFactory; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; 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.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.Serializable; import java.util.List; +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.sender.util.KeyBoardConvert.convertInlineKeyBoard; import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static java.lang.Boolean.TRUE; +import static java.util.concurrent.CompletableFuture.completedFuture; public class TelegramSender implements TelegramSending { @@ -125,25 +129,27 @@ public class TelegramSender implements TelegramSending { editMessageText.enableMarkdown(true); editMessageText.setText(boxAnswer.getMessage()); editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); - try { - absSender.execute(editMessageText); - return Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer)); - } catch (TelegramApiRequestException e) { - log.error(e.getApiResponse()); - if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { - return sendMessage(telegramId, boxAnswer, saveMessageId); - } - } catch (TelegramApiException e) { - log.error(e.getMessage(), e); - } - return Uni.createFrom().nullItem(); + + return Uni.createFrom().completionStage(executeAsync(editMessageText)) + .onItem().ifNotNull().transformToUni(t -> Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer))) + .onFailure(TelegramApiRequestException.class).recoverWithUni( + ex -> { + final TelegramApiRequestException exception = (TelegramApiRequestException) ex; + final String apiResponse = exception.getApiResponse(); + log.error(apiResponse, exception); + if (ERROR_REPLACE_MESSAGE.equals(apiResponse)) { + return sendMessage(telegramId, boxAnswer, saveMessageId); + } + return Uni.createFrom().nullItem(); + } + ); } ); } private Uni sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() - .onItem().transform( + .onItem().transformToUni( v -> { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); @@ -162,27 +168,39 @@ public class TelegramSender implements TelegramSending { if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview(); } ); - - try { - return absSender.execute(sendMessage); - } catch (TelegramApiRequestException e) { - log.error(e.getApiResponse()); - } catch (TelegramApiException e) { - log.error(e.getMessage()); - } - return null; + 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 -> { if (checkNotNull(senderRepository) && saveMessageId) { return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId().toString()); } - return null; + return Uni.createFrom().nullItem(); }) .onItem().ifNotNull().transformToUni( answerMessage -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessage.getMessageId().toString(), boxAnswer, boxAnswer)) ); } + private CompletableFuture executeAsync(SendMessage sendMessage) { + try { + return absSender.executeAsync(sendMessage); + } catch (TelegramApiException e) { + log.error(e.getMessage(), e); + } + return completedFuture(null); + } + + private CompletableFuture executeAsync(EditMessageText editMessageText) { + try { + return absSender.executeAsync(editMessageText); + } catch (TelegramApiException e) { + log.error(e.getMessage(), e); + } + return completedFuture(null); + } + @Override public SendType getType() { return SendType.PRIVATE;