Skip to content

Commit c7775d8

Browse files
committed
Added width, height, duration to Animation object. Added the field animation to the Message object. Added the method sendAnimation
1 parent e33856f commit c7775d8

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

library/src/main/java/com/pengrad/telegrambot/model/Animation.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class Animation implements Serializable {
1010
private final static long serialVersionUID = 0L;
1111

1212
private String file_id;
13+
private Integer width;
14+
private Integer height;
15+
private Integer duration;
1316
private PhotoSize thumb;
1417
private String file_name;
1518
private String mime_type;
@@ -19,6 +22,18 @@ public String fileId() {
1922
return file_id;
2023
}
2124

25+
public Integer width() {
26+
return width;
27+
}
28+
29+
public Integer height() {
30+
return height;
31+
}
32+
33+
public Integer duration() {
34+
return duration;
35+
}
36+
2237
public PhotoSize thumb() {
2338
return thumb;
2439
}
@@ -43,6 +58,9 @@ public boolean equals(Object o) {
4358
Animation animation = (Animation) o;
4459

4560
if (file_id != null ? !file_id.equals(animation.file_id) : animation.file_id != null) return false;
61+
if (width != null ? !width.equals(animation.width) : animation.width != null) return false;
62+
if (height != null ? !height.equals(animation.height) : animation.height != null) return false;
63+
if (duration != null ? !duration.equals(animation.duration) : animation.duration != null) return false;
4664
if (thumb != null ? !thumb.equals(animation.thumb) : animation.thumb != null) return false;
4765
if (file_name != null ? !file_name.equals(animation.file_name) : animation.file_name != null) return false;
4866
if (mime_type != null ? !mime_type.equals(animation.mime_type) : animation.mime_type != null) return false;
@@ -58,6 +76,9 @@ public int hashCode() {
5876
public String toString() {
5977
return "Animation{" +
6078
"file_id='" + file_id + '\'' +
79+
", width=" + width +
80+
", height=" + height +
81+
", duration=" + duration +
6182
", thumb=" + thumb +
6283
", file_name='" + file_name + '\'' +
6384
", mime_type='" + mime_type + '\'' +

library/src/main/java/com/pengrad/telegrambot/model/Message.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class Message implements Serializable {
2828
private MessageEntity[] caption_entities;
2929
private Audio audio;
3030
private Document document;
31+
private Animation animation;
3132
private Game game;
3233
private PhotoSize[] photo;
3334
private Sticker sticker;
@@ -126,6 +127,10 @@ public Document document() {
126127
return document;
127128
}
128129

130+
public Animation animation() {
131+
return animation;
132+
}
133+
129134
public Game game() {
130135
return game;
131136
}
@@ -263,6 +268,7 @@ public boolean equals(Object o) {
263268
if (!Arrays.equals(caption_entities, message.caption_entities)) return false;
264269
if (audio != null ? !audio.equals(message.audio) : message.audio != null) return false;
265270
if (document != null ? !document.equals(message.document) : message.document != null) return false;
271+
if (animation != null ? !animation.equals(message.animation) : message.animation != null) return false;
266272
if (game != null ? !game.equals(message.game) : message.game != null) return false;
267273
// Probably incorrect - comparing Object[] arrays with Arrays.equals
268274
if (!Arrays.equals(photo, message.photo)) return false;
@@ -330,6 +336,7 @@ public String toString() {
330336
", caption_entities=" + Arrays.toString(caption_entities) +
331337
", audio=" + audio +
332338
", document=" + document +
339+
", animation=" + animation +
333340
", game=" + game +
334341
", photo=" + Arrays.toString(photo) +
335342
", sticker=" + sticker +
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
import com.pengrad.telegrambot.model.request.ParseMode;
4+
5+
import java.io.File;
6+
7+
/**
8+
* Stas Parshin
9+
* 28 July 2018
10+
*/
11+
public class SendAnimation extends AbstractMultipartRequest<SendAnimation> {
12+
13+
public SendAnimation(Object chatId, String animation) {
14+
super(chatId, animation);
15+
}
16+
17+
public SendAnimation(Object chatId, File animation) {
18+
super(chatId, animation);
19+
}
20+
21+
public SendAnimation(Object chatId, byte[] animation) {
22+
super(chatId, animation);
23+
}
24+
25+
public SendAnimation duration(int duration) {
26+
return add("duration", duration);
27+
}
28+
29+
public SendAnimation width(int width) {
30+
return add("width", width);
31+
}
32+
33+
public SendAnimation height(int height) {
34+
return add("height", height);
35+
}
36+
37+
public SendAnimation thumb(byte[] thumb) {
38+
return super.thumb(thumb);
39+
}
40+
41+
public SendAnimation thumb(File thumb) {
42+
return super.thumb(thumb);
43+
}
44+
45+
public SendAnimation caption(String caption) {
46+
return add("caption", caption);
47+
}
48+
49+
public SendAnimation parseMode(ParseMode parseMode) {
50+
return add("parse_mode", parseMode.name());
51+
}
52+
53+
@Override
54+
protected String getFileParamName() {
55+
return "animation";
56+
}
57+
58+
@Override
59+
public String getDefaultFileName() {
60+
return ContentTypes.GIF_FILE_NAME;
61+
}
62+
63+
@Override
64+
public String getContentType() {
65+
return ContentTypes.GIF_MIME_TYPE;
66+
}
67+
}

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import com.pengrad.telegrambot.model.request.*;
55
import com.pengrad.telegrambot.request.*;
66
import com.pengrad.telegrambot.response.*;
7-
87
import okhttp3.OkHttpClient;
98
import okhttp3.Request;
109
import okhttp3.Response;
11-
1210
import org.junit.Test;
1311

1412
import java.io.File;
@@ -1133,4 +1131,34 @@ public void editMessageMedia() {
11331131
assertEquals(performer, audio.performer());
11341132
assertEquals(title, audio.title());
11351133
}
1134+
1135+
@Test
1136+
public void sendAnimation() {
1137+
Integer width = 340, height = 240;
1138+
String caption = "gif *file*", captionCheck = "gif file";
1139+
SendResponse response = bot.execute(new SendAnimation(chatId, gifFile)
1140+
.duration(222).width(width).height(height).thumb(thumbFile)
1141+
.caption(caption).parseMode(ParseMode.Markdown));
1142+
assertTrue(response.isOk());
1143+
Animation animation = response.message().animation();
1144+
assertEquals((Integer) 1, animation.duration());
1145+
assertEquals(width, animation.width());
1146+
assertEquals(height, animation.height());
1147+
assertEquals(thumbSize, animation.thumb().fileSize());
1148+
assertEquals(captionCheck, response.message().caption());
1149+
assertEquals(MessageEntity.Type.bold, response.message().captionEntities()[0].type());
1150+
1151+
response = bot.execute(new SendAnimation(chatId, gifBytes).thumb(thumbBytes));
1152+
animation = response.message().animation();
1153+
assertEquals((Integer) 1, animation.duration());
1154+
assertEquals((Integer) 160, animation.width());
1155+
assertEquals((Integer) 160, animation.height());
1156+
assertEquals(thumbSize, animation.thumb().fileSize());
1157+
1158+
response = bot.execute(new SendAnimation(chatId, gifFileId));
1159+
animation = response.message().animation();
1160+
assertEquals((Integer) 3, animation.duration());
1161+
assertEquals((Integer) 128, animation.width());
1162+
assertEquals((Integer) 128, animation.height());
1163+
}
11361164
}

0 commit comments

Comments
 (0)