2018-07-08 01:41:21 +02:00
|
|
|
package org.telegram.telegrambots.meta;
|
2016-11-17 02:41:12 +01:00
|
|
|
|
2021-03-07 04:28:24 +01:00
|
|
|
import com.google.common.base.Strings;
|
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;
|
2021-03-07 04:28:24 +01:00
|
|
|
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;
|
2016-11-17 02:41:12 +01:00
|
|
|
|
2020-11-03 03:57:36 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2016-11-17 02:41:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ruben Bermudez
|
|
|
|
* @version 1.0
|
2020-11-01 23:46:36 +01:00
|
|
|
* Bots manager
|
2016-11-17 02:41:12 +01:00
|
|
|
*/
|
|
|
|
public class TelegramBotsApi {
|
2020-11-03 03:57:36 +01:00
|
|
|
Class<? extends BotSession> botSessionClass;
|
2016-11-17 02:41:12 +01:00
|
|
|
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-11-17 02:41:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2016-11-17 02:41:12 +01:00
|
|
|
*
|
2020-11-03 03:57:36 +01:00
|
|
|
* @implSpec This option may require externally handled HTTPS support (i.e. via proxy)
|
2016-11-17 02:41:12 +01:00
|
|
|
*/
|
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");
|
2016-11-17 02:41:12 +01:00
|
|
|
}
|
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;
|
2016-11-17 02:41:12 +01:00
|
|
|
this.useWebhook = true;
|
2020-11-03 03:57:36 +01:00
|
|
|
this.webhook = webhook;
|
|
|
|
this.webhook.startServer();
|
2016-11-17 02:41:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2021-03-07 04:28:24 +01:00
|
|
|
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");
|
|
|
|
}
|
2020-10-05 16:59:39 +02:00
|
|
|
bot.onRegister();
|
2016-11-17 02:41:12 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-11-17 02:41:12 +01:00
|
|
|
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
|
2021-03-07 04:28:24 +01:00
|
|
|
*
|
|
|
|
* @apiNote The webhook url will be appended with `/callback/bot.getBotPath()` at the end
|
2016-11-17 02:41:12 +01:00
|
|
|
*/
|
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) {
|
2021-03-07 04:28:24 +01:00
|
|
|
throw new TelegramApiException("Parameter setWebhook can not be null");
|
2020-11-03 22:09:02 +01:00
|
|
|
}
|
2016-11-17 02:41:12 +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");
|
|
|
|
}
|
2021-03-07 04:28:24 +01:00
|
|
|
if (!validateBotUsernameAndToken(bot)) {
|
|
|
|
throw new TelegramApiException("Bot token and username can't be empty");
|
|
|
|
}
|
2020-10-05 16:59:39 +02:00
|
|
|
bot.onRegister();
|
2016-11-17 02:41:12 +01:00
|
|
|
webhook.registerWebhook(bot);
|
2020-11-01 23:46:36 +01:00
|
|
|
bot.setWebhook(setWebhook);
|
2016-11-17 02:41:12 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 04:28:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 !Strings.isNullOrEmpty(telegramBot.getBotToken()) &&
|
|
|
|
!Strings.isNullOrEmpty(telegramBot.getBotUsername());
|
|
|
|
}
|
2016-11-17 02:41:12 +01:00
|
|
|
}
|