Update BOT API 2.3.1

This commit is contained in:
Rubenlagus 2016-12-03 21:17:03 +01:00
parent 483c2d649b
commit 8a177b0ab6
17 changed files with 221 additions and 16 deletions

View File

@ -31,8 +31,8 @@ Just import add the library to your project with one of these options:
</dependency>
```
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`.

View File

@ -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]]**
**[[How to update to version 2.4.4|How-To-Update#2.4.4]]**
### <a id="2.4.4.1"></a>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`

View File

@ -11,13 +11,13 @@ First you need ot get the library and add it to your project. There are few poss
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4.3</version>
<version>2.4.4.1</version>
</dependency>
```
* 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).

View File

@ -10,7 +10,6 @@
* `getPersonal` from `AnswerInlineQuery`. Use `isPersonal` instead.
* `FILEBASEURL` from `File`. Use `getFileUrl` instead.
### <a id="2.4.4"></a>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))

View File

@ -7,7 +7,7 @@
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<packaging>pom</packaging>
<version>2.4.4</version>
<version>2.4.4.1</version>
<modules>
<module>telegrambots</module>
@ -24,6 +24,6 @@
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<bots.version>2.4.4</bots.version>
<bots.version>2.4.4.1</bots.version>
</properties>
</project>

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId>
<version>2.4.4</version>
<version>2.4.4.1</version>
<packaging>jar</packaging>
<name>Telegram Bots Meta</name>

View File

@ -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";
}

View File

@ -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<Boolean>{
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<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Boolean>>(){});
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{}";
}
}

View File

@ -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<ArrayList<Update>>{
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<ArrayList<Update>>{
*/
@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<String> allowedUpdates;
public GetUpdates() {
super();
@ -79,6 +93,15 @@ public class GetUpdates extends BotApiMethod<ArrayList<Update>>{
return this;
}
public List<String> getAllowedUpdates() {
return allowedUpdates;
}
public GetUpdates setAllowedUpdates(List<String> allowedUpdates) {
this.allowedUpdates = allowedUpdates;
return this;
}
@Override
public String getMethod() {
return PATH;
@ -110,6 +133,7 @@ public class GetUpdates extends BotApiMethod<ArrayList<Update>>{
"offset=" + offset +
", limit=" + limit +
", timeout=" + timeout +
", allowedUpdates=" + allowedUpdates +
'}';
}
}

View File

@ -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 bots server,
* and higher values to increase your bots 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<String> 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<String> getAllowedUpdates() {
return allowedUpdates;
}
public SetWebhook setAllowedUpdates(List<String> allowedUpdates) {
this.allowedUpdates = allowedUpdates;
return this;
}
@Override
public String toString() {
return "SetWebhook{" +
"url='" + url + '\'' +
", certificateFile='" + certificateFile + '\'' +
", maxConnections=" + maxConnections +
", allowedUpdates=" + allowedUpdates +
'}';
}
}

View File

@ -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<String> 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<String> getAllowedUpdates() {
return allowedUpdates;
}
}

View File

@ -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<Message> sentCallback) throws TelegramApiException {
@ -512,6 +520,16 @@ public abstract class AbsSender {
sendApiMethodAsync(sendGame, sentCallback);
}
public final void deleteWebhook(DeleteWebhook deleteWebhook, SentCallback<Boolean> 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;

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4.4</version>
<version>2.4.4.1</version>
<packaging>jar</packaging>
<name>Telegram Bots</name>
@ -65,7 +65,7 @@
<json.version>20160810</json.version>
<jackson.version>2.8.5</jackson.version>
<commonio.version>2.5</commonio.version>
<bots.version>2.4.4</bots.version>
<bots.version>2.4.4.1</bots.version>
</properties>
<dependencyManagement>

View File

@ -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;

View File

@ -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<String> 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<String> getAllowedUpdates() {
return allowedUpdates;
}
public void setAllowedUpdates(List<String> 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

View File

@ -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()) {

View File

@ -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);