diff --git a/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java b/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java index a78e4e5..fbad94d 100644 --- a/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java +++ b/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java @@ -19,6 +19,8 @@ public interface TelegramBot { File awaitDownloadFile(TelegramFile telegramFile, Path targetDirectory); + void shutdown(); + default CompletableFuture downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException { return CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory)); } diff --git a/longpolling-okhttp/src/main/java/hdvtdev/telegram/longpolling/okhttp/OkHttpTelegramBot.java b/longpolling-okhttp/src/main/java/hdvtdev/telegram/longpolling/okhttp/OkHttpTelegramBot.java index 61b7507..e65cb2a 100644 --- a/longpolling-okhttp/src/main/java/hdvtdev/telegram/longpolling/okhttp/OkHttpTelegramBot.java +++ b/longpolling-okhttp/src/main/java/hdvtdev/telegram/longpolling/okhttp/OkHttpTelegramBot.java @@ -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 messageHandlers; - private Map callbackQueryHandlers; - - private UserState userState; - private Map 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 updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass(); @@ -170,38 +152,44 @@ public class OkHttpTelegramBot implements TelegramBot { private void getUpdates() { List 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 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 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 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;