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/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2b4c7a2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: java +jdk: + - oraclejdk8 +script: mvn clean compile package +notifications: + webhooks: + secure: "L4E4xS1JJcnUDG2oQtBrz/rGxNK8dEbKIRjH6nNHZRGEjGUQh0ECWTEMotX3GDNZ4iIERwZuUS2saLqytlISHPTbpKegZX+YaVk8s9Zuhm/LgrcqtBp7GaxJj+jUx/TdqL/jjuwmstMgzfmMmsx5ALaukAHr1kvP2188XxEiEL9HyORb3hHHDFNOQCsu/evLnewNs1NnHd+OOGm8YLW1ztxE6z5tMx8nvk7jqHEfSkf3KfbZ6QI7fNcelW26b5bPAKyM3b+99Fyz0rXGRSpYw8x15EB4n7hXwQREoL+iZyuF6mKjsdsndiyFoN7UMfKYsQY31OQQKPpcU8OkuXtNqq6F2zfMacQyCfuYYd7szDykI3C4RYVY76PMI1Eym4C4kRYTeTsoyQyp8Lp6daPM0akOCKXvzI7xc9Cp3/EXtRPjncXTA5LJkLiuMwi0BOFBetoei4nOnLPlR1fTwxLVqx13siWl13aIXNIk4axD0PAmi4IAVmsJOLRWxCEvSgS5MQ+M0AZibilQOsZlTipbRovmhe+DkMZifU/mjTkF9DhLjMl1HawFAClell3JcU7IceHwfkZDGE036yvtUi3axcvyR4Sr7qMRno9MOkSbhKp3gQqTVCd7arZrKMhdAYf7PIu9IDUs2+2zlZp2pq2OlMc9c5Z+tXgcX+JqzB3GlHc=" + email: false \ No newline at end of file diff --git a/BotAPi.iml b/BotAPi.iml index a9b350a..827dbc7 100644 --- a/BotAPi.iml +++ b/BotAPi.iml @@ -11,7 +11,7 @@ - + diff --git a/README.md b/README.md index 20a13fc..e185df3 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,18 @@ # Telegram Bot Java Library -A simple to use library to create Telegram Bots in Java (Still Beta Version) +[![Build Status](https://travis-ci.org/rubenlagus/TelegramBotsExample.svg?branch=master)](https://travis-ci.org/rubenlagus/TelegramBotsExample) +[![Telegram](http://trellobot.doomdns.org/telegrambadge.svg)](https://telegram.me/JavaBotsApi) + +Samples for [TelegramBots](https://github.com/rubenlagus/TelegramBots) library ## Translations Bots are now supporting multilanguage. If you want to add your own, feel free to translate at [transifex](https://www.transifex.com/projects/p/telegrambots/) ## Contributions -Feel free to fork this project, work on it and then make a pull request. Most of the times I will accept them if they add something valuable to the code. - -Please, **DO NOT PUSH ANY TOKEN OR API KEY**, I will never accept a pull request with that content. - -## Webhooks vs GetUpdates -Both ways are supported (but I still didn't tested webhooks). To change between them, just go to *BuildVars.java* and change variable *useWebHook* value. - -I recommend using getUpdates methods. Webhooks a bit less trustful +Feel free to fork this project, work on it and then make a pull request agains **dev** branch. -## Example bots -Open them and send them */help* command to get some information about their capabilities: +Most of the times I will accept them if they add something valuable to the code. -https://telegram.me/weatherbot - -https://telegram.me/directionsbot - -https://telegram.me/filesbot - -https://telegram.me/TGlanguagesbot +Please, **DO NOT PUSH ANY TOKEN OR API KEY**, I will never accept a pull request with that content. ## Telegram Bot API This library use [Telegram bot API](https://core.telegram.org/bots), you can find more information following the link. diff --git a/pom.xml b/pom.xml index f9ccb4e..1982eb8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ BotApi org.telegram.botapi - 2.0 + 2.1 @@ -25,7 +25,7 @@ UTF-8 UTF-8 4.5.2 - v2.3.3.1 + v2.3.3.2 20160212 5.1.39 @@ -74,44 +74,87 @@ ${project.artifactId}-${project.version} ${project.build.directory}/test-classes ${project.basedir}/src/main/java + + + maven-clean-plugin + 3.0.0 + + + clean-project + clean + + clean + + + + + + maven-assembly-plugin + 2.6 + + + + org.telegram.Main + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.0 + + + attach-sources + verify + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + true + org.telegram.Main + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + site + + javadoc-no-fork + + + + + - - org.apache.maven.plugins - maven-jar-plugin - 2.4 - - - - true - lib/ - org.telegram.Main - - - - - - maven-assembly-plugin - - - - org.telegram.Main - - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - org.apache.maven.plugins maven-dependency-plugin @@ -132,9 +175,6 @@ - - maven-clean-plugin - org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/org/telegram/BotConfig.java b/src/main/java/org/telegram/BotConfig.java index 9f56e8e..353e876 100644 --- a/src/main/java/org/telegram/BotConfig.java +++ b/src/main/java/org/telegram/BotConfig.java @@ -19,4 +19,6 @@ public class BotConfig { public static final String USERNAMECHANNEL = "channelupdatesbot"; public static final String TOKENRAE = ""; public static final String USERNAMERAE = "raebot"; + public static final String TOKENWEBHOOK = ""; + public static final String USERNAMEWEBHOOK = "webhooksamplebot"; } diff --git a/src/main/java/org/telegram/BuildVars.java b/src/main/java/org/telegram/BuildVars.java index 9a973a0..3fc237f 100644 --- a/src/main/java/org/telegram/BuildVars.java +++ b/src/main/java/org/telegram/BuildVars.java @@ -6,14 +6,16 @@ package org.telegram; * @brief Custom build vars FILL EVERYTHING CORRECTLY * @date 20 of June of 2015 */ + public class BuildVars { public static final Boolean debug = true; - public static final Boolean useWebHook = true; + public static final Boolean useWebHook = false; public static final int PORT = 8443; - public static final String EXTERNALWEBHOOKURL = "your-external-url:" + PORT; - public static final String INTERNALWEBHOOKURL = "your-internal-url:" + PORT; - public static final String pathToCertificatePublicKey = "path/to/my/certkey.pem"; - public static final String certificatePublicKeyFileName = "certkey.pem"; + public static final String EXTERNALWEBHOOKURL = "https://example.changeme.com:" + PORT; // https://(xyz.)externaldomain.tld + public static final String INTERNALWEBHOOKURL = "https://localhost.changeme.com:" + PORT; // https://(xyz.)localip/domain(.tld) + public static final String pathToCertificatePublicKey = "./YOURPEM.pem"; //only for self-signed webhooks + public static final String pathToCertificateStore = "./YOURSTORE.jks"; //self-signed and non-self-signed. + public static final String certificateStorePassword = "yourpass"; //password for your certificate-store public static final String OPENWEATHERAPIKEY = ""; diff --git a/src/main/java/org/telegram/Main.java b/src/main/java/org/telegram/Main.java index 474e4ff..cc1b17a 100644 --- a/src/main/java/org/telegram/Main.java +++ b/src/main/java/org/telegram/Main.java @@ -10,6 +10,7 @@ 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; @@ -30,19 +31,76 @@ public class Main { try { BotLogger.registerLogger(new BotsFileHandler()); } catch (IOException e) { - BotLogger.severe("MAIN", e); + BotLogger.severe(LOGTAG, e); } - TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); 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()); - } catch (TelegramApiException e) { + TelegramBotsApi telegramBotsApi = createTelegramBotsApi(); + try { + // 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. + 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. + 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 new file mode 100644 index 0000000..f72ef5a --- /dev/null +++ b/src/main/java/org/telegram/updateshandlers/WebHookExampleHandlers.java @@ -0,0 +1,41 @@ +package org.telegram.updateshandlers; + +import org.telegram.BotConfig; +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; + +/** + * @author pithera + * @version 1.0 + * @brief Simple Webhook example + * @date 31 of May of 2016 + */ +public class WebHookExampleHandlers extends TelegramWebhookBot { + @Override + public BotApiMethod onWebhookUpdateReceived(Update update) { + if (update.hasMessage() && update.getMessage().hasText()) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(update.getMessage().getChatId().toString()); + sendMessage.setText("Well, all information looks like noise until you break the code."); + return sendMessage; + } + return null; + } + + @Override + public String getBotUsername() { + return BotConfig.USERNAMEWEBHOOK; + } + + @Override + public String getBotToken() { + return BotConfig.TOKENWEBHOOK; + } + + @Override + public String getBotPath() { + return BotConfig.USERNAMEWEBHOOK; //arbitrary path to deliver updates on, username is an example. + } +}