Browse Source

Webhook example

master
dapoldi 10 years ago
parent
commit
34bcebcc4b
  1. 2
      BotAPi.iml
  2. 2
      pom.xml
  3. 2
      src/main/java/org/telegram/BotConfig.java
  4. 12
      src/main/java/org/telegram/BuildVars.java
  5. 36
      src/main/java/org/telegram/Main.java
  6. 47
      src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java

2
BotAPi.iml

@ -11,7 +11,7 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.2-alpha-2" 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.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.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" /> <orderEntry type="library" name="Maven: org.glassfish.grizzly:grizzly-http-server:2.3.23" level="project" />

2
pom.xml

@ -25,7 +25,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<httpcompontents.version>4.5.2</httpcompontents.version> <httpcompontents.version>4.5.2</httpcompontents.version>
<telegrambots.version>v2.3.3.2-alpha-2</telegrambots.version> <telegrambots.version>v2.3.3.2</telegrambots.version>
<json.version>20160212</json.version> <json.version>20160212</json.version>
<mysql.version>5.1.39</mysql.version> <mysql.version>5.1.39</mysql.version>
</properties> </properties>

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 USERNAMECHANNEL = "channelupdatesbot";
public static final String TOKENRAE = "<token>"; public static final String TOKENRAE = "<token>";
public static final String USERNAMERAE = "raebot"; 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 * @brief Custom build vars FILL EVERYTHING CORRECTLY
* @date 20 of June of 2015 * @date 20 of June of 2015
*/ */
public class BuildVars { public class BuildVars {
public static final Boolean debug = true; 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 int PORT = 8443;
public static final String EXTERNALWEBHOOKURL = "your-external-url:" + PORT; public static final String EXTERNALWEBHOOKURL = "https://example.changeme.com:" + PORT; // https://(xyz.)externaldomain.tld
public static final String INTERNALWEBHOOKURL = "your-internal-url:" + PORT; public static final String INTERNALWEBHOOKURL = "https://localhost.changeme.com:" + PORT; // https://(xyz.)localip/domain(.tld)
public static final String pathToCertificatePublicKey = "path/to/my/certkey.pem"; public static final String pathToCertificatePublicKey = "./YOURPEM.pem"; //only for self-signed webhooks
public static final String certificatePublicKeyFileName = "certkey.pem"; 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>"; public static final String OPENWEATHERAPIKEY = "<your-api-key>";

36
src/main/java/org/telegram/Main.java

@ -4,12 +4,7 @@ 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.ChannelHandlers; import org.telegram.updateshandlers.*;
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 java.io.IOException; import java.io.IOException;
import java.util.logging.ConsoleHandler; import java.util.logging.ConsoleHandler;
@ -32,8 +27,10 @@ public class Main {
} catch (IOException e) { } catch (IOException e) {
BotLogger.severe("MAIN", e); BotLogger.severe("MAIN", e);
} }
// default, start all sample bots in getUpdates mode
if (!BuildVars.useWebHook) {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try { try {
telegramBotsApi.registerBot(new ChannelHandlers()); telegramBotsApi.registerBot(new ChannelHandlers());
telegramBotsApi.registerBot(new DirectionsHandlers()); telegramBotsApi.registerBot(new DirectionsHandlers());
@ -41,8 +38,33 @@ public class Main {
telegramBotsApi.registerBot(new WeatherHandlers()); telegramBotsApi.registerBot(new WeatherHandlers());
telegramBotsApi.registerBot(new TransifexHandlers()); telegramBotsApi.registerBot(new TransifexHandlers());
telegramBotsApi.registerBot(new FilesHandlers()); telegramBotsApi.registerBot(new FilesHandlers());
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
BotLogger.error(LOGTAG, e); BotLogger.error(LOGTAG, e);
} }
// 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 {
// 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);
}
}
} }
} }

47
src/main/java/org/telegram/updateshandlers/webHookExampleHandlers.java

@ -0,0 +1,47 @@
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.
*/
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());
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