From c7562823289107a76530ba3d05cc6844349dcb58 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Thu, 17 Nov 2016 03:31:32 +0100 Subject: [PATCH] Fix bug --- TelegramBots.wiki/Changelog.md | 3 ++- TelegramBots.wiki/How-To-Update.md | 1 + .../telegrambots/updateshandlers/SentCallback.java | 10 +++++----- .../telegram/telegrambots/bots/DefaultAbsSender.java | 10 ++++------ .../telegram/telegrambots/bots/TelegramWebhookBot.java | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index aeb6b287..ea7af23b 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -5,6 +5,7 @@ 4. Added extra validation to methods before performing requests. 5. BotOptions has been renamed ot DefaultBotOptions. It allows now to set number of threads for async methods execution and the complete `RequestConfig` for customization purpose. 6. Added convenient method for `setChatId` using just a `Long` value instead of an String. -7. Moved to MIT license +7. In `SentCallback` method `onError` changed second parameter to `TelegramApiRequestException` and `onResult` now receives the deserialized answer (of type `T`) instead of a `JSONObject` as second parameter +8. Moved to MIT license **[[How to update to version 2.4.1|How-To-Update#2.4.1]]** \ No newline at end of file diff --git a/TelegramBots.wiki/How-To-Update.md b/TelegramBots.wiki/How-To-Update.md index fd8f7c57..efdf8c81 100644 --- a/TelegramBots.wiki/How-To-Update.md +++ b/TelegramBots.wiki/How-To-Update.md @@ -4,6 +4,7 @@ ```java ApiContextInitializer.init(); ``` +3. In `SentCallback`, update parameter types of `onResult` and `onError`. Inside those two method, you don't need to deserialize anything now, it comes already done. 3. **Deprecated** (will be removed in next version): * `org.telegram.telegrambots.bots.BotOptions`. Use `org.telegram.telegrambots.bots.DefaultBotOptions` instead. * `getPersonal` from `AnswerInlineQuery`. Use `isPersonal` instead. diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/updateshandlers/SentCallback.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/updateshandlers/SentCallback.java index f2ec83ce..d7a3bf56 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/updateshandlers/SentCallback.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/updateshandlers/SentCallback.java @@ -1,7 +1,7 @@ package org.telegram.telegrambots.updateshandlers; -import org.json.JSONObject; import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import java.io.Serializable; @@ -15,16 +15,16 @@ public interface SentCallback { /** * Called when the request is successful * @param method Method executed - * @param jsonObject Answer from Telegram server + * @param response Answer from Telegram server */ - void onResult(BotApiMethod method, JSONObject jsonObject); + void onResult(BotApiMethod method, T response); /** * Called when the request fails * @param method Method executed - * @param jsonObject Answer from Telegram server (contains error information) + * @param apiException Answer from Telegram server (contains error information) */ - void onError(BotApiMethod method, JSONObject jsonObject); + void onError(BotApiMethod method, TelegramApiRequestException apiException); /** * Called when the http request throw an exception diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index 40eef66d..f008f1a7 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -18,7 +18,6 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; -import org.json.JSONObject; import org.telegram.telegrambots.ApiConstants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.methods.send.SendAudio; @@ -643,12 +642,11 @@ public abstract class DefaultAbsSender extends AbsSender{ HttpEntity ht = response.getEntity(); BufferedHttpEntity buf = new BufferedHttpEntity(ht); String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); - - JSONObject jsonObject = new JSONObject(responseContent); - if (!jsonObject.getBoolean(ApiConstants.RESPONSE_FIELD_OK)) { - callback.onError(method, jsonObject); + try { + callback.onResult(method, method.deserializeResponse(responseContent)); + } catch (TelegramApiRequestException e) { + callback.onError(method, e); } - callback.onResult(method, jsonObject); } } catch (IOException | TelegramApiValidationException e) { callback.onException(method, e); diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java index 2bec6770..827855d6 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java @@ -29,7 +29,7 @@ import java.nio.charset.StandardCharsets; * webhook * @date 14 of January of 2016 */ -public abstract class TelegramWebhookBot extends AbsSender implements WebhookBot { +public abstract class TelegramWebhookBot extends DefaultAbsSender implements WebhookBot { private final DefaultBotOptions botOptions; public TelegramWebhookBot() { @@ -37,7 +37,7 @@ public abstract class TelegramWebhookBot extends AbsSender implements WebhookBot } public TelegramWebhookBot(DefaultBotOptions options) { - super(); + super(options); this.botOptions = options; }