some refactoring

This commit is contained in:
hdvtdev
2025-04-24 23:14:43 +03:00
parent 97e93f7feb
commit 66d2172f51
3 changed files with 26 additions and 82 deletions

View File

@@ -21,12 +21,22 @@ public interface TelegramBot {
void shutdown(); void shutdown();
default CompletableFuture<File> downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException { default CompletableFuture<File> downloadFile(TelegramFile telegramFile, Path targetDirectory) {
return CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory)); CompletableFuture<File> completableFuture = CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory));
completableFuture.exceptionally(e -> {
e.printStackTrace();
return null;
});
return completableFuture;
} }
default <T> CompletableFuture<T> execute(TelegramApiMethod<T> telegramApiMethod) throws TelegramApiException, TelegramApiNetworkException, TelegramMethodParsingException { default <T> CompletableFuture<T> execute(TelegramApiMethod<T> telegramApiMethod) {
return CompletableFuture.supplyAsync(() -> awaitExecute(telegramApiMethod)); CompletableFuture<T> completableFuture = CompletableFuture.supplyAsync(() -> awaitExecute(telegramApiMethod));
completableFuture.exceptionally(e -> {
e.printStackTrace();
return null;
});
return completableFuture;
} }
default int getPing() throws TelegramApiNetworkException { default int getPing() throws TelegramApiNetworkException {

View File

@@ -19,8 +19,12 @@ public final class TelegramApiMethodBody {
if (value != null) this.elements.add(new Element(name, value)); if (value != null) this.elements.add(new Element(name, value));
} }
public void forEach(Consumer<? super Element> action) { public int size() {
this.elements.forEach(action); return this.elements.size();
}
public Element get(int i) {
return this.elements.get(i);
} }
public record Element(String name, String value) { public record Element(String name, String value) {

View File

@@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import hdvtdev.telegram.core.InvokeMethod; import hdvtdev.telegram.core.InvokeMethod;
import hdvtdev.telegram.core.TelegramBot; import hdvtdev.telegram.core.TelegramBot;
import hdvtdev.telegram.core.UpdateConsumer; import hdvtdev.telegram.core.UpdateConsumer;
import hdvtdev.telegram.core.UserState;
import hdvtdev.telegram.core.annotaions.Jsonable; import hdvtdev.telegram.core.annotaions.Jsonable;
import hdvtdev.telegram.core.exceptions.TelegramApiException; import hdvtdev.telegram.core.exceptions.TelegramApiException;
import hdvtdev.telegram.core.exceptions.TelegramApiNetworkException; 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.TelegramApiMethod;
import hdvtdev.telegram.core.methods.TelegramApiMethodBody; import hdvtdev.telegram.core.methods.TelegramApiMethodBody;
import hdvtdev.telegram.core.objects.Update; 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.media.TelegramFile;
import hdvtdev.telegram.core.objects.message.Message;
import okhttp3.*; import okhttp3.*;
@@ -32,7 +29,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
@@ -113,37 +109,17 @@ public class OkHttpTelegramBot implements TelegramBot {
if (builder.updateConsumer != null) setUpdateConsumer(builder.updateConsumer); if (builder.updateConsumer != null) setUpdateConsumer(builder.updateConsumer);
} }
/**
* Enables the default long polling update consumer for handlers.
* <p>
* This method is effective only if {@link #enableHandlers} is{@code true},
* otherwise, it just marks updates as read/processed without invoking any handlers.
* <p>
* <pre><code>
* Equivalent to: setUpdateConsumer(null);
* </code></pre>
* @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}, * Enables a long polling update consumer. If {@link #enableHandlers} is {@code true},
* the specified handlers will be invoked for each received update. * the specified handlers will be invoked for each received update.
* *
* @param updateConsumer class that implements {@code UpdateConsumer} * @param updateConsumer class that implements {@code UpdateConsumer}
* @throws IllegalStateException if an {@code UpdateConsumer} is already defined * @throws IllegalStateException if an {@code UpdateConsumer} is already defined
* @see #enableDefaultUpdateConsumer()
* @see #enableHandlers * @see #enableHandlers
* @since 0.0.1 * @since 0.0.1
*/ */
public void setUpdateConsumer(UpdateConsumer updateConsumer) throws IllegalStateException { private void setUpdateConsumer(UpdateConsumer updateConsumer) throws IllegalStateException {
if (thread != null) if (thread != null) throw new IllegalStateException("Update Consumer is already defined. You must first stop the previous");
throw new IllegalStateException("Update Consumer is already defined. You must first stop the previous");
this.updateConsumer = updateConsumer; this.updateConsumer = updateConsumer;
this.lastUpdateId = new AtomicLong(0); this.lastUpdateId = new AtomicLong(0);
thread = Executors.newSingleThreadExecutor(); thread = Executors.newSingleThreadExecutor();
@@ -162,55 +138,6 @@ public class OkHttpTelegramBot implements TelegramBot {
} }
} }
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) {
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 @Override
public void shutdown() { public void shutdown() {
this.thread.close(); this.thread.close();
@@ -233,7 +160,10 @@ public class OkHttpTelegramBot implements TelegramBot {
} }
} else { } else {
FormBody.Builder requestBody = new FormBody.Builder(); 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()); request.post(requestBody.build());
} }