Merge pull request #20 from rubenlagus/dev

Dev
This commit is contained in:
Andy Costanza 2021-04-26 20:31:31 +02:00 committed by GitHub
commit bb18562222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 241 additions and 62 deletions

View File

@ -1,10 +1,10 @@
* [Bot Token Dont's](#bot-token-donts) * [Bot Token Rules](#bot-token-rules)
* [Using Enviroment Variables](#using-environment-variables) * [Using Enviroment Variables](#using-environment-variables)
* [Setting Enviroment Variables](#setting-environment-variables) * [Setting Enviroment Variables](#setting-environment-variables)
* [Accessing Enviroment Variables](#accessing-enviroment-variables) * [Accessing Enviroment Variables](#accessing-enviroment-variables)
* [Using Command Line Arguments](#using-command-line-arguments) * [Using Command Line Arguments](#using-command-line-arguments)
# <a id="bot-token-donts"></a> Bot Token Dont's ## # <a id="bot-token-rules"></a> Bot Token Rules ##
* Tokens should not be hardcoded into the bot code * Tokens should not be hardcoded into the bot code
* Tokens should never be published * Tokens should never be published
* Tokens should not be pushed into Repositorys * Tokens should not be pushed into Repositorys
@ -36,7 +36,7 @@ It can also be set using the Windows GUI
export VARIABLE_NAME = {YOUR_BOT_TOKEN} export VARIABLE_NAME = {YOUR_BOT_TOKEN}
``` ```
* Save the file * Save the file
* Either reboot your system or run the command above in your terminal * Either start a new terminal or run the command above
### IntelliJ ### IntelliJ
* Go to Run->Edit Configuratuions... * Go to Run->Edit Configuratuions...
@ -45,7 +45,7 @@ export VARIABLE_NAME = {YOUR_BOT_TOKEN}
* Click the Plus Icon to add a new Variable * Click the Plus Icon to add a new Variable
* Enter a Name and your Token as the Value * Enter a Name and your Token as the Value
###Heroku Cloud ### Heroku Cloud
* Naviage to your App * Naviage to your App
* In the Settings Tab under Config Vars, click "Reveal Config Vars" * In the Settings Tab under Config Vars, click "Reveal Config Vars"
* Enter a Name and your Token as the Value * Enter a Name and your Token as the Value
@ -65,7 +65,7 @@ String BOT_TOKEN = System.getenv("VARIABLE_NAME");
In Spring the @Value annotation allows you to inject the Value into your class In Spring the @Value annotation allows you to inject the Value into your class
```java ```java
public class Bot extends TelegramLongPollingBot { public class Bot extends TelegramLongPollingBot {
public Bot(@Value("${VARIABLE_NAME") String botToken) { public Bot(@Value("${VARIABLE_NAME}") String botToken) {
this.botToken = botToken; this.botToken = botToken;
} }
} }
@ -85,4 +85,4 @@ public static void main(String[] args) {
You now have to call your jar by using You now have to call your jar by using
``` ```
java -jar myBot.jar [BOT_TOKEN] java -jar myBot.jar [BOT_TOKEN]
``` ```

View File

@ -200,6 +200,11 @@ public final class Ability {
return this; return this;
} }
public AbilityBuilder setStatsEnabled(boolean statsEnabled) {
this.statsEnabled = statsEnabled;
return this;
}
public AbilityBuilder privacy(Privacy privacy) { public AbilityBuilder privacy(Privacy privacy) {
this.privacy = privacy; this.privacy = privacy;
return this; return this;

View File

@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder; import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder;
class AbilityTest { class AbilityTest {
@ -55,5 +57,17 @@ class AbilityTest {
assertEquals(ability1, ability4, "Abilities should not be equal"); assertEquals(ability1, ability4, "Abilities should not be equal");
assertNotEquals(ability1, ability3, "Abilities should be equal"); assertNotEquals(ability1, ability3, "Abilities should be equal");
} }
@Test
void abilityBuilderSetStatsEnabledTrueTest() {
Ability statsEnabledAbility = getDefaultBuilder().setStatsEnabled(true).build();
assertTrue(statsEnabledAbility.statsEnabled());
}
@Test
void abilityBuilderSetStatsEnabledFalseTest() {
Ability statsDisabledAbility = getDefaultBuilder().setStatsEnabled(false).build();
assertFalse(statsDisabledAbility.statsEnabled());
}
} }

View File

@ -87,7 +87,7 @@ public class Message implements BotApiObject {
private static final String VIABOT_FIELD = "via_bot"; private static final String VIABOT_FIELD = "via_bot";
private static final String SENDERCHAT_FIELD = "sender_chat"; private static final String SENDERCHAT_FIELD = "sender_chat";
private static final String PROXIMITYALERTTRIGGERED_FIELD = "proximity_alert_triggered"; private static final String PROXIMITYALERTTRIGGERED_FIELD = "proximity_alert_triggered";
private static final String MESSAGEAUTODELETETIMERCHANGED_FIELD = "message_auto_delete_timer_changed\t"; private static final String MESSAGEAUTODELETETIMERCHANGED_FIELD = "message_auto_delete_timer_changed";
private static final String VOICECHATSTARTED_FIELD = "voice_chat_started"; private static final String VOICECHATSTARTED_FIELD = "voice_chat_started";
private static final String VOICECHATENDED_FIELD = "voice_chat_ended"; private static final String VOICECHATENDED_FIELD = "voice_chat_ended";
private static final String VOICECHATPARTICIPANTSINVITED_FIELD = "voice_chat_participants_invited"; private static final String VOICECHATPARTICIPANTSINVITED_FIELD = "voice_chat_participants_invited";

View File

@ -84,8 +84,13 @@ public class TelegramApiRequestException extends TelegramApiException {
return parameters; return parameters;
} }
/**
* More detailed error description.
*
* @return assembled response code and reason
*/
@Override @Override
public String toString() { public String getMessage() {
if (apiResponse == null) { if (apiResponse == null) {
return super.toString(); return super.toString();
} else if (errorCode == null) { } else if (errorCode == null) {
@ -94,4 +99,15 @@ public class TelegramApiRequestException extends TelegramApiException {
return super.toString() + ": [" + errorCode + "] " + apiResponse; return super.toString() + ": [" + errorCode + "] " + apiResponse;
} }
} }
/**
* Just left as backward compatibility if anybody used this in version 5.1.1 or older.
*
* @return the getMessage string
*/
@Override
public String toString() {
return getMessage();
}
} }

View File

@ -0,0 +1,19 @@
package org.telegram.telegrambots.meta.generics;
/**
* @author Calvin
* @version 1.0
* Interface for backoff retries
*/
public interface BackOff {
/**
* Should be able to reset to the starting
*/
void reset();
/**
* specify the next backoff interval in milliseconds
*/
long nextBackOffMillis();
}

View File

@ -185,6 +185,10 @@ public final class TelegramBotsHelper {
return "{\"ok\": false,\"error_code\": 400,\"description\": \"Error descriptions\",\"parameters\": {\"migrate_to_chat_id\": 12345,\"retry_after\": 12}}"; return "{\"ok\": false,\"error_code\": 400,\"description\": \"Error descriptions\",\"parameters\": {\"migrate_to_chat_id\": 12345,\"retry_after\": 12}}";
} }
public static String getResponseWithError409() {
return "{\"ok\": false,\"error_code\": 409,\"description\": \"Conflict: terminated by other getUpdates request; make sure that only one bot instance is running\"}";
}
public static String GetSetGameScoreBooleanResponse() { public static String GetSetGameScoreBooleanResponse() {
return "{\"ok\": true,\"result\": true}"; return "{\"ok\": true,\"result\": true}";
} }

View File

@ -51,7 +51,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates() throws Exception { void testListUpdates() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":79995144,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":79995144,\n" +
"\"message\":{\"message_id\":90,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":1234567,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154223,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995145,\n" + "\"message\":{\"message_id\":90,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":1234567,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154223,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995145,\n" +
"\"message\":{\"message_id\":91,\"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\":1604154306,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995146,\n" + "\"message\":{\"message_id\":91,\"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\":1604154306,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":79995146,\n" +
@ -89,7 +89,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates2() throws Exception { void testListUpdates2() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894298,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894298,\n" +
"\"message\":{\"message_id\":101,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604171814,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894299,\n" + "\"message\":{\"message_id\":101,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604171814,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894299,\n" +
"\"message\":{\"message_id\":102,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604188661,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894300,\n" + "\"message\":{\"message_id\":102,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FistName\",\"last_name\":\"LastName\",\"username\":\"username\",\"type\":\"private\"},\"date\":1604188661,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894300,\n" +
@ -106,7 +106,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdates3() throws Exception { void testListUpdates3() throws Exception {
String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894302,\n" + String updateText = "{\"ok\":true,\"result\":[{\"update_id\":259894302,\n" +
"\"message\":{\"message_id\":105,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226451,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894303,\n" + "\"message\":{\"message_id\":105,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226451,\"text\":\"/start\",\"entities\":[{\"offset\":0,\"length\":6,\"type\":\"bot_command\"}]}},{\"update_id\":259894303,\n" +
"\"message\":{\"message_id\":106,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226480,\"document\":{\"file_name\":\"aaa.txt\",\"mime_type\":\"text/plain\",\"file_id\":\"FILEID\",\"file_unique_id\":\"AgADiQEAAjTe-VQ\",\"file_size\":2}}},{\"update_id\":259894304,\n" + "\"message\":{\"message_id\":106,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"FirstName\",\"username\":\"Username\",\"language_code\":\"en\"},\"chat\":{\"id\":12345678,\"first_name\":\"FirstName\",\"username\":\"Username\",\"type\":\"private\"},\"date\":1604226480,\"document\":{\"file_name\":\"aaa.txt\",\"mime_type\":\"text/plain\",\"file_id\":\"FILEID\",\"file_unique_id\":\"AgADiQEAAjTe-VQ\",\"file_size\":2}}},{\"update_id\":259894304,\n" +
@ -165,7 +165,7 @@ class TestDeserialization {
} }
@Test @Test
void TestListUpdatesVoiceChat() throws Exception { void testListUpdatesVoiceChat() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": [\n" + " \"result\": [\n" +
@ -289,7 +289,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationCloseMethod() throws Exception { void testDeserializationCloseMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}"; String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new Close().deserializeResponse(updateText); Boolean response = new Close().deserializeResponse(updateText);
@ -298,7 +298,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationChatMember() throws Exception { void testDeserializationChatMember() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": {\n" + " \"result\": {\n" +
@ -344,7 +344,50 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationLogoutMethod() throws Exception { void testDeserializationMessageAutodeleteChanged() throws Exception {
String updateText = "{\n" +
" \"ok\": true,\n" +
" \"result\": [\n" +
" {\n" +
" \"update_id\": 259894298,\n" +
" \"message\": {\n" +
" \"message_id\": 101,\n" +
" \"from\": {\n" +
" \"id\": 12345678,\n" +
" \"is_bot\": false,\n" +
" \"first_name\": \"FistName\",\n" +
" \"last_name\": \"LastName\",\n" +
" \"username\": \"username\"\n" +
" },\n" +
" \"chat\": {\n" +
" \"id\": 12345678,\n" +
" \"first_name\": \"FistName\",\n" +
" \"last_name\": \"LastName\",\n" +
" \"username\": \"username\",\n" +
" \"type\": \"private\"\n" +
" },\n" +
" \"date\": 1604171814,\n" +
" \"message_auto_delete_timer_changed\": {\n" +
" \"message_auto_delete_time\": 100\n" +
" }\n" +
" \n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
ArrayList<Update> response = new GetUpdates().deserializeResponse(updateText);
assertNotNull(response);
assertEquals(1, response.size());
assertNotNull(response.get(0));
assertNotNull(response.get(0).getMessage());
assertNotNull(response.get(0).getMessage().getMessageAutoDeleteTimerChanged());
assertEquals(100, response.get(0).getMessage().getMessageAutoDeleteTimerChanged().getMessageAutoDeleteTime());
}
@Test
void testDeserializationLogoutMethod() throws Exception {
String updateText = "{\"ok\":true,\"result\": true}"; String updateText = "{\"ok\":true,\"result\": true}";
Boolean response = new LogOut().deserializeResponse(updateText); Boolean response = new LogOut().deserializeResponse(updateText);
@ -353,7 +396,7 @@ class TestDeserialization {
} }
@Test @Test
void TestDeserializationGetMyCommandsMethod() throws Exception { void testDeserializationGetMyCommandsMethod() throws Exception {
String updateText = "{\n" + String updateText = "{\n" +
" \"ok\": true,\n" + " \"ok\": true,\n" +
" \"result\": [\n" + " \"result\": [\n" +

View File

@ -28,7 +28,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeInitializableForLongPolling() { void testTelegramApiMustBeInitializableForLongPolling() {
try { try {
new TelegramBotsApi(BotSession.class); new TelegramBotsApi(BotSession.class);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
@ -37,7 +37,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeInitializableForWebhook() { void testTelegramApiMustBeInitializableForWebhook() {
try { try {
new TelegramBotsApi(BotSession.class, webhook); new TelegramBotsApi(BotSession.class, webhook);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
@ -46,7 +46,7 @@ class TestTelegramApi {
} }
@Test @Test
void TestTelegramApiMustBeThrowIfNotCreatedForWebhook() { void testTelegramApiMustBeThrowIfNotCreatedForWebhook() {
try { try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(BotSession.class, null); TelegramBotsApi telegramBotsApi = new TelegramBotsApi(BotSession.class, null);
telegramBotsApi.registerBot(new WebhookBot() { telegramBotsApi.registerBot(new WebhookBot() {

View File

@ -22,7 +22,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestInlineQueryIdMustBePresent() { void testInlineQueryIdMustBePresent() {
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
} catch (TelegramApiValidationException e) { } catch (TelegramApiValidationException e) {
@ -31,7 +31,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestInlineQueryIdCanNotBeEmpty() { void testInlineQueryIdCanNotBeEmpty() {
answerInlineQuery.setInlineQueryId(""); answerInlineQuery.setInlineQueryId("");
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
@ -41,7 +41,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestResultsMustBePresent() { void testResultsMustBePresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
try { try {
answerInlineQuery.validate(); answerInlineQuery.validate();
@ -51,7 +51,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmTextCanNotBeEmpty() { void testSwitchPmTextCanNotBeEmpty() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText(""); answerInlineQuery.setSwitchPmText("");
@ -64,7 +64,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterIsMandatoryIfSwitchPmTextIsPresent() { void testSwitchPmParameterIsMandatoryIfSwitchPmTextIsPresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -77,7 +77,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterCanNotBeEmptyIfSwitchPmTextIsPresent() { void testSwitchPmParameterCanNotBeEmptyIfSwitchPmTextIsPresent() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -91,7 +91,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterContainsUpTo64Chars() { void testSwitchPmParameterContainsUpTo64Chars() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");
@ -105,7 +105,7 @@ class TestAnswerInlineQuery {
} }
@Test @Test
void TestSwitchPmParameterOnlyContainsAcceptedCharacters() { void testSwitchPmParameterOnlyContainsAcceptedCharacters() {
answerInlineQuery.setInlineQueryId("RANDOMEID"); answerInlineQuery.setInlineQueryId("RANDOMEID");
answerInlineQuery.setResults(new ArrayList<>()); answerInlineQuery.setResults(new ArrayList<>());
answerInlineQuery.setSwitchPmText("Test Text"); answerInlineQuery.setSwitchPmText("Test Text");

View File

@ -11,8 +11,7 @@ import org.telegram.telegrambots.meta.test.TelegramBotsHelper;
import java.util.ArrayList; import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -32,14 +31,14 @@ class TestGetUpdates {
} }
@Test @Test
void TestGetUpdatesMustBeSerializable() throws Exception { void testGetUpdatesMustBeSerializable() throws Exception {
String json = mapper.writeValueAsString(getUpdates); String json = mapper.writeValueAsString(getUpdates);
assertNotNull(json); assertNotNull(json);
assertEquals("{\"offset\":15,\"limit\":100,\"timeout\":50,\"method\":\"getupdates\"}", json); assertEquals("{\"offset\":15,\"limit\":100,\"timeout\":50,\"method\":\"getupdates\"}", json);
} }
@Test @Test
void TestGetUpdatesMustDeserializeCorrectResponse() throws Exception { void testGetUpdatesMustDeserializeCorrectResponse() throws Exception {
ArrayList<Update> result = ArrayList<Update> result =
getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithoutError()); getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithoutError());
assertNotNull(result); assertNotNull(result);
@ -47,7 +46,7 @@ class TestGetUpdates {
} }
@Test @Test
void TestGetUpdatesMustThrowAnExceptionForInCorrectResponse() { void testGetUpdatesMustThrowAnExceptionForInCorrectResponse() {
try { try {
getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithError()); getUpdates.deserializeResponse(TelegramBotsHelper.GetResponseWithError());
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
@ -56,4 +55,15 @@ class TestGetUpdates {
assertEquals("Error descriptions", e.getApiResponse()); assertEquals("Error descriptions", e.getApiResponse());
} }
} }
@Test
void testGetUpdatesMustThrowAnExceptionForInCorrectResponse409() {
try {
getUpdates.deserializeResponse(TelegramBotsHelper.getResponseWithError409());
} catch (TelegramApiRequestException e) {
assertNull(e.getParameters());
assertEquals(Integer.valueOf(409), e.getErrorCode());
assertEquals("Conflict: terminated by other getUpdates request; make sure that only one bot instance is running", e.getApiResponse());
}
}
} }

View File

@ -18,9 +18,4 @@
}, },
"text": "Original" "text": "Original"
} }
}
{
"ok": true,
"result": true
} }

View File

@ -71,7 +71,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<telegrambots.version>5.1.1</telegrambots.version> <telegrambots.version>5.1.1</telegrambots.version>
<spring-boot.version>2.4.3</spring-boot.version> <spring-boot.version>2.4.5</spring-boot.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version> <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version> <nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>

View File

@ -5,7 +5,7 @@ import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.telegram.telegrambots.meta.ApiConstants; import org.telegram.telegrambots.meta.ApiConstants;
import org.telegram.telegrambots.meta.generics.BotOptions; import org.telegram.telegrambots.meta.generics.BotOptions;
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff; import org.telegram.telegrambots.meta.generics.BackOff;
import java.util.List; import java.util.List;
@ -19,7 +19,7 @@ public class DefaultBotOptions implements BotOptions {
private int maxThreads; ///< Max number of threads used for async methods executions (default 1) private int maxThreads; ///< Max number of threads used for async methods executions (default 1)
private RequestConfig requestConfig; private RequestConfig requestConfig;
private volatile HttpContext httpContext; private volatile HttpContext httpContext;
private ExponentialBackOff exponentialBackOff; private BackOff backOff;
private Integer maxWebhookConnections; private Integer maxWebhookConnections;
private String baseUrl; private String baseUrl;
private List<String> allowedUpdates; private List<String> allowedUpdates;
@ -91,23 +91,23 @@ public class DefaultBotOptions implements BotOptions {
} }
/** /**
* @implSpec Default implementation assumes no proxy is needed and sets a 75secs timoute
* @param requestConfig Request config to be used in all Http requests * @param requestConfig Request config to be used in all Http requests
* @implSpec Default implementation assumes no proxy is needed and sets a 75secs timoute
*/ */
public void setRequestConfig(RequestConfig requestConfig) { public void setRequestConfig(RequestConfig requestConfig) {
this.requestConfig = requestConfig; this.requestConfig = requestConfig;
} }
public ExponentialBackOff getExponentialBackOff() { public BackOff getBackOff() {
return exponentialBackOff; return backOff;
} }
/** /**
* @param BackOff backOff to be used when long polling fails
* @implSpec Default implementation assumes starting at 500ms and max time of 60 minutes * @implSpec Default implementation assumes starting at 500ms and max time of 60 minutes
* @param exponentialBackOff ExponentialBackOff to be used when long polling fails
*/ */
public void setExponentialBackOff(ExponentialBackOff exponentialBackOff) { public void setBackOff(BackOff BackOff) {
this.exponentialBackOff = exponentialBackOff; this.backOff = BackOff;
} }
public ProxyType getProxyType() { public ProxyType getProxyType() {

View File

@ -16,11 +16,7 @@ import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates; import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotOptions; import org.telegram.telegrambots.meta.generics.*;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.UpdatesHandler;
import org.telegram.telegrambots.meta.generics.UpdatesReader;
import java.io.IOException; import java.io.IOException;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
@ -142,7 +138,7 @@ public class DefaultBotSession implements BotSession {
private final UpdatesSupplier updatesSupplier; private final UpdatesSupplier updatesSupplier;
private final Object lock; private final Object lock;
private CloseableHttpClient httpclient; private CloseableHttpClient httpclient;
private ExponentialBackOff exponentialBackOff; private BackOff backOff;
private RequestConfig requestConfig; private RequestConfig requestConfig;
public ReaderThread(UpdatesSupplier updatesSupplier, Object lock) { public ReaderThread(UpdatesSupplier updatesSupplier, Object lock) {
@ -154,10 +150,11 @@ public class DefaultBotSession implements BotSession {
public synchronized void start() { public synchronized void start() {
httpclient = TelegramHttpClientBuilder.build(options); httpclient = TelegramHttpClientBuilder.build(options);
requestConfig = options.getRequestConfig(); requestConfig = options.getRequestConfig();
exponentialBackOff = options.getExponentialBackOff(); backOff = options.getBackOff();
if (exponentialBackOff == null) { // fall back to default exponential backoff strategy if no backoff specified
exponentialBackOff = new ExponentialBackOff(); if (backOff == null) {
backOff = new ExponentialBackOff();
} }
if (requestConfig == null) { if (requestConfig == null) {
@ -215,7 +212,7 @@ public class DefaultBotSession implements BotSession {
log.error(global.getLocalizedMessage(), global); log.error(global.getLocalizedMessage(), global);
try { try {
synchronized (lock) { synchronized (lock) {
lock.wait(exponentialBackOff.nextBackOffMillis()); lock.wait(backOff.nextBackOffMillis());
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
if (!running.get()) { if (!running.get()) {
@ -261,7 +258,7 @@ public class DefaultBotSession implements BotSession {
} else { } else {
try { try {
List<Update> updates = request.deserializeResponse(responseContent); List<Update> updates = request.deserializeResponse(responseContent);
exponentialBackOff.reset(); backOff.reset();
return updates; return updates;
} catch (JSONException e) { } catch (JSONException e) {
log.error("Error deserializing update: " + responseContent, e); log.error("Error deserializing update: " + responseContent, e);

View File

@ -14,6 +14,7 @@
package org.telegram.telegrambots.updatesreceivers; package org.telegram.telegrambots.updatesreceivers;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.telegram.telegrambots.meta.generics.BackOff;
/** /**
* Implementation of BackOff that increases the back off period for each retry attempt using * Implementation of BackOff that increases the back off period for each retry attempt using
@ -68,7 +69,7 @@ import com.google.common.base.Preconditions;
* @since 1.15 * @since 1.15
* @author Ravi Mistry * @author Ravi Mistry
*/ */
public class ExponentialBackOff { public class ExponentialBackOff implements BackOff {
/** The default initial interval value in milliseconds (0.5 seconds). */ /** The default initial interval value in milliseconds (0.5 seconds). */
private static final int DEFAULT_INITIAL_INTERVAL_MILLIS = 500; private static final int DEFAULT_INITIAL_INTERVAL_MILLIS = 500;
@ -82,7 +83,7 @@ public class ExponentialBackOff {
private static final double DEFAULT_MULTIPLIER = 1.5; private static final double DEFAULT_MULTIPLIER = 1.5;
/** The default maximum back off time in milliseconds (15 minutes). */ /** The default maximum back off time in milliseconds (15 minutes). */
private static final int DEFAULT_MAX_INTERVAL_MILLIS = 30000; private static final int DEFAULT_MAX_INTERVAL_MILLIS = 900000;
/** The default maximum elapsed time in milliseconds (60 minutes). */ /** The default maximum elapsed time in milliseconds (60 minutes). */
private static final int DEFAULT_MAX_ELAPSED_TIME_MILLIS = 3600000; private static final int DEFAULT_MAX_ELAPSED_TIME_MILLIS = 3600000;
@ -161,7 +162,8 @@ public class ExponentialBackOff {
} }
/** Sets the interval back to the initial retry interval and restarts the timer. */ /** Sets the interval back to the initial retry interval and restarts the timer. */
final void reset() { @Override
public void reset() {
currentIntervalMillis = initialIntervalMillis; currentIntervalMillis = initialIntervalMillis;
startTimeNanos = nanoTime(); startTimeNanos = nanoTime();
} }
@ -178,7 +180,8 @@ public class ExponentialBackOff {
* Subclasses may override if a different algorithm is required. * Subclasses may override if a different algorithm is required.
* </p> * </p>
*/ */
long nextBackOffMillis() { @Override
public long nextBackOffMillis() {
// Make sure we have not gone over the maximum elapsed time. // Make sure we have not gone over the maximum elapsed time.
if (getElapsedTimeMillis() > maxElapsedTimeMillis) { if (getElapsedTimeMillis() > maxElapsedTimeMillis) {
return maxElapsedTimeMillis; return maxElapsedTimeMillis;
@ -274,7 +277,32 @@ public class ExponentialBackOff {
*/ */
int maxElapsedTimeMillis = DEFAULT_MAX_ELAPSED_TIME_MILLIS; int maxElapsedTimeMillis = DEFAULT_MAX_ELAPSED_TIME_MILLIS;
Builder() { public Builder() {
}
public Builder setInitialIntervalMillis(int initialIntervalMillis) {
this.initialIntervalMillis = initialIntervalMillis;
return this;
}
public Builder setRandomizationFactor(double randomizationFactor) {
this.randomizationFactor = randomizationFactor;
return this;
}
public Builder setMultiplier(double multiplier) {
this.multiplier = multiplier;
return this;
}
public Builder setMaxIntervalMillis(int maxIntervalMillis) {
this.maxIntervalMillis = maxIntervalMillis;
return this;
}
public Builder setMaxElapsedTimeMillis(int maxElapsedTimeMillis) {
this.maxElapsedTimeMillis = maxElapsedTimeMillis;
return this;
} }
/** Builds a new instance of {@link ExponentialBackOff}. */ /** Builds a new instance of {@link ExponentialBackOff}. */

View File

@ -15,9 +15,11 @@ 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.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.generics.BackOff;
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;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -139,6 +141,30 @@ public class TestDefaultBotSession {
session.stop(); session.stop();
} }
@Test
public void testDefaultBotSessionWithCustomExponentialBackOff() {
ExponentialBackOff ex = new ExponentialBackOff.Builder()
.setInitialIntervalMillis(500)
.setRandomizationFactor(0.5)
.setMultiplier(1.5)
.setMaxIntervalMillis(900000)
.setMaxElapsedTimeMillis(3600000)
.build();
DefaultBotOptions options = new DefaultBotOptions();
options.setBackOff(ex);
DefaultBotSession session = new DefaultBotSession();
session.setOptions(options);
}
@Test
public void testDefaultBotSessionWithCustomConstantBackOff() {
DefaultBotOptions options = new DefaultBotOptions();
ConstantBackOff backOff = new ConstantBackOff(3000);
options.setBackOff(backOff);
DefaultBotSession session = new DefaultBotSession();
session.setOptions(options);
}
private Update[] createFakeUpdates(int count) { private Update[] createFakeUpdates(int count) {
return IntStream.range(0, count).mapToObj(x -> { return IntStream.range(0, count).mapToObj(x -> {
Update mock = Mockito.mock(Update.class); Update mock = Mockito.mock(Update.class);
@ -178,4 +204,26 @@ public class TestDefaultBotSession {
session.setOptions(new DefaultBotOptions()); session.setOptions(new DefaultBotOptions());
return session; return session;
} }
private static class ConstantBackOff implements BackOff {
private long backOffMillis;
ConstantBackOff() {
new ConstantBackOff(3000);
}
ConstantBackOff(long millis) {
this.backOffMillis = millis;
}
@Override
public void reset() {
}
@Override
public long nextBackOffMillis() {
return backOffMillis;
}
}
} }