TelegramBots/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java

222 lines
7.1 KiB
Java

package org.telegram.telegrambots.api.methods.send;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.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;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Use this method to send text messages. On success, the sent Message is returned.
* @date 20 of June of 2015
*/
public class SendMessage extends BotApiMethod<Message> {
public static final String PATH = "sendmessage";
private static final String CHATID_FIELD = "chat_id";
private static final String TEXT_FIELD = "text";
private static final String PARSEMODE_FIELD = "parse_mode";
private static final String DISABLEWEBPAGEPREVIEW_FIELD = "disable_web_page_preview";
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 text; ///< Text of the message to be sent
private String parseMode; ///< Optional. Send Markdown, if you want Telegram apps to show bold, italic and URL text in your bot's message.
private Boolean disableWebPagePreview; ///< Optional. Disables link previews for links in this message
/**
* 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
*/
private Boolean disableNotification;
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
public SendMessage() {
super();
}
public String getChatId() {
return chatId;
}
public SendMessage setChatId(String chatId) {
this.chatId = chatId;
return this;
}
public String getText() {
return text;
}
public SendMessage setText(String text) {
this.text = text;
return this;
}
public Integer getReplyToMessageId() {
return replyToMessageId;
}
public SendMessage setReplyToMessageId(Integer replyToMessageId) {
this.replyToMessageId = replyToMessageId;
return this;
}
public ReplyKeyboard getReplyMarkup() {
return replyMarkup;
}
public SendMessage setReplyMarkup(ReplyKeyboard replyMarkup) {
this.replyMarkup = replyMarkup;
return this;
}
public Boolean getDisableWebPagePreview() {
return disableWebPagePreview;
}
public Boolean getDisableNotification() {
return disableNotification;
}
public SendMessage disableWebPagePreview() {
disableWebPagePreview = true;
return this;
}
public SendMessage enableWebPagePreview() {
disableWebPagePreview = null;
return this;
}
public SendMessage enableNotification() {
this.disableNotification = null;
return this;
}
public SendMessage disableNotification() {
this.disableNotification = true;
return this;
}
public SendMessage enableMarkdown(boolean enable) {
if (enable) {
this.parseMode = ParseMode.MARKDOWN;
} else {
this.parseMode = null;
}
return this;
}
public SendMessage enableHtml(boolean enable) {
if (enable) {
this.parseMode = ParseMode.HTML;
} else {
this.parseMode = null;
}
return this;
}
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put(CHATID_FIELD, chatId);
jsonObject.put(TEXT_FIELD, text);
if (parseMode != null) {
jsonObject.put(PARSEMODE_FIELD, parseMode);
}
if (disableWebPagePreview != null) {
jsonObject.put(DISABLEWEBPAGEPREVIEW_FIELD, disableWebPagePreview);
}
if (disableNotification != null) {
jsonObject.put(DISABLENOTIFICATION_FIELD, disableNotification);
}
if (replyToMessageId != null) {
jsonObject.put(REPLYTOMESSAGEID_FIELD, replyToMessageId);
}
if (replyMarkup != null) {
jsonObject.put(REPLYMARKUP_FIELD, replyMarkup.toJson());
}
return jsonObject;
}
@Override
public String getPath() {
return PATH;
}
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
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();
gen.writeStringField(METHOD_FIELD, PATH);
gen.writeStringField(CHATID_FIELD, chatId);
gen.writeStringField(TEXT_FIELD, text);
if (parseMode != null) {
gen.writeStringField(PARSEMODE_FIELD, parseMode);
}
if (disableWebPagePreview != null) {
gen.writeBooleanField(DISABLEWEBPAGEPREVIEW_FIELD, disableWebPagePreview);
}
if (disableNotification != null) {
gen.writeBooleanField(DISABLENOTIFICATION_FIELD, disableNotification);
}
if (replyToMessageId != null) {
gen.writeNumberField(REPLYTOMESSAGEID_FIELD, replyToMessageId);
}
if (replyMarkup != null) {
gen.writeObjectField(REPLYMARKUP_FIELD, replyMarkup);
}
gen.writeEndObject();
gen.flush();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
@Override
public String toString() {
return "SendMessage{" +
"chatId='" + chatId + '\'' +
", text='" + text + '\'' +
", parseMode='" + parseMode + '\'' +
", disableWebPagePreview=" + disableWebPagePreview +
", replyToMessageId=" + replyToMessageId +
", replyMarkup=" + replyMarkup +
'}';
}
}