You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.2 KiB

11 years ago
package org.telegram;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import org.telegram.telegrambots.updatesreceivers.DefaultWebhook;
import org.telegram.updateshandlers.WeatherHandlers;
import org.telegram.updateshandlers.WebHookExampleHandlers;
11 years ago
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Main class to create all bots
* @date 20 of June of 2015
*/
@Slf4j
11 years ago
public class Main {
public static void main(String[] args) {
10 years ago
try {
TelegramBotsApi telegramBotsApi = createTelegramBotsApi();
10 years ago
try {
10 years ago
// Register long polling bots. They work regardless type of TelegramBotsApi we are creating
// telegramBotsApi.registerBot(new ChannelHandlers());
// telegramBotsApi.registerBot(new DirectionsHandlers());
// telegramBotsApi.registerBot(new RaeHandlers());
10 years ago
telegramBotsApi.registerBot(new WeatherHandlers());
// telegramBotsApi.registerBot(new TransifexHandlers());
// telegramBotsApi.registerBot(new FilesHandlers());
// telegramBotsApi.registerBot(new CommandsHandler(BotConfig.COMMANDS_USER));
// telegramBotsApi.registerBot(new ElektrollArtFanHandler());
10 years ago
} catch (TelegramApiException e) {
log.error(e.getLocalizedMessage(), e);
10 years ago
}
10 years ago
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
10 years ago
}
}
private static TelegramBotsApi createTelegramBotsApi() throws TelegramApiException {
TelegramBotsApi telegramBotsApi;
if (!BuildVars.useWebHook) {
// Default (long polling only)
telegramBotsApi = createLongPollingTelegramBotsApi();
} else if (!BuildVars.pathToCertificatePublicKey.isEmpty()) {
10 years ago
// Filled a path to a pem file ? looks like you're going for the self signed option then, invoke with store and pem file to supply.
10 years ago
telegramBotsApi = createSelfSignedTelegramBotsApi();
telegramBotsApi.registerBot(new WebHookExampleHandlers(), null);
10 years ago
} else {
10 years ago
// Non self signed, make sure you've added private/public and if needed intermediate to your cert-store.
10 years ago
telegramBotsApi = createNoSelfSignedTelegramBotsApi();
telegramBotsApi.registerBot(new WebHookExampleHandlers(), null);
}
10 years ago
return telegramBotsApi;
}
/**
* @brief Creates a Telegram Bots Api to use Long Polling (getUpdates) bots.
* @return TelegramBotsApi to register the bots.
*/
private static TelegramBotsApi createLongPollingTelegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(DefaultBotSession.class);
10 years ago
}
/**
* @brief Creates a Telegram Bots Api to use Long Polling bots and webhooks bots with self-signed certificates.
* @return TelegramBotsApi to register the bots.
*
* @note https://core.telegram.org/bots/self-signed#java-keystore for generating a keypair in store and exporting the pem.
* @note Don't forget to split the pem bundle (begin/end), use only the public key as input!
*/
private static TelegramBotsApi createSelfSignedTelegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(DefaultBotSession.class, new DefaultWebhook());
10 years ago
}
/**
* @brief Creates a Telegram Bots Api to use Long Polling bots and webhooks bots with no-self-signed certificates.
* @return TelegramBotsApi to register the bots.
*
* @note Coming from a set of pem files here's one way to do it:
* @code{.sh}
* openssl pkcs12 -export -in public.pem -inkey private.pem > keypair.p12
* keytool -importkeystore -srckeystore keypair.p12 -destkeystore server.jks -srcstoretype pkcs12
* #have (an) intermediate(s) to supply? first:
* cat public.pem intermediate.pem > set.pem (use set.pem as -in)
* @endcode
*/
private static TelegramBotsApi createNoSelfSignedTelegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(DefaultBotSession.class, new DefaultWebhook());
11 years ago
}
}