some refactoring
This commit is contained in:
@@ -19,6 +19,8 @@ public interface TelegramBot {
|
|||||||
|
|
||||||
File awaitDownloadFile(TelegramFile telegramFile, Path targetDirectory);
|
File awaitDownloadFile(TelegramFile telegramFile, Path targetDirectory);
|
||||||
|
|
||||||
|
void shutdown();
|
||||||
|
|
||||||
default CompletableFuture<File> downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException {
|
default CompletableFuture<File> downloadFile(TelegramFile telegramFile, Path targetDirectory) throws TelegramApiException, TelegramApiNetworkException {
|
||||||
return CompletableFuture.supplyAsync(() -> awaitDownloadFile(telegramFile, targetDirectory));
|
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_API_URL;
|
||||||
private final String TELEGRAM_FILE_API_URL;
|
private final String TELEGRAM_FILE_API_URL;
|
||||||
private final ObjectMapper json = new ObjectMapper();
|
private final ObjectMapper json;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
@@ -86,27 +86,10 @@ public class OkHttpTelegramBot implements TelegramBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private UpdateConsumer updateConsumer;
|
private UpdateConsumer updateConsumer;
|
||||||
|
|
||||||
private boolean enableHandlers = false;
|
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) {
|
public OkHttpTelegramBot(String token) {
|
||||||
|
this.json = new ObjectMapper();
|
||||||
this.TELEGRAM_API_URL = "https://api.telegram.org/bot" + token + "/";
|
this.TELEGRAM_API_URL = "https://api.telegram.org/bot" + token + "/";
|
||||||
this.TELEGRAM_FILE_API_URL = "https://api.telegram.org/file/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;
|
updateLimit = builder.updateLimit;
|
||||||
updateTimeout = builder.updateTimeout;
|
updateTimeout = builder.updateTimeout;
|
||||||
enableHandlers = builder.enableHandlers;
|
enableHandlers = builder.enableHandlers;
|
||||||
userStateStorage = builder.userStateStorage;
|
json = builder.objectMapper == null ? new ObjectMapper() : builder.objectMapper;
|
||||||
userState = builder.userState;
|
|
||||||
/*
|
/*
|
||||||
if (false) {
|
if (false) {
|
||||||
Class<? extends UpdateConsumer> updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass();
|
Class<? extends UpdateConsumer> updateConsumerClass = builder.updateConsumer == null ? UpdateConsumer.class : builder.updateConsumer.getClass();
|
||||||
@@ -170,38 +152,44 @@ public class OkHttpTelegramBot implements TelegramBot {
|
|||||||
|
|
||||||
private void getUpdates() {
|
private void getUpdates() {
|
||||||
List<Update> updates = List.of(awaitExecute(new GetUpdates(lastUpdateId.get() + 1, updateLimit, updateTimeout)));
|
List<Update> updates = List.of(awaitExecute(new GetUpdates(lastUpdateId.get() + 1, updateLimit, updateTimeout)));
|
||||||
System.out.println("UPDATE");
|
try {
|
||||||
if (!updates.isEmpty()) {
|
if (!updates.isEmpty()) {
|
||||||
try {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (updateConsumer != null) CompletableFuture.runAsync(() -> updateConsumer.onUpdates(updates));
|
if (updateConsumer != null) CompletableFuture.runAsync(() -> updateConsumer.onUpdates(updates));
|
||||||
} finally {
|
|
||||||
lastUpdateId.set(updates.getLast().updateId());
|
lastUpdateId.set(updates.getLast().updateId());
|
||||||
if (!thread.isShutdown()) getUpdates();
|
}
|
||||||
|
} finally {
|
||||||
|
if (!thread.isShutdown()) getUpdates();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
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();
|
this.thread.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,36 +270,29 @@ public class OkHttpTelegramBot implements TelegramBot {
|
|||||||
return getFile(telegramFile, targetDirectory);
|
return getFile(telegramFile, targetDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final class Builder {
|
public static final class Builder {
|
||||||
private int updateLimit = 10;
|
private int updateLimit = 10;
|
||||||
private int updateTimeout = 25;
|
private int updateTimeout = 25;
|
||||||
private boolean enableHandlers = false;
|
private boolean enableHandlers = false;
|
||||||
private boolean enableScan = false;
|
private boolean enableScan = false;
|
||||||
private final String token;
|
private final String token;
|
||||||
private Map<String, UserState> userStateStorage;
|
|
||||||
private UserState userState;
|
|
||||||
private UpdateConsumer updateConsumer;
|
private UpdateConsumer updateConsumer;
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
public Builder(String token) {
|
public Builder(String token) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder objectMapper(ObjectMapper objectMapper) {
|
||||||
|
this.objectMapper = objectMapper;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder updateConsumer(UpdateConsumer updateConsumer) {
|
public Builder updateConsumer(UpdateConsumer updateConsumer) {
|
||||||
this.updateConsumer = updateConsumer;
|
this.updateConsumer = updateConsumer;
|
||||||
return this;
|
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) {
|
public Builder updateLimit(int updateLimit) {
|
||||||
this.updateLimit = updateLimit;
|
this.updateLimit = updateLimit;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user