Add Get webhook information method

This commit is contained in:
Rubenlagus 2016-08-12 21:23:30 +02:00
parent 5db0516516
commit 6421d624c8
3 changed files with 174 additions and 1 deletions

View File

@ -0,0 +1,64 @@
package org.telegram.telegrambots.api.methods.updates;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.WebhookInfo;
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 2.1
* @brief Return webhook information for current bot.
*
* If webhook is not configured, this method raise an exception
*
* @date 12 of August of 2016
*/
public class GetWebhookInfo extends BotApiMethod<WebhookInfo> {
public static final String PATH = "getwebhookinfo";
public GetWebhookInfo() {
}
@Override
public String toString() {
return "GetWebhookInfo{}";
}
@Override
public String getPath() {
return PATH;
}
@Override
public WebhookInfo deserializeResponse(JSONObject answer) {
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new WebhookInfo(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeStringField(METHOD_FIELD, PATH);
gen.writeEndObject();
gen.flush();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
@Override
public JSONObject toJson() {
return new JSONObject();
}
}

View File

@ -0,0 +1,94 @@
package org.telegram.telegrambots.api.objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.api.interfaces.IBotApiObject;
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 2.1
* @brief Information about configured webhook
* @date 12 of August of 2016
*/
public class WebhookInfo implements IBotApiObject {
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 LASTERRORDATE_FIELD = "last_error_date";
private static final String LASTERRORMESSAGE_FIELD = "last_error_message";
@JsonProperty(URL_FIELD)
private String url; ///< Url of the webhook
@JsonProperty(HASCUSTOMCERTIFICATE_FIELD)
private Boolean hasCustomCertificate; ///< True if the webhook use a self signed certificate
@JsonProperty(PENDINGUPDATESCOUNT_FIELD)
private Integer pendingUpdatesCount; ///< Number of updates pending to be delivered
@JsonProperty(LASTERRORDATE_FIELD)
private Integer lastErrorDate; ///< Optional. Date of that error
@JsonProperty(LASTERRORMESSAGE_FIELD)
private String lastErrorMessage; ///< Optional. Error message
public WebhookInfo() {
}
public WebhookInfo(JSONObject object) {
url = object.getString(URL_FIELD);
hasCustomCertificate = object.getBoolean(HASCUSTOMCERTIFICATE_FIELD);
pendingUpdatesCount = object.getInt(PENDINGUPDATESCOUNT_FIELD);
if (object.has(LASTERRORDATE_FIELD)) {
lastErrorDate = object.getInt(LASTERRORDATE_FIELD);
}
if (object.has(LASTERRORMESSAGE_FIELD)) {
lastErrorMessage = object.getString(LASTERRORMESSAGE_FIELD);
}
}
public String getUrl() {
return url;
}
public boolean isHasCustomCertificate() {
return hasCustomCertificate;
}
public int getPendingUpdatesCount() {
return pendingUpdatesCount;
}
public int getLastErrorDate() {
return lastErrorDate;
}
public String getLastErrorMessage() {
return lastErrorMessage;
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeStringField(URL_FIELD, url);
gen.writeBooleanField(HASCUSTOMCERTIFICATE_FIELD, hasCustomCertificate);
gen.writeNumberField(PENDINGUPDATESCOUNT_FIELD, pendingUpdatesCount);
if (lastErrorDate != null) {
gen.writeNumberField(LASTERRORDATE_FIELD, lastErrorDate);
}
if (lastErrorMessage != null) {
gen.writeStringField(LASTERRORMESSAGE_FIELD, lastErrorMessage);
}
gen.writeEndObject();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
}

View File

@ -44,6 +44,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.GetWebhookInfo;
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageCaption;
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup;
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageText;
@ -53,6 +54,7 @@ import org.telegram.telegrambots.api.objects.File;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.api.objects.UserProfilePhotos;
import org.telegram.telegrambots.api.objects.WebhookInfo;
import org.telegram.telegrambots.updateshandlers.SentCallback;
import java.io.IOException;
@ -270,6 +272,11 @@ public abstract class AbsSender {
return sendApiMethod(getMe);
}
public final WebhookInfo getWebhookInfo() throws TelegramApiException {
GetWebhookInfo getWebhookInfo = new GetWebhookInfo();
return sendApiMethod(getWebhookInfo);
}
// Send Requests Async
public final void sendMessageAsync(SendMessage sendMessage, SentCallback<Message> sentCallback) throws TelegramApiException {
@ -428,7 +435,6 @@ public abstract class AbsSender {
sendApiMethodAsync(getChatMemberCount, sentCallback);
}
public final void editMessageTextAsync(EditMessageText editMessageText, SentCallback<Message> sentCallback) throws TelegramApiException {
if (editMessageText == null) {
throw new TelegramApiException("Parameter editMessageText can not be null");
@ -504,6 +510,15 @@ public abstract class AbsSender {
sendApiMethodAsync(getMe, sentCallback);
}
public final void getWebhookInfoAsync(SentCallback<WebhookInfo> sentCallback) throws TelegramApiException {
if (sentCallback == null) {
throw new TelegramApiException("Parameter sentCallback can not be null");
}
GetWebhookInfo getWebhookInfo = new GetWebhookInfo();
sendApiMethodAsync(getWebhookInfo, sentCallback);
}
// Specific Send Requests
public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException {