TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/media/InputMedia.java

128 lines
4.8 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.objects.media;
2017-11-17 15:47:22 +01:00
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
2019-06-08 21:33:28 +02:00
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2018-07-27 00:27:26 +02:00
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.Singular;
import lombok.ToString;
2020-10-31 14:04:38 +01:00
import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.interfaces.Validable;
2020-11-01 23:46:36 +01:00
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.media.serialization.InputMediaDeserializer;
import org.telegram.telegrambots.meta.api.objects.media.serialization.InputMediaSerializer;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2017-11-17 15:47:22 +01:00
import java.io.File;
import java.io.InputStream;
2020-11-01 23:46:36 +01:00
import java.util.List;
2017-11-17 15:47:22 +01:00
/**
* @author Ruben Bermudez
* @version 3.5
*/
2020-11-01 23:46:36 +01:00
@SuppressWarnings({"unused"})
2018-07-27 00:27:26 +02:00
@JsonSerialize(using = InputMediaSerializer.class)
2019-06-08 21:33:28 +02:00
@JsonDeserialize(using = InputMediaDeserializer.class)
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
public abstract class InputMedia implements Validable, BotApiObject {
2018-07-27 00:27:26 +02:00
public static final String TYPE_FIELD = "type";
public static final String MEDIA_FIELD = "media";
public static final String CAPTION_FIELD = "caption";
public static final String PARSEMODE_FIELD = "parse_mode";
2022-01-02 00:06:28 +01:00
public static final String CAPTIONENTITIES_FIELD = "caption_entities";
2017-11-17 15:47:22 +01:00
/**
* File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
* pass an HTTP URL for Telegram to get a file from the Internet, or pass "attach://<file_attach_name>"
* to upload a new one using multipart/form-data under <file_attach_name> name.
*/
2018-07-27 00:27:26 +02:00
@JsonProperty(MEDIA_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2017-11-17 15:47:22 +01:00
private String media;
@JsonProperty(CAPTION_FIELD)
private String caption; ///< Optional. Caption of the media to be sent, 0-200 characters
2018-02-14 20:36:22 +01:00
@JsonProperty(PARSEMODE_FIELD)
2018-07-27 00:27:26 +02:00
private String parseMode; ///< Optional. Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.
2022-01-02 00:06:28 +01:00
@JsonProperty(CAPTIONENTITIES_FIELD)
2020-11-01 23:46:36 +01:00
@Singular
2022-01-02 00:06:28 +01:00
private List<MessageEntity> captionEntities; ///< Optional. List of special entities that appear in message text, which can be specified instead of parse_mode
2017-11-17 15:47:22 +01:00
@JsonIgnore
private boolean isNewMedia; ///< True to upload a new media, false to use a fileId or URL
@JsonIgnore
private String mediaName; ///< Name of the media to upload
@JsonIgnore
private File newMediaFile; ///< New media file
@JsonIgnore
private InputStream newMediaStream; ///< New media stream
@JsonIgnore
public boolean isNewMedia() {
return isNewMedia;
}
/**
* Use this setter to send an existing file (using file_id) or an url.
* @param media File_id or URL of the file to send
*/
2020-11-01 23:46:36 +01:00
public void setMedia(String media) {
2017-11-17 15:47:22 +01:00
this.media = media;
this.isNewMedia = false;
}
/**
* Use this setter to send new file.
* @param mediaFile File to send
*/
2020-11-01 23:46:36 +01:00
public void setMedia(File mediaFile, String fileName) {
2017-11-17 15:47:22 +01:00
this.newMediaFile = mediaFile;
this.isNewMedia = true;
this.mediaName = fileName;
this.media = "attach://" + fileName;
}
/**
* Use this setter to send new file as stream.
* @param mediaStream File to send
*/
2020-11-01 23:46:36 +01:00
public void setMedia(InputStream mediaStream, String fileName) {
2017-11-17 15:47:22 +01:00
this.newMediaStream = mediaStream;
this.isNewMedia = true;
this.mediaName = fileName;
this.media = "attach://" + fileName;
2018-02-14 20:36:22 +01:00
}
2017-11-17 15:47:22 +01:00
@Override
public void validate() throws TelegramApiValidationException {
if (isNewMedia) {
if (mediaName == null || mediaName.isEmpty()) {
throw new TelegramApiValidationException("Media name can't be empty", this);
}
if (newMediaFile == null && newMediaStream == null) {
throw new TelegramApiValidationException("Media can't be empty", this);
}
} else if (media == null || media.isEmpty()) {
throw new TelegramApiValidationException("Media can't be empty", this);
}
2022-01-02 00:06:28 +01:00
if (parseMode != null && (captionEntities != null && !captionEntities.isEmpty()) ) {
2020-11-01 23:46:36 +01:00
throw new TelegramApiValidationException("Parse mode can't be enabled if Entities are provided", this);
}
2017-11-17 15:47:22 +01:00
}
@JsonProperty(TYPE_FIELD)
public abstract String getType();
}