Skip to content

Commit 714993d

Browse files
committed
Added the new method sendMediaGroup and two kinds of InputMedia objects to support the new albums feature
1 parent 7a6af9d commit 714993d

File tree

8 files changed

+218
-14
lines changed

8 files changed

+218
-14
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import java.io.Serializable;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
/**
9+
* Stas Parshin
10+
* 23 November 2017
11+
*/
12+
abstract public class InputMedia implements Serializable {
13+
private final static long serialVersionUID = 0L;
14+
15+
private static final AtomicInteger counter = new AtomicInteger();
16+
17+
private final String type;
18+
private final String media;
19+
transient private Map<String, Object> attachments;
20+
21+
public InputMedia(String type, Object media) {
22+
this.type = type;
23+
if (media instanceof String) {
24+
this.media = (String) media;
25+
} else {
26+
String attachName = "inputMedia" + nextId();
27+
this.media = "attach://" + attachName;
28+
if (attachments == null) {
29+
attachments = new HashMap<String, Object>();
30+
}
31+
attachments.put(attachName, media);
32+
}
33+
}
34+
35+
private int nextId() {
36+
return counter.incrementAndGet();
37+
}
38+
39+
// Nullable
40+
public Map<String, Object> getAttachments() {
41+
return attachments;
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import java.io.File;
4+
import java.io.Serializable;
5+
6+
/**
7+
* Stas Parshin
8+
* 23 November 2017
9+
*/
10+
public class InputMediaPhoto extends InputMedia implements Serializable {
11+
private final static long serialVersionUID = 0L;
12+
13+
private String caption;
14+
15+
public InputMediaPhoto(String media) {
16+
super("photo", media);
17+
}
18+
19+
public InputMediaPhoto(File media) {
20+
super("photo", media);
21+
}
22+
23+
public InputMediaPhoto(byte[] media) {
24+
super("photo", media);
25+
}
26+
27+
public InputMediaPhoto caption(String caption) {
28+
this.caption = caption;
29+
return this;
30+
}
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.pengrad.telegrambot.model.request;
2+
3+
import java.io.File;
4+
import java.io.Serializable;
5+
6+
/**
7+
* Stas Parshin
8+
* 23 November 2017
9+
*/
10+
public class InputMediaVideo extends InputMedia implements Serializable {
11+
private final static long serialVersionUID = 0L;
12+
13+
private String caption;
14+
private Integer width, height, duration;
15+
16+
public InputMediaVideo(String media) {
17+
super("video", media);
18+
}
19+
20+
public InputMediaVideo(File media) {
21+
super("video", media);
22+
}
23+
24+
public InputMediaVideo(byte[] media) {
25+
super("video", media);
26+
}
27+
28+
public InputMediaVideo caption(String caption) {
29+
this.caption = caption;
30+
return this;
31+
}
32+
33+
public InputMediaVideo width(Integer width) {
34+
this.width = width;
35+
return this;
36+
}
37+
38+
public InputMediaVideo height(Integer height) {
39+
this.height = height;
40+
return this;
41+
}
42+
43+
public InputMediaVideo duration(Integer duration) {
44+
this.duration = duration;
45+
return this;
46+
}
47+
}

library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.pengrad.telegrambot.request;
22

3-
import com.google.gson.Gson;
43
import com.pengrad.telegrambot.model.request.ShippingOption;
54
import com.pengrad.telegrambot.response.BaseResponse;
65

@@ -10,8 +9,6 @@
109
*/
1110
public class AnswerShippingQuery extends BaseRequest<AnswerShippingQuery, BaseResponse> {
1211

13-
private static Gson gson = new Gson();
14-
1512
public AnswerShippingQuery(String shippingQueryId, ShippingOption... shippingOptions) {
1613
super(BaseResponse.class);
1714
add("shipping_query_id", shippingQueryId).add("ok", true).add("shipping_options", serialize(shippingOptions));

library/src/main/java/com/pengrad/telegrambot/request/BaseRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ protected T add(String name, Object val) {
3131
return thisAsT;
3232
}
3333

34+
protected T addAll(Map<String, Object> values) {
35+
parameters.putAll(values);
36+
return thisAsT;
37+
}
38+
3439
public String getMethod() {
3540
String className = this.getClass().getSimpleName();
3641
return Character.toLowerCase(className.charAt(0)) + className.substring(1);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
import com.pengrad.telegrambot.model.request.InputMedia;
4+
import com.pengrad.telegrambot.response.MessagesResponse;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* Stas Parshin
10+
* 23 November 2017
11+
*/
12+
public class SendMediaGroup extends BaseRequest<SendMediaGroup, MessagesResponse> {
13+
14+
private boolean isMultipart = false;
15+
16+
public SendMediaGroup(Object chatId, InputMedia... media) {
17+
super(MessagesResponse.class);
18+
add("chat_id", chatId).add("media", serialize(media));
19+
20+
for (InputMedia m : media) {
21+
Map<String, Object> attachments = m.getAttachments();
22+
if (attachments != null) {
23+
addAll(attachments);
24+
isMultipart = true;
25+
}
26+
}
27+
}
28+
29+
public SendMediaGroup disableNotification(boolean disableNotification) {
30+
return add("disable_notification", disableNotification);
31+
}
32+
33+
public SendMediaGroup replyToMessageId(int replyToMessageId) {
34+
return add("reply_to_message_id", replyToMessageId);
35+
}
36+
37+
@Override
38+
public boolean isMultipart() {
39+
return isMultipart;
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pengrad.telegrambot.response;
2+
3+
import com.pengrad.telegrambot.model.Message;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* Stas Parshin
9+
* 23 November 2017
10+
*/
11+
public class MessagesResponse extends BaseResponse {
12+
13+
private Message[] result;
14+
15+
MessagesResponse() {
16+
}
17+
18+
public Message[] messages() {
19+
return result;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "MessagesResponse{" +
25+
"result=" + Arrays.toString(result) +
26+
'}';
27+
}
28+
}

library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ public class TelegramBotTest {
4141
Integer memberBot = 215003245;
4242

4343
Path resourcePath = Paths.get("src/test/resources");
44-
String imagefile = resourcePath.resolve("image.png").toString();
4544
File imageFile = resourcePath.resolve("image.jpg").toFile();
45+
byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
4646
File stickerFile = resourcePath.resolve("imageSticker.png").toFile();
4747
String audioFile = resourcePath.resolve("beep.mp3").toString();
4848
String docFile = resourcePath.resolve("doc.txt").toString();
49-
String videoFile = resourcePath.resolve("tabs.mp4").toString();
49+
File videoFile = resourcePath.resolve("tabs.mp4").toFile();
50+
byte[] videoBytes = Files.readAllBytes(videoFile.toPath());
5051
String videoNoteFile = resourcePath.resolve("video_note.mp4").toString();
5152
String certificateFile = resourcePath.resolve("cert.pem").toString();
5253
String someUrl = "http://google.com";
@@ -505,13 +506,12 @@ public void sendPhoto() throws IOException {
505506
MessageTest.checkMessage(message);
506507
PhotoSizeTest.checkPhotos(false, message.photo());
507508

508-
message = bot.execute(new SendPhoto(chatId, new File(imagefile))).message();
509+
message = bot.execute(new SendPhoto(chatId, imageFile)).message();
509510
MessageTest.checkMessage(message);
510511
PhotoSizeTest.checkPhotos(message.photo());
511512

512-
byte[] bytes = Files.readAllBytes(new File(imagefile).toPath());
513513
String caption = "caption";
514-
message = bot.execute(new SendPhoto(channelName, bytes).caption(caption)).message();
514+
message = bot.execute(new SendPhoto(channelName, imageBytes).caption(caption)).message();
515515
MessageTest.checkMessage(message);
516516
assertEquals(caption, message.caption());
517517
PhotoSizeTest.checkPhotos(message.photo());
@@ -523,12 +523,11 @@ public void sendSticker() throws IOException {
523523
MessageTest.checkMessage(message);
524524
StickerTest.check(message.sticker(), true, false);
525525

526-
message = bot.execute(new SendSticker(chatId, new File(imagefile))).message();
526+
message = bot.execute(new SendSticker(chatId, imageFile)).message();
527527
MessageTest.checkMessage(message);
528528
StickerTest.check(message.sticker(), false, true);
529529

530-
byte[] bytes = Files.readAllBytes(new File(imagefile).toPath());
531-
message = bot.execute(new SendSticker(chatId, bytes)).message();
530+
message = bot.execute(new SendSticker(chatId, imageBytes)).message();
532531
MessageTest.checkMessage(message);
533532
StickerTest.check(message.sticker(), false, true);
534533
}
@@ -539,14 +538,13 @@ public void sendVideo() throws IOException {
539538
MessageTest.checkMessage(message);
540539
VideoTest.check(message.video(), false);
541540

542-
message = bot.execute(new SendVideo(chatId, new File(videoFile))).message();
541+
message = bot.execute(new SendVideo(chatId, videoFile)).message();
543542
MessageTest.checkMessage(message);
544543
VideoTest.check(message.video());
545544

546-
byte[] bytes = Files.readAllBytes(new File(videoFile).toPath());
547545
String caption = "my video";
548546
Integer duration = 100;
549-
message = bot.execute(new SendVideo(chatId, bytes).caption(caption).duration(duration).height(1).width(2)).message();
547+
message = bot.execute(new SendVideo(chatId, videoBytes).caption(caption).duration(duration).height(1).width(2)).message();
550548
MessageTest.checkMessage(message);
551549
assertEquals(caption, message.caption());
552550

@@ -982,4 +980,18 @@ public void deleteChatStickerSet() {
982980
assertEquals(400, response.errorCode());
983981
assertEquals("Bad Request: can't set channel sticker set", response.description());
984982
}
983+
984+
@Test
985+
public void sendMediaGroup() {
986+
MessagesResponse response = bot.execute(new SendMediaGroup(chatId,
987+
new InputMediaPhoto(photoFileId),
988+
new InputMediaPhoto(imageFile).caption("some caption"),
989+
new InputMediaPhoto(imageBytes),
990+
new InputMediaVideo(videoFileId),
991+
new InputMediaVideo(videoFile),
992+
new InputMediaVideo(videoBytes).caption("my video").duration(10).width(11).height(12)
993+
));
994+
assertTrue(response.isOk());
995+
assertEquals(6, response.messages().length);
996+
}
985997
}

0 commit comments

Comments
 (0)