com.w4p.telegram.TelegramBotService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-telegram Show documentation
Show all versions of spring-telegram Show documentation
Java TelegramBot for Spring Framework with async multithreading support
package com.w4p.telegram;
import com.w4p.telegram.annotation.W4TelegramCommand;
import com.w4p.telegram.config.TelegramBotBuilder;
import com.w4p.telegram.model.TelegramBotCommand;
import com.w4p.telegram.model.TelegramHandler;
import com.w4p.telegram.model.TelegramMessageCommand;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.generics.LongPollingBot;
import org.telegram.telegrambots.generics.WebhookBot;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
@Slf4j
public class TelegramBotService {
private String username;
private String token;
private String path;
private TelegramBotsApi api;
private final Map commandList = new LinkedHashMap<>();
private Executor botExecutor;
private TelegramHandler defaultMessageHandler;
private DefaultAbsSender client;
public TelegramBotService(TelegramBotBuilder botBuilder) {
log.info("Build W4TelegramBot: {}", botBuilder);
this.username = botBuilder.getUsername();
this.token = botBuilder.getToken();
this.path = botBuilder.getPath();
this.api = new TelegramBotsApi();
try {
if (botBuilder.getType() == TelegramBotBuilder.BotType.LONG_POLLING) {
this.botExecutor = Executors.newFixedThreadPool(
(botBuilder.getMaxThreads() > 0) ? botBuilder.getMaxThreads():100);
this.client = new TelegramBotLongPollingImpl();
api.registerBot((LongPollingBot) this.client);
} else if (botBuilder.getType() == TelegramBotBuilder.BotType.WEBHOOK) {
this.client = new TelegramBotWebHooksImpl();
api.registerBot((WebhookBot) this.client);
}
} catch (TelegramApiException e) {
log.error("Error while creating TelegramBotsApi: {}", e.getMessage());
}
}
public void updateLongPolling(Update update) {
CompletableFuture.runAsync(() -> {
BotApiMethod result = updateProcess(update);
if (result != null) {
try {
this.client.execute(result);
} catch (TelegramApiException e) {
log.error("TelegramBotService: {}", e.getMessage());
}
}
}, botExecutor);
}
public BotApiMethod updateProcess(Update update) {
log.debug("Update received: {}", update);
try {
if (update.getMessage() != null) {
TelegramMessageCommand command = getCommand(update.getMessage().getText());
TelegramHandler commandHandler = this.defaultMessageHandler;
if (command.isCommand()) {
commandHandler = this.commandList.get(command.getCmd());
}
if (commandHandler != null) {
Object[] arguments = makeArgumentList(commandHandler.getMethod(), command, update);
if (commandHandler.getW4TelegramCommand() != null && commandHandler.getW4TelegramCommand().isHelp()) {
sendHelpList(update);
} else if (commandHandler.getMethod().getGenericReturnType().equals(Void.TYPE)) {
//Void method
commandHandler.getMethod().invoke(commandHandler.getBean(), arguments);
} else if (commandHandler.getMethod().getGenericReturnType().equals(SendMessage.class)) {
//SendMessage
SendMessage sendMessage = (SendMessage) commandHandler.getMethod()
.invoke(commandHandler.getBean(), arguments);
return sendMessage;
}
}
}
} catch (IllegalAccessException e) {
log.error("TelegramBotService: {}", e.getMessage());
} catch (InvocationTargetException e) {
log.error("TelegramBotService: {}",e.getMessage());
} catch (TelegramApiException e) {
log.error("TelegramBotService: {}", e.getMessage());
}
return null;
}
private Object[] makeArgumentList(Method method, TelegramMessageCommand telegramMessageCommand, Update update) {
Type[] commandArguments = method.getGenericParameterTypes();
List