From 13e45139f416af94064ea259ada49d20dd9e2daa Mon Sep 17 00:00:00 2001 From: dartwata Date: Wed, 13 Jul 2016 16:39:51 +0800 Subject: [PATCH 1/3] Message filter for TelegramLongPollingCommandBot --- .../bots/TelegramLongPollingCommandBot.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index f9f8091f..d9c81d35 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -32,7 +32,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB public final void onUpdateReceived(Update update) { if (update.hasMessage()) { Message message = update.getMessage(); - if (message.isCommand()) { + if ((message.isCommand()) && (filter(message))) { if (commandRegistry.executeCommand(this, message)) { return; } @@ -41,6 +41,19 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB processNonCommandUpdate(update); } + /** + * function message filter. + * Override this function in your bot implementation to filter messages with commands + * + * For example, if you want to prevent commands execution incoming from group chat: + * # + * # return !message.getChat().isGroupChat(); + * # + */ + protected boolean filter(Message message) { + return true; + } + @Override public final boolean register(BotCommand botCommand) { return commandRegistry.register(botCommand); From cffe1519f5e6d87978a0200f7e287d0dc9d391e1 Mon Sep 17 00:00:00 2001 From: Ruben Bermudez Date: Wed, 13 Jul 2016 20:17:55 +0200 Subject: [PATCH 2/3] Revert "Message filter for TelegramLongPollingCommandBot" --- .../bots/TelegramLongPollingCommandBot.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index d9c81d35..f9f8091f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -32,7 +32,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB public final void onUpdateReceived(Update update) { if (update.hasMessage()) { Message message = update.getMessage(); - if ((message.isCommand()) && (filter(message))) { + if (message.isCommand()) { if (commandRegistry.executeCommand(this, message)) { return; } @@ -41,19 +41,6 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB processNonCommandUpdate(update); } - /** - * function message filter. - * Override this function in your bot implementation to filter messages with commands - * - * For example, if you want to prevent commands execution incoming from group chat: - * # - * # return !message.getChat().isGroupChat(); - * # - */ - protected boolean filter(Message message) { - return true; - } - @Override public final boolean register(BotCommand botCommand) { return commandRegistry.register(botCommand); From 1344a68b593c9d861993b3aa1eec2ff66d80c1f0 Mon Sep 17 00:00:00 2001 From: Ruben Bermudez Date: Fri, 12 Aug 2016 21:30:58 +0200 Subject: [PATCH 3/3] 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 --- HOWTO.md | 50 +--------- README.md | 2 +- pom.xml | 2 +- .../api/methods/updates/GetWebhookInfo.java | 64 +++++++++++++ .../telegrambots/api/objects/WebhookInfo.java | 94 +++++++++++++++++++ .../telegram/telegrambots/bots/AbsSender.java | 17 +++- 6 files changed, 177 insertions(+), 52 deletions(-) create mode 100644 src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java create mode 100644 src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java 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 {