Update changelog and how to update

This commit is contained in:
rubenlagus 2020-11-03 21:09:02 +00:00 committed by Ruben Bermudez
parent 95216b90c5
commit 93bd1e0efe
12 changed files with 206 additions and 55 deletions

View File

@ -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 // Example taken from https://github.com/rubenlagus/TelegramBotsExample
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ApiContextInitializer.init(); TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try { try {
telegramBotsApi.registerBot(new ChannelHandlers()); telegramBotsApi.registerBot(new ChannelHandlers());
telegramBotsApi.registerBot(new DirectionsHandlers()); telegramBotsApi.registerBot(new DirectionsHandlers());

View File

@ -1,13 +1,17 @@
### <a id="5.0.0"></a>5.0.0 ### ### <a id="5.0.0"></a>5.0.0 ###
1. Update Api version [5.0](https://core.telegram.org/bots/api-changelog#june-4-2020) 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 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 refence to itself, use Builder for that. 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) 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. 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. 6. When registering a Webhook Bot, a SetWebhook object must be provided.
7. When using Webhook with Spring, extends class SpringWebhookBot instead of WebhookBot 7. When using Webhook with Spring, extends class SpringWebhookBot instead of WebhookBot
8. New Async methods returning CompletableFutures. 8. New Async methods returning CompletableFutures (yes, we still have the existing callback methods)
9. No more Guice to define custom class 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]]**
### <a id="4.9.2"></a>4.9.2 ### ### <a id="4.9.2"></a>4.9.2 ###
1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814 1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814

View File

@ -4,10 +4,3 @@
## <a id="terminted_by_other"></a>Terminated by other long poll or webhook ## ## <a id="terminted_by_other"></a>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. 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.
## <a id="no_implementation_was_bound"></a>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();
```

View File

@ -265,9 +265,6 @@ Your main spring boot class should look like this:
public class YourApplicationMainClass { public class YourApplicationMainClass {
public static void main(String[] args) { public static void main(String[] args) {
//Add this line to initialize bots context
ApiContextInitializer.init();
SpringApplication.run(YourApplicationMainClass.class, args); SpringApplication.run(YourApplicationMainClass.class, args);
} }
} }

View File

@ -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:** 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 ```java
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Initialize Api Context
// TODO Instantiate Telegram Bots API // TODO Instantiate Telegram Bots API
// TODO Register our bot // 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`): * **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 ```java
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// You can use your own BotSession implementation if needed.
ApiContextInitializer.init(); TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
TelegramBotsApi botsApi = new TelegramBotsApi();
// TODO Register our bot // 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 class Main {
public static void main(String[] args) { public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi(); TelegramBotsApi botsApi = new TelegramBotsApi();
try { try {

View File

@ -1,3 +1,111 @@
### <a id="5.0.0"></a>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()
```
### <a id="4.4.0.2"></a>To version 4.4.0.2 ### ### <a id="4.4.0.2"></a>To version 4.4.0.2 ###
1. Logging framework has been replaced by slf4j, so now you'll need to manage your own implementation. 1. Logging framework has been replaced by slf4j, so now you'll need to manage your own implementation.

View File

@ -44,14 +44,11 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
ApiContextInitializer.init();
// Create the TelegramBotsApi object to register your bots // Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi(); TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSessioin.class);
// Set up Http proxy // Set up Http proxy
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); DefaultBotOptions botOptions = new DefaultBotOptions());
botOptions.setProxyHost(PROXY_HOST); botOptions.setProxyHost(PROXY_HOST);
botOptions.setProxyPort(PROXY_PORT); botOptions.setProxyPort(PROXY_PORT);
@ -96,13 +93,11 @@ public class Main {
} }
}); });
ApiContextInitializer.init();
// Create the TelegramBotsApi object to register your bots // Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi(); TelegramBotsApi botsApi = new TelegramBotsApi();
// Set up Http proxy // Set up Http proxy
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setProxyHost(PROXY_HOST); botOptions.setProxyHost(PROXY_HOST);
botOptions.setProxyPort(PROXY_PORT); botOptions.setProxyPort(PROXY_PORT);

View File

@ -76,7 +76,13 @@ public class TelegramBotsApi {
* @param setWebhook Set webhook request to initialize the bot * @param setWebhook Set webhook request to initialize the bot
*/ */
public void registerBot(WebhookBot bot, SetWebhook setWebhook) throws TelegramApiException { 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 (useWebhook) {
if (webhook == null) {
throw new TelegramApiException("This instance doesn't support Webhook bot, use correct constructor");
}
bot.onRegister(); bot.onRegister();
webhook.registerWebhook(bot); webhook.registerWebhook(bot);
bot.setWebhook(setWebhook); bot.setWebhook(setWebhook);

View File

@ -3,9 +3,13 @@ package org.telegram.telegrambots.meta.test;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.TelegramBotsApi; 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.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession; import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.Webhook; 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.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -40,4 +44,40 @@ class TestTelegramApi {
fail(); 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
}
}
} }

View File

@ -41,10 +41,7 @@ Your main spring boot class should look like this:
@SpringBootApplication @SpringBootApplication
public class YourApplicationMainClass { public class YourApplicationMainClass {
public static void main(String[] args) { public static void main(String[] args) {
//Add this line to initialize bots context
ApiContextInitializer.init();
SpringApplication.run(YourApplicationMainClass.class, args); SpringApplication.run(YourApplicationMainClass.class, args);
} }
} }

View File

@ -2,7 +2,9 @@ package org.telegram.telegrambots.starter;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramWebhookBot; 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.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.api.objects.Update;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -24,4 +26,35 @@ public abstract class SpringWebhookBot extends TelegramWebhookBot {
public SetWebhook getSetWebhook() { public SetWebhook getSetWebhook() {
return setWebhook; 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;
}
}
} }

View File

@ -13,6 +13,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.telegram.telegrambots.bots.DefaultBotOptions; 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.api.objects.Update;
import org.telegram.telegrambots.meta.generics.LongPollingBot; import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot; import org.telegram.telegrambots.test.Fakes.FakeLongPollingBot;
@ -37,6 +38,7 @@ public class TestDefaultBotSession {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
session = getDefaultBotSession(); session = getDefaultBotSession();
new TelegramBotsApi(DefaultBotSession.class);
} }
@After @After