From 5903cd63381533c893a872a28bd4e200403dc90d Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sat, 24 Sep 2016 22:37:25 +0200 Subject: [PATCH] 1. File downloads 2. Update with latest api changes 3. Improve exceptions 4. Added validation in api methods 5. Moved to maven central --- HOWTO.md | 12 +- README.md | 16 ++- pom.xml | 45 ++++++- .../telegrambots/TelegramApiException.java | 40 ------ .../telegrambots/TelegramBotsApi.java | 31 ++--- .../api/interfaces/IValidable.java | 34 +++++ .../telegrambots/api/methods/ActionType.java | 28 ---- .../api/methods/AnswerCallbackQuery.java | 15 ++- .../api/methods/AnswerInlineQuery.java | 14 ++ .../api/methods/BotApiMethod.java | 4 +- .../api/methods/ForwardMessage.java | 26 ++-- .../telegrambots/api/methods/GetFile.java | 10 +- .../telegrambots/api/methods/GetMe.java | 5 + .../api/methods/GetUserProfilePhotos.java | 11 ++ .../methods/groupadministration/GetChat.java | 8 ++ .../GetChatAdministrators.java | 8 ++ .../groupadministration/GetChatMember.java | 11 ++ .../GetChatMemberCount.java | 8 ++ .../groupadministration/KickChatMember.java | 11 ++ .../groupadministration/LeaveChat.java | 8 ++ .../groupadministration/UnbanChatMember.java | 11 ++ .../api/methods/send/SendAudio.java | 69 +++------- .../api/methods/send/SendChatAction.java | 32 ++--- .../api/methods/send/SendContact.java | 60 +++------ .../api/methods/send/SendDocument.java | 50 +------- .../api/methods/send/SendLocation.java | 46 ++----- .../api/methods/send/SendMessage.java | 43 ++----- .../api/methods/send/SendPhoto.java | 50 +------- .../api/methods/send/SendSticker.java | 50 +------- .../api/methods/send/SendVenue.java | 52 +++----- .../api/methods/send/SendVideo.java | 50 +------- .../api/methods/send/SendVoice.java | 121 ++++-------------- .../api/methods/updates/GetWebhookInfo.java | 14 +- .../updatingmessages/EditMessageCaption.java | 20 +++ .../EditMessageReplyMarkup.java | 20 +++ .../updatingmessages/EditMessageText.java | 23 ++++ .../api/objects/CallbackQuery.java | 18 ++- .../telegrambots/api/objects/Chat.java | 14 ++ .../telegrambots/api/objects/File.java | 2 + .../telegrambots/api/objects/Message.java | 4 + .../telegrambots/api/objects/WebhookInfo.java | 14 +- .../api/objects/inlinequery/InlineQuery.java | 8 ++ .../InputContactMessageContent.java | 11 ++ .../InputLocationMessageContent.java | 11 ++ .../InputMessageContent.java | 3 +- .../InputTextMessageContent.java | 8 ++ .../InputVenueMessageContent.java | 17 +++ .../inlinequery/result/InlineQueryResult.java | 3 +- .../result/InlineQueryResultArticle.java | 18 +++ .../result/InlineQueryResultAudio.java | 45 ++++++- .../result/InlineQueryResultContact.java | 20 +++ .../result/InlineQueryResultDocument.java | 23 ++++ .../result/InlineQueryResultGif.java | 17 +++ .../result/InlineQueryResultLocation.java | 23 ++++ .../result/InlineQueryResultMpeg4Gif.java | 17 +++ .../result/InlineQueryResultPhoto.java | 17 +++ .../result/InlineQueryResultVenue.java | 27 ++++ .../result/InlineQueryResultVideo.java | 17 +++ .../result/InlineQueryResultVoice.java | 49 ++++++- .../chached/InlineQueryResultCachedAudio.java | 45 ++++++- .../InlineQueryResultCachedDocument.java | 20 +++ .../chached/InlineQueryResultCachedGif.java | 17 +++ .../InlineQueryResultCachedMpeg4Gif.java | 17 +++ .../chached/InlineQueryResultCachedPhoto.java | 17 +++ .../InlineQueryResultCachedSticker.java | 17 +++ .../chached/InlineQueryResultCachedVideo.java | 17 +++ .../chached/InlineQueryResultCachedVoice.java | 43 ++++++- .../replykeyboard/ForceReplyKeyboard.java | 8 ++ .../replykeyboard/InlineKeyboardMarkup.java | 13 ++ .../objects/replykeyboard/ReplyKeyboard.java | 3 +- .../replykeyboard/ReplyKeyboardHide.java | 16 ++- .../replykeyboard/ReplyKeyboardMarkup.java | 11 ++ .../buttons/InlineKeyboardButton.java | 46 ++++++- .../replykeyboard/buttons/KeyboardButton.java | 11 +- .../replykeyboard/buttons/KeyboardRow.java | 12 +- .../telegram/telegrambots/bots/AbsSender.java | 91 +++++++++++-- .../exceptions/TelegramApiException.java | 46 +++++++ .../TelegramApiRequestException.java | 62 +++++++++ .../TelegramApiValidationException.java | 53 ++++++++ .../updateshandlers/DownloadFileCallback.java | 42 ++++++ .../updatesreceivers/BotSession.java | 18 +-- .../updatesreceivers/Webhook.java | 12 +- 82 files changed, 1399 insertions(+), 680 deletions(-) delete mode 100644 src/main/java/org/telegram/telegrambots/TelegramApiException.java create mode 100644 src/main/java/org/telegram/telegrambots/api/interfaces/IValidable.java create mode 100644 src/main/java/org/telegram/telegrambots/exceptions/TelegramApiException.java create mode 100644 src/main/java/org/telegram/telegrambots/exceptions/TelegramApiRequestException.java create mode 100644 src/main/java/org/telegram/telegrambots/exceptions/TelegramApiValidationException.java create mode 100644 src/main/java/org/telegram/telegrambots/updateshandlers/DownloadFileCallback.java diff --git a/HOWTO.md b/HOWTO.md index c4a31f9d..b2721ffa 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -21,7 +21,17 @@ Create a new project for your bot, in the example below we are showing you how t If you don't know how to include a external .jar into your Eclipse project, maybe [this](https://www.youtube.com/watch?v=VWnfHkBgO1I) video is helpful for you -More information on how to use it with Gradle or Maven, can be found here [here](https://jitpack.io/#rubenlagus/TelegramBots) +You can use it with Maven or Gradle directly from Central repository, just this to your *pom.xml* file: + +```xml + + org.telegram + telegrambots + 2.4 + +``` + +You can also find it in a different repository, just in case, search [here](https://jitpack.io/#rubenlagus/TelegramBots). diff --git a/README.md b/README.md index e6456a41..9aa02808 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Telegram Bot Java Library [![Build Status](https://travis-ci.org/rubenlagus/TelegramBots.svg?branch=master)](https://travis-ci.org/rubenlagus/TelegramBots) [![Jitpack](https://jitpack.io/v/rubenlagus/TelegramBots.svg)](https://jitpack.io/#rubenlagus/TelegramBots) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.telegram/telegrambots/badge.svg)](http://mvnrepository.com/artifact/org.telegram/telegrambots) [![Telegram](http://trellobot.doomdns.org/telegrambadge.svg)](https://telegram.me/JavaBotsApi) A simple to use library to create Telegram Bots in Java @@ -15,7 +16,20 @@ 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.5) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.5) +Just import add the library to your project with one of these options: + + 1. Using Maven Central Repository: + +```xml + + org.telegram + telegrambots + 2.4 + +``` + + 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/v2.4) + 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.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 a78d0f91..7eb58d93 100644 --- a/pom.xml +++ b/pom.xml @@ -6,10 +6,10 @@ jar org.telegram telegrambots - 2.3.5 + 2.3.8.BETA-SNAPSHOT Telegram Bots - https://telegram.me/JavaBotsApi + https://github.com/rubenlagus/TelegramBots Easy to use library to create Telegram Bots @@ -19,6 +19,17 @@ + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + UTF-8 UTF-8 @@ -77,6 +88,11 @@ httpmime ${httpcompontents.version} + + commons-io + commons-io + 2.4 + @@ -86,6 +102,31 @@ ${project.build.directory}/test-classes ${project.basedir}/src/main/java + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + ossrh + https://oss.sonatype.org/ + true + + maven-clean-plugin 3.0.0 diff --git a/src/main/java/org/telegram/telegrambots/TelegramApiException.java b/src/main/java/org/telegram/telegrambots/TelegramApiException.java deleted file mode 100644 index d9653b9f..00000000 --- a/src/main/java/org/telegram/telegrambots/TelegramApiException.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.telegram.telegrambots; - -/** - * @author Ruben Bermudez - * @version 1.0 - * @brief Exception thrown when something goes wrong in the api - * @date 14 of January of 2016 - */ -public class TelegramApiException extends Exception { - private String apiResponse = null; - private Integer errorCode; - - public TelegramApiException(String message) { - super(message); - } - - public TelegramApiException(String message, String apiResponse, Integer errorCode) { - super(message); - this.apiResponse = apiResponse; - } - - public TelegramApiException(String message, Throwable cause) { - super(message, cause); - } - - public String getApiResponse() { - return apiResponse; - } - - @Override - public String toString() { - if (apiResponse == null) { - return super.toString(); - } else if (errorCode == null) { - return super.toString() + ": " + apiResponse; - } else { - return super.toString() + ": [" + errorCode + "] " + apiResponse; - } - } -} diff --git a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java index 0f0577f8..de295a1d 100644 --- a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java +++ b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java @@ -18,6 +18,7 @@ import org.telegram.telegrambots.api.methods.updates.SetWebhook; import org.telegram.telegrambots.bots.BotOptions; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramWebhookBot; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.updatesreceivers.BotSession; import org.telegram.telegrambots.updatesreceivers.Webhook; @@ -56,12 +57,12 @@ public class TelegramBotsApi { * @param externalUrl * @param internalUrl */ - public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiException { + public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiRequestException { if (externalUrl == null || externalUrl.isEmpty()) { - throw new TelegramApiException("Parameter externalUrl can not be null or empty"); + throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty"); } if (internalUrl == null || internalUrl.isEmpty()) { - throw new TelegramApiException("Parameter internalUrl can not be null or empty"); + throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty"); } this.useWebhook = true; @@ -78,12 +79,12 @@ public class TelegramBotsApi { * @param internalUrl * @param pathToCertificate Full path until .pem public certificate keys */ - public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiException { + public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiRequestException { if (externalUrl == null || externalUrl.isEmpty()) { - throw new TelegramApiException("Parameter externalUrl can not be null or empty"); + throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty"); } if (internalUrl == null || internalUrl.isEmpty()) { - throw new TelegramApiException("Parameter internalUrl can not be null or empty"); + throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty"); } this.useWebhook = true; this.extrenalUrl = fixExternalUrl(externalUrl); @@ -96,7 +97,7 @@ public class TelegramBotsApi { * Register a bot. The Bot Session is started immediately, and may be disconnected by calling close. * @param bot the bot to register */ - public BotSession registerBot(TelegramLongPollingBot bot) throws TelegramApiException { + public BotSession registerBot(TelegramLongPollingBot bot) throws TelegramApiRequestException { setWebhook(bot.getBotToken(), null, bot.getOptions()); return new BotSession(bot.getBotToken(), bot, bot.getOptions()); } @@ -105,7 +106,7 @@ public class TelegramBotsApi { * * @param bot */ - public void registerBot(TelegramWebhookBot bot) throws TelegramApiException { + public void registerBot(TelegramWebhookBot bot) throws TelegramApiRequestException { if (useWebhook) { webhook.registerWebhook(bot); setWebhook(bot.getBotToken(), bot.getBotPath(), bot.getOptions()); @@ -130,10 +131,10 @@ public class TelegramBotsApi { * @param botToken Bot token * @param publicCertificatePath Path to certificate public key * @param options Bot options - * @throws TelegramApiException If any error occurs setting the webhook + * @throws TelegramApiRequestException If any error occurs setting the webhook */ private static void setWebhook(String webHookURL, String botToken, - String publicCertificatePath, BotOptions options) throws TelegramApiException { + String publicCertificatePath, BotOptions options) throws TelegramApiRequestException { try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) { String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH; @@ -164,13 +165,13 @@ public class TelegramBotsApi { String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } } } catch (JSONException e) { - throw new TelegramApiException("Error deserializing setWebhook method response", e); + throw new TelegramApiRequestException("Error deserializing setWebhook method response", e); } catch (IOException e) { - throw new TelegramApiException("Error executing setWebook method", e); + throw new TelegramApiRequestException("Error executing setWebook method", e); } } @@ -180,9 +181,9 @@ public class TelegramBotsApi { * @param urlPath Url for the webhook or null to remove it * @param botOptions Bot Options */ - private void setWebhook(String botToken, String urlPath, BotOptions botOptions) throws TelegramApiException { + private void setWebhook(String botToken, String urlPath, BotOptions botOptions) throws TelegramApiRequestException { if (botToken == null) { - throw new TelegramApiException("Parameter botToken can not be null"); + throw new TelegramApiRequestException("Parameter botToken can not be null"); } String completeExternalUrl = urlPath == null ? "" : extrenalUrl + urlPath; setWebhook(completeExternalUrl, botToken, pathToCertificate, botOptions); diff --git a/src/main/java/org/telegram/telegrambots/api/interfaces/IValidable.java b/src/main/java/org/telegram/telegrambots/api/interfaces/IValidable.java new file mode 100644 index 00000000..b123b4d4 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/interfaces/IValidable.java @@ -0,0 +1,34 @@ +/* + * This file is part of TelegramBots. + * + * Foobar is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +package org.telegram.telegrambots.api.interfaces; + +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief TODO + * @date 16 of September of 2016 + */ +public interface IValidable { + /** + * Validates that mandatory fields are filled + * @throws TelegramApiValidationException If any mandatory field is empty + */ + void validate() throws TelegramApiValidationException; +} diff --git a/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java b/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java index fb107abe..db1a79cf 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java @@ -22,34 +22,6 @@ public enum ActionType { this.text = text; } - /** - * @deprecated Added for backward compatibility, will be dropped in next mayor release - * @param text text of the action - * @return ActionType - */ - @Deprecated - public static ActionType GetActionType(String text) throws IllegalArgumentException { - switch (text) { - case "typing": - return TYPING; - case "record_video": - return RECORDVIDEO; - case "record_audio": - return RECORDAUDIO; - case "upload_photo": - return UPLOADPHOTO; - case "upload_video": - return UPLOADVIDEO; - case "upload_audio": - return UPLOADAUDIO; - case "upload_document": - return UPLOADDOCUMENT; - case "find_location": - return FINDLOCATION; - } - throw new IllegalArgumentException(text + " doesn't match any know ActionType"); - } - @Override public String toString() { return text; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/AnswerCallbackQuery.java b/src/main/java/org/telegram/telegrambots/api/methods/AnswerCallbackQuery.java index ed930884..a0028544 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/AnswerCallbackQuery.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/AnswerCallbackQuery.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -28,11 +29,10 @@ public class AnswerCallbackQuery extends BotApiMethod { @JsonProperty(CALLBACKQUERYID_FIELD) private String callbackQueryId; ///< Unique identifier for the query to be answered @JsonProperty(TEXT_FIELD) - private String text; ///< Text of the notification. If not specified, nothing will be shown to the user + private String text; ///< Optional Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters @JsonProperty(SHOWALERT_FIELD) private Boolean showAlert; ///< Optional. If true, an alert will be shown by the client instead of a notificaiton at the top of the chat screen. Defaults to false. - public AnswerCallbackQuery() { super(); } @@ -87,6 +87,13 @@ public class AnswerCallbackQuery extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (callbackQueryId == null) { + throw new TelegramApiValidationException("CallbackQueryId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); @@ -111,8 +118,8 @@ public class AnswerCallbackQuery extends BotApiMethod { public String toString() { return "AnswerCallbackQuery{" + "callbackQueryId='" + callbackQueryId + '\'' + - ", text=" + text + - ", showAlert=" + showAlert + '\'' + + ", text='" + text + '\'' + + ", showAlert=" + showAlert + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java b/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java index 12192324..54ba81c7 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java @@ -8,6 +8,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.List; @@ -103,6 +104,19 @@ public class AnswerInlineQuery extends BotApiMethod { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (inlineQueryId == null) { + throw new TelegramApiValidationException("InlineQueryId can't be empty", this); + } + if (results == null) { + throw new TelegramApiValidationException("Results array can't be null", this); + } + for (InlineQueryResult result : results) { + result.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/BotApiMethod.java b/src/main/java/org/telegram/telegrambots/api/methods/BotApiMethod.java index f7f4e86b..d5f7be31 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/BotApiMethod.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/BotApiMethod.java @@ -1,8 +1,10 @@ package org.telegram.telegrambots.api.methods; import com.fasterxml.jackson.databind.JsonSerializable; + import org.json.JSONObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; import java.io.Serializable; @@ -12,7 +14,7 @@ import java.io.Serializable; * @brief A method of Telegram Bots Api that is fully supported in json format * @date 07 of September of 2015 */ -public abstract class BotApiMethod implements JsonSerializable, IToJson { +public abstract class BotApiMethod implements JsonSerializable, IToJson, IValidable { protected static final String METHOD_FIELD = "method"; /** diff --git a/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java b/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java index c7254102..31b4e4e5 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -47,12 +48,6 @@ public class ForwardMessage extends BotApiMethod { return this; } - @Deprecated - public ForwardMessage setFromChatId(Integer fromChatId) { - this.fromChatId = fromChatId.toString(); - return this; - } - public String getFromChatId() { return fromChatId; } @@ -75,12 +70,27 @@ public class ForwardMessage extends BotApiMethod { return disableNotification; } - public void enableNotification() { + public ForwardMessage enableNotification() { this.disableNotification = false; + return this; } - public void disableNotification() { + public ForwardMessage disableNotification() { this.disableNotification = true; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (fromChatId == null) { + throw new TelegramApiValidationException("FromChatId can't be empty", this); + } + if (messageId == null) { + throw new TelegramApiValidationException("MessageId can't be empty", this); + } } @Override diff --git a/src/main/java/org/telegram/telegrambots/api/methods/GetFile.java b/src/main/java/org/telegram/telegrambots/api/methods/GetFile.java index 2089f7ed..3d44d8c8 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/GetFile.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/GetFile.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.objects.File; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -25,7 +26,7 @@ import java.io.IOException; public class GetFile extends BotApiMethod { public static final String PATH = "getFile"; - public static final String FILEID_FIELD = "file_id"; + private static final String FILEID_FIELD = "file_id"; private String fileId; ///< File identifier to get info about public GetFile() { @@ -41,6 +42,13 @@ public class GetFile extends BotApiMethod { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (fileId == null) { + throw new TelegramApiValidationException("FileId can't be empty", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/GetMe.java b/src/main/java/org/telegram/telegrambots/api/methods/GetMe.java index 3d455cd1..56b65639 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/GetMe.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/GetMe.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.objects.User; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -38,6 +39,10 @@ public class GetMe extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/GetUserProfilePhotos.java b/src/main/java/org/telegram/telegrambots/api/methods/GetUserProfilePhotos.java index fb5ef555..80e68a5c 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/GetUserProfilePhotos.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/GetUserProfilePhotos.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.objects.UserProfilePhotos; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -88,6 +89,16 @@ public class GetUserProfilePhotos extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (userId == null) { + throw new TelegramApiValidationException("UserId parameter can't be empty", this); + } + if (offset == null) { + throw new TelegramApiValidationException("Offset parameter can't be empty", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java index 74e3af2b..c90a36fa 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java @@ -8,6 +8,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -56,6 +57,13 @@ public class GetChat extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java index cd910ada..ca98c41a 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.ChatMember; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.ArrayList; @@ -67,6 +68,13 @@ public class GetChatAdministrators extends BotApiMethod> { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java index a14a270f..651f85a3 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java @@ -8,6 +8,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.ChatMember; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -69,6 +70,16 @@ public class GetChatMember extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + if (userId == null) { + throw new TelegramApiValidationException("UserId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java index d75eb49f..7c413b6b 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java @@ -7,6 +7,7 @@ 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.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -55,6 +56,13 @@ public class GetChatMemberCount extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java index c1d53356..56e7e1f0 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java @@ -7,6 +7,7 @@ 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.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -73,6 +74,16 @@ public class KickChatMember extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + if (userId == null) { + throw new TelegramApiValidationException("UserId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java index 20fea86c..b2c671fc 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java @@ -7,6 +7,7 @@ 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.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -55,6 +56,13 @@ public class LeaveChat extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java index 16381b69..931eaaf1 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java @@ -7,6 +7,7 @@ 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.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -69,6 +70,16 @@ public class UnbanChatMember extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + if (userId == null) { + throw new TelegramApiValidationException("UserId can't be null", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java index e3e1afe8..b79f9922 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java @@ -27,10 +27,11 @@ public class SendAudio { public static final String REPLYMARKUP_FIELD = "reply_markup"; public static final String PERFOMER_FIELD = "performer"; public static final String TITLE_FIELD = "title"; + public static final String CAPTION_FIELD = "caption"; private Integer duration; ///< Integer Duration of the audio in seconds as defined by sender private String chatId; ///< Unique identifier for the chat to send the message to (or Username fro channels) - private String audio; ///< Audio file to send. file_id as String to resend an audio that is already on the Telegram servers + private String audio; ///< Audio file to send. file_id as String to resend an audio that is already on the Telegram servers or Url to upload it private Integer replyToMessageId; ///< Optional. If the message is a reply, ID of the original message /** * Optional. Sends the message silently. iOS users will not receive a notification, Android @@ -40,6 +41,7 @@ public class SendAudio { private ReplyKeyboard replyMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private String performer; ///< Optional. Performer of sent audio private String title; ///< Optional. Title of sent audio + private String caption; ///< Optional. Audio caption (may also be used when resending documents by file_id), 0-200 characters private boolean isNewAudio; ///< True to upload a new audio, false to use a fileId private String audioName; @@ -84,22 +86,6 @@ public class SendAudio { return this; } - /** - * Use this method to set the audio to a new file - * - * @param audio Path to the new file in your server - * @param audioName Name of the file itself - * - * @deprecated use {@link #setNewAudio(File)} or {@link #setNewAudio(InputStream)} instead. - */ - @Deprecated - public SendAudio setNewAudio(String audio, String audioName) { - this.audio = audio; - this.isNewAudio = true; - this.audioName = audioName; - return this; - } - /** * Use this method to set the audio to a new file * @@ -139,38 +125,6 @@ public class SendAudio { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendAudio setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendAudio setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public String getPerformer() { return performer; } @@ -219,16 +173,31 @@ public class SendAudio { return newAudioStream; } + public String getCaption() { + return caption; + } + + public SendAudio setCaption(String caption) { + this.caption = caption; + return this; + } + @Override public String toString() { return "SendAudio{" + - "chatId='" + chatId + '\'' + + "duration=" + duration + + ", chatId='" + chatId + '\'' + ", audio='" + audio + '\'' + ", replyToMessageId=" + replyToMessageId + + ", disableNotification=" + disableNotification + ", replyMarkup=" + replyMarkup + ", performer='" + performer + '\'' + ", title='" + title + '\'' + ", isNewAudio=" + isNewAudio + + ", audioName='" + audioName + '\'' + + ", newAudioFile=" + newAudioFile + + ", newAudioStream=" + newAudioStream + + ", caption='" + caption + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java index 7ae28d9e..dd9d6277 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java @@ -8,6 +8,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.ActionType; import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -43,31 +44,10 @@ public class SendChatAction extends BotApiMethod { return this; } - /** - * @deprecated - * @return Action type text - */ - @Deprecated - public String getAction() { - return action.toString(); - } - public void setAction(ActionType action) { this.action = action; } - /** - * @deprecated Use {@link #setAction(ActionType)} instead - * @param action Text of the action to create - * @return Reference to this same instance - * @throws IllegalArgumentException if action is not valid - */ - @Deprecated - public SendChatAction setAction(String action) throws IllegalArgumentException { - this.action = ActionType.GetActionType(action); - return this; - } - @Override public String getPath() { return PATH; @@ -81,6 +61,16 @@ public class SendChatAction extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (action == null) { + throw new TelegramApiValidationException("Action parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendContact.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendContact.java index cef6aa93..734eb89a 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendContact.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendContact.java @@ -9,6 +9,7 @@ import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -22,13 +23,13 @@ import java.io.IOException; public class SendContact extends BotApiMethod { public static final String PATH = "sendContact"; - public static final String CHATID_FIELD = "chat_id"; - public static final String PHONE_NUMBER_FIELD = "phone_number"; - public static final String FIRST_NAME_FIELD = "first_name"; - public static final String LAST_NAME_FIELD = "last_name"; - public static final String DISABLENOTIFICATION_FIELD = "disable_notification"; - public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; - public static final String REPLYMARKUP_FIELD = "reply_markup"; + private static final String CHATID_FIELD = "chat_id"; + private static final String PHONE_NUMBER_FIELD = "phone_number"; + private static final String FIRST_NAME_FIELD = "first_name"; + private static final String LAST_NAME_FIELD = "last_name"; + private static final String DISABLENOTIFICATION_FIELD = "disable_notification"; + private static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; + private static final String REPLYMARKUP_FIELD = "reply_markup"; private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) private String phoneNumber; ///< User's phone number private String firstName; ///< User's first name @@ -68,38 +69,6 @@ public class SendContact extends BotApiMethod { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendContact setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendContact setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public Boolean getDisableNotification() { return disableNotification; } @@ -154,6 +123,19 @@ public class SendContact extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (phoneNumber == null) { + throw new TelegramApiValidationException("PhoneNumber parameter can't be empty", this); + } + if (firstName == null) { + throw new TelegramApiValidationException("FirstName parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java index 493e566f..465b738e 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java @@ -22,7 +22,7 @@ public class SendDocument { public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; private String chatId; ///< Unique identifier for the chat to send the message to or Username for the channel to send the message to - private String document; ///< File file to send. file_id as String to resend a file that is already on the Telegram servers + private String document; ///< File file to send. file_id as String to resend a file that is already on the Telegram servers or Url to upload it private String caption; ///< Optional. Document caption (may also be used when resending documents by file_id), 0-200 characters /** * Optional. Sends the message silently. iOS users will not receive a notification, Android @@ -66,22 +66,6 @@ public class SendDocument { return this; } - /** - * Use this method to set the document to a new file - * - * @param document Path to the new file in your server - * @param documentName Name of the file itself - * - * @deprecated use {@link #setNewDocument(File)} or {@link #setNewDocument(InputStream)} instead. - */ - @Deprecated - public SendDocument setNewDocument(String document, String documentName) { - this.document = document; - this.isNewDocument = true; - this.documentName = documentName; - return this; - } - /** * Use this method to set the document to a new file * @@ -128,22 +112,6 @@ public class SendDocument { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendDocument setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - public Boolean getDisableNotification() { return disableNotification; } @@ -176,22 +144,6 @@ public class SendDocument { return this; } - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendDocument setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - @Override public String toString() { return "SendDocument{" + diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendLocation.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendLocation.java index 31f2701b..a9b483b0 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendLocation.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendLocation.java @@ -9,6 +9,7 @@ import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -83,38 +84,6 @@ public class SendLocation extends BotApiMethod { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendLocation setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendLocation setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public Boolean getDisableNotification() { return disableNotification; } @@ -142,6 +111,19 @@ public class SendLocation extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java index b0f885bd..209e2cff 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java @@ -10,6 +10,7 @@ import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.methods.ParseMode; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -81,38 +82,6 @@ public class SendMessage extends BotApiMethod { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendMessage setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendMessage setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public Boolean getDisableWebPagePreview() { return disableWebPagePreview; } @@ -196,6 +165,16 @@ public class SendMessage extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (text == null || text.isEmpty()) { + throw new TelegramApiValidationException("Text parameter can't be empty", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java index d4839fa7..688c1e59 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java @@ -22,7 +22,7 @@ public class SendPhoto { public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) - private String photo; ///< Photo to send. file_id as String to resend a photo that is already on the Telegram servers + private String photo; ///< Photo to send. file_id as String to resend a photo that is already on the Telegram servers or URL to upload it private String caption; ///< Optional Photo caption (may also be used when resending photos by file_id). /** * Optional. Sends the message silently. iOS users will not receive a notification, Android @@ -87,38 +87,6 @@ public class SendPhoto { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendPhoto setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendPhoto setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public boolean isNewPhoto() { return isNewPhoto; } @@ -149,22 +117,6 @@ public class SendPhoto { return this; } - /** - * Use this method to set the photo to a new file - * - * @param photo Path to the new file in your server - * @param photoName Name of the file itself - * - * @deprecated use {@link #setNewPhoto(File)} or {@link #setNewPhoto(InputStream)} instead. - */ - @Deprecated - public SendPhoto setNewPhoto(String photo, String photoName) { - this.photo = photo; - this.isNewPhoto = true; - this.photoName = photoName; - return this; - } - public SendPhoto setNewPhoto(File file) { this.photo = file.getName(); this.newPhotoFile = file; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java index 2a06c9fb..f0ead297 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java @@ -21,7 +21,7 @@ public class SendSticker { public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) - private String sticker; ///< Sticker file to send. file_id as String to resend a sticker that is already on the Telegram servers + private String sticker; ///< Sticker file to send. file_id as String to resend a sticker that is already on the Telegram servers or URL to upload it /** * Optional. Sends the message silently. iOS users will not receive a notification, Android * users will receive a notification with no sound. Other apps coming soon @@ -76,54 +76,6 @@ public class SendSticker { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendSticker setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendSticker setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - - /** - * Use this method to set the sticker to a new file - * - * @param sticker Path to the new file in your server - * @param stickerName Name of the file itself - * - * @deprecated use {@link #setNewSticker(File)} or {@link #setNewSticker(InputStream)} instead. - */ - @Deprecated - public SendSticker setSticker(String sticker, String stickerName) { - this.sticker = sticker; - this.isNewSticker = true; - this.stickerName = stickerName; - return this; - } - public SendSticker setNewSticker(File file) { this.sticker = file.getName(); this.isNewSticker = true; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVenue.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVenue.java index 5e57207c..3cd5337a 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVenue.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVenue.java @@ -9,6 +9,7 @@ import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -90,38 +91,6 @@ public class SendVenue extends BotApiMethod { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendVenue setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendVenue setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public Boolean getDisableNotification() { return disableNotification; } @@ -176,6 +145,25 @@ public class SendVenue extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (address == null) { + throw new TelegramApiValidationException("Address parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java index f354f503..f303f2d3 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java @@ -26,7 +26,7 @@ public class SendVideo { public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) - private String video; ///< Video to send. file_id as String to resend a video that is already on the Telegram servers + private String video; ///< Video to send. file_id as String to resend a video that is already on the Telegram servers or URL to upload it private Integer duration; ///< Optional. Duration of sent video in seconds private String caption; ///< OptionaL. Video caption (may also be used when resending videos by file_id). private Integer width; ///< Optional. Video width @@ -103,38 +103,6 @@ public class SendVideo { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendVideo setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendVideo setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public boolean isNewVideo() { return isNewVideo; } @@ -183,22 +151,6 @@ public class SendVideo { return this; } - /** - * Use this method to set the video to a new file - * - * @param video Path to the new file in your server - * @param videoName Name of the file itself - * - * @deprecated use {@link #setNewVideo(File)} or {@link #setNewVideo(InputStream)} instead. - */ - @Deprecated - public SendVideo setNewVideo(String video, String videoName) { - this.video = video; - this.isNewVideo = true; - this.videoName = videoName; - return this; - } - public SendVideo setNewVideo(File file) { this.video = file.getName(); this.isNewVideo = true; diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java index 3c8a865f..febad10d 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java @@ -23,6 +23,7 @@ public class SendVoice { public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; public static final String DURATION_FIELD = "duration"; + public static final String CAPTION_FIELD = "caption"; private String chatId; ///< Unique identifier for the chat sent message to (Or username for channels) private String voice; ///< Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data. @@ -34,6 +35,7 @@ public class SendVoice { private Integer replyToMessageId; ///< Optional. If the message is a reply, ID of the original message private ReplyKeyboard replyMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private Integer duration; ///< Optional. Duration of sent audio in seconds + private String caption; ///< Optional. Voice caption (may also be used when resending videos by file_id). private boolean isNewVoice; ///< True to upload a new voice note, false to use a fileId private String voiceName; ///< Name of the voice note @@ -44,17 +46,6 @@ public class SendVoice { super(); } - @Override - public String toString() { - return "SendVoice{" + - "chatId='" + chatId + '\'' + - ", voice='" + voice + '\'' + - ", replyToMessageId=" + replyToMessageId + - ", replyMarkup=" + replyMarkup + - ", duration=" + duration + - '}'; - } - public Boolean getDisableNotification() { return disableNotification; } @@ -82,62 +73,12 @@ public class SendVoice { return voice; } - /** - * @deprecated Use {@link #getVoice()} instead - */ - @Deprecated - public String getAudio() { - return voice; - } - public SendVoice setVoice(String voice) { this.voice = voice; this.isNewVoice = false; return this; } - /** - * @deprecated Use {@link #setVoice(String)} instead - */ - @Deprecated - public SendVoice setAudio(String voice) { - this.voice = voice; - this.isNewVoice = false; - return this; - } - - /** - * Use this method to set the voice to a new file - * - * @param voice Path to the new file in your server - * @param voiceName Name of the file itself - * - * @deprecated use {@link #setNewVoice(File)} or {@link #setNewVoice(InputStream)} instead. - */ - @Deprecated - public SendVoice setNewVoice(String voice, String voiceName) { - this.voice = voice; - this.isNewVoice = false; - this.voiceName = voiceName; - return this; - } - - /** - * Use this method to set the voice to a new file - * - * @param voice Path to the new file in your server - * @param voiceName Name of the file itself - * - * @deprecated use {@link #setNewVoice(File)} or {@link #setNewVoice(InputStream)} instead. - */ - @Deprecated - public SendVoice setNewAudio(String voice, String voiceName) { - this.voice = voice; - this.isNewVoice = false; - this.voiceName = voiceName; - return this; - } - public SendVoice setNewVoice(File file) { this.voice = file.getName(); this.isNewVoice = true; @@ -172,38 +113,6 @@ public class SendVoice { return this; } - /** - * @deprecated Use {@link #getReplyToMessageId()} instead. - */ - @Deprecated - public Integer getReplayToMessageId() { - return getReplyToMessageId(); - } - - /** - * @deprecated Use {@link #setReplyToMessageId(Integer)} instead. - */ - @Deprecated - public SendVoice setReplayToMessageId(Integer replyToMessageId) { - return setReplyToMessageId(replyToMessageId); - } - - /** - * @deprecated Use {@link #getReplyMarkup()} instead. - */ - @Deprecated - public ReplyKeyboard getReplayMarkup() { - return getReplyMarkup(); - } - - /** - * @deprecated Use {@link #setReplyMarkup(ReplyKeyboard)} instead. - */ - @Deprecated - public SendVoice setReplayMarkup(ReplyKeyboard replyMarkup) { - return setReplyMarkup(replyMarkup); - } - public Integer getDuration() { return duration; } @@ -228,4 +137,30 @@ public class SendVoice { public InputStream getNewVoiceStream() { return newVoiceStream; } + + public String getCaption() { + return caption; + } + + public SendVoice setCaption(String caption) { + this.caption = caption; + return this; + } + + @Override + public String toString() { + return "SendVoice{" + + "chatId='" + chatId + '\'' + + ", voice='" + voice + '\'' + + ", disableNotification=" + disableNotification + + ", replyToMessageId=" + replyToMessageId + + ", replyMarkup=" + replyMarkup + + ", duration=" + duration + + ", caption='" + caption + '\'' + + ", isNewVoice=" + isNewVoice + + ", voiceName='" + voiceName + '\'' + + ", newVoiceFile=" + newVoiceFile + + ", newVoiceStream=" + newVoiceStream + + '}'; + } } 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 index ef89fc24..d62b675c 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/updates/GetWebhookInfo.java @@ -8,15 +8,17 @@ import org.json.JSONObject; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.WebhookInfo; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; 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 + * @version 2.4 + * @brief Use this method to get current webhook status. + * Requires no parameters. + * On success, returns a WebhookInfo object. + * Will throw an error, if the bot is using getUpdates. * * @date 12 of August of 2016 */ @@ -44,6 +46,10 @@ public class GetWebhookInfo extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageCaption.java b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageCaption.java index 33674c54..2a377f70 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageCaption.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageCaption.java @@ -10,6 +10,7 @@ import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -129,6 +130,25 @@ public class EditMessageCaption extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (inlineMessageId == null) { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty if inlineMessageId is not present", this); + } + if (messageId == null) { + throw new TelegramApiValidationException("MessageId parameter can't be empty if inlineMessageId is not present", this); + } + } else { + if (chatId != null) { + throw new TelegramApiValidationException("ChatId parameter must be empty if inlineMessageId is provided", this); + } + if (messageId != null) { + throw new TelegramApiValidationException("MessageId parameter must be empty if inlineMessageId is provided", this); + } + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageReplyMarkup.java b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageReplyMarkup.java index f126e194..da755f2f 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageReplyMarkup.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageReplyMarkup.java @@ -10,6 +10,7 @@ import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -115,6 +116,25 @@ public class EditMessageReplyMarkup extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (inlineMessageId == null) { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty if inlineMessageId is not present", this); + } + if (messageId == null) { + throw new TelegramApiValidationException("MessageId parameter can't be empty if inlineMessageId is not present", this); + } + } else { + if (chatId != null) { + throw new TelegramApiValidationException("ChatId parameter must be empty if inlineMessageId is provided", this); + } + if (messageId != null) { + throw new TelegramApiValidationException("MessageId parameter must be empty if inlineMessageId is provided", this); + } + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageText.java b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageText.java index 14f7a1bf..6663accd 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageText.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/updatingmessages/EditMessageText.java @@ -11,6 +11,7 @@ import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.methods.ParseMode; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -173,6 +174,28 @@ public class EditMessageText extends BotApiMethod { return null; } + @Override + public void validate() throws TelegramApiValidationException { + if (inlineMessageId == null) { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId parameter can't be empty if inlineMessageId is not present", this); + } + if (messageId == null) { + throw new TelegramApiValidationException("MessageId parameter can't be empty if inlineMessageId is not present", this); + } + } else { + if (chatId != null) { + throw new TelegramApiValidationException("ChatId parameter must be empty if inlineMessageId is provided", this); + } + if (messageId != null) { + throw new TelegramApiValidationException("MessageId parameter must be empty if inlineMessageId is provided", this); + } + } + if (text == null || text.isEmpty()) { + throw new TelegramApiValidationException("Text parameter can't be empty", this); + } + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/CallbackQuery.java b/src/main/java/org/telegram/telegrambots/api/objects/CallbackQuery.java index 6487d28d..9ba89e0c 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/CallbackQuery.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/CallbackQuery.java @@ -42,11 +42,11 @@ public class CallbackQuery implements IBotApiObject { private String inlineMessageId; ///< Optional. Identifier of the message sent via the bot in inline mode, that originated the query @JsonProperty(DATA_FIELD) /** - * Data associated with the callback button. + * + * Optional. Data associated with the callback button. * @note Be aware that a bad client can send arbitrary data in this field */ private String data; - public CallbackQuery() { super(); } @@ -61,7 +61,9 @@ public class CallbackQuery implements IBotApiObject { if (jsonObject.has(INLINE_MESSAGE_ID_FIELD)) { this.inlineMessageId = jsonObject.getString(INLINE_MESSAGE_ID_FIELD); } - data = jsonObject.getString(DATA_FIELD); + if (jsonObject.has(DATA_FIELD)) { + data = jsonObject.getString(DATA_FIELD); + } } public String getId() { @@ -95,7 +97,9 @@ public class CallbackQuery implements IBotApiObject { if (inlineMessageId != null) { gen.writeStringField(INLINE_MESSAGE_ID_FIELD, inlineMessageId); } - gen.writeStringField(DATA_FIELD, data); + if (data != null) { + gen.writeStringField(DATA_FIELD, data); + } gen.writeEndObject(); gen.flush(); } @@ -109,10 +113,10 @@ public class CallbackQuery implements IBotApiObject { public String toString() { return "CallbackQuery{" + "id='" + id + '\'' + - ", from='" + from + '\'' + - ", message='" + message + '\'' + + ", from=" + from + + ", message=" + message + ", inlineMessageId='" + inlineMessageId + '\'' + - ", data=" + data + + ", data='" + data + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Chat.java b/src/main/java/org/telegram/telegrambots/api/objects/Chat.java index 18b77d8f..6dea803f 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Chat.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Chat.java @@ -23,6 +23,7 @@ public class Chat implements IBotApiObject { private static final String FIRSTNAME_FIELD = "first_name"; private static final String LASTNAME_FIELD = "last_name"; private static final String USERNAME_FIELD = "username"; + private static final String ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD = "all_members_are_administrators"; private static final String USERCHATTYPE = "private"; private static final String GROUPCHATTYPE = "group"; private static final String CHANNELCHATTYPE = "channel"; @@ -45,6 +46,8 @@ public class Chat implements IBotApiObject { private String lastName; ///< Optional. Interlocutor's first name for private chats @JsonProperty(USERNAME_FIELD) private String userName; ///< Optional. Interlocutor's last name for private chats + @JsonProperty(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD) + private Boolean allMembersAreAdministrators; ///< Optional. True if admins are disabled in the chat public Chat() { super(); @@ -66,6 +69,9 @@ public class Chat implements IBotApiObject { if (jsonObject.has(USERNAME_FIELD)) { this.userName = jsonObject.getString(USERNAME_FIELD); } + if (jsonObject.has(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD)) { + allMembersAreAdministrators = jsonObject.getBoolean(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD); + } } public Long getId() { @@ -104,6 +110,10 @@ public class Chat implements IBotApiObject { return userName; } + public Boolean getAllMembersAreAdministrators() { + return allMembersAreAdministrators; + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); @@ -124,6 +134,9 @@ public class Chat implements IBotApiObject { if (!isGroupChat() && userName != null) { gen.writeStringField(USERNAME_FIELD, userName); } + if (allMembersAreAdministrators != null) { + gen.writeBooleanField(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD, allMembersAreAdministrators); + } gen.writeEndObject(); gen.flush(); } @@ -142,6 +155,7 @@ public class Chat implements IBotApiObject { ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", userName='" + userName + '\'' + + ", allMembersAreAdministrators=" + allMembersAreAdministrators + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/File.java b/src/main/java/org/telegram/telegrambots/api/objects/File.java index 19b4b2f7..fb93da7f 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/File.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/File.java @@ -17,6 +17,8 @@ import java.io.IOException; * @date 24 of June of 2015 */ public class File implements IBotApiObject { + public static final String FILEBASEURL = "https://api.telegram.org/file/bot{0}/{1}"; + private static final String FILE_ID = "file_id"; private static final String FILE_SIZE_FIELD = "file_size"; private static final String FILE_PATH_FIELD = "file_path"; diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Message.java b/src/main/java/org/telegram/telegrambots/api/objects/Message.java index 7cc91759..2ae93441 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Message.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Message.java @@ -441,6 +441,10 @@ public class Message implements IBotApiObject { return editDate; } + public boolean hasEntities() { + return entities != null && !entities.isEmpty(); + } + @Override public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java b/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java index 63fc70f9..91284e60 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/WebhookInfo.java @@ -12,8 +12,8 @@ import java.io.IOException; /** * @author Ruben Bermudez - * @version 2.1 - * @brief Information about configured webhook + * @version 2.4 + * @brief Contains information about the current status of a webhook. * @date 12 of August of 2016 */ public class WebhookInfo implements IBotApiObject { @@ -25,15 +25,15 @@ public class WebhookInfo implements IBotApiObject { private static final String LASTERRORMESSAGE_FIELD = "last_error_message"; @JsonProperty(URL_FIELD) - private String url; ///< Url of the webhook + private String url; ///< Webhook URL, may be empty if webhook is not set up @JsonProperty(HASCUSTOMCERTIFICATE_FIELD) - private Boolean hasCustomCertificate; ///< True if the webhook use a self signed certificate + private Boolean hasCustomCertificate; ///< True, if a custom certificate was provided for webhook certificate checks @JsonProperty(PENDINGUPDATESCOUNT_FIELD) - private Integer pendingUpdatesCount; ///< Number of updates pending to be delivered + private Integer pendingUpdatesCount; ///< Number updates awaiting delivery @JsonProperty(LASTERRORDATE_FIELD) - private Integer lastErrorDate; ///< Optional. Date of that error + private Integer lastErrorDate; ///< Optional. Unix time for the most recent error that happened when trying to deliver an update via webhook @JsonProperty(LASTERRORMESSAGE_FIELD) - private String lastErrorMessage; ///< Optional. Error message + private String lastErrorMessage; ///< Optional. Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook public WebhookInfo() { diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/InlineQuery.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/InlineQuery.java index 60ec2317..ee5812b6 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/InlineQuery.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/InlineQuery.java @@ -85,6 +85,14 @@ public class InlineQuery implements IBotApiObject { return offset; } + public boolean hasQuery() { + return query != null && !query.isEmpty(); + } + + public boolean hasLocation() { + return location != null; + } + @Override public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException { serialize(gen, serializers); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputContactMessageContent.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputContactMessageContent.java index 67ee65ea..a2cf7714 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputContactMessageContent.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputContactMessageContent.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -60,6 +61,16 @@ public class InputContactMessageContent implements InputMessageContent { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (phoneNumber == null || phoneNumber.isEmpty()) { + throw new TelegramApiValidationException("PhoneNumber parameter can't be empty", this); + } + if (firstName == null || firstName.isEmpty()) { + throw new TelegramApiValidationException("FirstName parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputLocationMessageContent.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputLocationMessageContent.java index 553e1eeb..7dfd57a4 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputLocationMessageContent.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputLocationMessageContent.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -48,6 +49,16 @@ public class InputLocationMessageContent implements InputMessageContent { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputMessageContent.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputMessageContent.java index c9e59e77..ce512a60 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputMessageContent.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputMessageContent.java @@ -2,6 +2,7 @@ package org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent; import org.telegram.telegrambots.api.interfaces.IBotApiObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; /** * @author Ruben Bermudez @@ -10,5 +11,5 @@ import org.telegram.telegrambots.api.interfaces.IToJson; * query. * @date 10 of April of 2016 */ -public interface InputMessageContent extends IBotApiObject, IToJson { +public interface InputMessageContent extends IBotApiObject, IToJson, IValidable { } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputTextMessageContent.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputTextMessageContent.java index 2c741574..e8d61b49 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputTextMessageContent.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputTextMessageContent.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.methods.ParseMode; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -87,6 +88,13 @@ public class InputTextMessageContent implements InputMessageContent { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (messageText == null || messageText.isEmpty()) { + throw new TelegramApiValidationException("MessageText parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputVenueMessageContent.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputVenueMessageContent.java index a133b9e0..e02c9a6d 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputVenueMessageContent.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/inputmessagecontent/InputVenueMessageContent.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -84,6 +85,22 @@ public class InputVenueMessageContent implements InputMessageContent { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (address == null || address.isEmpty()) { + throw new TelegramApiValidationException("Address parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResult.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResult.java index f2110710..38ee3505 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResult.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResult.java @@ -2,6 +2,7 @@ package org.telegram.telegrambots.api.objects.inlinequery.result; import org.telegram.telegrambots.api.interfaces.IBotApiObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; /** * @author Ruben Bermudez @@ -9,5 +10,5 @@ import org.telegram.telegrambots.api.interfaces.IToJson; * @brief This object represents one result of an inline query. * @date 01 of January of 2016 */ -public interface InlineQueryResult extends IBotApiObject, IToJson { +public interface InlineQueryResult extends IBotApiObject, IToJson, IValidable { } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultArticle.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultArticle.java index 3a09675d..769e4832 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultArticle.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultArticle.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -147,6 +148,23 @@ public class InlineQueryResultArticle implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (inputMessageContent == null) { + throw new TelegramApiValidationException("InputMessageContent parameter can't be null", this); + } + inputMessageContent.validate(); + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultAudio.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultAudio.java index 4ac6d0a8..5f24a607 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultAudio.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -32,6 +33,7 @@ public class InlineQueryResultAudio implements InlineQueryResult { private static final String AUDIO_DURATION_FIELD = "audio_duration"; private static final String INPUTMESSAGECONTENT_FIELD = "input_message_content"; private static final String REPLY_MARKUP_FIELD = "reply_markup"; + private static final String CAPTION_FIELD = "caption"; @JsonProperty(ID_FIELD) private String id; ///< Unique identifier of this result @JsonProperty(AUDIOURL_FIELD) @@ -46,6 +48,8 @@ public class InlineQueryResultAudio implements InlineQueryResult { private InputMessageContent inputMessageContent; ///< Optional. Content of the message to be sent instead of the audio @JsonProperty(REPLY_MARKUP_FIELD) private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message + @JsonProperty(CAPTION_FIELD) + private String caption; ///< Optional. Audio caption (may also be used when resending documents by file_id), 0-200 characters public static String getType() { return type; @@ -114,6 +118,31 @@ public class InlineQueryResultAudio implements InlineQueryResult { return this; } + public String getCaption() { + return caption; + } + + public InlineQueryResultAudio setCaption(String caption) { + this.caption = caption; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (audioUrl == null || audioUrl.isEmpty()) { + throw new TelegramApiValidationException("AudioUrl parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); @@ -136,6 +165,9 @@ public class InlineQueryResultAudio implements InlineQueryResult { if (inputMessageContent != null) { jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent.toJson()); } + if (caption != null) { + jsonObject.put(CAPTION_FIELD, caption); + } return jsonObject; } @@ -161,6 +193,9 @@ public class InlineQueryResultAudio implements InlineQueryResult { if (audioDuration != null) { gen.writeNumberField(AUDIO_DURATION_FIELD, audioDuration); } + if (caption != null) { + gen.writeStringField(CAPTION_FIELD, caption); + } gen.writeEndObject(); gen.flush(); } @@ -173,14 +208,14 @@ public class InlineQueryResultAudio implements InlineQueryResult { @Override public String toString() { return "InlineQueryResultAudio{" + - "type='" + type + '\'' + - ", id='" + id + '\'' + + "id='" + id + '\'' + ", audioUrl='" + audioUrl + '\'' + ", title='" + title + '\'' + - ", performer=" + performer + + ", performer='" + performer + '\'' + ", audioDuration=" + audioDuration + - ", inputMessageContent='" + inputMessageContent + '\'' + - ", replyMarkup='" + replyMarkup + '\'' + + ", inputMessageContent=" + inputMessageContent + + ", replyMarkup=" + replyMarkup + + ", caption='" + caption + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultContact.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultContact.java index 63e36f62..26d7b0db 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultContact.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultContact.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -138,6 +139,25 @@ public class InlineQueryResultContact implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (phoneNumber == null || phoneNumber.isEmpty()) { + throw new TelegramApiValidationException("PhoneNumber parameter can't be empty", this); + } + if (firstName == null || firstName.isEmpty()) { + throw new TelegramApiValidationException("FirstName parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultDocument.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultDocument.java index 1634265f..be381a47 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultDocument.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -163,6 +164,28 @@ public class InlineQueryResultDocument implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (mimeType == null || mimeType.isEmpty()) { + throw new TelegramApiValidationException("Mimetype parameter can't be empty", this); + } + if (documentUrl == null || documentUrl.isEmpty()) { + throw new TelegramApiValidationException("DocumentUrl parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultGif.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultGif.java index 3cb3db9a..29e51874 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultGif.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultGif.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -137,6 +138,22 @@ public class InlineQueryResultGif implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (gifUrl == null || gifUrl.isEmpty()) { + throw new TelegramApiValidationException("GifUrl parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultLocation.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultLocation.java index 28773b3d..33d658a4 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultLocation.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultLocation.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -139,6 +140,28 @@ public class InlineQueryResultLocation implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultMpeg4Gif.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultMpeg4Gif.java index 6185db31..aba0aaa0 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultMpeg4Gif.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultMpeg4Gif.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -137,6 +138,22 @@ public class InlineQueryResultMpeg4Gif implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (mpeg4Url == null || mpeg4Url.isEmpty()) { + throw new TelegramApiValidationException("Mpeg4Url parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultPhoto.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultPhoto.java index 6b9ef17c..e3bd28c2 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultPhoto.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -160,6 +161,22 @@ public class InlineQueryResultPhoto implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (photoUrl == null || photoUrl.isEmpty()) { + throw new TelegramApiValidationException("PhotoUrl parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVenue.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVenue.java index 9fd689fb..df21d6bb 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVenue.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVenue.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -162,6 +163,32 @@ public class InlineQueryResultVenue implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (latitude == null) { + throw new TelegramApiValidationException("Latitude parameter can't be empty", this); + } + if (longitude == null) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + if (address == null || address.isEmpty()) { + throw new TelegramApiValidationException("Longitude parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVideo.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVideo.java index 5d6b0b80..53839476 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVideo.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -172,6 +173,22 @@ public class InlineQueryResultVideo implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (videoUrl == null || videoUrl.isEmpty()) { + throw new TelegramApiValidationException("VideoUrl parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVoice.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVoice.java index 7bbc7549..eacc7a2d 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/InlineQueryResultVoice.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -31,6 +32,8 @@ public class InlineQueryResultVoice implements InlineQueryResult { private static final String VOICE_DURATION_FIELD = "voice_duration"; private static final String INPUTMESSAGECONTENT_FIELD = "input_message_content"; private static final String REPLY_MARKUP_FIELD = "reply_markup"; + private static final String CAPTION_FIELD = "caption"; + @JsonProperty(ID_FIELD) private String id; ///< Unique identifier of this result, 1-64 bytes @JsonProperty(VOICEURL_FIELD) @@ -43,6 +46,9 @@ public class InlineQueryResultVoice implements InlineQueryResult { private InputMessageContent inputMessageContent; ///< Optional. Content of the message to be sent instead of the voice recording @JsonProperty(REPLY_MARKUP_FIELD) private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message + @JsonProperty(CAPTION_FIELD) + private String caption; ///< Optional. Voice caption (may also be used when resending documents by file_id), 0-200 characters + public static String getType() { return type; @@ -102,6 +108,31 @@ public class InlineQueryResultVoice implements InlineQueryResult { return this; } + public String getCaption() { + return caption; + } + + public InlineQueryResultVoice setCaption(String caption) { + this.caption = caption; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (voiceUrl == null || voiceUrl.isEmpty()) { + throw new TelegramApiValidationException("VoiceUrl parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); @@ -120,6 +151,9 @@ public class InlineQueryResultVoice implements InlineQueryResult { if (inputMessageContent != null) { jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent.toJson()); } + if (caption != null) { + jsonObject.put(CAPTION_FIELD, caption); + } return jsonObject; } @@ -141,6 +175,9 @@ public class InlineQueryResultVoice implements InlineQueryResult { if (inputMessageContent != null) { gen.writeObjectField(INPUTMESSAGECONTENT_FIELD, inputMessageContent); } + if (caption != null) { + gen.writeStringField(CAPTION_FIELD, caption); + } gen.writeEndObject(); gen.flush(); } @@ -153,13 +190,13 @@ public class InlineQueryResultVoice implements InlineQueryResult { @Override public String toString() { return "InlineQueryResultVoice{" + - "type='" + type + '\'' + - ", id='" + id + '\'' + - ", voiceDuration='" + voiceDuration + '\'' + - ", voiceUrl=" + voiceUrl + + "id='" + id + '\'' + + ", voiceUrl='" + voiceUrl + '\'' + ", title='" + title + '\'' + - ", inputMessageContent='" + inputMessageContent + '\'' + - ", replyMarkup='" + replyMarkup + '\'' + + ", voiceDuration=" + voiceDuration + + ", inputMessageContent=" + inputMessageContent + + ", replyMarkup=" + replyMarkup + + ", caption='" + caption + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedAudio.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedAudio.java index 08a504b3..8c1a5747 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedAudio.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -31,6 +32,8 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { private static final String AUDIO_FILE_ID_FIELD = "audio_file_id"; private static final String INPUTMESSAGECONTENT_FIELD = "input_message_content"; private static final String REPLY_MARKUP_FIELD = "reply_markup"; + private static final String CAPTION_FIELD = "caption"; + @JsonProperty(ID_FIELD) private String id; ///< Unique identifier of this result @JsonProperty(AUDIO_FILE_ID_FIELD) @@ -39,6 +42,9 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { private InputMessageContent inputMessageContent; ///< Optional. Content of the message to be sent instead of the audio @JsonProperty(REPLY_MARKUP_FIELD) private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message + @JsonProperty(CAPTION_FIELD) + private String caption; ///< Optional. Audio caption (may also be used when resending documents by file_id), 0-200 characters + public static String getType() { return type; @@ -80,6 +86,31 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { return this; } + public String getCaption() { + return caption; + } + + public InlineQueryResultCachedAudio setCaption(String caption) { + this.caption = caption; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (audioFileId == null || audioFileId.isEmpty()) { + throw new TelegramApiValidationException("AudioFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); @@ -92,6 +123,9 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { if (inputMessageContent != null) { jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent); } + if (caption != null) { + jsonObject.put(CAPTION_FIELD, caption); + } return jsonObject; } @@ -108,6 +142,9 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { if (inputMessageContent != null) { gen.writeObjectField(INPUTMESSAGECONTENT_FIELD, inputMessageContent); } + if (caption != null) { + gen.writeStringField(CAPTION_FIELD, caption); + } gen.writeEndObject(); gen.flush(); } @@ -120,11 +157,11 @@ public class InlineQueryResultCachedAudio implements InlineQueryResult { @Override public String toString() { return "InlineQueryResultCachedAudio{" + - "type='" + type + '\'' + - ", id='" + id + '\'' + + "id='" + id + '\'' + ", audioFileId='" + audioFileId + '\'' + - ", inputMessageContent='" + inputMessageContent + '\'' + - ", replyMarkup='" + replyMarkup + '\'' + + ", inputMessageContent=" + inputMessageContent + + ", replyMarkup=" + replyMarkup + + ", caption='" + caption + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedDocument.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedDocument.java index e19cd3ee..e1ae2231 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedDocument.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -117,6 +118,25 @@ public class InlineQueryResultCachedDocument implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (documentFileId == null || documentFileId.isEmpty()) { + throw new TelegramApiValidationException("DocumentFileId parameter can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedGif.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedGif.java index 04790156..0f0cead8 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedGif.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedGif.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -102,6 +103,22 @@ public class InlineQueryResultCachedGif implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (gifFileId == null || gifFileId.isEmpty()) { + throw new TelegramApiValidationException("GifFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedMpeg4Gif.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedMpeg4Gif.java index df7dbbfc..cd1f369a 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedMpeg4Gif.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedMpeg4Gif.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -102,6 +103,22 @@ public class InlineQueryResultCachedMpeg4Gif implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (mpeg4FileId == null || mpeg4FileId.isEmpty()) { + throw new TelegramApiValidationException("Mpeg4FileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedPhoto.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedPhoto.java index 489d3a09..14f10bb5 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedPhoto.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -114,6 +115,22 @@ public class InlineQueryResultCachedPhoto implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (photoFileId == null || photoFileId.isEmpty()) { + throw new TelegramApiValidationException("PhotoFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedSticker.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedSticker.java index c52bf668..624711a4 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedSticker.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedSticker.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -80,6 +81,22 @@ public class InlineQueryResultCachedSticker implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (stickerFileId == null || stickerFileId.isEmpty()) { + throw new TelegramApiValidationException("StickerFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVideo.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVideo.java index 565f9943..05417b7d 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVideo.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -114,6 +115,22 @@ public class InlineQueryResultCachedVideo implements InlineQueryResult { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (videoFileId == null || videoFileId.isEmpty()) { + throw new TelegramApiValidationException("VideoFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVoice.java b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVoice.java index b770252c..0c263fde 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/inlinequery/result/chached/InlineQueryResultCachedVoice.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -32,6 +33,7 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { private static final String TITLE_FIELD = "title"; private static final String INPUTMESSAGECONTENT_FIELD = "input_message_content"; private static final String REPLY_MARKUP_FIELD = "reply_markup"; + private static final String CAPTION_FIELD = "caption"; @JsonProperty(ID_FIELD) private String id; ///< Unique identifier of this result, 1-64 bytes @JsonProperty(VOICE_FILE_ID_FIELD) @@ -42,6 +44,8 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { private InputMessageContent inputMessageContent; ///< Optional. Content of the message to be sent instead of the voice recording @JsonProperty(REPLY_MARKUP_FIELD) private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message + @JsonProperty(CAPTION_FIELD) + private String caption; ///< Optional. Voice caption (may also be used when resending documents by file_id), 0-200 characters public static String getType() { return type; @@ -92,6 +96,31 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { return this; } + public String getCaption() { + return caption; + } + + public InlineQueryResultCachedVoice setCaption(String caption) { + this.caption = caption; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (id == null || id.isEmpty()) { + throw new TelegramApiValidationException("ID parameter can't be empty", this); + } + if (voiceFileId == null || voiceFileId.isEmpty()) { + throw new TelegramApiValidationException("VoiceFileId parameter can't be empty", this); + } + if (inputMessageContent != null) { + inputMessageContent.validate(); + } + if (replyMarkup != null) { + replyMarkup.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); @@ -107,6 +136,9 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { if (inputMessageContent != null) { jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent); } + if (caption != null) { + jsonObject.put(CAPTION_FIELD, caption); + } return jsonObject; } @@ -125,6 +157,9 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { if (inputMessageContent != null) { gen.writeObjectField(INPUTMESSAGECONTENT_FIELD, inputMessageContent); } + if (caption != null) { + gen.writeStringField(CAPTION_FIELD, caption); + } gen.writeEndObject(); gen.flush(); } @@ -137,12 +172,12 @@ public class InlineQueryResultCachedVoice implements InlineQueryResult { @Override public String toString() { return "InlineQueryResultCachedVoice{" + - "type='" + type + '\'' + - ", id='" + id + '\'' + + "id='" + id + '\'' + ", voiceFileId='" + voiceFileId + '\'' + ", title='" + title + '\'' + - ", inputMessageContent='" + inputMessageContent + '\'' + - ", replyMarkup='" + replyMarkup + '\'' + + ", inputMessageContent=" + inputMessageContent + + ", replyMarkup=" + replyMarkup + + ", caption='" + caption + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java index b03c2d98..42a7ffd2 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -63,6 +64,13 @@ public class ForceReplyKeyboard implements ReplyKeyboard { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (forceReply == null) { + throw new TelegramApiValidationException("ForceReply parameter can't not be null", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java index da0f5825..13712817 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/InlineKeyboardMarkup.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONArray; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.replykeyboard.buttons.InlineKeyboardButton; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.ArrayList; @@ -42,6 +43,18 @@ public class InlineKeyboardMarkup implements ReplyKeyboard { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (keyboard == null) { + throw new TelegramApiValidationException("Keyboard parameter can't be null", this); + } + for (List inlineKeyboardButtons : keyboard) { + for (InlineKeyboardButton inlineKeyboardButton : inlineKeyboardButtons) { + inlineKeyboardButton.validate(); + } + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboard.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboard.java index 657e86c7..0d784d7a 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboard.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboard.java @@ -2,6 +2,7 @@ package org.telegram.telegrambots.api.objects.replykeyboard; import org.telegram.telegrambots.api.interfaces.IBotApiObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; /** * @author Ruben Bermudez @@ -9,5 +10,5 @@ import org.telegram.telegrambots.api.interfaces.IToJson; * @brief Reply keyboard abstract type * @date 20 of June of 2015 */ -public interface ReplyKeyboard extends IBotApiObject, IToJson { +public interface ReplyKeyboard extends IBotApiObject, IToJson, IValidable { } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java index b47e5fc4..04a9e7df 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -59,11 +60,20 @@ public class ReplyKeyboardHide implements ReplyKeyboard { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (hideKeyboard == null) { + throw new TelegramApiValidationException("Hidekeyboard parameter can't be null", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); jsonObject.put(HIDEKEYBOARD_FIELD, this.hideKeyboard); - jsonObject.put(SELECTIVE_FIELD, this.selective); + if (selective != null) { + jsonObject.put(SELECTIVE_FIELD, this.selective); + } return jsonObject; } @@ -71,7 +81,9 @@ public class ReplyKeyboardHide implements ReplyKeyboard { public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeBooleanField(HIDEKEYBOARD_FIELD, hideKeyboard); - gen.writeBooleanField(SELECTIVE_FIELD, selective); + if (selective != null) { + gen.writeBooleanField(SELECTIVE_FIELD, selective); + } gen.writeEndObject(); gen.flush(); } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java index a372443b..1fa59062 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardMarkup.java @@ -9,6 +9,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton; import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.ArrayList; @@ -82,6 +83,16 @@ public class ReplyKeyboardMarkup implements ReplyKeyboard { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (keyboard == null) { + throw new TelegramApiValidationException("Keyboard parameter can't be null", this); + } + for (KeyboardRow keyboardButtons : keyboard) { + keyboardButtons.validate(); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java index 581566c8..6f126280 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/InlineKeyboardButton.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.interfaces.IBotApiObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -20,12 +22,13 @@ import java.io.IOException; * display unsupported message. * @date 10 of April of 2016 */ -public class InlineKeyboardButton implements IBotApiObject, IToJson { +public class InlineKeyboardButton implements IBotApiObject, IToJson, IValidable { private static final String TEXT_FIELD = "text"; private static final String URL_FIELD = "url"; private static final String CALLBACK_DATA_FIELD = "callback_data"; private static final String SWITCH_INLINE_QUERY_FIELD = "switch_inline_query"; + private static final String SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD = "switch_inline_query_current_chat"; @JsonProperty(TEXT_FIELD) private String text; ///< Label text on the button @JsonProperty(URL_FIELD) @@ -44,6 +47,13 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { * be automatically returned to the chat they switched from, skipping the chat selection screen. */ private String switchInlineQuery; + @JsonProperty(SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD) + /** + * Optional. If set, pressing the button will insert the bot‘s username and the specified + * inline query in the current chat's input field. Can be empty, + * in which case only the bot’s username will be inserted. + */ + private String switchInlineQueryCurrentChat; public InlineKeyboardButton() { super(); @@ -61,6 +71,9 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { if (jsonObject.has(SWITCH_INLINE_QUERY_FIELD)) { switchInlineQuery = jsonObject.getString(SWITCH_INLINE_QUERY_FIELD); } + if (jsonObject.has(SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD)) { + switchInlineQueryCurrentChat = jsonObject.getString(SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD); + } } public String getText() { @@ -99,6 +112,22 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { return this; } + public String getSwitchInlineQueryCurrentChat() { + return switchInlineQueryCurrentChat; + } + + public InlineKeyboardButton setSwitchInlineQueryCurrentChat(String switchInlineQueryCurrentChat) { + this.switchInlineQueryCurrentChat = switchInlineQueryCurrentChat; + return this; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (text == null || text.isEmpty()) { + throw new TelegramApiValidationException("Text parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); @@ -112,6 +141,9 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { if (switchInlineQuery != null) { jsonObject.put(SWITCH_INLINE_QUERY_FIELD, switchInlineQuery); } + if (switchInlineQueryCurrentChat != null) { + jsonObject.put(SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD, switchInlineQueryCurrentChat); + } return jsonObject; } @@ -129,6 +161,9 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { if (switchInlineQuery != null) { gen.writeStringField(SWITCH_INLINE_QUERY_FIELD, switchInlineQuery); } + if (switchInlineQueryCurrentChat != null) { + gen.writeStringField(SWITCH_INLINE_QUERY_CURRENT_CHAT_FIELD, switchInlineQueryCurrentChat); + } gen.writeEndObject(); gen.flush(); } @@ -141,10 +176,11 @@ public class InlineKeyboardButton implements IBotApiObject, IToJson { @Override public String toString() { return "InlineKeyboardButton{" + - "text=" + text + - ", url=" + url + - ", callbackData=" + callbackData + - ", switchInlineQuery=" + switchInlineQuery + + "text='" + text + '\'' + + ", url='" + url + '\'' + + ", callbackData='" + callbackData + '\'' + + ", switchInlineQuery='" + switchInlineQuery + '\'' + + ", switchInlineQueryCurrentChat='" + switchInlineQueryCurrentChat + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java index a34dd371..7a031570 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardButton.java @@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.api.interfaces.IBotApiObject; import org.telegram.telegrambots.api.interfaces.IToJson; +import org.telegram.telegrambots.api.interfaces.IValidable; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; @@ -21,7 +23,7 @@ import java.io.IOException; * after 9 April, 2016. Older clients will ignore them. * @date 10 of April of 2016 */ -public class KeyboardButton implements IBotApiObject, IToJson { +public class KeyboardButton implements IBotApiObject, IToJson, IValidable { private static final String TEXT_FIELD = "text"; private static final String REQUEST_CONTACT_FIELD = "request_contact"; @@ -94,6 +96,13 @@ public class KeyboardButton implements IBotApiObject, IToJson { return this; } + @Override + public void validate() throws TelegramApiValidationException { + if (text == null || text.isEmpty()) { + throw new TelegramApiValidationException("Text parameter can't be empty", this); + } + } + @Override public JSONObject toJson() { JSONObject jsonObject = new JSONObject(); diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardRow.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardRow.java index bc4bb772..ffd3dddb 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardRow.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/buttons/KeyboardRow.java @@ -1,5 +1,8 @@ package org.telegram.telegrambots.api.objects.replykeyboard.buttons; +import org.telegram.telegrambots.api.interfaces.IValidable; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + import java.util.ArrayList; /** @@ -8,7 +11,7 @@ import java.util.ArrayList; * @brief Row for ReplyKeyBoardMarkup * @date 10 of April of 2016 */ -public class KeyboardRow extends ArrayList { +public class KeyboardRow extends ArrayList implements IValidable { public boolean add(String text) { return super.add(new KeyboardButton(text)); } @@ -36,4 +39,11 @@ public class KeyboardRow extends ArrayList { public boolean remove(String text) { return super.remove(new KeyboardButton(text)); } + + @Override + public void validate() throws TelegramApiValidationException { + for (KeyboardButton keyboardButton : this) { + keyboardButton.validate(); + } + } } diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index c88b0575..190fbfae 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -1,5 +1,6 @@ package org.telegram.telegrambots.bots; +import org.apache.commons.io.FileUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.NameValuePair; @@ -18,7 +19,6 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import org.telegram.telegrambots.Constants; -import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.AnswerCallbackQuery; import org.telegram.telegrambots.api.methods.AnswerInlineQuery; import org.telegram.telegrambots.api.methods.BotApiMethod; @@ -55,11 +55,18 @@ 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.exceptions.TelegramApiException; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import org.telegram.telegrambots.updateshandlers.DownloadFileCallback; import org.telegram.telegrambots.updateshandlers.SentCallback; import java.io.IOException; import java.io.Serializable; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.charset.StandardCharsets; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -256,7 +263,7 @@ public abstract class AbsSender { return sendApiMethod(getUserProfilePhotos); } - public final File getFile(GetFile getFile) throws TelegramApiException{ + public final File getFile(GetFile getFile) throws TelegramApiException { if(getFile == null){ throw new TelegramApiException("Parameter getFile can not be null"); } @@ -277,6 +284,24 @@ public abstract class AbsSender { return sendApiMethod(getWebhookInfo); } + public final java.io.File downloadFile(File file) throws TelegramApiException { + if(file == null){ + throw new TelegramApiException("Parameter file can not be null"); + } + String url = MessageFormat.format(File.FILEBASEURL, getBotToken(), file.getFilePath()); + java.io.File output; + try { + output = java.io.File.createTempFile(file.getFileId(), ".tmp"); + FileUtils.copyURLToFile(new URL(url), output); + } catch (MalformedURLException e) { + throw new TelegramApiException("Wrong url for file: " + url); + } catch (IOException e) { + throw new TelegramApiRequestException("Error downloading the file", e); + } + + return output; + } + // Send Requests Async public final void sendMessageAsync(SendMessage sendMessage, SentCallback sentCallback) throws TelegramApiException { @@ -483,7 +508,6 @@ public abstract class AbsSender { if (getUserProfilePhotos == null) { throw new TelegramApiException("Parameter getUserProfilePhotos can not be null"); } - if (sentCallback == null) { throw new TelegramApiException("Parameter sentCallback can not be null"); } @@ -494,8 +518,9 @@ public abstract class AbsSender { public final void getFileAsync(GetFile getFile, SentCallback sentCallback) throws TelegramApiException { if (getFile == null) { throw new TelegramApiException("Parameter getFile can not be null"); - } else if (getFile.getFileId() == null) { - throw new TelegramApiException("Attribute file_id in parameter getFile can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); } sendApiMethodAsync(getFile, sentCallback); @@ -505,7 +530,6 @@ public abstract class AbsSender { if (sentCallback == null) { throw new TelegramApiException("Parameter sentCallback can not be null"); } - GetMe getMe = new GetMe(); sendApiMethodAsync(getMe, sentCallback); } @@ -519,6 +543,31 @@ public abstract class AbsSender { sendApiMethodAsync(getWebhookInfo, sentCallback); } + public final void downloadFileAsync(File file, DownloadFileCallback callback) throws TelegramApiException { + if(file == null){ + throw new TelegramApiException("Parameter file can not be null"); + } + if (callback == null) { + throw new TelegramApiException("Parameter callback can not be null"); + } + + exe.submit(new Runnable() { + @Override + public void run() { + String url = MessageFormat.format(File.FILEBASEURL, getBotToken(), file.getFilePath()); + try { + java.io.File output = java.io.File.createTempFile(file.getFileId(), ".tmp"); + FileUtils.copyURLToFile(new URL(url), output); + callback.onResult(file, output); + } catch (MalformedURLException e) { + callback.onException(file, new TelegramApiException("Wrong url for file: " + url)); + } catch (IOException e) { + callback.onException(file, new TelegramApiRequestException("Error downloading the file", e)); + } + } + }); + } + // Specific Send Requests public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException { @@ -582,7 +631,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendDocument", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendDocument", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -648,7 +697,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendPhoto", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendPhoto", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -732,7 +781,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendVideo", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendVideo", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -793,7 +842,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendSticker", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendSticker", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -841,6 +890,9 @@ public abstract class AbsSender { if (sendAudio.getDisableNotification() != null) { builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString()); } + if (sendAudio.getCaption() != null) { + builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption()); + } HttpEntity multipart = builder.build(); httppost.setEntity(multipart); } else { @@ -862,6 +914,9 @@ public abstract class AbsSender { if (sendAudio.getDisableNotification() != null) { nameValuePairs.add(new BasicNameValuePair(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString())); } + if (sendAudio.getCaption() != null) { + nameValuePairs.add(new BasicNameValuePair(SendAudio.CAPTION_FIELD, sendAudio.getCaption())); + } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); } @@ -881,7 +936,7 @@ public abstract class AbsSender { * {"description":"[Error]: Bad Request: chat not found","error_code":400,"ok":false} */ if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendAudio", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendAudio", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } // and if not, we can expect a "result" section. and out of this can a new Message object be built @@ -924,6 +979,9 @@ public abstract class AbsSender { if (sendVoice.getDuration() != null) { builder.addTextBody(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString()); } + if (sendVoice.getCaption() != null) { + builder.addTextBody(SendVoice.CAPTION_FIELD, sendVoice.getCaption()); + } HttpEntity multipart = builder.build(); httppost.setEntity(multipart); } else { @@ -942,6 +1000,9 @@ public abstract class AbsSender { if (sendVoice.getDuration() != null) { nameValuePairs.add(new BasicNameValuePair(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString())); } + if (sendVoice.getCaption() != null) { + nameValuePairs.add(new BasicNameValuePair(SendVoice.CAPTION_FIELD, sendVoice.getCaption())); + } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); } @@ -956,7 +1017,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at sendVoice", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at sendVoice", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT)); @@ -970,6 +1031,7 @@ public abstract class AbsSender { @Override public void run() { try { + method.validate(); String url = getBaseUrl() + method.getPath(); HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); @@ -986,7 +1048,7 @@ public abstract class AbsSender { } callback.onResult(method, jsonObject); } - } catch (IOException e) { + } catch (IOException | TelegramApiValidationException e) { callback.onException(method, e); } @@ -995,6 +1057,7 @@ public abstract class AbsSender { } private T sendApiMethod(BotApiMethod method) throws TelegramApiException { + method.validate(); String responseContent; try { String url = getBaseUrl() + method.getPath(); @@ -1013,7 +1076,7 @@ public abstract class AbsSender { JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error at " + method.getPath(), jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiRequestException("Error at " + method.getPath(), jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); } return method.deserializeResponse(jsonObject); diff --git a/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiException.java b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiException.java new file mode 100644 index 00000000..05d09385 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiException.java @@ -0,0 +1,46 @@ +/* + * This file is part of TelegramBots. + * + * Foobar is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +package org.telegram.telegrambots.exceptions; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief TODO + * @date 16 of September of 2016 + */ +public class TelegramApiException extends Exception { + public TelegramApiException() { + super(); + } + + public TelegramApiException(String message) { + super(message); + } + + public TelegramApiException(String message, Throwable cause) { + super(message, cause); + } + + public TelegramApiException(Throwable cause) { + super(cause); + } + + protected TelegramApiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiRequestException.java b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiRequestException.java new file mode 100644 index 00000000..4e892ec7 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiRequestException.java @@ -0,0 +1,62 @@ +/* + * This file is part of TelegramBots. + * + * Foobar is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +package org.telegram.telegrambots.exceptions; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Exception thrown when something goes wrong in the api + * @date 14 of January of 2016 + */ +public class TelegramApiRequestException extends TelegramApiException { + private String apiResponse = null; + private Integer errorCode = 0; + + public TelegramApiRequestException(String message) { + super(message); + } + + public TelegramApiRequestException(String message, String apiResponse, Integer errorCode) { + super(message); + this.apiResponse = apiResponse; + this.errorCode = errorCode; + } + + public TelegramApiRequestException(String message, Throwable cause) { + super(message, cause); + } + + public String getApiResponse() { + return apiResponse; + } + + public Integer getErrorCode() { + return errorCode; + } + + @Override + public String toString() { + if (apiResponse == null) { + return super.toString(); + } else if (errorCode == null) { + return super.toString() + ": " + apiResponse; + } else { + return super.toString() + ": [" + errorCode + "] " + apiResponse; + } + } +} diff --git a/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiValidationException.java b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiValidationException.java new file mode 100644 index 00000000..733fd008 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/exceptions/TelegramApiValidationException.java @@ -0,0 +1,53 @@ +/* + * This file is part of TelegramBots. + * + * Foobar is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +package org.telegram.telegrambots.exceptions; + +import org.telegram.telegrambots.api.interfaces.IBotApiObject; +import org.telegram.telegrambots.api.methods.BotApiMethod; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Exception from method validations + * @date 16 of September of 2016 + */ +public class TelegramApiValidationException extends TelegramApiException { + private BotApiMethod method; + private IBotApiObject object; + + public TelegramApiValidationException(String message, BotApiMethod method) { + super(message); + this.method = method; + } + + public TelegramApiValidationException(String message, IBotApiObject object) { + super(message); + this.object = object; + } + + @Override + public String toString() { + if (method != null) { + return super.toString() + " in method: " + method.toString(); + } + if (object != null) { + return super.toString() + " in object: " + object.toString(); + } + return super.toString(); + } +} diff --git a/src/main/java/org/telegram/telegrambots/updateshandlers/DownloadFileCallback.java b/src/main/java/org/telegram/telegrambots/updateshandlers/DownloadFileCallback.java new file mode 100644 index 00000000..18188220 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/updateshandlers/DownloadFileCallback.java @@ -0,0 +1,42 @@ +/* + * This file is part of TelegramBots. + * + * Foobar is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +package org.telegram.telegrambots.updateshandlers; + +import org.telegram.telegrambots.api.objects.File; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Callback to execute api method asynchronously + * @date 10 of September of 2015 + */ +public interface DownloadFileCallback { + /** + * Called when the request is successful + * @param method Method executed + * @param jsonObject Answer from Telegram server + */ + void onResult(File file, java.io.File output); + + /** + * Called when the http request throw an exception + * @param method Method executed + * @param exception Excepction thrown + */ + void onException(File file, Exception exception); +} diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java index b13c5b2f..9ff8a0ec 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java @@ -16,7 +16,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.telegram.telegrambots.Constants; -import org.telegram.telegrambots.TelegramApiException; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.BotOptions; @@ -49,18 +49,6 @@ public class BotSession { private volatile CloseableHttpClient httpclient; private volatile RequestConfig requestConfig; - /** - * Constructor present just to keep backward compatibility will be removed in next mayor release. - * - * @deprecated @deprecated use {@link #BotSession(String, ITelegramLongPollingBot, BotOptions)} - * @param token Token of the bot - * @param callback Callback for incomming updates - */ - @Deprecated - public BotSession(String token, ITelegramLongPollingBot callback) { - this(token, callback, new BotOptions()); - } - public BotSession(String token, ITelegramLongPollingBot callback, BotOptions options) { this.token = token; this.callback = callback; @@ -131,7 +119,7 @@ public class BotSession { String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error getting updates", + throw new TelegramApiRequestException("Error getting updates", jsonObject.getString(Constants.ERRORDESCRIPTIONFIELD), jsonObject.getInt(Constants.ERRORCODEFIELD)); } @@ -156,7 +144,7 @@ public class BotSession { BotLogger.severe(LOGTAG, e); } } - } catch (InvalidObjectException | JSONException | TelegramApiException e) { + } catch (InvalidObjectException | JSONException | TelegramApiRequestException e) { BotLogger.severe(LOGTAG, e); } } catch (Exception global) { diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/Webhook.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/Webhook.java index 96245969..464a4c13 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/Webhook.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/Webhook.java @@ -7,7 +7,7 @@ import org.glassfish.grizzly.ssl.SSLEngineConfigurator; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; -import org.telegram.telegrambots.TelegramApiException; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.bots.ITelegramWebhookBot; import java.io.File; @@ -27,7 +27,7 @@ public class Webhook { private final RestApi restApi; private final String internalUrl; - public Webhook(String keyStore, String keyStorePassword, String internalUrl) throws TelegramApiException { + public Webhook(String keyStore, String keyStorePassword, String internalUrl) throws TelegramApiRequestException { this.KEYSTORE_SERVER_FILE = keyStore; this.KEYSTORE_SERVER_PWD = keyStorePassword; validateServerKeystoreFile(keyStore); @@ -39,7 +39,7 @@ public class Webhook { restApi.registerCallback(callback); } - public void startServer() throws TelegramApiException { + public void startServer() throws TelegramApiRequestException { SSLContextConfigurator sslContext = new SSLContextConfigurator(); // set up security context @@ -58,7 +58,7 @@ public class Webhook { try { grizzlyServer.start(); } catch (IOException e) { - throw new TelegramApiException("Error starting webhook server", e); + throw new TelegramApiRequestException("Error starting webhook server", e); } } @@ -66,10 +66,10 @@ public class Webhook { return URI.create(internalUrl); } - private static void validateServerKeystoreFile(String keyStore) throws TelegramApiException { + private static void validateServerKeystoreFile(String keyStore) throws TelegramApiRequestException { File file = new File(keyStore); if (!file.exists() || !file.canRead()) { - throw new TelegramApiException("Can't find or access server keystore file."); + throw new TelegramApiRequestException("Can't find or access server keystore file."); } } }