diff --git a/HOWTO.md b/HOWTO.md index 50857c4b..fac0dc7b 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -183,55 +183,7 @@ public void onUpdateReceived(Update update) { Question: How to use custom keyboards? - 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 selectLanguage(Message message){ - /* changed from "List>" to "List" - * Now we have just a one dimension array. Like a stack or something like that - */ - List rows = new ArrayList(); - 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. Question: How can I compile my project? diff --git a/README.md b/README.md index 7048c3b9..ecc0533b 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/pom.xml b/pom.xml index c158fa39..a7e72bf0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ jar org.telegram telegrambots - 2.3.3.7 + 2.3.4 Telegram Bots https://telegram.me/JavaBotsApi diff --git a/src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java b/src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java new file mode 100644 index 00000000..ef89fc24 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java @@ -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 { + 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(); + } +} diff --git a/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java b/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java new file mode 100644 index 00000000..63fc70f9 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java @@ -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); + } +} diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index b56131c3..c88b0575 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -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 sentCallback) throws TelegramApiException { @@ -428,7 +435,6 @@ public abstract class AbsSender { sendApiMethodAsync(getChatMemberCount, sentCallback); } - public final void editMessageTextAsync(EditMessageText editMessageText, SentCallback 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 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 {