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

86 lines
3.0 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta;
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.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 {
private static final String webhookUrlFormat = "{0}callback/";
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 {
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
*/
2020-11-01 23:46:36 +01:00
public void registerBot(WebhookBot bot, SetWebhook setWebhook) throws TelegramApiException {
if (useWebhook) {
bot.onRegister();
webhook.registerWebhook(bot);
2020-11-01 23:46:36 +01:00
bot.setWebhook(setWebhook);
}
}
}