TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/send/SendGame.java

122 lines
4.8 KiB
Java
Raw Normal View History

2016-09-28 00:29:44 +02:00
/*
* This file is part of TelegramBots.
*
2016-09-28 00:31:34 +02:00
* TelegramBots is free software: you can redistribute it and/or modify
2016-09-28 00:29:44 +02:00
* 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.
*
2016-09-28 00:31:34 +02:00
* TelegramBots is distributed in the hope that it will be useful,
2016-09-28 00:29:44 +02:00
* 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
2016-09-28 00:31:34 +02:00
* along with TelegramBots. If not, see <http://www.gnu.org/licenses/>.
2016-09-28 00:29:44 +02:00
*/
2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods.send;
2016-09-28 00:29:44 +02:00
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
2020-11-01 23:46:36 +01:00
import org.telegram.telegrambots.meta.api.objects.Message;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2016-09-28 00:29:44 +02:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
2019-06-08 21:33:28 +02:00
* Use this method to send a game. On success, the sent Message is returned.
2016-09-28 00:29:44 +02:00
*/
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Builder
2016-09-28 00:29:44 +02:00
public class SendGame extends BotApiMethod<Message> {
public static final String PATH = "sendGame";
private static final String CHATID_FIELD = "chat_id";
private static final String GAMESHORTNAME_FIELD = "game_short_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";
2020-11-01 23:46:36 +01:00
private static final String ALLOWSENDINGWITHOUTREPLY_FIELD = "allow_sending_without_reply";
2022-01-02 00:06:28 +01:00
private static final String PROTECTCONTENT_FIELD = "protect_content";
2016-09-28 00:29:44 +02:00
@JsonProperty(CHATID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2016-09-28 00:29:44 +02:00
private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels)
@JsonProperty(GAMESHORTNAME_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2016-09-28 00:29:44 +02:00
private String gameShortName; ///< Short name of the game
@JsonProperty(DISABLENOTIFICATION_FIELD)
2017-03-27 00:49:08 +02:00
private Boolean disableNotification; ///< Optional. Sends the message silently. Users will receive a notification with no sound.
@JsonProperty(REPLYTOMESSAGEID_FIELD)
2016-09-28 00:29:44 +02:00
private Integer replyToMessageId; ///< Optional. If the message is a reply, ID of the original message
@JsonProperty(REPLYMARKUP_FIELD)
2016-09-28 00:29:44 +02:00
private ReplyKeyboard replyMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard
2020-11-01 23:46:36 +01:00
@JsonProperty(ALLOWSENDINGWITHOUTREPLY_FIELD)
private Boolean allowSendingWithoutReply; ///< Optional Pass True, if the message should be sent even if the specified replied-to message is not found
2022-01-02 00:06:28 +01:00
@JsonProperty(PROTECTCONTENT_FIELD)
private Boolean protectContent; ///< Optional. Protects the contents of sent messages from forwarding and saving
2016-09-28 00:29:44 +02:00
2020-11-01 23:46:36 +01:00
public void enableNotification() {
2016-09-28 00:29:44 +02:00
this.disableNotification = null;
}
2020-11-01 23:46:36 +01:00
public void disableNotification() {
2016-09-28 00:29:44 +02:00
this.disableNotification = true;
}
@Override
public String getMethod() {
2016-09-28 00:29:44 +02:00
return PATH;
}
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Message>>(){});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error sending game", result);
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
2016-09-28 00:29:44 +02:00
}
}
@Override
public void validate() throws TelegramApiValidationException {
2021-04-26 21:55:00 +02:00
if (chatId == null || chatId.isEmpty()) {
2016-09-28 00:29:44 +02:00
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
if (gameShortName == null || gameShortName.isEmpty()) {
throw new TelegramApiValidationException("GameShortName parameter can't be empty", this);
}
if (replyMarkup != null) {
replyMarkup.validate();
2016-09-28 00:29:44 +02:00
}
}
}