From 0a4f008f69f5a7b907ec95230278842a8a16644d Mon Sep 17 00:00:00 2001 From: Ivan Galushko Date: Wed, 4 Dec 2024 21:13:12 +0300 Subject: [PATCH] [FIX]: various improvements and fix some bugs --- .../telegram/TelegramAutoConfiguration.java | 14 +++++--- .../telegram/core/DefaultTelegramBot.java | 32 +++++++++++++------ .../drednote/telegram/core/TelegramBot.java | 10 ++++++ .../core/request/AbstractUpdateRequest.java | 4 +++ .../telegram/core/request/UpdateRequest.java | 8 +++++ src/main/resources/telegram-messages_en.xml | 8 +++++ 6 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 src/main/resources/telegram-messages_en.xml diff --git a/src/main/java/io/github/drednote/telegram/TelegramAutoConfiguration.java b/src/main/java/io/github/drednote/telegram/TelegramAutoConfiguration.java index 2148eed..0324d34 100644 --- a/src/main/java/io/github/drednote/telegram/TelegramAutoConfiguration.java +++ b/src/main/java/io/github/drednote/telegram/TelegramAutoConfiguration.java @@ -44,6 +44,16 @@ @AutoConfiguration public class TelegramAutoConfiguration { + private static final String TELEGRAM_BOT = "TelegramBot"; + + public TelegramAutoConfiguration(TelegramProperties properties) { + if (StringUtils.isBlank(properties.getToken())) { + throw new BeanCreationException(TELEGRAM_BOT, + "If you want to use telegram bot library consider specify a drednote.telegram.token or disable creating " + + "telegram bot by setting a drednote.telegram.enabled to false"); + } + } + /** * Autoconfiguration class for configuring the Telegram bot instance. */ @@ -80,10 +90,6 @@ public TelegramBot telegramLongPollingBot( throw new BeanCreationException(TELEGRAM_BOT, "Consider specify drednote.telegram.token"); } - if (StringUtils.isBlank(properties.getName())) { - throw new BeanCreationException(TELEGRAM_BOT, - "Consider specify drednote.telegram.name"); - } if (properties.getSession().getUpdateStrategy() == UpdateStrategy.LONG_POLLING) { return new DefaultTelegramBot(properties, updateHandlers, objectMapper, exceptionHandler, updateFilterProvider, messageSource, telegramClient); diff --git a/src/main/java/io/github/drednote/telegram/core/DefaultTelegramBot.java b/src/main/java/io/github/drednote/telegram/core/DefaultTelegramBot.java index 6aa68c3..d2a421a 100644 --- a/src/main/java/io/github/drednote/telegram/core/DefaultTelegramBot.java +++ b/src/main/java/io/github/drednote/telegram/core/DefaultTelegramBot.java @@ -70,8 +70,7 @@ public class DefaultTelegramBot implements TelegramBot { private final TelegramClient telegramClient; /** - * Creates a new instance of the {@code DefaultTelegramBot} class with the provided properties - * and dependencies + * Creates a new instance of the {@code DefaultTelegramBot} class with the provided properties and dependencies * * @param properties the Telegram properties, not null * @param updateHandlers the collection of update handlers, not null @@ -105,12 +104,12 @@ public DefaultTelegramBot( } /** - * Handles the received update. Creates a {@link DefaultUpdateRequest} to encapsulate the - * {@link Update}. Processes the request through pre-filtering, handling, post-filtering, and - * answering. Handle any exceptions thrown during processing. + * Handles the received update. Creates a {@link DefaultUpdateRequest} to encapsulate the {@link Update}. Processes + * the request through pre-filtering, handling, post-filtering, and answering. Handle any exceptions thrown during + * processing. *

- * Before processing saves request to context, for further usage. After processing delete - * request from context and spring beans too if any were created + * Before processing saves request to context, for further usage. After processing delete request from context and + * spring beans too if any were created * * @param update the received update, not null */ @@ -127,8 +126,8 @@ public void onUpdateReceived(Update update) { } /** - * Performs the processing of the received update. Executes pre-filtering, handling, - * post-filtering, and answering. Handle any exceptions thrown during processing + * Performs the processing of the received update. Executes pre-filtering, handling, post-filtering, and answering. + * Handle any exceptions thrown during processing * * @param request the update request */ @@ -141,7 +140,20 @@ private void doReceive(DefaultUpdateRequest request) { } finally { try { doPostFilter(request); - doAnswer(request); + try { + doAnswer(request); + } catch (TelegramApiException e) { + handleException(request, e); + } catch (Exception e) { + handleException(request, e); + if (request.getResponse() != null) { + try { + doAnswer(request); + } catch (Exception ex) { + handleException(request, ex); + } + } + } doConclusivePostFilter(request); } catch (Exception e) { handleException(request, e); diff --git a/src/main/java/io/github/drednote/telegram/core/TelegramBot.java b/src/main/java/io/github/drednote/telegram/core/TelegramBot.java index d0642f3..b0fdeb9 100644 --- a/src/main/java/io/github/drednote/telegram/core/TelegramBot.java +++ b/src/main/java/io/github/drednote/telegram/core/TelegramBot.java @@ -2,7 +2,17 @@ import org.telegram.telegrambots.meta.api.objects.Update; +/** + * This interface represents a Telegram Bot that can receive updates from the Telegram API. + * + * @author Ivan Galushko + */ public interface TelegramBot { + /** + * Called when an update is received from the Telegram API. + * + * @param update the {@code Update} object containing the details of the received update + */ void onUpdateReceived(Update update); } diff --git a/src/main/java/io/github/drednote/telegram/core/request/AbstractUpdateRequest.java b/src/main/java/io/github/drednote/telegram/core/request/AbstractUpdateRequest.java index 35fe0c8..bdc795b 100644 --- a/src/main/java/io/github/drednote/telegram/core/request/AbstractUpdateRequest.java +++ b/src/main/java/io/github/drednote/telegram/core/request/AbstractUpdateRequest.java @@ -31,6 +31,8 @@ public abstract class AbstractUpdateRequest implements UpdateRequest, UpdateRequ protected final Integer id; protected final Update origin; protected final Long chatId; + @Nullable + protected final Long userId; protected final RequestType requestType; protected final Set messageTypes; @@ -156,6 +158,7 @@ protected AbstractUpdateRequest(Update update) { } this.chatId = resolveChatId(); this.messageTypes = parseMessageType(); + this.userId = user != null ? user.getId() : null; } private static RequestType parseMessageType(Update origin) { @@ -191,6 +194,7 @@ protected AbstractUpdateRequest(UpdateRequest request) { this.chat = request.getChat(); this.user = request.getUser(); this.text = request.getText(); + this.userId = request.getUserId(); } @NonNull diff --git a/src/main/java/io/github/drednote/telegram/core/request/UpdateRequest.java b/src/main/java/io/github/drednote/telegram/core/request/UpdateRequest.java index 6e13c91..d7f4b36 100644 --- a/src/main/java/io/github/drednote/telegram/core/request/UpdateRequest.java +++ b/src/main/java/io/github/drednote/telegram/core/request/UpdateRequest.java @@ -56,6 +56,14 @@ public interface UpdateRequest { @NonNull Long getChatId(); + /** + * Returns the ID of the user associated with the update + * + * @return the ID of the user + */ + @Nullable + Long getUserId(); + /** * Returns the type of the request * diff --git a/src/main/resources/telegram-messages_en.xml b/src/main/resources/telegram-messages_en.xml new file mode 100644 index 0000000..7a25179 --- /dev/null +++ b/src/main/resources/telegram-messages_en.xml @@ -0,0 +1,8 @@ + + + + You do not have access to this resource + Oops, something went wrong, please try again later + Unknown command or text, try something else + Too many requests. Please try later + \ No newline at end of file