some refactoring
This commit is contained in:
@@ -19,6 +19,8 @@ public interface TelegramBot {
|
||||
|
||||
File awaitDownloadFile(TelegramFile telegramFile, Path targetDirectory);
|
||||
|
||||
void shutdown();
|
||||
|
||||
default CompletableFuture<File> downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException {
|
||||
return CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory));
|
||||
}
|
||||
|
||||
@@ -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<String, InvokeMethod> messageHandlers;
|
||||
private Map<String, InvokeMethod> callbackQueryHandlers;
|
||||
|
||||
private UserState userState;
|
||||
private Map<String, UserState> 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<? extends UpdateConsumer> updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass();
|
||||
@@ -170,9 +152,20 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
|
||||
private void getUpdates() {
|
||||
List<Update> updates = List.of(awaitExecute(new GetUpdates(lastUpdateId.get() + 1, updateLimit, updateTimeout)));
|
||||
System.out.println("UPDATE");
|
||||
if (!updates.isEmpty()) {
|
||||
try {
|
||||
if (!updates.isEmpty()) {
|
||||
if (updateConsumer != null) CompletableFuture.runAsync(() -> updateConsumer.onUpdates(updates));
|
||||
lastUpdateId.set(updates.getLast().updateId());
|
||||
}
|
||||
} finally {
|
||||
if (!thread.isShutdown()) getUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
private void handlers(List<Update> updates) {
|
||||
//TODO
|
||||
//FIXME
|
||||
/*
|
||||
if (enableHandlers) {
|
||||
for (Update update : updates) {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
@@ -195,13 +188,8 @@ public class OkHttpTelegramBot implements TelegramBot {
|
||||
});
|
||||
}
|
||||
}
|
||||
if (updateConsumer != null) CompletableFuture.runAsync(() -> updateConsumer.onUpdates(updates));
|
||||
} finally {
|
||||
lastUpdateId.set(updates.getLast().updateId());
|
||||
if (!thread.isShutdown()) getUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
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<String, UserState> 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<String, UserState> 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;
|
||||
|
||||
Reference in New Issue
Block a user