Переход на асинхронную отправку команд в телеграм для quarkus

This commit is contained in:
Struchkov Mark 2023-02-17 16:43:37 +03:00
parent f1065301d7
commit a103f20dd0
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6

View File

@ -16,17 +16,21 @@ import org.slf4j.LoggerFactory;
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.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.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.Serializable;
import java.util.List; 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_NOTIFICATION;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_WEB_PAGE_PREVIEW; 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.godfather.telegram.main.sender.util.KeyBoardConvert.convertInlineKeyBoard;
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;
public class TelegramSender implements TelegramSending { public class TelegramSender implements TelegramSending {
@ -125,25 +129,27 @@ public class TelegramSender implements TelegramSending {
editMessageText.enableMarkdown(true); editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage()); editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
try {
absSender.execute(editMessageText); return Uni.createFrom().completionStage(executeAsync(editMessageText))
return Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer)); .onItem().ifNotNull().transformToUni(t -> Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer)))
} catch (TelegramApiRequestException e) { .onFailure(TelegramApiRequestException.class).recoverWithUni(
log.error(e.getApiResponse()); ex -> {
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { 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 sendMessage(telegramId, boxAnswer, saveMessageId);
} }
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
return Uni.createFrom().nullItem(); return Uni.createFrom().nullItem();
} }
); );
} }
);
}
private Uni<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { private Uni<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().transform( .onItem().transformToUni(
v -> { v -> {
final SendMessage sendMessage = new SendMessage(); final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true); sendMessage.enableMarkdown(true);
@ -162,27 +168,39 @@ public class TelegramSender implements TelegramSending {
if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview(); if (TRUE.equals(isDisable)) sendMessage.disableWebPagePreview();
} }
); );
return Uni.createFrom().completionStage(executeAsync(sendMessage))
try { .onFailure(TelegramApiRequestException.class).invoke(t -> log.error(((TelegramApiRequestException) t).getApiResponse()))
return absSender.execute(sendMessage); .onFailure().invoke(t -> log.error(t.getMessage(), t));
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return null;
} }
).onItem().ifNotNull().call(answerMessage -> { ).onItem().ifNotNull().call(answerMessage -> {
if (checkNotNull(senderRepository) && saveMessageId) { if (checkNotNull(senderRepository) && saveMessageId) {
return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId().toString()); return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId().toString());
} }
return null; return Uni.createFrom().nullItem();
}) })
.onItem().ifNotNull().transformToUni( .onItem().ifNotNull().transformToUni(
answerMessage -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessage.getMessageId().toString(), boxAnswer, boxAnswer)) answerMessage -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessage.getMessageId().toString(), boxAnswer, boxAnswer))
); );
} }
private CompletableFuture<Message> executeAsync(SendMessage sendMessage) {
try {
return absSender.executeAsync(sendMessage);
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
return completedFuture(null);
}
private CompletableFuture<Serializable> executeAsync(EditMessageText editMessageText) {
try {
return absSender.executeAsync(editMessageText);
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
return completedFuture(null);
}
@Override @Override
public SendType getType() { public SendType getType() {
return SendType.PRIVATE; return SendType.PRIVATE;