diff --git a/README.md b/README.md index 08b9af9e..1967396f 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ Just import add the library to your project with one of these options: ``` - 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.4.4) - 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.4.4) + 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.4.4.1) + 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.4.4.1) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index bfb0bf44..0e394c05 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -17,4 +17,10 @@ 4. Added field `force` and `disable_edit_message` to `SetGameScore`, removed `edit_message` one. 5. Added `channel_post` and `edited_channel_post` to `Update` object. -**[[How to update to version 2.4.4|How-To-Update#2.4.4]]** \ No newline at end of file +**[[How to update to version 2.4.4|How-To-Update#2.4.4]]** + +### 2.4.4.1 ### +1. New `max_connections` in `setWebhook` method. +2. New `allowed_updates` in `setWebhook` and `getUpdates` +3. New `deleteWebhook` method +4. Added new configs to DefaultBotOptions to handle `max_connections` and `allowed_updates` \ No newline at end of file diff --git a/TelegramBots.wiki/Getting-Started.md b/TelegramBots.wiki/Getting-Started.md index 70fa7a54..7b74bf20 100644 --- a/TelegramBots.wiki/Getting-Started.md +++ b/TelegramBots.wiki/Getting-Started.md @@ -11,13 +11,13 @@ First you need ot get the library and add it to your project. There are few poss org.telegram telegrambots - 2.4.3 + 2.4.4.1 ``` * With **Gradle**: ```groovy - compile group: 'org.telegram', name: 'telegrambots', version: '2.4.3' + compile group: 'org.telegram', name: 'telegrambots', version: '2.4.4.1' ``` 2. Don't like **Maven Central Repository**? It can also be taken from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots). diff --git a/TelegramBots.wiki/How-To-Update.md b/TelegramBots.wiki/How-To-Update.md index 0cd2c929..7b51a2a4 100644 --- a/TelegramBots.wiki/How-To-Update.md +++ b/TelegramBots.wiki/How-To-Update.md @@ -10,7 +10,6 @@ * `getPersonal` from `AnswerInlineQuery`. Use `isPersonal` instead. * `FILEBASEURL` from `File`. Use `getFileUrl` instead. - ### To version 2.4.4 ### 1. Replace `ReplyKeyboardHide` by `ReplyKeyboardRemove` and its field `hideKeyboard` by `removeKeyboard` (remember getter and setters) 2. Replace usage of `edit_message` by `disable_edit_message` (see [this post](https://telegram.me/BotNews/22)) diff --git a/pom.xml b/pom.xml index 97c9fbcf..83c78ffd 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots pom - 2.4.4 + 2.4.4.1 telegrambots @@ -24,6 +24,6 @@ true - 2.4.4 + 2.4.4.1 \ No newline at end of file diff --git a/telegrambots-meta/pom.xml b/telegrambots-meta/pom.xml index 5df2fd0a..ea2149da 100644 --- a/telegrambots-meta/pom.xml +++ b/telegrambots-meta/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots-meta - 2.4.4 + 2.4.4.1 jar Telegram Bots Meta diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/AllowedUpdates.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/AllowedUpdates.java new file mode 100644 index 00000000..c921a4c8 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/AllowedUpdates.java @@ -0,0 +1,15 @@ +package org.telegram.telegrambots.api.methods.updates; + +/** + * @author Ruben Bermudez + * @version 1.0 + */ +public final class AllowedUpdates { + public static final String MESSAGE = "message"; + public static final String EDITEDMESSAGE = "edited_message"; + public static final String CHANNELPOST = "channel_post"; + public static final String EDITEDCHANNELPOST = "edited_channel_post"; + public static final String INLINEQUERY = "inline_query"; + public static final String CHOSENINLINERESULT = "chosen_inline_result"; + public static final String CALLBACKQUERY = "callback_query"; +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/DeleteWebhook.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/DeleteWebhook.java new file mode 100644 index 00000000..3eb13b23 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/DeleteWebhook.java @@ -0,0 +1,55 @@ +package org.telegram.telegrambots.api.methods.updates; + +import com.fasterxml.jackson.core.type.TypeReference; + +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Use this method to receive incoming updates using long polling (wiki). An Array of Update + * objects is returned. + * @date 20 of June of 2015 + */ +public class DeleteWebhook extends BotApiMethod{ + public static final String PATH = "deleteWebhook"; + + public DeleteWebhook() { + super(); + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws + TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error deleting webhook", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + } + + @Override + public String toString() { + return "DeleteWebhook{}"; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/GetUpdates.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/GetUpdates.java index b7fa957d..d67d27f5 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/GetUpdates.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/GetUpdates.java @@ -11,6 +11,7 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.ArrayList; +import java.util.List; /** * @author Ruben Bermudez @@ -25,6 +26,8 @@ public class GetUpdates extends BotApiMethod>{ private static final String OFFSET_FIELD = "offset"; private static final String LIMIT_FIELD = "limit"; private static final String TIMEOUT_FIELD = "timeout"; + private static final String ALLOWEDUPDATES_FIELD = "allowed_updates"; + /** * Optional. Identifier of the first update to be returned. Must be greater by one than the * highest among the identifiers of previously received updates. By default, updates starting @@ -47,6 +50,17 @@ public class GetUpdates extends BotApiMethod>{ */ @JsonProperty(TIMEOUT_FIELD) private Integer timeout; + /** + * List the types of updates you want your bot to receive. + * For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive + * updates of these types. Specify an empty list to receive all updates regardless of type (default). + * If not specified, the previous setting will be used. + * + * Please note that this parameter doesn't affect updates created before the call to the setWebhook, + * so unwanted updates may be received for a short period of time. + */ + @JsonProperty(ALLOWEDUPDATES_FIELD) + private List allowedUpdates; public GetUpdates() { super(); @@ -79,6 +93,15 @@ public class GetUpdates extends BotApiMethod>{ return this; } + public List getAllowedUpdates() { + return allowedUpdates; + } + + public GetUpdates setAllowedUpdates(List allowedUpdates) { + this.allowedUpdates = allowedUpdates; + return this; + } + @Override public String getMethod() { return PATH; @@ -110,6 +133,7 @@ public class GetUpdates extends BotApiMethod>{ "offset=" + offset + ", limit=" + limit + ", timeout=" + timeout + + ", allowedUpdates=" + allowedUpdates + '}'; } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/SetWebhook.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/SetWebhook.java index 31ae5555..e80e1ee4 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/SetWebhook.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/updates/SetWebhook.java @@ -1,5 +1,7 @@ package org.telegram.telegrambots.api.methods.updates; +import java.util.List; + /** * @author Ruben Bermudez * @version 1.0 @@ -14,9 +16,27 @@ public class SetWebhook { public static final String URL_FIELD = "url"; public static final String CERTIFICATE_FIELD = "certificate"; + public static final String MAXCONNECTIONS_FIELD = "max_connections"; + public static final String ALLOWEDUPDATES_FIELD = "allowed_updates"; private String url; ///< Optional. HTTPS url to send updates to. Use an empty string to remove webhook integration private String certificateFile; ///< Optional. Upload your public key certificate so that the root certificate in use can be checked + /** + * Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. + * Defaults to 40. Use lower values to limit the load on your bot‘s server, + * and higher values to increase your bot’s throughput. + */ + private Integer maxConnections; + /** + * List the types of updates you want your bot to receive. + * For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive + * updates of these types. Specify an empty list to receive all updates regardless of type (default). + * If not specified, the previous setting will be used. + * + * Please note that this parameter doesn't affect updates created before the call to the setWebhook, + * so unwanted updates may be received for a short period of time. + */ + private List allowedUpdates; public SetWebhook() { this.url = ""; @@ -40,11 +60,31 @@ public class SetWebhook { return this; } + public Integer getMaxConnections() { + return maxConnections; + } + + public SetWebhook setMaxConnections(Integer maxConnections) { + this.maxConnections = maxConnections; + return this; + } + + public List getAllowedUpdates() { + return allowedUpdates; + } + + public SetWebhook setAllowedUpdates(List allowedUpdates) { + this.allowedUpdates = allowedUpdates; + return this; + } + @Override public String toString() { return "SetWebhook{" + "url='" + url + '\'' + ", certificateFile='" + certificateFile + '\'' + + ", maxConnections=" + maxConnections + + ", allowedUpdates=" + allowedUpdates + '}'; } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java index 4d5a1bd1..6a61a8f1 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.telegram.telegrambots.api.interfaces.BotApiObject; +import java.util.List; + /** * @author Ruben Bermudez * @version 2.4 @@ -15,6 +17,8 @@ public class WebhookInfo implements BotApiObject { private static final String URL_FIELD = "url"; private static final String HASCUSTOMCERTIFICATE_FIELD = "has_custom_certificate"; private static final String PENDINGUPDATESCOUNT_FIELD = "pending_updates_count"; + private static final String MAXCONNECTIONS_FIELD = "max_connections"; + private static final String ALLOWEDUPDATES_FIELD = "allowed_updates"; private static final String LASTERRORDATE_FIELD = "last_error_date"; private static final String LASTERRORMESSAGE_FIELD = "last_error_message"; @@ -28,6 +32,10 @@ public class WebhookInfo implements BotApiObject { private Integer lastErrorDate; ///< Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook @JsonProperty(LASTERRORMESSAGE_FIELD) private String lastErrorMessage; ///< Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook + @JsonProperty(MAXCONNECTIONS_FIELD) + private Integer maxConnections; ///< Optional. Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery + @JsonProperty(ALLOWEDUPDATES_FIELD) + private List allowedUpdates; ///< Optional. A list of update types the bot is subscribed to. Defaults to all update types public WebhookInfo() { super(); @@ -52,4 +60,12 @@ public class WebhookInfo implements BotApiObject { public String getLastErrorMessage() { return lastErrorMessage; } + + public Integer getMaxConnections() { + return maxConnections; + } + + public List getAllowedUpdates() { + return allowedUpdates; + } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index cba8c75a..a438dccd 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -28,6 +28,7 @@ import org.telegram.telegrambots.api.methods.send.SendSticker; import org.telegram.telegrambots.api.methods.send.SendVenue; import org.telegram.telegrambots.api.methods.send.SendVideo; import org.telegram.telegrambots.api.methods.send.SendVoice; +import org.telegram.telegrambots.api.methods.updates.DeleteWebhook; import org.telegram.telegrambots.api.methods.updates.GetWebhookInfo; import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageCaption; import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup; @@ -241,6 +242,13 @@ public abstract class AbsSender { return sendApiMethod(sendGame); } + public final Boolean deleteWebhook(DeleteWebhook deleteWebhook) throws TelegramApiException { + if(deleteWebhook == null){ + throw new TelegramApiException("Parameter deleteWebhook can not be null"); + } + return sendApiMethod(deleteWebhook); + } + // Send Requests Async public final void sendMessageAsync(SendMessage sendMessage, SentCallback sentCallback) throws TelegramApiException { @@ -512,6 +520,16 @@ public abstract class AbsSender { sendApiMethodAsync(sendGame, sentCallback); } + public final void deleteWebhook(DeleteWebhook deleteWebhook, SentCallback sentCallback) throws TelegramApiException { + if (deleteWebhook == null) { + throw new TelegramApiException("Parameter deleteWebhook can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(deleteWebhook, sentCallback); + } + // Specific Send Requests public abstract Message sendDocument(SendDocument sendDocument) throws TelegramApiException; diff --git a/telegrambots/pom.xml b/telegrambots/pom.xml index 3cb9d444..868344f1 100644 --- a/telegrambots/pom.xml +++ b/telegrambots/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots - 2.4.4 + 2.4.4.1 jar Telegram Bots @@ -65,7 +65,7 @@ 20160810 2.8.5 2.5 - 2.4.4 + 2.4.4.1 diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index f008f1a7..4537a1df 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -52,7 +52,7 @@ import java.util.concurrent.TimeUnit; * @date 14 of January of 2016 */ @SuppressWarnings("unused") -public abstract class DefaultAbsSender extends AbsSender{ +public abstract class DefaultAbsSender extends AbsSender { private static final ContentType TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", StandardCharsets.UTF_8); private final ExecutorService exe; diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java index 86b98363..4f2aebcb 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultBotOptions.java @@ -3,6 +3,8 @@ package org.telegram.telegrambots.bots; import org.apache.http.client.config.RequestConfig; import org.telegram.telegrambots.generics.BotOptions; +import java.util.List; + /** * @author Ruben Bermudez * @version 1.0 @@ -12,6 +14,8 @@ import org.telegram.telegrambots.generics.BotOptions; public class DefaultBotOptions implements BotOptions { private int maxThreads; ///< Max number of threads used for async methods executions (default 1) private RequestConfig requestConfig; + private Integer maxWebhookConnections; + private List allowedUpdates; public DefaultBotOptions() { maxThreads = 1; @@ -29,6 +33,22 @@ public class DefaultBotOptions implements BotOptions { return requestConfig; } + public Integer getMaxWebhookConnections() { + return maxWebhookConnections; + } + + public void setMaxWebhookConnections(Integer maxWebhookConnections) { + this.maxWebhookConnections = maxWebhookConnections; + } + + public List getAllowedUpdates() { + return allowedUpdates; + } + + public void setAllowedUpdates(List allowedUpdates) { + this.allowedUpdates = allowedUpdates; + } + /** * @implSpec Default implementation assumes no proxy is needed and sets a 75secs timoute * @param requestConfig Request config to be used in all Http requests 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 827855d6..b4df22e5 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java @@ -10,6 +10,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.telegram.telegrambots.ApiConstants; @@ -50,6 +51,12 @@ public abstract class TelegramWebhookBot extends DefaultAbsSender implements Web httppost.setConfig(botOptions.getRequestConfig()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SetWebhook.URL_FIELD, url); + if (botOptions.getMaxWebhookConnections() != null) { + builder.addTextBody(SetWebhook.MAXCONNECTIONS_FIELD, botOptions.getMaxWebhookConnections().toString()); + } + if (botOptions.getAllowedUpdates() != null) { + builder.addTextBody(SetWebhook.ALLOWEDUPDATES_FIELD, new JSONArray(botOptions.getMaxWebhookConnections()).toString()); + } if (publicCertificatePath != null) { File certificate = new File(publicCertificatePath); if (certificate.exists()) { 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 d33fd24c..42e32f49 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java @@ -158,10 +158,15 @@ public class DefaultBotSession implements BotSession { setPriority(Thread.MIN_PRIORITY); while (running) { try { - GetUpdates request = new GetUpdates(); - request.setLimit(100); - request.setTimeout(ApiConstants.GETUPDATES_TIMEOUT); - request.setOffset(lastReceivedUpdate + 1); + GetUpdates request = new GetUpdates() + .setLimit(100) + .setTimeout(ApiConstants.GETUPDATES_TIMEOUT) + .setOffset(lastReceivedUpdate + 1); + + if (options.getAllowedUpdates() != null) { + request.setAllowedUpdates(options.getAllowedUpdates()); + } + String url = ApiConstants.BASE_URL + token + "/" + GetUpdates.PATH; //http client HttpPost httpPost = new HttpPost(url);