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

127 lines
5.2 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods.send;
2017-11-17 15:47:22 +01:00
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;
2022-06-14 22:20:22 +02:00
import lombok.experimental.Tolerate;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.media.InputMedia;
2020-11-01 23:46:36 +01:00
import org.telegram.telegrambots.meta.api.objects.media.InputMediaAnimation;
import org.telegram.telegrambots.meta.api.objects.media.InputMediaAudio;
import org.telegram.telegrambots.meta.api.objects.media.InputMediaDocument;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2017-11-17 15:47:22 +01:00
import java.util.ArrayList;
import java.util.List;
/**
* @author Ruben Bermudez
* @version 3.5
*
2020-11-01 23:46:36 +01:00
* Use this method to send a group of photos, videos, documents or audios as an album.
* Documents and audio files can be only group in an album with messages of the same type.
* On success, an array of Messages that were sent is returned.
2017-11-17 15:47:22 +01:00
*/
@SuppressWarnings("unused")
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Builder
2017-11-17 15:47:22 +01:00
public class SendMediaGroup extends PartialBotApiMethod<ArrayList<Message>> {
public static final String PATH = "sendMediaGroup";
public static final String CHATID_FIELD = "chat_id";
2022-11-08 19:33:50 +01:00
public static final String MESSAGETHREADID_FIELD = "message_thread_id";
2017-11-17 15:47:22 +01:00
public static final String MEDIA_FIELD = "media";
public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id";
public static final String DISABLENOTIFICATION_FIELD = "disable_notification";
2020-11-01 23:46:36 +01:00
public static final String ALLOWSENDINGWITHOUTREPLY_FIELD = "allow_sending_without_reply";
2022-01-02 00:06:28 +01:00
public static final String PROTECTCONTENT_FIELD = "protect_content";
2017-11-17 15:47:22 +01:00
2020-11-01 23:46:36 +01:00
@NonNull
2017-11-17 15:47:22 +01:00
private String chatId; ///< Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2022-11-08 19:33:50 +01:00
/**
* Unique identifier for the target message thread (topic) of the forum;
* for forum supergroups only
*/
private Integer messageThreadId;
2020-11-01 23:46:36 +01:00
@NonNull
private List<InputMedia> medias; ///< A JSON-serialized array describing photos and videos to be sent, must include 2–10 items
2017-11-17 15:47:22 +01:00
private Integer replyToMessageId; ///< Optional. If the messages are a reply, ID of the original message
private Boolean disableNotification; ///< Optional. Sends the messages silently. Users will receive a notification with no sound.
2020-11-01 23:46:36 +01:00
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
private Boolean protectContent; ///< Optional. Protects the contents of sent messages from forwarding and saving
2017-11-17 15:47:22 +01:00
2022-06-14 22:20:22 +02:00
@Tolerate
public void setChatId(@NonNull Long chatId) {
this.chatId = chatId.toString();
}
2020-11-01 23:46:36 +01:00
public void enableNotification() {
2017-11-17 15:47:22 +01:00
this.disableNotification = false;
}
2020-11-01 23:46:36 +01:00
public void disableNotification() {
2017-11-17 15:47:22 +01:00
this.disableNotification = true;
}
@Override
public ArrayList<Message> deserializeResponse(String answer) throws TelegramApiRequestException {
2022-06-16 19:36:20 +02:00
return deserializeResponseArray(answer, Message.class);
2017-11-17 15:47:22 +01:00
}
@Override
public void validate() throws TelegramApiValidationException {
2022-06-14 22:20:22 +02:00
if (chatId.isEmpty()) {
2017-11-17 15:47:22 +01:00
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
2022-06-14 22:20:22 +02:00
if (medias.isEmpty()) {
2017-11-17 15:47:22 +01:00
throw new TelegramApiValidationException("Media parameter can't be empty", this);
2020-11-01 23:46:36 +01:00
} else if (medias.size() < 2 || medias.size() > 10) {
throw new TelegramApiValidationException("Number of media should be between 2 and 10", this);
2017-11-17 15:47:22 +01:00
}
2020-11-01 23:46:36 +01:00
for (InputMedia inputMedia : medias) {
if (inputMedia == null) {
throw new TelegramApiValidationException("Media parameter can not be empty", this);
} else if (inputMedia instanceof InputMediaAnimation) {
throw new TelegramApiValidationException("Media parameter can not be an Animation", this);
2018-07-27 00:27:26 +02:00
} else {
2020-11-01 23:46:36 +01:00
inputMedia.validate();
2018-07-27 00:27:26 +02:00
}
2017-11-17 15:47:22 +01:00
}
2020-11-01 23:46:36 +01:00
if (medias.stream().anyMatch(x -> x instanceof InputMediaAudio)) {
if (!medias.stream().allMatch(x -> x instanceof InputMediaAudio)) {
throw new TelegramApiValidationException("Media parameter containing Audio can not have other types", this);
}
} else if (medias.stream().anyMatch(x -> x instanceof InputMediaDocument)) {
if (!medias.stream().allMatch(x -> x instanceof InputMediaDocument)) {
throw new TelegramApiValidationException("Media parameter containing Document can not have other types", this);
}
}
2017-11-17 15:47:22 +01:00
}
2022-06-14 22:20:22 +02:00
public static class SendMediaGroupBuilder {
@Tolerate
public SendMediaGroupBuilder chatId(@NonNull Long chatId) {
this.chatId = chatId.toString();
return this;
}
}
2017-11-17 15:47:22 +01:00
}