From 2d748832b06c97e15d02eaf1a0ffb6055c02fac6 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Tue, 31 May 2016 21:26:58 +0200 Subject: [PATCH] 1. Webhook sample --- ...thub_rubenlagus_TelegramBots_v2_3_3_2.xml} | 8 +- ...benlagus_TelegramBots_v2_3_3_2_alpha_2.xml | 13 --- BotAPi.iml | 2 + src/main/java/org/telegram/Main.java | 102 ++++++++++++------ .../webHookExampleHandlers.java | 24 ++--- 5 files changed, 84 insertions(+), 65 deletions(-) rename .idea/libraries/{Maven__com_github_rubenlagus_TelegramBots_v2_3_3_1.xml => Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml} (66%) delete mode 100644 .idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2_alpha_2.xml diff --git a/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_1.xml b/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml similarity index 66% rename from .idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_1.xml rename to .idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml index 00b2b88..4154829 100644 --- a/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_1.xml +++ b/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml @@ -1,13 +1,13 @@ - + - + - + - + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2_alpha_2.xml b/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2_alpha_2.xml deleted file mode 100644 index 37ba0be..0000000 --- a/.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2_alpha_2.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/BotAPi.iml b/BotAPi.iml index 827dbc7..14a48d3 100644 --- a/BotAPi.iml +++ b/BotAPi.iml @@ -12,6 +12,8 @@ + + diff --git a/src/main/java/org/telegram/Main.java b/src/main/java/org/telegram/Main.java index 0aef971..cc1b17a 100644 --- a/src/main/java/org/telegram/Main.java +++ b/src/main/java/org/telegram/Main.java @@ -4,7 +4,13 @@ import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.TelegramBotsApi; import org.telegram.telegrambots.logging.BotLogger; 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.util.logging.ConsoleHandler; @@ -25,46 +31,76 @@ public class Main { try { BotLogger.registerLogger(new BotsFileHandler()); } 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 { - telegramBotsApi.registerBot(new ChannelHandlers()); - telegramBotsApi.registerBot(new DirectionsHandlers()); - telegramBotsApi.registerBot(new RaeHandlers()); - telegramBotsApi.registerBot(new WeatherHandlers()); - telegramBotsApi.registerBot(new TransifexHandlers()); - telegramBotsApi.registerBot(new FilesHandlers()); - + // 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()); + telegramBotsApi.registerBot(new WeatherHandlers()); + telegramBotsApi.registerBot(new TransifexHandlers()); + telegramBotsApi.registerBot(new FilesHandlers()); } catch (TelegramApiException 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. - // check https://core.telegram.org/bots/self-signed#java-keystore for generating a keypair in store and exporting the pem. - // dont forget to split the pem bundle (begin/end), use only the public key as input! - } else if (!BuildVars.pathToCertificatePublicKey.isEmpty()) { - 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 { + telegramBotsApi = createSelfSignedTelegramBotsApi(); + telegramBotsApi.registerBot(new WebHookExampleHandlers()); + } else { // 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: - // 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) - try { - TelegramBotsApi telegramBotsWebhookApi = new TelegramBotsApi(BuildVars.pathToCertificateStore, BuildVars.certificateStorePassword, BuildVars.EXTERNALWEBHOOKURL, BuildVars.INTERNALWEBHOOKURL); - telegramBotsWebhookApi.registerBot(new webHookExampleHandlers()); - } catch (Exception e) { - BotLogger.error(LOGTAG, e); - } - + telegramBotsApi = createNoSelfSignedTelegramBotsApi(); + telegramBotsApi.registerBot(new WebHookExampleHandlers()); } + 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); } } diff --git a/src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java b/src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java index d9c8a7a..fc73c93 100644 --- a/src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java +++ b/src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java @@ -1,32 +1,29 @@ package org.telegram.updateshandlers; import org.telegram.BotConfig; -import org.telegram.BuildVars; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.TelegramWebhookBot; -import org.telegram.telegrambots.logging.BotLogger; /** - * Created by pithera on 5/31/16. - * Yes this is an ugly example, feel free to supply something nice. + * @author pithera + * @version 1.0 + * @brief Simple Webhook example + * @date 31 of May of 2016 */ -public class webHookExampleHandlers extends TelegramWebhookBot { +public class WebHookExampleHandlers extends TelegramWebhookBot { @Override public BotApiMethod onWebhookUpdateReceived(Update update) { - BotLogger.severe("UPDATE", update.toString()); if (update.hasMessage() && update.getMessage().hasText()) { SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(update.getMessage().getChatId().toString()); - sendMessage.setText("Your webhook works!, this is your callback:\n" + BuildVars.EXTERNALWEBHOOKURL + "/" - + "callback/" + getBotPath()); + sendMessage.setText("Well, all information looks like noise until you break the code."); return sendMessage; } return null; } - @Override public String getBotUsername() { return BotConfig.USERNAMEWEBHOOK; @@ -39,9 +36,6 @@ public class webHookExampleHandlers extends TelegramWebhookBot { @Override public String getBotPath() { - return BotConfig.USERNAMEWEBHOOK; - } //arbitrary path to deliver updates on, username is an example. - - -} - + return BotConfig.USERNAMEWEBHOOK; //arbitrary path to deliver updates on, username is an example. + } +} \ No newline at end of file