Skip to content

Commit

Permalink
[FIX]: various improvements and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Drednote committed Dec 4, 2024
1 parent 9041474 commit 0a4f008
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
* <p>
* 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
*/
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/drednote/telegram/core/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageType> messageTypes;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/telegram-messages_en.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
<entry key="response.forbidden">You do not have access to this resource</entry>
<entry key="response.internalError">Oops, something went wrong, please try again later</entry>
<entry key="response.notHandled">Unknown command or text, try something else</entry>
<entry key="response.tooManyRequests">Too many requests. Please try later</entry>
</properties>

0 comments on commit 0a4f008

Please sign in to comment.