From 66d2172f51037de2c0e910a7791ce6e2052a0347 Mon Sep 17 00:00:00 2001 From: hdvtdev Date: Thu, 24 Apr 2025 23:14:43 +0300 Subject: [PATCH] some refactoring --- .../hdvtdev/telegram/core/TelegramBot.java | 18 +++- .../core/methods/TelegramApiMethodBody.java | 8 +- .../longpolling/okhttp/OkHttpTelegramBot.java | 82 ++----------------- 3 files changed, 26 insertions(+), 82 deletions(-) diff --git a/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java b/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java index fbad94d..9085f3c 100644 --- a/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java +++ b/core/src/main/java/hdvtdev/telegram/core/TelegramBot.java @@ -21,12 +21,22 @@ public interface TelegramBot { void shutdown(); - default CompletableFuture downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException { - return CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory)); + default CompletableFuture downloadFile(TelegramFile telegramFile, Path targetDirectory) { + CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory)); + completableFuture.exceptionally(e -> { + e.printStackTrace(); + return null; + }); + return completableFuture; } - default CompletableFuture execute(TelegramApiMethod telegramApiMethod) throws TelegramApiException, TelegramApiNetworkException, TelegramMethodParsingException { - return CompletableFuture.supplyAsync(() -> awaitExecute(telegramApiMethod)); + default CompletableFuture execute(TelegramApiMethod telegramApiMethod) { + CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> awaitExecute(telegramApiMethod)); + completableFuture.exceptionally(e -> { + e.printStackTrace(); + return null; + }); + return completableFuture; } default int getPing() throws TelegramApiNetworkException { diff --git a/core/src/main/java/hdvtdev/telegram/core/methods/TelegramApiMethodBody.java b/core/src/main/java/hdvtdev/telegram/core/methods/TelegramApiMethodBody.java index 4b8da57..804eea5 100644 --- a/core/src/main/java/hdvtdev/telegram/core/methods/TelegramApiMethodBody.java +++ b/core/src/main/java/hdvtdev/telegram/core/methods/TelegramApiMethodBody.java @@ -19,8 +19,12 @@ public final class TelegramApiMethodBody { if (value != null) this.elements.add(new Element(name, value)); } - public void forEach(Consumer action) { - this.elements.forEach(action); + public int size() { + return this.elements.size(); + } + + public Element get(int i) { + return this.elements.get(i); } public record Element(String name, String value) { 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 e65cb2a..bfba11b 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 @@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import hdvtdev.telegram.core.InvokeMethod; import hdvtdev.telegram.core.TelegramBot; import hdvtdev.telegram.core.UpdateConsumer; -import hdvtdev.telegram.core.UserState; import hdvtdev.telegram.core.annotaions.Jsonable; import hdvtdev.telegram.core.exceptions.TelegramApiException; import hdvtdev.telegram.core.exceptions.TelegramApiNetworkException; @@ -16,9 +15,7 @@ import hdvtdev.telegram.core.methods.GetUpdates; import hdvtdev.telegram.core.methods.TelegramApiMethod; import hdvtdev.telegram.core.methods.TelegramApiMethodBody; import hdvtdev.telegram.core.objects.Update; -import hdvtdev.telegram.core.objects.callback.CallbackQuery; import hdvtdev.telegram.core.objects.media.TelegramFile; -import hdvtdev.telegram.core.objects.message.Message; import okhttp3.*; @@ -32,7 +29,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicLong; @@ -113,37 +109,17 @@ public class OkHttpTelegramBot implements TelegramBot { if (builder.updateConsumer != null) setUpdateConsumer(builder.updateConsumer); } - /** - * Enables the default long polling update consumer for handlers. - *

- * This method is effective only if {@link #enableHandlers} is{@code true}, - * otherwise, it just marks updates as read/processed without invoking any handlers. - *

- *


-     * Equivalent to: setUpdateConsumer(null);
-     * 
- * @throws IllegalStateException if an {@code UpdateConsumer} is already defined - * @see #setUpdateConsumer(UpdateConsumer) - * @see #enableHandlers - * @since 0.0.1 - */ - public void enableDefaultUpdateConsumer() throws IllegalStateException { - setUpdateConsumer(null); - } - /** * Enables a long polling update consumer. If {@link #enableHandlers} is {@code true}, * the specified handlers will be invoked for each received update. * * @param updateConsumer class that implements {@code UpdateConsumer} * @throws IllegalStateException if an {@code UpdateConsumer} is already defined - * @see #enableDefaultUpdateConsumer() * @see #enableHandlers * @since 0.0.1 */ - public void setUpdateConsumer(UpdateConsumer updateConsumer) throws IllegalStateException { - if (thread != null) - throw new IllegalStateException("Update Consumer is already defined. You must first stop the previous"); + private void setUpdateConsumer(UpdateConsumer updateConsumer) throws IllegalStateException { + if (thread != null) throw new IllegalStateException("Update Consumer is already defined. You must first stop the previous"); this.updateConsumer = updateConsumer; this.lastUpdateId = new AtomicLong(0); thread = Executors.newSingleThreadExecutor(); @@ -162,55 +138,6 @@ public class OkHttpTelegramBot implements TelegramBot { } } - 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) { - - if (invokeMethod != null) { - Method method = invokeMethod.method(); - method.setAccessible(true); - try { - switch (invokeMethod.parameterType().getSimpleName()) { - case "Message" -> method.invoke(null, update.message()); - case "Update" -> method.invoke(null, update); - case "CallbackQuery" -> method.invoke(null, update.callbackQuery()); - default -> method.invoke(null); - } - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - throw new RuntimeException(e); - } - } - } - @Override public void shutdown() { this.thread.close(); @@ -233,7 +160,10 @@ public class OkHttpTelegramBot implements TelegramBot { } } else { FormBody.Builder requestBody = new FormBody.Builder(); - body.forEach(e -> requestBody.add(e.name(), e.value())); + for (int i = 0; i < body.size(); i++) { + TelegramApiMethodBody.Element e = body.get(i); + requestBody.add(e.name(), e.value()); + } request.post(requestBody.build()); }