TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updatingmessages/EditMessageText.java

162 lines
5.7 KiB
Java

package org.telegram.telegrambots.meta.api.methods.updatingmessages;
import com.fasterxml.jackson.annotation.JsonProperty;
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;
import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.ParseMode;
import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodSerializable;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.Serializable;
import java.util.List;
/**
* @author Ruben Bermudez
* @version 1.0
* Use this method to edit text messages. On
* success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
*/
@SuppressWarnings("unused")
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
@AllArgsConstructor
@Builder
public class EditMessageText extends BotApiMethodSerializable {
public static final String PATH = "editmessagetext";
private static final String CHATID_FIELD = "chat_id";
private static final String MESSAGEID_FIELD = "message_id";
private static final String INLINE_MESSAGE_ID_FIELD = "inline_message_id";
private static final String TEXT_FIELD = "text";
private static final String PARSE_MODE_FIELD = "parse_mode";
private static final String DISABLE_WEB_PREVIEW_FIELD = "disable_web_page_preview";
private static final String REPLYMARKUP_FIELD = "reply_markup";
private static final String ENTITIES_FIELD = "entities";
/**
* Required if inline_message_id is not specified. Unique identifier for the chat to send the
* message to (Or username for channels)
*/
@JsonProperty(CHATID_FIELD)
private String chatId;
/**
* Required if inline_message_id is not specified. Unique identifier of the sent message
*/
@JsonProperty(MESSAGEID_FIELD)
private Integer messageId;
/**
* Required if chat_id and message_id are not specified. Identifier of the inline message
*/
@JsonProperty(INLINE_MESSAGE_ID_FIELD)
private String inlineMessageId;
/**
* New text of the message
*/
@JsonProperty(TEXT_FIELD)
@NonNull
private String text;
/**
* Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width
* text or inline URLs in your bot's message.
*/
@JsonProperty(PARSE_MODE_FIELD)
private String parseMode;
@JsonProperty(DISABLE_WEB_PREVIEW_FIELD)
private Boolean disableWebPagePreview; ///< Optional. Disables link previews for links in this message
@JsonProperty(REPLYMARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. A JSON-serialized object for an inline keyboard.
@JsonProperty(ENTITIES_FIELD)
private List<MessageEntity> entities; ///< Optional. List of special entities that appear in message text, which can be specified instead of parse_mode
public void disableWebPagePreview() {
disableWebPagePreview = true;
}
public void enableWebPagePreview() {
disableWebPagePreview = null;
}
public void enableMarkdown(boolean enable) {
if (enable) {
this.parseMode = ParseMode.MARKDOWN;
} else {
this.parseMode = null;
}
}
public void enableHtml(boolean enable) {
if (enable) {
this.parseMode = ParseMode.HTML;
} else {
this.parseMode = null;
}
}
@Tolerate
public void setChatId(Long chatId) {
this.chatId = chatId == null ? null : chatId.toString();
}
@Override
public String getMethod() {
return PATH;
}
@Override
public Serializable deserializeResponse(String answer) throws TelegramApiRequestException {
return deserializeResponseMessageOrBoolean(answer);
}
@Override
public void validate() throws TelegramApiValidationException {
if (inlineMessageId == null) {
if (chatId == null || chatId.isEmpty()) {
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.isEmpty()) {
throw new TelegramApiValidationException("Text parameter can't be empty", this);
}
if (parseMode != null && (entities != null && !entities.isEmpty()) ) {
throw new TelegramApiValidationException("Parse mode can't be enabled if Entities are provided", this);
}
if (replyMarkup != null) {
replyMarkup.validate();
}
}
public static class EditMessageTextBuilder {
@Tolerate
public EditMessageTextBuilder chatId(Long chatId) {
this.chatId = chatId == null ? null : chatId.toString();
return this;
}
}
}