Fix webhooks (unstable)

This commit is contained in:
Rubenlagus 2016-05-27 14:11:11 +02:00
parent b07bf5531b
commit 2825090112
3 changed files with 40 additions and 17 deletions

View File

@ -6,7 +6,7 @@
<packaging>jar</packaging>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.3.3.1</version>
<version>2.3.3.2</version>
<name>Telegram Bots</name>
<url>https://telegram.me/JavaBotsApi</url>

View File

@ -21,6 +21,7 @@ import org.telegram.telegrambots.updatesreceivers.Webhook;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import static org.telegram.telegrambots.Constants.ERRORCODEFIELD;
import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
@ -32,11 +33,11 @@ import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
* @date 14 of January of 2016
*/
public class TelegramBotsApi {
private static final String webhookUrlFormat = "{0}callback/";
private boolean useWebhook; ///<
private Webhook webhook; ///<
private String extrenalUrl; ///<
private String pathToCertificate; ///<
private String publicCertificateName; ///<
/**
*
@ -52,6 +53,13 @@ public class TelegramBotsApi {
* @param internalUrl
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiException("Parameter internalUrl can not be null or empty");
}
this.useWebhook = true;
this.extrenalUrl = fixExternalUrl(externalUrl);
webhook = new Webhook(keyStore, keyStorePassword, internalUrl);
@ -64,14 +72,18 @@ public class TelegramBotsApi {
* @param keyStorePassword
* @param externalUrl
* @param internalUrl
* @param pathToCertificate
* @param publicCertificateName
* @param pathToCertificate Full path until .pem public certificate keys
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate, String publicCertificateName) throws TelegramApiException {
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiException("Parameter internalUrl can not be null or empty");
}
this.useWebhook = true;
this.extrenalUrl = fixExternalUrl(externalUrl);
this.pathToCertificate = pathToCertificate;
this.publicCertificateName = publicCertificateName;
webhook = new Webhook(keyStore, keyStorePassword, internalUrl);
webhook.startServer();
}
@ -85,7 +97,7 @@ public class TelegramBotsApi {
if (externalUrl != null && !externalUrl.endsWith("/")) {
externalUrl = externalUrl + "/";
}
return externalUrl;
return MessageFormat.format(webhookUrlFormat, externalUrl);
}
/**
@ -96,7 +108,7 @@ public class TelegramBotsApi {
* @param publicCertificateName
* @throws TelegramApiException
*/
private static void setWebhook(String webHookURL, String botToken, String publicCertificatePath, String publicCertificateName) throws TelegramApiException {
private static void setWebhook(String webHookURL, String botToken, String publicCertificatePath) throws TelegramApiException {
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
@ -104,7 +116,10 @@ public class TelegramBotsApi {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SetWebhook.URL_FIELD, webHookURL);
if (publicCertificatePath != null) {
builder.addBinaryBody(SetWebhook.CERTIFICATE_FIELD, new File(publicCertificatePath), ContentType.APPLICATION_OCTET_STREAM, publicCertificateName);
File certificate = new File(publicCertificatePath);
if (certificate.exists()) {
builder.addBinaryBody(SetWebhook.CERTIFICATE_FIELD, certificate, ContentType.TEXT_PLAIN, certificate.getName());
}
}
HttpEntity multipart = builder.build();
httppost.setEntity(multipart);
@ -129,7 +144,7 @@ public class TelegramBotsApi {
* @param bot the bot to register
*/
public BotSession registerBot(TelegramLongPollingBot bot) throws TelegramApiException {
setWebhook(bot.getBotToken());
setWebhook(bot.getBotToken(), null);
return new BotSession(bot.getBotToken(), bot);
}
@ -140,7 +155,7 @@ public class TelegramBotsApi {
public void registerBot(TelegramWebhookBot bot) throws TelegramApiException {
if (useWebhook) {
webhook.registerWebhook(bot);
setWebhook(bot.getBotToken());
setWebhook(bot.getBotToken(), bot.getBotPath());
}
}
@ -148,10 +163,11 @@ public class TelegramBotsApi {
*
* @param botToken
*/
private void setWebhook(String botToken) throws TelegramApiException {
private void setWebhook(String botToken, String urlPath) throws TelegramApiException {
if (botToken == null) {
throw new TelegramApiException("Parameter botToken can not be null");
}
setWebhook(extrenalUrl == null ? "" : extrenalUrl, botToken, pathToCertificate, publicCertificateName);
String completeExternalUrl = urlPath == null ? "" : extrenalUrl + urlPath;
setWebhook(completeExternalUrl, botToken, pathToCertificate);
}
}

View File

@ -10,6 +10,7 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.bots.ITelegramWebhookBot;
import java.io.File;
import java.io.IOException;
import java.net.URI;
@ -20,17 +21,16 @@ import java.net.URI;
* @date 20 of June of 2015
*/
public class Webhook {
private static final String LOGTAG = "WEBHOOK";
private final String KEYSTORE_SERVER_FILE;
private final String KEYSTORE_SERVER_PWD;
private final RestApi restApi;
private final String internalUrl;
public Webhook(String keyStore, String keyStorePassword, String internalUrl) {
public Webhook(String keyStore, String keyStorePassword, String internalUrl) throws TelegramApiException {
this.KEYSTORE_SERVER_FILE = keyStore;
this.KEYSTORE_SERVER_PWD = keyStorePassword;
validateServerKeystoreFile(keyStore);
this.internalUrl = internalUrl;
this.restApi = new RestApi();
}
@ -65,4 +65,11 @@ public class Webhook {
private URI getBaseURI() {
return URI.create(internalUrl);
}
private static void validateServerKeystoreFile(String keyStore) throws TelegramApiException {
File file = new File(keyStore);
if (!file.exists() || !file.canRead()) {
throw new TelegramApiException("Can't find or access server keystore file.");
}
}
}