some refactoring

This commit is contained in:
hdvtdev
2025-04-24 21:57:53 +03:00
parent 5fe53b2539
commit 97e93f7feb
2 changed files with 46 additions and 62 deletions

View File

@@ -42,7 +42,7 @@ public class OkHttpTelegramBot implements TelegramBot {
private final String TELEGRAM_API_URL;
private final String TELEGRAM_FILE_API_URL;
private final ObjectMapper json = new ObjectMapper();
private final ObjectMapper json;
static {
try {
@@ -86,27 +86,10 @@ public class OkHttpTelegramBot implements TelegramBot {
}
private UpdateConsumer updateConsumer;
private boolean enableHandlers = false;
private Map<String, InvokeMethod> messageHandlers;
private Map<String, InvokeMethod> callbackQueryHandlers;
private UserState userState;
private Map<String, UserState> userStateStorage;
public UserState getUserState(String id) {
return userStateStorage.getOrDefault(id, userState);
}
public void setUserState(String id, UserState userState) {
userStateStorage.put(id, userState);
}
public boolean compareUserState(String id, UserState userState) {
return getUserState(id).equals(userState);
}
public OkHttpTelegramBot(String token) {
this.json = new ObjectMapper();
this.TELEGRAM_API_URL = "https://api.telegram.org/bot" + token + "/";
this.TELEGRAM_FILE_API_URL = "https://api.telegram.org/file/bot" + token + "/";
}
@@ -115,8 +98,7 @@ public class OkHttpTelegramBot implements TelegramBot {
updateLimit = builder.updateLimit;
updateTimeout = builder.updateTimeout;
enableHandlers = builder.enableHandlers;
userStateStorage = builder.userStateStorage;
userState = builder.userState;
json = builder.objectMapper == null ? new ObjectMapper() : builder.objectMapper;
/*
if (false) {
Class<? extends UpdateConsumer> updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass();
@@ -170,38 +152,44 @@ public class OkHttpTelegramBot implements TelegramBot {
private void getUpdates() {
List<Update> updates = List.of(awaitExecute(new GetUpdates(lastUpdateId.get() + 1, updateLimit, updateTimeout)));
System.out.println("UPDATE");
if (!updates.isEmpty()) {
try {
if (enableHandlers) {
for (Update update : updates) {
CompletableFuture.runAsync(() -> {
if (update.hasMessage()) {
Message message = update.message();
if (message.hasText() && !messageHandlers.isEmpty()) {
String key = message.text();
invokeAnnotatedMethod(messageHandlers.containsKey("*") ? messageHandlers.get("*") : messageHandlers.get(key), update);
}
}
if (update.hasCallbackQuery()) {
CallbackQuery callbackQuery = update.callbackQuery();
if (callbackQuery.hasMessage()) {
Message message = (Message) callbackQuery.message();
if (message.hasText() && !callbackQueryHandlers.isEmpty()) {
invokeAnnotatedMethod(callbackQueryHandlers.containsKey("*") ? callbackQueryHandlers.get("*") : callbackQueryHandlers.get(message.text()), update);
}
}
}
});
}
}
try {
if (!updates.isEmpty()) {
if (updateConsumer != null) CompletableFuture.runAsync(() -> updateConsumer.onUpdates(updates));
} finally {
lastUpdateId.set(updates.getLast().updateId());
if (!thread.isShutdown()) getUpdates();
}
} finally {
if (!thread.isShutdown()) getUpdates();
}
}
private void handlers(List<Update> updates) {
//TODO
//FIXME
/*
if (enableHandlers) {
for (Update update : updates) {
CompletableFuture.runAsync(() -> {
if (update.hasMessage()) {
Message message = update.message();
if (message.hasText() && !messageHandlers.isEmpty()) {
String key = message.text();
invokeAnnotatedMethod(messageHandlers.containsKey("*") ? messageHandlers.get("*") : messageHandlers.get(key), update);
}
}
if (update.hasCallbackQuery()) {
CallbackQuery callbackQuery = update.callbackQuery();
if (callbackQuery.hasMessage()) {
Message message = (Message) callbackQuery.message();
if (message.hasText() && !callbackQueryHandlers.isEmpty()) {
invokeAnnotatedMethod(callbackQueryHandlers.containsKey("*") ? callbackQueryHandlers.get("*") : callbackQueryHandlers.get(message.text()), update);
}
}
}
});
}
}
*/
}
private static void invokeAnnotatedMethod(InvokeMethod invokeMethod, Update update) {
@@ -223,7 +211,8 @@ public class OkHttpTelegramBot implements TelegramBot {
}
}
public void shutdownUpdateConsumer() {
@Override
public void shutdown() {
this.thread.close();
}
@@ -281,36 +270,29 @@ public class OkHttpTelegramBot implements TelegramBot {
return getFile(telegramFile, targetDirectory);
}
public static final class Builder {
private int updateLimit = 10;
private int updateTimeout = 25;
private boolean enableHandlers = false;
private boolean enableScan = false;
private final String token;
private Map<String, UserState> userStateStorage;
private UserState userState;
private UpdateConsumer updateConsumer;
private ObjectMapper objectMapper;
public Builder(String token) {
this.token = token;
}
public Builder objectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
public Builder updateConsumer(UpdateConsumer updateConsumer) {
this.updateConsumer = updateConsumer;
return this;
}
public Builder enableUserStateStorage(UserState defaultValue, Map<String, UserState> userStateStorage) {
this.userState = defaultValue;
this.userStateStorage = userStateStorage;
return this;
}
public Builder enableUserStateStorage(UserState defaultValue) {
return enableUserStateStorage(defaultValue, new ConcurrentHashMap<>());
}
public Builder updateLimit(int updateLimit) {
this.updateLimit = updateLimit;
return this;