some refactoring
This commit is contained in:
@@ -1,29 +1,27 @@
|
||||
package hdvtdev.telegram.longpolling;
|
||||
package hdvtdev.telegram.longpolling.okhttp;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import hdvtdev.telegram.annotations.handlers.CallbackQueryHandler;
|
||||
import hdvtdev.telegram.annotations.handlers.TextMessageHandler;
|
||||
import hdvtdev.telegram.annotations.util.Jsonable;
|
||||
import hdvtdev.telegram.core.InvokeMethod;
|
||||
import hdvtdev.telegram.core.TelegramBot;
|
||||
import hdvtdev.telegram.exceptions.TelegramApiException;
|
||||
import hdvtdev.telegram.exceptions.TelegramApiNetworkException;
|
||||
import hdvtdev.telegram.exceptions.TelegramMethodParsingException;
|
||||
import hdvtdev.telegram.methods.GetUpdates;
|
||||
import hdvtdev.telegram.methods.TelegramApiMethod;
|
||||
import hdvtdev.telegram.objects.CallbackQuery;
|
||||
import hdvtdev.telegram.objects.Message;
|
||||
import hdvtdev.telegram.objects.TelegramFile;
|
||||
import hdvtdev.telegram.objects.Update;
|
||||
import hdvtdev.telegram.util.ClassFinder;
|
||||
import hdvtdev.telegram.util.InvokeMethod;
|
||||
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;
|
||||
import hdvtdev.telegram.core.exceptions.TelegramMethodParsingException;
|
||||
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.*;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -33,7 +31,6 @@ import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -121,12 +118,15 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
userStateStorage = builder.userStateStorage;
|
||||
userState = builder.userState;
|
||||
if (builder.updateConsumer != null) setUpdateConsumer(builder.updateConsumer);
|
||||
if (enableHandlers) {
|
||||
/*
|
||||
if (false) {
|
||||
Class<? extends UpdateConsumer> updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass();
|
||||
Map<Class<?>, Map<String, InvokeMethod>> handlers = builder.enableScan ? ClassFinder.getClasses() : ClassFinder.localScan(updateConsumerClass);
|
||||
this.messageHandlers = Collections.unmodifiableMap(handlers.get(TextMessageHandler.class));
|
||||
this.callbackQueryHandlers = Collections.unmodifiableMap(handlers.get(CallbackQueryHandler.class));
|
||||
}
|
||||
|
||||
*/
|
||||
this.TELEGRAM_API_URL = "https://api.telegram.org/bot" + builder.token + "/";
|
||||
this.TELEGRAM_FILE_API_URL = "https://api.telegram.org/file/bot" + builder.token + "/";
|
||||
}
|
||||
@@ -227,9 +227,10 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
this.thread.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T awaitExecute(TelegramApiMethod<T> telegramApiMethod) throws TelegramApiException, TelegramApiNetworkException, TelegramMethodParsingException {
|
||||
|
||||
RequestBody body = telegramApiMethod.getBody();
|
||||
TelegramApiMethodBody body = telegramApiMethod.getBody();
|
||||
|
||||
Request.Builder request = new Request.Builder()
|
||||
.url(TELEGRAM_API_URL + telegramApiMethod.getMethodName());
|
||||
@@ -241,7 +242,11 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
throw new TelegramMethodParsingException(e);
|
||||
}
|
||||
}
|
||||
} else request.post(body);
|
||||
} else {
|
||||
FormBody.Builder requestBody = new FormBody.Builder();
|
||||
body.forEach(e -> requestBody.add(e.name(), e.value()));
|
||||
request.post(requestBody.build());
|
||||
}
|
||||
|
||||
try (Response response = client.newCall(request.build()).execute()) {
|
||||
String responseBody = Objects.requireNonNull(response.body()).string();
|
||||
@@ -259,7 +264,6 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private File getFile(TelegramFile telegramFile, Path targetDirectory) {
|
||||
try (Response response = client.newCall(new Request.Builder().url(TELEGRAM_FILE_API_URL + telegramFile.filePath()).build()).execute()) {
|
||||
ResponseBody responseBody = Objects.requireNonNull(response.body());
|
||||
@@ -292,7 +296,7 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public Builder updateConsumer(Main.Upd updateConsumer) {
|
||||
public Builder updateConsumer(UpdateConsumer updateConsumer) {
|
||||
this.updateConsumer = updateConsumer;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module longpolling.okhttp {
|
||||
exports hdvtdev.telegram.longpolling;
|
||||
exports hdvtdev.telegram.longpolling.okhttp;
|
||||
requires com.fasterxml.jackson.databind;
|
||||
requires com.fasterxml.jackson.core;
|
||||
requires core;
|
||||
requires okhttp3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user