some refactoring
This commit is contained in:
@@ -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.
|
||||
* <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},
|
||||
* 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<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
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user