Browse Source

Merge pull request #26 from rubenlagus/dev

New webhook sample, travis support and update to last library version
master
Ruben Bermudez 10 years ago
parent
commit
7bda7ca368
  1. 8
      .idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml
  2. 8
      .travis.yml
  3. 2
      BotAPi.iml
  4. 25
      README.md
  5. 122
      pom.xml
  6. 2
      src/main/java/org/telegram/BotConfig.java
  7. 12
      src/main/java/org/telegram/BuildVars.java
  8. 76
      src/main/java/org/telegram/Main.java
  9. 41
      src/main/java/org/telegram/updateshandlers/WebHookExampleHandlers.java

8
.idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_1.xml → .idea/libraries/Maven__com_github_rubenlagus_TelegramBots_v2_3_3_2.xml

@ -1,13 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.1">
<library name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.2">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.1/TelegramBots-v2.3.3.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.1/TelegramBots-v2.3.3.1-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.1/TelegramBots-v2.3.3.1-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2-sources.jar!/" />
</SOURCES>
</library>
</component>

8
.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

2
BotAPi.iml

@ -11,7 +11,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.1" level="project" />
<orderEntry type="library" name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.2" level="project" />
<orderEntry type="library" name="Maven: org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.23" level="project" />
<orderEntry type="library" name="Maven: org.glassfish.hk2.external:javax.inject:2.4.0-b34" level="project" />
<orderEntry type="library" name="Maven: org.glassfish.grizzly:grizzly-http-server:2.3.23" level="project" />

25
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.

122
pom.xml

@ -6,7 +6,7 @@
<groupId>BotApi</groupId>
<artifactId>org.telegram.botapi</artifactId>
<version>2.0</version>
<version>2.1</version>
<repositories>
<repository>
@ -25,7 +25,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<httpcompontents.version>4.5.2</httpcompontents.version>
<telegrambots.version>v2.3.3.1</telegrambots.version>
<telegrambots.version>v2.3.3.2</telegrambots.version>
<json.version>20160212</json.version>
<mysql.version>5.1.39</mysql.version>
</properties>
@ -74,44 +74,87 @@
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>clean-project</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>org.telegram.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.telegram.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>site</phase>
<goals>
<goal>javadoc-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.telegram.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.telegram.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
@ -132,9 +175,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

2
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 = "<token>";
public static final String USERNAMERAE = "raebot";
public static final String TOKENWEBHOOK = "<token>";
public static final String USERNAMEWEBHOOK = "webhooksamplebot";
}

12
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 = "<your-api-key>";

76
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);
}
}

41
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.
}
}
Loading…
Cancel
Save