Compare commits

...

4 Commits

Author SHA1 Message Date
Andrea Cavalli
6e3c41f7c4 Fix grizzly server null check 2024-03-02 22:41:11 +01:00
Andrea Cavalli
dee9941a1f Add closeable webhook 2024-03-02 21:27:50 +01:00
Andrea Cavalli
4b4364f32d Fix modules 2024-02-14 12:01:46 +01:00
Andrea Cavalli
72af8d36ae Add custom method 2024-02-14 11:54:21 +01:00
5 changed files with 41 additions and 4 deletions

View File

@ -237,7 +237,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version> <version>3.12.1</version>
<configuration> <configuration>
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>

View File

@ -34,10 +34,10 @@ module telegrambots.meta {
exports org.telegram.telegrambots.meta.api.interfaces; exports org.telegram.telegrambots.meta.api.interfaces;
exports org.telegram.telegrambots.meta.api.objects.inlinequery.inputmessagecontent; exports org.telegram.telegrambots.meta.api.objects.inlinequery.inputmessagecontent;
exports org.telegram.telegrambots.meta.api.objects.inlinequery.result; exports org.telegram.telegrambots.meta.api.objects.inlinequery.result;
requires com.google.common;
requires static lombok; requires static lombok;
requires com.fasterxml.jackson.annotation; requires com.fasterxml.jackson.annotation;
requires com.fasterxml.jackson.core; requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind; requires com.fasterxml.jackson.databind;
requires org.slf4j; requires org.slf4j;
requires org.apache.commons.lang3;
} }

View File

@ -100,6 +100,25 @@ public class TelegramBotsApi {
} }
} }
/**
* Register a bot in the api that will receive updates using webhook method
* @param bot Bot to register
*
* @apiNote The webhook url will be appended with `/callback/bot.getBotPath()` at the end
*/
public void registerBotWithoutToken(WebhookBot bot) throws TelegramApiException {
if (useWebhook) {
if (webhook == null) {
throw new TelegramApiException("This instance doesn't support Webhook bot, use correct constructor");
}
if (StringUtils.isEmpty(bot.getBotUsername())) {
throw new TelegramApiException("Bot username can't be empty");
}
bot.onRegister();
webhook.registerWebhook(bot);
}
}
/** /**
* Checks that the username and token are presented * Checks that the username and token are presented
* @param telegramBot bot to register * @param telegramBot bot to register

View File

@ -18,7 +18,6 @@ module telegrambots {
requires jersey.server; requires jersey.server;
requires org.apache.commons.compress; requires org.apache.commons.compress;
requires jersey.common; requires jersey.common;
requires com.google.common;
requires jakarta.annotation; requires jakarta.annotation;
requires jakarta.activation; requires jakarta.activation;
requires jakarta.xml.bind; requires jakarta.xml.bind;

View File

@ -1,5 +1,8 @@
package org.telegram.telegrambots.updatesreceivers; package org.telegram.telegrambots.updatesreceivers;
import java.io.Closeable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.ssl.SSLContextConfigurator; import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator; import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
@ -19,12 +22,13 @@ import java.net.URI;
* @version 1.0 * @version 1.0
* Webhook to receive updates * Webhook to receive updates
*/ */
public class DefaultWebhook implements Webhook { public class DefaultWebhook implements Webhook, Closeable {
private String keystoreServerFile; private String keystoreServerFile;
private String keystoreServerPwd; private String keystoreServerPwd;
private String internalUrl; private String internalUrl;
private final RestApi restApi; private final RestApi restApi;
private HttpServer grizzlyServer;
public DefaultWebhook() { public DefaultWebhook() {
this.restApi = new RestApi(); this.restApi = new RestApi();
@ -67,6 +71,7 @@ public class DefaultWebhook implements Webhook {
} }
try { try {
this.grizzlyServer = grizzlyServer;
grizzlyServer.start(); grizzlyServer.start();
} catch (IOException e) { } catch (IOException e) {
throw new TelegramApiException("Error starting webhook server", e); throw new TelegramApiException("Error starting webhook server", e);
@ -83,4 +88,18 @@ public class DefaultWebhook implements Webhook {
throw new TelegramApiException("Can't find or access server keystore file."); throw new TelegramApiException("Can't find or access server keystore file.");
} }
} }
@Override
public void close() throws IOException {
if (grizzlyServer != null) {
try {
grizzlyServer.shutdown(1, TimeUnit.MINUTES).get();
} catch (ExecutionException ex) {
throw new IOException("Failed to stop grizzly server", ex.getCause());
} catch (InterruptedException e) {
// timeout
}
grizzlyServer.shutdownNow();
}
}
} }