Merge pull request #1168 from Chase22/extract-send-media-interface

Externalize interface for MediaSendingMethods and add getMethod to PartialBotApiMethod
This commit is contained in:
Ruben Bermudez 2023-01-03 22:53:02 +01:00 committed by GitHub
commit d1c70210f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 193 additions and 26 deletions

View File

@ -2,7 +2,6 @@ package org.telegram.telegrambots.meta.api.methods;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable; import java.io.Serializable;
@ -17,10 +16,4 @@ import java.io.Serializable;
public abstract class BotApiMethod<T extends Serializable> extends PartialBotApiMethod<T> { public abstract class BotApiMethod<T extends Serializable> extends PartialBotApiMethod<T> {
protected static final String METHOD_FIELD = "method"; protected static final String METHOD_FIELD = "method";
/**
* Getter for method path (that is the same as method name)
* @return Method path
*/
@JsonProperty(METHOD_FIELD)
public abstract String getMethod();
} }

View File

@ -1,6 +1,7 @@
package org.telegram.telegrambots.meta.api.methods; package org.telegram.telegrambots.meta.api.methods;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.CollectionType;
@ -56,4 +57,11 @@ public abstract class PartialBotApiMethod<T extends Serializable> implements Val
throw new TelegramApiRequestException("Unable to deserialize response", e); throw new TelegramApiRequestException("Unable to deserialize response", e);
} }
} }
/**
* Getter for method path (that is the same as method name)
* @return Method path
*/
@JsonProperty(BotApiMethod.METHOD_FIELD)
public abstract String getMethod();
} }

View File

@ -46,6 +46,11 @@ public class SetChatPhoto extends PartialBotApiMethod<Boolean> {
this.chatId = chatId.toString(); this.chatId = chatId.toString();
} }
@Override
public String getMethod() {
return PATH;
}
@Override @Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
return deserializeResponse(answer, Boolean.class); return deserializeResponse(answer, Boolean.class);

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -37,7 +36,7 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendAnimation extends PartialBotApiMethod<Message> { public class SendAnimation extends SendMediaBotMethod<Message> {
public static final String PATH = "sendAnimation"; public static final String PATH = "sendAnimation";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -59,6 +58,8 @@ public class SendAnimation extends PartialBotApiMethod<Message> {
@NonNull @NonNull
private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels)
private Integer messageThreadId;
/** /**
* Animation to send. Pass a file_id as String to send an animation that exists on the * Animation to send. Pass a file_id as String to send an animation that exists on the
* Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation * Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation
@ -68,7 +69,6 @@ public class SendAnimation extends PartialBotApiMethod<Message> {
* Unique identifier for the target message thread (topic) of the forum; * Unique identifier for the target message thread (topic) of the forum;
* for forum supergroups only * for forum supergroups only
*/ */
private Integer messageThreadId;
@NonNull @NonNull
private InputFile animation; private InputFile animation;
private Integer duration; ///< Optional. Duration of sent animation in seconds private Integer duration; ///< Optional. Duration of sent animation in seconds
@ -136,6 +136,21 @@ public class SendAnimation extends PartialBotApiMethod<Message> {
} }
} }
@Override
public String getMethod() {
return PATH;
}
@Override
public InputFile getFile() {
return animation;
}
@Override
public String getFileField() {
return ANIMATION_FIELD;
}
public static class SendAnimationBuilder { public static class SendAnimationBuilder {
@Tolerate @Tolerate

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -38,7 +37,7 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendAudio extends PartialBotApiMethod<Message> { public class SendAudio extends SendMediaBotMethod<Message> {
public static final String PATH = "sendaudio"; public static final String PATH = "sendaudio";
public static final String DURATION_FIELD = "duration"; public static final String DURATION_FIELD = "duration";
@ -127,6 +126,21 @@ public class SendAudio extends PartialBotApiMethod<Message> {
} }
} }
@Override
public String getMethod() {
return PATH;
}
@Override
public InputFile getFile() {
return audio;
}
@Override
public String getFileField() {
return AUDIO_FIELD;
}
public static class SendAudioBuilder { public static class SendAudioBuilder {
@Tolerate @Tolerate

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -34,7 +33,7 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendDocument extends PartialBotApiMethod<Message> { public class SendDocument extends SendMediaBotMethod<Message> {
public static final String PATH = "senddocument"; public static final String PATH = "senddocument";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -97,6 +96,7 @@ public class SendDocument extends PartialBotApiMethod<Message> {
public Message deserializeResponse(String answer) throws TelegramApiRequestException { public Message deserializeResponse(String answer) throws TelegramApiRequestException {
return deserializeResponse(answer, Message.class); return deserializeResponse(answer, Message.class);
} }
@Override @Override
public void validate() throws TelegramApiValidationException { public void validate() throws TelegramApiValidationException {
if (chatId.isEmpty()) { if (chatId.isEmpty()) {
@ -118,6 +118,21 @@ public class SendDocument extends PartialBotApiMethod<Message> {
} }
} }
@Override
public InputFile getFile() {
return document;
}
@Override
public String getFileField() {
return DOCUMENT_FIELD;
}
@Override
public String getMethod() {
return PATH;
}
public static class SendDocumentBuilder { public static class SendDocumentBuilder {
@Tolerate @Tolerate

View File

@ -0,0 +1,24 @@
package org.telegram.telegrambots.meta.api.methods.send;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import java.io.Serializable;
public abstract class SendMediaBotMethod<T extends Serializable> extends PartialBotApiMethod<T> {
public static String CHATID_FIELD = "chat_id";
public static String MESSAGETHREADID_FIELD = "message_thread_id";
public static String REPLYTOMESSAGEID_FIELD = "reply_to_message_id";
public static String DISABLENOTIFICATION_FIELD = "disable_notification";
public static String PROTECTCONTENT_FIELD = "protect_content";
public static String ALLOWSENDINGWITHOUTREPLY_FIELD = "allow_sending_without_reply";
public abstract String getChatId();
public abstract Integer getMessageThreadId();
public abstract Integer getReplyToMessageId();
public abstract Boolean getDisableNotification();
public abstract Boolean getAllowSendingWithoutReply();
public abstract Boolean getProtectContent();
public abstract InputFile getFile();
public abstract String getFileField();
}

View File

@ -115,6 +115,11 @@ public class SendMediaGroup extends PartialBotApiMethod<ArrayList<Message>> {
} }
} }
@Override
public String getMethod() {
return PATH;
}
public static class SendMediaGroupBuilder { public static class SendMediaGroupBuilder {
@Tolerate @Tolerate

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -35,7 +34,7 @@ import java.util.Objects;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendPhoto extends PartialBotApiMethod<Message> { public class SendPhoto extends SendMediaBotMethod<Message> {
public static final String PATH = "sendphoto"; public static final String PATH = "sendphoto";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -113,8 +112,22 @@ public class SendPhoto extends PartialBotApiMethod<Message> {
} }
} }
@Override
public InputFile getFile() {
return photo;
}
@Override
public String getFileField() {
return PHOTO_FIELD;
}
@Override
public String getMethod() {
return PATH;
}
public static class SendPhotoBuilder { public static class SendPhotoBuilder {
@Tolerate @Tolerate
public SendPhotoBuilder chatId(@NonNull Long chatId) { public SendPhotoBuilder chatId(@NonNull Long chatId) {
this.chatId = chatId.toString(); this.chatId = chatId.toString();

View File

@ -10,7 +10,6 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
@ -31,7 +30,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendSticker extends PartialBotApiMethod<Message> { public class SendSticker extends SendMediaBotMethod<Message> {
public static final String PATH = "sendsticker"; public static final String PATH = "sendsticker";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -93,6 +92,22 @@ public class SendSticker extends PartialBotApiMethod<Message> {
} }
} }
@Override
public String getMethod() {
return null;
}
@Override
public InputFile getFile() {
return sticker;
}
@Override
public String getFileField() {
return STICKER_FIELD;
}
public static class SendStickerBuilder { public static class SendStickerBuilder {
@Tolerate @Tolerate

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -35,7 +34,7 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendVideo extends PartialBotApiMethod<Message> { public class SendVideo extends SendMediaBotMethod<Message> {
public static final String PATH = "sendvideo"; public static final String PATH = "sendvideo";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -131,6 +130,21 @@ public class SendVideo extends PartialBotApiMethod<Message> {
} }
} }
@Override
public InputFile getFile() {
return video;
}
@Override
public String getFileField() {
return VIDEO_FIELD;
}
@Override
public String getMethod() {
return PATH;
}
public static class SendVideoBuilder { public static class SendVideoBuilder {
@Tolerate @Tolerate

View File

@ -10,7 +10,6 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
@ -32,7 +31,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendVideoNote extends PartialBotApiMethod<Message> { public class SendVideoNote extends SendMediaBotMethod<Message> {
public static final String PATH = "sendvideonote"; public static final String PATH = "sendvideonote";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -106,6 +105,21 @@ public class SendVideoNote extends PartialBotApiMethod<Message> {
} }
} }
@Override
public String getMethod() {
return PATH;
}
@Override
public InputFile getFile() {
return videoNote;
}
@Override
public String getFileField() {
return VIDEONOTE_FIELD;
}
public static class SendVideoNoteBuilder { public static class SendVideoNoteBuilder {
@Tolerate @Tolerate

View File

@ -11,7 +11,6 @@ import lombok.Setter;
import lombok.Singular; import lombok.Singular;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity; import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -36,7 +35,7 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class SendVoice extends PartialBotApiMethod<Message> { public class SendVoice extends SendMediaBotMethod<Message> {
public static final String PATH = "sendvoice"; public static final String PATH = "sendvoice";
public static final String CHATID_FIELD = "chat_id"; public static final String CHATID_FIELD = "chat_id";
@ -106,6 +105,21 @@ public class SendVoice extends PartialBotApiMethod<Message> {
} }
} }
@Override
public String getMethod() {
return PATH;
}
@Override
public InputFile getFile() {
return voice;
}
@Override
public String getFileField() {
return VOICE_FIELD;
}
public static class SendVoiceBuilder { public static class SendVoiceBuilder {
@Tolerate @Tolerate

View File

@ -76,6 +76,11 @@ public class AddStickerToSet extends PartialBotApiMethod<Boolean> {
return deserializeResponse(answer, Boolean.class); return deserializeResponse(answer, Boolean.class);
} }
@Override
public String getMethod() {
return PATH;
}
@Override @Override
public void validate() throws TelegramApiValidationException { public void validate() throws TelegramApiValidationException {
if (userId <= 0) { if (userId <= 0) {

View File

@ -10,7 +10,6 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Tolerate; import lombok.experimental.Tolerate;
import org.telegram.telegrambots.meta.api.methods.CopyMessage;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod; import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.objects.InputFile; import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.stickers.MaskPosition; import org.telegram.telegrambots.meta.api.objects.stickers.MaskPosition;
@ -18,7 +17,6 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -124,6 +122,11 @@ public class CreateNewStickerSet extends PartialBotApiMethod<Boolean> {
return "mask".equals(stickerType); return "mask".equals(stickerType);
} }
@Override
public String getMethod() {
return PATH;
}
@Override @Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
return deserializeResponse(answer, Boolean.class); return deserializeResponse(answer, Boolean.class);

View File

@ -42,6 +42,11 @@ public class UploadStickerFile extends PartialBotApiMethod<File> {
@NonNull @NonNull
private InputFile pngSticker; ///< New sticker file private InputFile pngSticker; ///< New sticker file
@Override
public String getMethod() {
return PATH;
}
@Override @Override
public File deserializeResponse(String answer) throws TelegramApiRequestException { public File deserializeResponse(String answer) throws TelegramApiRequestException {
return deserializeResponse(answer, File.class); return deserializeResponse(answer, File.class);

View File

@ -81,6 +81,11 @@ public class EditMessageMedia extends PartialBotApiMethod<Serializable> {
this.chatId = chatId == null ? null : chatId.toString(); this.chatId = chatId == null ? null : chatId.toString();
} }
@Override
public String getMethod() {
return PATH;
}
@Override @Override
public Serializable deserializeResponse(String answer) throws TelegramApiRequestException { public Serializable deserializeResponse(String answer) throws TelegramApiRequestException {
try { try {