|
|
@ -4,7 +4,13 @@ import org.telegram.telegrambots.TelegramApiException; |
|
|
import org.telegram.telegrambots.TelegramBotsApi; |
|
|
import org.telegram.telegrambots.TelegramBotsApi; |
|
|
import org.telegram.telegrambots.logging.BotLogger; |
|
|
import org.telegram.telegrambots.logging.BotLogger; |
|
|
import org.telegram.telegrambots.logging.BotsFileHandler; |
|
|
import org.telegram.telegrambots.logging.BotsFileHandler; |
|
|
import org.telegram.updateshandlers.*; |
|
|
import org.telegram.updateshandlers.ChannelHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.DirectionsHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.FilesHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.RaeHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.TransifexHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.WeatherHandlers; |
|
|
|
|
|
import org.telegram.updateshandlers.WebHookExampleHandlers; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.util.logging.ConsoleHandler; |
|
|
import java.util.logging.ConsoleHandler; |
|
|
@ -25,46 +31,76 @@ public class Main { |
|
|
try { |
|
|
try { |
|
|
BotLogger.registerLogger(new BotsFileHandler()); |
|
|
BotLogger.registerLogger(new BotsFileHandler()); |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
BotLogger.severe("MAIN", e); |
|
|
BotLogger.severe(LOGTAG, e); |
|
|
} |
|
|
} |
|
|
// default, start all sample bots in getUpdates mode
|
|
|
|
|
|
if (!BuildVars.useWebHook) { |
|
|
|
|
|
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
TelegramBotsApi telegramBotsApi = createTelegramBotsApi(); |
|
|
try { |
|
|
try { |
|
|
telegramBotsApi.registerBot(new ChannelHandlers()); |
|
|
// Register long polling bots. They work regardless type of TelegramBotsApi we are creating
|
|
|
telegramBotsApi.registerBot(new DirectionsHandlers()); |
|
|
telegramBotsApi.registerBot(new ChannelHandlers()); |
|
|
telegramBotsApi.registerBot(new RaeHandlers()); |
|
|
telegramBotsApi.registerBot(new DirectionsHandlers()); |
|
|
telegramBotsApi.registerBot(new WeatherHandlers()); |
|
|
telegramBotsApi.registerBot(new RaeHandlers()); |
|
|
telegramBotsApi.registerBot(new TransifexHandlers()); |
|
|
telegramBotsApi.registerBot(new WeatherHandlers()); |
|
|
telegramBotsApi.registerBot(new FilesHandlers()); |
|
|
telegramBotsApi.registerBot(new TransifexHandlers()); |
|
|
|
|
|
telegramBotsApi.registerBot(new FilesHandlers()); |
|
|
} catch (TelegramApiException e) { |
|
|
} catch (TelegramApiException e) { |
|
|
BotLogger.error(LOGTAG, e); |
|
|
BotLogger.error(LOGTAG, e); |
|
|
} |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
BotLogger.error(LOGTAG, e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static TelegramBotsApi createTelegramBotsApi() throws TelegramApiException { |
|
|
|
|
|
TelegramBotsApi telegramBotsApi; |
|
|
|
|
|
if (!BuildVars.useWebHook) { |
|
|
|
|
|
// Default (long polling only)
|
|
|
|
|
|
telegramBotsApi = createLongPollingTelegramBotsApi(); |
|
|
|
|
|
} else if (!BuildVars.pathToCertificatePublicKey.isEmpty()) { |
|
|
// 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.
|
|
|
// 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.
|
|
|
// check https://core.telegram.org/bots/self-signed#java-keystore for generating a keypair in store and exporting the pem.
|
|
|
telegramBotsApi = createSelfSignedTelegramBotsApi(); |
|
|
// dont forget to split the pem bundle (begin/end), use only the public key as input!
|
|
|
telegramBotsApi.registerBot(new WebHookExampleHandlers()); |
|
|
} else if (!BuildVars.pathToCertificatePublicKey.isEmpty()) { |
|
|
} else { |
|
|
try { |
|
|
|
|
|
TelegramBotsApi telegramBotsSelfWebhookApi = new TelegramBotsApi(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL,BuildVars.pathToCertificatePublicKey); |
|
|
|
|
|
telegramBotsSelfWebhookApi.registerBot(new webHookExampleHandlers()); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
BotLogger.error(LOGTAG, e); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// Non self signed, make sure you've added private/public and if needed intermediate to your cert-store.
|
|
|
// Non self signed, make sure you've added private/public and if needed intermediate to your cert-store.
|
|
|
// Coming from a set of pem files here's one way to do it:
|
|
|
telegramBotsApi = createNoSelfSignedTelegramBotsApi(); |
|
|
// openssl pkcs12 -export -in public.pem -inkey private.pem > keypair.p12
|
|
|
telegramBotsApi.registerBot(new WebHookExampleHandlers()); |
|
|
// 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)
|
|
|
|
|
|
try { |
|
|
|
|
|
TelegramBotsApi telegramBotsWebhookApi = new TelegramBotsApi(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL); |
|
|
|
|
|
telegramBotsWebhookApi.registerBot(new webHookExampleHandlers()); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
BotLogger.error(LOGTAG, e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return telegramBotsApi; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @brief Creates a Telegram Bots Api to use Long Polling (getUpdates) bots. |
|
|
|
|
|
* @return TelegramBotsApi to register the bots. |
|
|
|
|
|
*/ |
|
|
|
|
|
private static TelegramBotsApi createLongPollingTelegramBotsApi() { |
|
|
|
|
|
return new TelegramBotsApi(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @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(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL, BuildVars.pathToCertificatePublicKey); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @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(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|