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