TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/TelegramBotsApi.java

132 lines
5.0 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta;
import org.apache.commons.lang3.StringUtils;
2020-11-01 23:46:36 +01:00
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.TelegramBot;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;
2020-11-03 03:57:36 +01:00
import java.lang.reflect.InvocationTargetException;
/**
* @author Ruben Bermudez
* @version 1.0
2020-11-01 23:46:36 +01:00
* Bots manager
*/
public class TelegramBotsApi {
2020-11-03 03:57:36 +01:00
Class<? extends BotSession> botSessionClass;
private boolean useWebhook; ///< True to enable webhook usage
private Webhook webhook; ///< Webhook instance
/**
*
*/
2020-11-03 03:57:36 +01:00
public TelegramBotsApi(Class<? extends BotSession> botSessionClass) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
this.botSessionClass = botSessionClass;
}
/**
2016-12-26 03:46:46 +01:00
* Creates an HTTP server to receive webhook request
2020-11-03 03:57:36 +01:00
* @param webhook Webhook class
*
2020-11-03 03:57:36 +01:00
* @implSpec This option may require externally handled HTTPS support (i.e. via proxy)
*/
2020-11-03 03:57:36 +01:00
public TelegramBotsApi(Class<? extends BotSession> botSessionClass, Webhook webhook) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
2020-11-03 03:57:36 +01:00
if (webhook == null) {
throw new TelegramApiException("Parameter webhook can not be null or empty");
2016-12-26 03:46:46 +01:00
}
2020-11-03 03:57:36 +01:00
this.botSessionClass = botSessionClass;
this.useWebhook = true;
2020-11-03 03:57:36 +01:00
this.webhook = webhook;
this.webhook.startServer();
}
/**
* Register a bot. The Bot Session is started immediately, and may be disconnected by calling close.
* @param bot the bot to register
*/
2020-11-03 03:57:36 +01:00
public BotSession registerBot(LongPollingBot bot) throws TelegramApiException {
if (bot == null) {
throw new TelegramApiException("Parameter bot can not be null");
}
if (!validateBotUsernameAndToken(bot)) {
throw new TelegramApiException("Bot token and username can't be empty");
}
bot.onRegister();
bot.clearWebhook();
2020-11-03 03:57:36 +01:00
BotSession session;
try {
session = botSessionClass.getConstructor().newInstance();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new TelegramApiException(e);
}
session.setToken(bot.getBotToken());
session.setOptions(bot.getOptions());
session.setCallback(bot);
session.start();
return session;
}
/**
* Register a bot in the api that will receive updates using webhook method
* @param bot Bot to register
2020-11-01 23:46:36 +01:00
* @param setWebhook Set webhook request to initialize the bot
*
* @apiNote The webhook url will be appended with `/callback/bot.getBotPath()` at the end
*/
2020-11-01 23:46:36 +01:00
public void registerBot(WebhookBot bot, SetWebhook setWebhook) throws TelegramApiException {
2020-11-03 22:09:02 +01:00
if (setWebhook == null) {
throw new TelegramApiException("Parameter setWebhook can not be null");
2020-11-03 22:09:02 +01:00
}
if (useWebhook) {
2020-11-03 22:09:02 +01:00
if (webhook == null) {
throw new TelegramApiException("This instance doesn't support Webhook bot, use correct constructor");
}
if (!validateBotUsernameAndToken(bot)) {
throw new TelegramApiException("Bot token and username can't be empty");
}
bot.onRegister();
webhook.registerWebhook(bot);
2020-11-01 23:46:36 +01:00
bot.setWebhook(setWebhook);
}
}
2024-02-14 11:53:53 +01:00
/**
* Register a bot in the api that will receive updates using webhook method
* @param bot Bot to register
*
* @apiNote The webhook url will be appended with `/callback/bot.getBotPath()` at the end
*/
public void registerBotWithoutToken(WebhookBot bot) throws TelegramApiException {
if (useWebhook) {
if (webhook == null) {
throw new TelegramApiException("This instance doesn't support Webhook bot, use correct constructor");
}
if (StringUtils.isEmpty(bot.getBotUsername())) {
throw new TelegramApiException("Bot username can't be empty");
}
bot.onRegister();
webhook.registerWebhook(bot);
}
}
/**
* Checks that the username and token are presented
* @param telegramBot bot to register
* @return False if username or token are empty or null, true otherwise
*/
private boolean validateBotUsernameAndToken(TelegramBot telegramBot) {
return StringUtils.isNotEmpty(telegramBot.getBotToken()) &&
StringUtils.isNotEmpty(telegramBot.getBotUsername());
}
}