Skip to content

Commit 7fc4296

Browse files
committed
Added the method sendPaidMedia and the classes InputPaidMedia, InputPaidMediaPhoto and InputPaidMediaVideo
1 parent b59782f commit 7fc4296

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import com.pengrad.telegrambot.AttachName;
4+
5+
import java.io.File;
6+
import java.io.Serializable;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* Stas Parshin
12+
* 23 November 2017
13+
*/
14+
abstract public class InputPaidMedia implements Serializable {
15+
private final static long serialVersionUID = 0L;
16+
17+
private final String type;
18+
private final String media;
19+
private String thumbnail;
20+
transient private InputFile inputFile;
21+
transient private String inputFileAttachId;
22+
transient private String fileName;
23+
transient private String contentType;
24+
25+
InputPaidMedia(String type, Object media) {
26+
this.type = type;
27+
if (media instanceof String) {
28+
this.media = (String) media;
29+
} else {
30+
String attachName = AttachName.next();
31+
this.media = "attach://" + attachName;
32+
inputFileAttachId = attachName;
33+
if (media instanceof File) {
34+
fileName = ((File) media).getName();
35+
inputFile = new InputFile((File) media, getFileName(), getContentType());
36+
} else if (media instanceof byte[]) {
37+
inputFile = new InputFile((byte[]) media, getFileName(), getContentType());
38+
}
39+
}
40+
}
41+
42+
43+
public InputFile inputFile() {
44+
return inputFile;
45+
}
46+
47+
public String getInputFileId() {
48+
return inputFileAttachId;
49+
}
50+
51+
public String getFileName() {
52+
return (fileName != null && !fileName.isEmpty()) ? fileName : getDefaultFileName();
53+
}
54+
55+
public String getContentType() {
56+
return (contentType != null && !contentType.isEmpty()) ? contentType : getDefaultContentType();
57+
}
58+
59+
abstract protected String getDefaultFileName();
60+
61+
abstract protected String getDefaultContentType();
62+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import com.pengrad.telegrambot.request.ContentTypes;
4+
5+
import java.io.File;
6+
import java.io.Serializable;
7+
8+
public class InputPaidMediaPhoto extends InputPaidMedia implements Serializable {
9+
10+
private final static long serialVersionUID = 1L;
11+
12+
public InputPaidMediaPhoto(String media) {
13+
super("photo", media);
14+
}
15+
16+
public InputPaidMediaPhoto(File media) {
17+
super("photo", media);
18+
}
19+
20+
public InputPaidMediaPhoto(byte[] media) {
21+
super("photo", media);
22+
}
23+
24+
25+
@Override
26+
public String getDefaultFileName() {
27+
return ContentTypes.PHOTO_FILE_NAME;
28+
}
29+
30+
@Override
31+
public String getDefaultContentType() {
32+
return ContentTypes.PHOTO_MIME_TYPE;
33+
}
34+
35+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import com.pengrad.telegrambot.AttachName;
4+
import com.pengrad.telegrambot.request.ContentTypes;
5+
6+
import java.io.File;
7+
import java.io.Serializable;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class InputPaidMediaVideo extends InputPaidMedia implements Serializable {
12+
13+
private final static long serialVersionUID = 1L;
14+
15+
private Integer width, height, duration;
16+
private Boolean supports_streaming;
17+
18+
private String thumbnail;
19+
20+
transient private Map<String, Object> attachments = new HashMap<>();
21+
22+
23+
public InputPaidMediaVideo(String media) {
24+
super("video", media);
25+
}
26+
27+
public InputPaidMediaVideo(File media) {
28+
super("video", media);
29+
}
30+
31+
public InputPaidMediaVideo(byte[] media) {
32+
super("video", media);
33+
}
34+
35+
public InputPaidMediaVideo width(Integer width) {
36+
this.width = width;
37+
return this;
38+
}
39+
40+
public InputPaidMediaVideo height(Integer height) {
41+
this.height = height;
42+
return this;
43+
}
44+
45+
public InputPaidMediaVideo duration(Integer duration) {
46+
this.duration = duration;
47+
return this;
48+
}
49+
50+
public InputPaidMediaVideo supportsStreaming(boolean supportsStreaming) {
51+
this.supports_streaming = supportsStreaming;
52+
return this;
53+
}
54+
55+
public InputPaidMediaVideo thumbnail(File thumbnail) {
56+
String attachName = AttachName.next();
57+
attachments.put(attachName, thumbnail);
58+
this.thumbnail = "attach://" + attachName;
59+
return this;
60+
}
61+
62+
public InputPaidMediaVideo thumbnail(byte[] thumbnail) {
63+
String attachName = AttachName.next();
64+
attachments.put(attachName, thumbnail);
65+
this.thumbnail = "attach://" + attachName;
66+
return this;
67+
}
68+
69+
public Map<String, Object> getAttachments() {
70+
return attachments;
71+
}
72+
73+
74+
@Override
75+
public String getDefaultFileName() {
76+
return ContentTypes.VIDEO_FILE_NAME;
77+
}
78+
79+
@Override
80+
public String getDefaultContentType() {
81+
return ContentTypes.VIDEO_MIME_TYPE;
82+
}
83+
84+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
import com.pengrad.telegrambot.model.MessageEntity;
4+
import com.pengrad.telegrambot.model.request.*;
5+
import com.pengrad.telegrambot.response.MessagesResponse;
6+
import com.pengrad.telegrambot.response.SendResponse;
7+
8+
import java.io.File;
9+
import java.util.Map;
10+
11+
public class SendPaidMedia extends BaseRequest<SendPaidMedia, SendResponse> {
12+
13+
14+
public SendPaidMedia(Object chatId, Integer starCount, InputPaidMedia... media) {
15+
super(SendResponse.class);
16+
add("chat_id", chatId).add("star_count", starCount).add("media", media);
17+
}
18+
19+
public SendPaidMedia caption(String caption) {
20+
return add("caption", caption);
21+
}
22+
23+
public SendPaidMedia parseMode(ParseMode parseMode) {
24+
return add("parse_mode", parseMode.name());
25+
}
26+
27+
public SendPaidMedia captionEntities(MessageEntity... entities) {
28+
return add("caption_entities", entities);
29+
}
30+
31+
public SendPaidMedia showCaptionAboveMedia(boolean showCaptionAboveMedia) {
32+
return add("show_caption_above_media", showCaptionAboveMedia);
33+
}
34+
35+
public SendPaidMedia disableNotification(boolean disableNotification) {
36+
return add("disable_notification", disableNotification);
37+
}
38+
39+
public SendPaidMedia protectContent(boolean protectContent) {
40+
return add("protect_content", protectContent);
41+
}
42+
43+
public SendPaidMedia replyParameters(ReplyParameters replyParameters) {
44+
return add("reply_parameters", replyParameters);
45+
}
46+
47+
public SendPaidMedia replyMarkup(InlineKeyboardMarkup replyMarkup) {
48+
return add("reply_markup", replyMarkup);
49+
}
50+
51+
public SendPaidMedia replyMarkup(ReplyKeyboardMarkup replyMarkup) {
52+
return add("reply_markup", replyMarkup);
53+
}
54+
55+
public SendPaidMedia replyMarkup(ReplyKeyboardRemove replyMarkup) {
56+
return add("reply_markup", replyMarkup);
57+
}
58+
59+
public SendPaidMedia replyMarkup(ForceReply replyMarkup) {
60+
return add("reply_markup", replyMarkup);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)