diff --git a/README.md b/README.md index 6dd58284..c7ddef60 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,7 @@ Once done, you just need to create a `org.telegram.telegrambots.meta.TelegramBot // Example taken from https://github.com/rubenlagus/TelegramBotsExample public class Main { public static void main(String[] args) { - ApiContextInitializer.init(); - TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); try { telegramBotsApi.registerBot(new ChannelHandlers()); telegramBotsApi.registerBot(new DirectionsHandlers()); diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index 70675239..1cc0689b 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -1,13 +1,17 @@ ### 5.0.0 ### 1. Update Api version [5.0](https://core.telegram.org/bots/api-changelog#june-4-2020) -2. Added Builders for many of the API methods and objects -3. Some setters/getters may have change name. They no longer return a refence to itself, use Builder for that. +2. Added Builders for many of the API methods and objects (hopefully all of them unless I missed something) +3. Some setters/getters may have change name. They no longer return a reference to itself, use Builder for that. 4. Simplified methods to set files in methods. Only InputFile is available now (this class contains constructors for all the cases) 5. Locations now use Double instead of Float to avoid rounding. +6. When using a TelegramApi for webhook usage, a Webhook instance has to be provided in constructor (i.e. DefaultWebhook class) 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 +8. New Async methods returning CompletableFutures (yes, we still have the existing callback methods) +9. Added new Async methods for missing cases returning CompletableFutures. Like for sendAudio or sendVideo. +10. No more Guice to define custom class + +**[[How to update to version 5.0.0|How-To-Update#5.0.0]]** ### 4.9.2 ### 1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814 diff --git a/TelegramBots.wiki/Errors-Handling.md b/TelegramBots.wiki/Errors-Handling.md index 7bfa5780..169f407c 100644 --- a/TelegramBots.wiki/Errors-Handling.md +++ b/TelegramBots.wiki/Errors-Handling.md @@ -4,10 +4,3 @@ ## Terminated by other long poll or webhook ## It means that you have already a running instance of your bot. To solve it, close all running ones and then you can start a new instance. - -## No implementation for org.telegram.meta.telegrambots.generics.BotSession was bound ## -Please follow the steps as explained [here](https://github.com/rubenlagus/TelegramBots/wiki/How-To-Update#to-version-243) in "How To Update" - > At the beginning of your program (before creating your TelegramBotsApi instance, add the following line: - ``` - ApiContextInitializer.init(); - ``` diff --git a/TelegramBots.wiki/FAQ.md b/TelegramBots.wiki/FAQ.md index 5a94cbf4..a54758e1 100644 --- a/TelegramBots.wiki/FAQ.md +++ b/TelegramBots.wiki/FAQ.md @@ -265,9 +265,6 @@ Your main spring boot class should look like this: public class YourApplicationMainClass { public static void main(String[] args) { - //Add this line to initialize bots context - ApiContextInitializer.init(); - SpringApplication.run(YourApplicationMainClass.class, args); } } diff --git a/TelegramBots.wiki/Getting-Started.md b/TelegramBots.wiki/Getting-Started.md index ec56403d..53ffab72 100644 --- a/TelegramBots.wiki/Getting-Started.md +++ b/TelegramBots.wiki/Getting-Started.md @@ -98,15 +98,13 @@ Now that we have the library, we can start coding. There are few steps to follow ``` 2. **Instantiate `TelegramBotsApi` and register our new bot:** - For this part, we need to actually perform 3 steps: _Initialize Api Context_, _Instantiate Telegram Api_ and _Register our Bot_. In this tutorial, we are going to make it in our `main` method: + For this part, we need to actually perform 2 steps: _Instantiate Telegram Api_ and _Register our Bot_. In this tutorial, we are going to make it in our `main` method: ```java public class Main { public static void main(String[] args) { - // TODO Initialize Api Context - // TODO Instantiate Telegram Bots API // TODO Register our bot @@ -115,33 +113,14 @@ Now that we have the library, we can start coding. There are few steps to follow ``` - * **Initialize Api Context**: This can be easily done calling the only method present in `ApiContextInitializer`: - - ```java - - public class Main { - public static void main(String[] args) { - - ApiContextInitializer.init(); - - // TODO Instantiate Telegram Bots API - - // TODO Register our bot - } - } - - ``` - * **Instantiate Telegram Bots API**: Easy as well, just create a new instance. Remember that a single instance can handle different bots but each bot can run only once (Telegram doesn't support concurrent calls to `GetUpdates`): ```java - + public class Main { public static void main(String[] args) { - - ApiContextInitializer.init(); - - TelegramBotsApi botsApi = new TelegramBotsApi(); + // You can use your own BotSession implementation if needed. + TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); // TODO Register our bot } @@ -156,8 +135,6 @@ Now that we have the library, we can start coding. There are few steps to follow public class Main { public static void main(String[] args) { - ApiContextInitializer.init(); - TelegramBotsApi botsApi = new TelegramBotsApi(); try { diff --git a/TelegramBots.wiki/How-To-Update.md b/TelegramBots.wiki/How-To-Update.md index 11fe80e8..9c1c4733 100644 --- a/TelegramBots.wiki/How-To-Update.md +++ b/TelegramBots.wiki/How-To-Update.md @@ -1,3 +1,111 @@ +### To version 5.0.0 ### +1. ApiContextInitializer.init(); has been removed and is not required anymore, instead: + ```java + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class); + + // When using webhook, create your own version of DefaultWebhook with all your parameters set. + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class, defaultWebhookInstance); + ``` +2. For location related class, change from Float to Double type, i.e: + ```java + Double latitude = location.getLatitude() + ``` +3. Instead of chain set method, use builder pattern: + ```java + // Before + new SendMessage() + .setChatId("@test") + .setText("Hithere") + .setReplyToMessageId(12) + .setParseMode(ParseMode.HTML) + .setReplyMarkup(new ForceReplyKeyboard()) + // After + SendMessage + .builder() + .chatId("@test") + .text("Hithere") + .replyToMessageId(12) + .parseMode(ParseMode.HTML) + .replyMarkup(new ForceReplyKeyboard()) + .build(); + ``` +4. Method doesn't accept chatId as Long any more, only as a String. Use Long.toString(...) when needed I.e: + ```java + Long chatIdLong = message.getChatId(); + SendMessage + .builder() + .chatId(Long.toString(chatIdLong)) + .text("Hithere") + .build(); + ``` +5. When registering a Webhook bot, provide the SetWebhook method object: + ```java + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class, defaultWebhookInstance); + telegramApi.registerBot(myWebhookBot, mySetWebhook); + ``` +6. When using Spring with a webhook bot, make your bot inherit form SpringWebhookBot instead of WebhookBot and provide your SetWebhook method in the constructor: + ```java + // Extend correct class + public class TestSpringWebhookBot extends SpringWebhookBot { + + public TestSpringWebhookBot(SetWebhook setWebhook) { + super(setWebhook); + } + + public TestSpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook) { + super(options, setWebhook); + } + + @Override + public String getBotUsername() { + return null; + } + + @Override + public String getBotToken() { + return null; + } + + @Override + public BotApiMethod onWebhookUpdateReceived(Update update) { + return null; + } + + @Override + public String getBotPath() { + return null; + } + } + + // Create your SetWebhook method + @Bean + public SetWebhook setWebhookInstance() { + return SetWebhook.builder()....build(); + } + + // Create it as + @Bean + public TestSpringWebhookBot testSpringWebhookBot(SetWebhook setWebhookInstance) { + return new TestSpringWebhookBot(setWebhookInstance); + } + ``` +7. Use InputFile to set files to upload instead of different setters, i.e: + ```java + // With a file + SendDocument + .builder() + .chatId("123456") + .document(new InputFile(new File("Filename.pdf"))) + .build() + // With a Stream + SendDocument + .builder() + .chatId("123456") + .document(new InputFile("FileName", new FileInputStream("Filename.pdf"))) + .build() + ``` + + ### To version 4.4.0.2 ### 1. Logging framework has been replaced by slf4j, so now you'll need to manage your own implementation. diff --git a/TelegramBots.wiki/Using-Http-Proxy.md b/TelegramBots.wiki/Using-Http-Proxy.md index 0b7d4c0a..2a44a9c2 100644 --- a/TelegramBots.wiki/Using-Http-Proxy.md +++ b/TelegramBots.wiki/Using-Http-Proxy.md @@ -44,14 +44,11 @@ public class Main { public static void main(String[] args) { try { - - ApiContextInitializer.init(); - // Create the TelegramBotsApi object to register your bots - TelegramBotsApi botsApi = new TelegramBotsApi(); + TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSessioin.class); // Set up Http proxy - DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); + DefaultBotOptions botOptions = new DefaultBotOptions()); botOptions.setProxyHost(PROXY_HOST); botOptions.setProxyPort(PROXY_PORT); @@ -96,13 +93,11 @@ public class Main { } }); - ApiContextInitializer.init(); - // Create the TelegramBotsApi object to register your bots TelegramBotsApi botsApi = new TelegramBotsApi(); // Set up Http proxy - DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); + DefaultBotOptions botOptions = new DefaultBotOptions(); botOptions.setProxyHost(PROXY_HOST); botOptions.setProxyPort(PROXY_PORT); 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 2642d7b9..437838fa 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 @@ -76,7 +76,13 @@ public class TelegramBotsApi { * @param setWebhook Set webhook request to initialize the bot */ public void registerBot(WebhookBot bot, SetWebhook setWebhook) throws TelegramApiException { + if (setWebhook == null) { + throw new TelegramApiException("Parameter setWebhook can not be null or empty"); + } if (useWebhook) { + if (webhook == null) { + throw new TelegramApiException("This instance doesn't support Webhook bot, use correct constructor"); + } bot.onRegister(); webhook.registerWebhook(bot); bot.setWebhook(setWebhook); 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 c3cb73c0..1d0d7aae 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 @@ -3,9 +3,13 @@ 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.api.methods.BotApiMethod; +import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; +import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.generics.BotSession; import org.telegram.telegrambots.meta.generics.Webhook; +import org.telegram.telegrambots.meta.generics.WebhookBot; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; @@ -40,4 +44,40 @@ class TestTelegramApi { fail(); } } + + @Test + void TestTelegramApiMustBeThrowIfNotCreatedForWebhook() { + try { + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(BotSession.class, null); + telegramBotsApi.registerBot(new WebhookBot() { + @Override + public BotApiMethod onWebhookUpdateReceived(Update update) { + return null; + } + + @Override + public void setWebhook(SetWebhook setWebhook) throws TelegramApiException { + + } + + @Override + public String getBotPath() { + return null; + } + + @Override + public String getBotUsername() { + return null; + } + + @Override + public String getBotToken() { + return null; + } + }, new SetWebhook()); + fail(); + } catch (TelegramApiException e) { + // Ignore + } + } } diff --git a/telegrambots-spring-boot-starter/README.md b/telegrambots-spring-boot-starter/README.md index 9644e564..1548f833 100644 --- a/telegrambots-spring-boot-starter/README.md +++ b/telegrambots-spring-boot-starter/README.md @@ -41,10 +41,7 @@ Your main spring boot class should look like this: @SpringBootApplication public class YourApplicationMainClass { - public static void main(String[] args) { - //Add this line to initialize bots context - ApiContextInitializer.init(); - + public static void main(String[] args) { SpringApplication.run(YourApplicationMainClass.class, args); } } diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java index a3a7c624..c271956a 100644 --- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java @@ -2,7 +2,9 @@ package org.telegram.telegrambots.starter; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.TelegramWebhookBot; +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; +import org.telegram.telegrambots.meta.api.objects.Update; /** * @author Ruben Bermudez @@ -24,4 +26,35 @@ public abstract class SpringWebhookBot extends TelegramWebhookBot { public SetWebhook getSetWebhook() { return setWebhook; } + + public class TestSpringWebhookBot extends SpringWebhookBot { + + public TestSpringWebhookBot(SetWebhook setWebhook) { + super(setWebhook); + } + + public TestSpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook) { + super(options, setWebhook); + } + + @Override + public String getBotUsername() { + return null; + } + + @Override + public String getBotToken() { + return null; + } + + @Override + public BotApiMethod onWebhookUpdateReceived(Update update) { + return null; + } + + @Override + public String getBotPath() { + return null; + } + } } diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java index 941b3508..7a5c77c9 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TestDefaultBotSession.java @@ -13,6 +13,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.generics.LongPollingBot; import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot; @@ -37,6 +38,7 @@ public class TestDefaultBotSession { @Before public void setUp() throws Exception { session = getDefaultBotSession(); + new TelegramBotsApi(DefaultBotSession.class); } @After