diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md
index 418a5d26..70675239 100644
--- a/TelegramBots.wiki/Changelog.md
+++ b/TelegramBots.wiki/Changelog.md
@@ -7,6 +7,7 @@
6. When registering a Webhook Bot, a SetWebhook object must be provided.
7. When using Webhook with Spring, extends class SpringWebhookBot instead of WebhookBot
8. New Async methods returning CompletableFutures.
+9. No more Guice to define custom class
### 4.9.2 ###
1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814
diff --git a/TelegramBots.wiki/abilities/Simple-Example.md b/TelegramBots.wiki/abilities/Simple-Example.md
index 0a4e2567..7c3b7326 100644
--- a/TelegramBots.wiki/abilities/Simple-Example.md
+++ b/TelegramBots.wiki/abilities/Simple-Example.md
@@ -81,11 +81,8 @@ Running the bot is just like running the regular Telegram bots. Create a Java cl
```java
public class Application {
public static void main(String[] args) {
- // Initializes dependencies necessary for the base bot - Guice
- ApiContextInitializer.init();
-
// Create the TelegramBotsApi object to register your bots
- TelegramBotsApi botsApi = new TelegramBotsApi();
+ TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
try {
// Register your newly created AbilityBot
diff --git a/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java b/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java
index 1cceff89..2c6751bb 100644
--- a/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java
+++ b/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java
@@ -6,7 +6,6 @@ import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.telegram.telegrambots.bots.DefaultBotOptions;
-import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
@@ -24,7 +23,7 @@ public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingB
}
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
- this(chatIdConverter, ApiContext.getInstance(DefaultBotOptions.class));
+ this(chatIdConverter, new DefaultBotOptions());
}
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions){
diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java
index 24aadaf0..117a7d4e 100644
--- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java
+++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java
@@ -1,7 +1,6 @@
package org.telegram.telegrambots.extensions.bots.commandbot;
-import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
@@ -29,7 +28,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
*
*/
public TelegramLongPollingCommandBot() {
- this(ApiContext.getInstance(DefaultBotOptions.class));
+ this(new DefaultBotOptions());
}
/**
diff --git a/telegrambots-meta/pom.xml b/telegrambots-meta/pom.xml
index 0410cf24..165f91f5 100644
--- a/telegrambots-meta/pom.xml
+++ b/telegrambots-meta/pom.xml
@@ -69,7 +69,6 @@
UTF-8
UTF-8
- 4.2.3
2.11.3
2.11.3
20180813
@@ -77,17 +76,6 @@
-
- com.google.inject
- guice
- ${guice.version}
-
-
- com.google.guava
- guava
-
-
-
com.fasterxml.jackson.core
jackson-databind
diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/ApiContext.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/ApiContext.java
deleted file mode 100644
index 3859fa91..00000000
--- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/ApiContext.java
+++ /dev/null
@@ -1,67 +0,0 @@
-package org.telegram.telegrambots.meta;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Singleton;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.text.MessageFormat;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author Ruben Bermudez
- * @version 1.0
- */
-public class ApiContext {
- private static final Logger log = LoggerFactory.getLogger(ApiContext.class);
-
- private static final Object lock = new Object();
- private static Injector INJECTOR;
- private static Map bindings = new HashMap<>();
- private static Map singletonBindings = new HashMap<>();
-
- public static T getInstance(Class type) {
- return getInjector().getInstance(type);
- }
-
- public static void register(Class type, Class implementation) {
- if (bindings.containsKey(type)) {
- log.debug(MessageFormat.format("Class {0} already registered", type.getName()));
- }
- bindings.put(type, implementation);
- }
-
- public static void registerSingleton(Class type, Class implementation) {
- if (singletonBindings.containsKey(type)) {
- log.debug(MessageFormat.format("Class {0} already registered", type.getName()));
- }
- singletonBindings.put(type, implementation);
- }
-
- private static Injector getInjector() {
- if (INJECTOR == null) {
- synchronized (lock) {
- if (INJECTOR == null) {
- INJECTOR = Guice.createInjector(new ApiModule());
- }
- }
- }
- return INJECTOR;
- }
-
- @SuppressWarnings("unchecked")
- private static class ApiModule extends AbstractModule {
- @Override
- protected void configure() {
- for (Map.Entry binding : bindings.entrySet()) {
- bind(binding.getKey()).to(binding.getValue());
- }
- for (Map.Entry binding : singletonBindings.entrySet()) {
- bind(binding.getKey()).to(binding.getValue()).in(Singleton.class);
- }
- }
- }
-}
diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/TelegramBotsApi.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/TelegramBotsApi.java
index bd574a87..2642d7b9 100644
--- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/TelegramBotsApi.java
+++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/TelegramBotsApi.java
@@ -2,13 +2,12 @@ package org.telegram.telegrambots.meta;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
-import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;
-import java.text.MessageFormat;
+import java.lang.reflect.InvocationTargetException;
/**
* @author Ruben Bermudez
@@ -17,110 +16,53 @@ import java.text.MessageFormat;
*/
public class TelegramBotsApi {
private static final String webhookUrlFormat = "{0}callback/";
+
+ Class extends BotSession> botSessionClass;
private boolean useWebhook; ///< True to enable webhook usage
private Webhook webhook; ///< Webhook instance
- private String externalUrl; ///< External url of the bots
- private String pathToCertificate; ///< Path to public key certificate
/**
*
*/
- public TelegramBotsApi() {
+ public TelegramBotsApi(Class extends BotSession> botSessionClass) throws TelegramApiException {
+ if (botSessionClass == null) {
+ throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
+ }
+ this.botSessionClass = botSessionClass;
}
/**
* Creates an HTTP server to receive webhook request
- * @param externalUrl External base url for the webhook
- * @param internalUrl Internal base url for the webhook
+ * @param webhook Webhook class
*
- * @implSpec This option requires externally handled HTTPS support (i.e. via proxy)
+ * @implSpec This option may require externally handled HTTPS support (i.e. via proxy)
*/
- public TelegramBotsApi(String externalUrl, String internalUrl) throws TelegramApiRequestException {
- if (externalUrl == null || externalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
+ public TelegramBotsApi(Class extends BotSession> botSessionClass, Webhook webhook) throws TelegramApiException {
+ if (botSessionClass == null) {
+ throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
- if (internalUrl == null || internalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
+ if (webhook == null) {
+ throw new TelegramApiException("Parameter webhook can not be null or empty");
}
-
+ this.botSessionClass = botSessionClass;
this.useWebhook = true;
- this.externalUrl = fixExternalUrl(externalUrl);
- webhook = ApiContext.getInstance(Webhook.class);
- webhook.setInternalUrl(internalUrl);
- webhook.startServer();
- }
-
- /**
- * Creates an HTTPS server to receive webhook request
- * @param keyStore KeyStore for the server
- * @param keyStorePassword Key store password for the server
- * @param externalUrl External base url for the webhook
- * @param internalUrl Internal base url for the webhook
- */
- public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiRequestException {
- if (externalUrl == null || externalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
- }
- if (internalUrl == null || internalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
- }
- if (keyStore == null || keyStore.isEmpty()) {
- throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
- }
- if (keyStorePassword == null || keyStorePassword.isEmpty()) {
- throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
- }
-
- this.useWebhook = true;
- this.externalUrl = fixExternalUrl(externalUrl);
- webhook = ApiContext.getInstance(Webhook.class);
- webhook.setInternalUrl(internalUrl);
- webhook.setKeyStore(keyStore, keyStorePassword);
- webhook.startServer();
- }
-
- /**
- * Creates an HTTPS server with self-signed certificate to receive webhook request
- * @param keyStore KeyStore for the server
- * @param keyStorePassword Key store password for the server
- * @param externalUrl External base url for the webhook
- * @param internalUrl Internal base url for the webhook
- * @param pathToCertificate Full path until .pem public certificate keys
- */
- public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiRequestException {
- if (externalUrl == null || externalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
- }
- if (internalUrl == null || internalUrl.isEmpty()) {
- throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
- }
- if (keyStore == null || keyStore.isEmpty()) {
- throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
- }
- if (keyStorePassword == null || keyStorePassword.isEmpty()) {
- throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
- }
- if (pathToCertificate == null || pathToCertificate.isEmpty()) {
- throw new TelegramApiRequestException("Parameter pathToCertificate can not be null or empty");
- }
-
- this.useWebhook = true;
- this.externalUrl = fixExternalUrl(externalUrl);
- this.pathToCertificate = pathToCertificate;
- webhook = ApiContext.getInstance(Webhook.class);
- webhook.setInternalUrl(internalUrl);
- webhook.setKeyStore(keyStore, keyStorePassword);
- webhook.startServer();
+ this.webhook = webhook;
+ this.webhook.startServer();
}
/**
* Register a bot. The Bot Session is started immediately, and may be disconnected by calling close.
* @param bot the bot to register
*/
- public BotSession registerBot(LongPollingBot bot) throws TelegramApiRequestException {
+ public BotSession registerBot(LongPollingBot bot) throws TelegramApiException {
bot.onRegister();
bot.clearWebhook();
- BotSession session = ApiContext.getInstance(BotSession.class);
+ BotSession session;
+ try {
+ session = botSessionClass.getConstructor().newInstance();
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
+ throw new TelegramApiException(e);
+ }
session.setToken(bot.getBotToken());
session.setOptions(bot.getOptions());
session.setCallback(bot);
@@ -140,11 +82,4 @@ public class TelegramBotsApi {
bot.setWebhook(setWebhook);
}
}
-
- private static String fixExternalUrl(String externalUrl) {
- if (externalUrl != null && !externalUrl.endsWith("/")) {
- externalUrl = externalUrl + "/";
- }
- return MessageFormat.format(webhookUrlFormat, externalUrl);
- }
}
diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/Webhook.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/Webhook.java
index 09e3b7e6..4b4020c8 100644
--- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/Webhook.java
+++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/Webhook.java
@@ -1,6 +1,6 @@
package org.telegram.telegrambots.meta.generics;
-import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
/**
* @author Ruben Bermudez
@@ -8,8 +8,8 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
* Webhook interface
*/
public interface Webhook {
- void startServer() throws TelegramApiRequestException;
+ void startServer() throws TelegramApiException;
void registerWebhook(WebhookBot callback);
void setInternalUrl(String internalUrl);
- void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiRequestException;
+ void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiException;
}
diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestDeserialization.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestDeserialization.java
index 41b14334..9c351069 100644
--- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestDeserialization.java
+++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestDeserialization.java
@@ -54,7 +54,7 @@ class TestDeserialization {
"\"message\":{\"message_id\":97,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154360,\"video_note\":{\"duration\":3,\"length\":240,\"thumb\":{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":6852,\"width\":240,\"height\":240},\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":100544}}},{\"update_id\":79995152,\n" +
"\"message\":{\"message_id\":98,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154363,\"voice\":{\"duration\":1,\"mime_type\":\"audio/ogg\",\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":5198}}},{\"update_id\":79995153,\n" +
"\"message\":{\"message_id\":99,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154371,\"photo\":[{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":14395,\"width\":180,\"height\":320},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":52852,\"width\":450,\"height\":800},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":84493,\"width\":720,\"height\":1280}]}},{\"update_id\":79995154,\n" +
- "\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-110011556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";
+ "\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-1556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";
ArrayList response = new GetUpdates().deserializeResponse(updateText);
diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java
index 35dce949..c3cb73c0 100644
--- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java
+++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java
@@ -1,28 +1,33 @@
package org.telegram.telegrambots.meta.test;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.TelegramBotsApi;
-import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
-import org.telegram.telegrambots.meta.test.base.TestBase;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
+import org.telegram.telegrambots.meta.generics.BotSession;
+import org.telegram.telegrambots.meta.generics.Webhook;
import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.mock;
/**
* @author Ruben Bermudez
* @version 1.0
*/
-class TestTelegramApi extends TestBase {
+class TestTelegramApi {
- @Test
- void TestTelegramApiMustBeInitializableForLongPolling() {
- new TelegramBotsApi();
+ private Webhook webhook;
+
+ @BeforeEach
+ public void setUp() {
+ webhook = mock(Webhook.class);
}
@Test
- void TestTelegramApiMustBeInitializableForWebhookWithoutSecureSupport() {
+ void TestTelegramApiMustBeInitializableForLongPolling() {
try {
- new TelegramBotsApi("externalUrl", "internalUrl");
- } catch (TelegramApiRequestException e) {
+ new TelegramBotsApi(BotSession.class);
+ } catch (TelegramApiException e) {
fail();
}
}
@@ -30,17 +35,8 @@ class TestTelegramApi extends TestBase {
@Test
void TestTelegramApiMustBeInitializableForWebhook() {
try {
- new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl");
- } catch (TelegramApiRequestException e) {
- fail();
- }
- }
-
- @Test
- void TestTelegramApiMustBeInitializableForWebhookWithSelfSignedCertificate() {
- try {
- new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl", "selfSignedPath");
- } catch (TelegramApiRequestException e) {
+ new TelegramBotsApi(BotSession.class, webhook);
+ } catch (TelegramApiException e) {
fail();
}
}
diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/base/TestBase.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/base/TestBase.java
deleted file mode 100644
index 08fd07e6..00000000
--- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/base/TestBase.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package org.telegram.telegrambots.meta.test.base;
-
-import org.junit.jupiter.api.BeforeAll;
-import org.telegram.telegrambots.meta.ApiContext;
-import org.telegram.telegrambots.meta.generics.BotSession;
-import org.telegram.telegrambots.meta.generics.Webhook;
-import org.telegram.telegrambots.meta.test.fakes.FakeBotSession;
-import org.telegram.telegrambots.meta.test.fakes.FakeWebhook;
-
-/**
- * @author Ruben Bermudez
- * @version 1.0
- */
-public abstract class TestBase {
-
- @BeforeAll
- public static void beforeClass() {
- ApiContext.register(BotSession.class, FakeBotSession.class);
- ApiContext.register(Webhook.class, FakeWebhook.class);
- }
-}
diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java
index c47dcdbf..0afac6f6 100644
--- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java
+++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java
@@ -6,7 +6,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.meta.TelegramBotsApi;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
+import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import java.util.Collections;
import java.util.List;
@@ -19,8 +21,8 @@ public class TelegramBotStarterConfiguration {
@Bean
@ConditionalOnMissingBean(TelegramBotsApi.class)
- public TelegramBotsApi telegramBotsApi() {
- return new TelegramBotsApi();
+ public TelegramBotsApi telegramBotsApi() throws TelegramApiException {
+ return new TelegramBotsApi(DefaultBotSession.class);
}
@Bean
diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java
index 1764c4ad..fa2e395c 100644
--- a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java
+++ b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java
@@ -8,13 +8,18 @@ import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Update;
-import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
class TestTelegramBotStarterRegistrationHooks {
@@ -29,7 +34,7 @@ class TestTelegramBotStarterRegistrationHooks {
TelegramBotStarterConfiguration.class));
@Test
- void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiRequestException {
+ void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiException {
when(mockTelegramBotsApi.registerBot(any(LongPollingBot.class))).thenReturn(someBotSession);
diff --git a/telegrambots/pom.xml b/telegrambots/pom.xml
index 1cf9e4bf..61a550a3 100644
--- a/telegrambots/pom.xml
+++ b/telegrambots/pom.xml
@@ -97,6 +97,11 @@
telegrambots-meta
5.0.0
+
+ org.projectlombok
+ lombok
+ provided
+
com.fasterxml.jackson.core
jackson-annotations
diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/ApiContextInitializer.java b/telegrambots/src/main/java/org/telegram/telegrambots/ApiContextInitializer.java
deleted file mode 100644
index 325106bd..00000000
--- a/telegrambots/src/main/java/org/telegram/telegrambots/ApiContextInitializer.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package org.telegram.telegrambots;
-
-import org.telegram.telegrambots.meta.generics.BotSession;
-import org.telegram.telegrambots.meta.generics.Webhook;
-import org.telegram.telegrambots.meta.ApiContext;
-import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
-import org.telegram.telegrambots.updatesreceivers.DefaultWebhook;
-
-/**
- * @author Ruben Bermudez
- * @version 1.0
- * Initialization of ApiContext
- */
-public final class ApiContextInitializer {
- private ApiContextInitializer() {
- }
-
- public static void init() {
- ApiContext.register(BotSession.class, DefaultBotSession.class);
- ApiContext.register(Webhook.class, DefaultWebhook.class);
- }
-}
diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java
index 9b2c2855..4468e52b 100644
--- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java
+++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java
@@ -1,6 +1,5 @@
package org.telegram.telegrambots.bots;
-import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.util.WebhookUtils;
@@ -13,7 +12,7 @@ import org.telegram.telegrambots.util.WebhookUtils;
*/
public abstract class TelegramLongPollingBot extends DefaultAbsSender implements LongPollingBot {
public TelegramLongPollingBot() {
- this(ApiContext.getInstance(DefaultBotOptions.class));
+ this(new DefaultBotOptions());
}
public TelegramLongPollingBot(DefaultBotOptions options) {
diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java
index 4a9e702c..0cabcee9 100644
--- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java
+++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java
@@ -1,6 +1,5 @@
package org.telegram.telegrambots.bots;
-import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.WebhookBot;
@@ -15,7 +14,7 @@ import org.telegram.telegrambots.util.WebhookUtils;
@SuppressWarnings("WeakerAccess")
public abstract class TelegramWebhookBot extends DefaultAbsSender implements WebhookBot {
public TelegramWebhookBot() {
- this(ApiContext.getInstance(DefaultBotOptions.class));
+ this(new DefaultBotOptions());
}
public TelegramWebhookBot(DefaultBotOptions options) {
diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java
index 4ce3b853..ef777e51 100644
--- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java
+++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java
@@ -1,7 +1,6 @@
package org.telegram.telegrambots.updatesreceivers;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.inject.Inject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@@ -61,7 +60,6 @@ public class DefaultBotSession implements BotSession {
private DefaultBotOptions options;
private UpdatesSupplier updatesSupplier;
- @Inject
public DefaultBotSession() {
}
diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java
index c27806f4..1ad1fba1 100644
--- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java
+++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java
@@ -1,14 +1,12 @@
package org.telegram.telegrambots.updatesreceivers;
-import com.google.inject.Inject;
-
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
-import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;
@@ -19,8 +17,7 @@ import java.net.URI;
/**
* @author Ruben Bermudez
* @version 1.0
- * @brief Webhook to receive updates
- * @date 20 of June of 2015
+ * Webhook to receive updates
*/
public class DefaultWebhook implements Webhook {
private String keystoreServerFile;
@@ -29,8 +26,7 @@ public class DefaultWebhook implements Webhook {
private final RestApi restApi;
- @Inject
- public DefaultWebhook() throws TelegramApiRequestException {
+ public DefaultWebhook() {
this.restApi = new RestApi();
}
@@ -38,7 +34,7 @@ public class DefaultWebhook implements Webhook {
this.internalUrl = internalUrl;
}
- public void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiRequestException {
+ public void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiException {
this.keystoreServerFile = keyStore;
this.keystoreServerPwd = keyStorePassword;
validateServerKeystoreFile(keyStore);
@@ -48,7 +44,7 @@ public class DefaultWebhook implements Webhook {
restApi.registerCallback(callback);
}
- public void startServer() throws TelegramApiRequestException {
+ public void startServer() throws TelegramApiException {
ResourceConfig rc = new ResourceConfig();
rc.register(restApi);
rc.register(JacksonFeature.class);
@@ -71,7 +67,7 @@ public class DefaultWebhook implements Webhook {
try {
grizzlyServer.start();
} catch (IOException e) {
- throw new TelegramApiRequestException("Error starting webhook server", e);
+ throw new TelegramApiException("Error starting webhook server", e);
}
}
@@ -79,10 +75,10 @@ public class DefaultWebhook implements Webhook {
return URI.create(internalUrl);
}
- private static void validateServerKeystoreFile(String keyStore) throws TelegramApiRequestException {
+ private static void validateServerKeystoreFile(String keyStore) throws TelegramApiException {
File file = new File(keyStore);
if (!file.exists() || !file.canRead()) {
- throw new TelegramApiRequestException("Can't find or access server keystore file.");
+ throw new TelegramApiException("Can't find or access server keystore file.");
}
}
}