Add getWebhookInfo method (#137)

* Fix issue in HOWTO (#134)

* Message filter for TelegramLongPollingCommandBot

* Revert "Message filter for TelegramLongPollingCommandBot"

* Fix issue in HOWTO

I've removed the code for custom keyboards and linked to the weather bot in the example repo

- it was not fully working (weatherbot is)
- it was just an approach on how to use custom keyboards but was not showing all details (weatherbot has much more details)
- why managing multiple code "snippets"? The code can change but the HOWTO still works

* Add Get webhook information method

* Update to version 2.3.4
This commit is contained in:
Ruben Bermudez 2016-08-12 21:30:58 +02:00 committed by GitHub
parent 0f78861f05
commit 1344a68b59
6 changed files with 177 additions and 52 deletions

View File

@ -183,55 +183,7 @@ public void onUpdateReceived(Update update) {
Question: <a name="question_how_to_use_custom_keyboards"/>
<b>How to use custom keyboards?</b>
Answer:
```java
//first, create a normal SendMessage object. Our CutsomKeyboard is attached to this object. But don't forget to assign a text. Otherwise the user get's no message
SendMessage sendMessageRequest = this.selectLanguage();
sendMessageRequest.setChatId(message.getChatId().toString());
sendMessageRequest.setText("Change language");
//ready top takeoff!
sendMessage(sendMessageRequest);
```
The selectLanguage() method could look like this (you also could put the code of the extra method in your main() )
```java
public static List<KeyboardRow> selectLanguage(Message message){
/* changed from "List<List<String>>" to "List<KeyboardRow>"
* Now we have just a one dimension array. Like a stack or something like that
*/
List<KeyboardRow> rows = new ArrayList<KeyboardRow>();
rows.add(ReplyKeyboardFabricator.getHeader(message));
//Instead of a list that collects our strings, or "buttons" for each row, now we have a own Object for that
KeyboardRow row = new KeyboardRow();
row.add(LocalisationService.getInstance().getString("change_language", DatabaseManager.getInstance().getUserLanguage(EchoHandler.getUserId(message))));
rows.add(row);
row = new KeyboardRow();
row.add("🇦🇹 Deutsch (Östereich)");
rows.add(row);
//I just reused the object above. Of cource you could also create a new Keyboardrow for each real row
row = new KeyboardRow();
row.add("🇩🇪 Deutsch (Deutschland)");
rows.add(row);
row = new KeyboardRow();
row.add("🇧🇷 Português");
rows.add(row);
row = new KeyboardRow();
row.add("🇺🇸 English (United States)");
rows.add(row);
return rows;
}
```
For more example on this, please have a look at [this](https://github.com/rubenlagus/TelegramBotsExample/blob/master/src/main/java/org/telegram/updateshandlers/WeatherHandlers.java) bot in the example repo
Answer: You can look at the [source code](https://github.com/rubenlagus/TelegramBotsExample/blob/master/src/main/java/org/telegram/updateshandlers/WeatherHandlers.java) for [@Weatherbot](https://telegram.me/weatherbot) in the [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) repo. It should contain all necessary information about using custom keyboards.
<a name="question_how_to_compile"/>
Question: <b>How can I compile my project?</b>

View File

@ -15,7 +15,7 @@ Both ways are supported, but I recommend long polling method.
## Usage
Just import add the library to your project using [Maven, Gradle, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.7) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.3.7)
Just import add the library to your project using [Maven, Gradle, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.4) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.4)
In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`.

View File

@ -6,7 +6,7 @@
<packaging>jar</packaging>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.3.3.7</version>
<version>2.3.4</version>
<name>Telegram Bots</name>
<url>https://telegram.me/JavaBotsApi</url>

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 {