1. File downloads

2. Update with latest api changes
3. Improve exceptions
4. Added validation in api methods
5. Moved to maven central
This commit is contained in:
Rubenlagus 2016-09-24 22:37:25 +02:00
parent 7867e7a345
commit 5903cd6338
82 changed files with 1399 additions and 680 deletions

View File

@ -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
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4</version>
</dependency>
```
You can also find it in a different repository, just in case, search [here](https://jitpack.io/#rubenlagus/TelegramBots).
<a name="lets_code"/>

View File

@ -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
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.4</version>
</dependency>
```
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`.

45
pom.xml
View File

@ -6,10 +6,10 @@
<packaging>jar</packaging>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>2.3.5</version>
<version>2.3.8.BETA-SNAPSHOT</version>
<name>Telegram Bots</name>
<url>https://telegram.me/JavaBotsApi</url>
<url>https://github.com/rubenlagus/TelegramBots</url>
<description>Easy to use library to create Telegram Bots</description>
<licenses>
@ -19,6 +19,17 @@
</license>
</licenses>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@ -77,6 +88,11 @@
<artifactId>httpmime</artifactId>
<version>${httpcompontents.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
@ -86,6 +102,31 @@
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}

View File

@ -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;

View File

@ -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<Boolean> {
@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<Boolean> {
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<Boolean> {
public String toString() {
return "AnswerCallbackQuery{" +
"callbackQueryId='" + callbackQueryId + '\'' +
", text=" + text +
", showAlert=" + showAlert + '\'' +
", text='" + text + '\'' +
", showAlert=" + showAlert +
'}';
}
}

View File

@ -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<Boolean> {
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();

View File

@ -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<T extends Serializable> implements JsonSerializable, IToJson {
public abstract class BotApiMethod<T extends Serializable> implements JsonSerializable, IToJson, IValidable {
protected static final String METHOD_FIELD = "method";
/**

View File

@ -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<Message> {
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<Message> {
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

View File

@ -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<File> {
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<File> {
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();

View File

@ -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<User> {
return null;
}
@Override
public void validate() throws TelegramApiValidationException {
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();

View File

@ -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<UserProfilePhotos> {
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();

View File

@ -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<Chat> {
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();

View File

@ -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<ArrayList<ChatMember>> {
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();

View File

@ -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<ChatMember> {
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();

View File

@ -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<Integer> {
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();

View File

@ -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<Boolean> {
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();

View File

@ -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<Boolean> {
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();

View File

@ -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<Boolean> {
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();

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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<Boolean> {
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<Boolean> {
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();

View File

@ -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<Message> {
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<Message> {
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<Message> {
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();

View File

@ -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{" +

View File

@ -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<Message> {
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<Message> {
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();

View File

@ -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<Message> {
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<Message> {
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();

View File

@ -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;

View File

@ -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;

View File

@ -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<Message> {
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<Message> {
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();

View File

@ -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;

View File

@ -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 +
'}';
}
}

View File

@ -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<WebhookInfo> {
return null;
}
@Override
public void validate() throws TelegramApiValidationException {
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();

View File

@ -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<Message> {
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();

View File

@ -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<Message> {
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();

View File

@ -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<Message> {
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();

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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 +
'}';
}
}

View File

@ -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";

View File

@ -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();

View File

@ -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() {

View File

@ -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);

View File

@ -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();

View File

@ -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();

View File

@ -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 {
}

View File

@ -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();

View File

@ -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();

View File

@ -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 {
}

View File

@ -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();

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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();

View File

@ -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<InlineKeyboardButton> inlineKeyboardButtons : keyboard) {
for (InlineKeyboardButton inlineKeyboardButton : inlineKeyboardButtons) {
inlineKeyboardButton.validate();
}
}
}
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();

View File

@ -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 {
}

View File

@ -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();
}

View File

@ -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();

View File

@ -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 bots username and the specified
* inline query in the current chat's input field. Can be empty,
* in which case only the bots 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 + '\'' +
'}';
}
}

View File

@ -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();

View File

@ -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<KeyboardButton> {
public class KeyboardRow extends ArrayList<KeyboardButton> implements IValidable {
public boolean add(String text) {
return super.add(new KeyboardButton(text));
}
@ -36,4 +39,11 @@ public class KeyboardRow extends ArrayList<KeyboardButton> {
public boolean remove(String text) {
return super.remove(new KeyboardButton(text));
}
@Override
public void validate() throws TelegramApiValidationException {
for (KeyboardButton keyboardButton : this) {
keyboardButton.validate();
}
}
}

View File

@ -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<Message> 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<File> 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 extends Serializable> T sendApiMethod(BotApiMethod<T> 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);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -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) {

View File

@ -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.");
}
}
}