Skip to content

Commit cac5fd4

Browse files
committed
Added support for native polls: added the object Poll, the methods sendPoll and stopPoll and the field poll in the Message and Update objects
1 parent 2dd866f commit cac5fd4

File tree

9 files changed

+222
-2
lines changed

9 files changed

+222
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class Message implements Serializable {
4141
private Contact contact;
4242
private Location location;
4343
private Venue venue;
44+
private Poll poll;
4445
private User new_chat_member;
4546
private User[] new_chat_members;
4647
private User left_chat_member;
@@ -174,6 +175,10 @@ public Venue venue() {
174175
return venue;
175176
}
176177

178+
public Poll poll() {
179+
return poll;
180+
}
181+
177182
/**
178183
* @deprecated Replaced with new_chat_members
179184
*/
@@ -287,6 +292,7 @@ public boolean equals(Object o) {
287292
if (contact != null ? !contact.equals(message.contact) : message.contact != null) return false;
288293
if (location != null ? !location.equals(message.location) : message.location != null) return false;
289294
if (venue != null ? !venue.equals(message.venue) : message.venue != null) return false;
295+
if (poll != null ? !poll.equals(message.poll) : message.poll != null) return false;
290296
if (new_chat_member != null ? !new_chat_member.equals(message.new_chat_member) : message.new_chat_member != null)
291297
return false;
292298
// Probably incorrect - comparing Object[] arrays with Arrays.equals
@@ -356,6 +362,7 @@ public String toString() {
356362
", contact=" + contact +
357363
", location=" + location +
358364
", venue=" + venue +
365+
", poll=" + poll +
359366
", new_chat_member=" + new_chat_member +
360367
", new_chat_members=" + Arrays.toString(new_chat_members) +
361368
", left_chat_member=" + left_chat_member +
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.pengrad.telegrambot.model;
2+
3+
import java.io.Serializable;
4+
import java.util.Arrays;
5+
6+
/**
7+
* Stas Parshin
8+
* 17 April 2019
9+
*/
10+
public class Poll implements Serializable {
11+
private final static long serialVersionUID = 0L;
12+
13+
private String id;
14+
private String question;
15+
private PollOption[] options;
16+
private Boolean is_closed;
17+
18+
public String id() {
19+
return id;
20+
}
21+
22+
public String question() {
23+
return question;
24+
}
25+
26+
public PollOption[] options() {
27+
return options;
28+
}
29+
30+
public Boolean isClosed() {
31+
return is_closed;
32+
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (this == o) return true;
37+
if (o == null || getClass() != o.getClass()) return false;
38+
39+
Poll poll = (Poll) o;
40+
41+
if (id != null ? !id.equals(poll.id) : poll.id != null) return false;
42+
if (question != null ? !question.equals(poll.question) : poll.question != null) return false;
43+
// Probably incorrect - comparing Object[] arrays with Arrays.equals
44+
if (!Arrays.equals(options, poll.options)) return false;
45+
return is_closed != null ? is_closed.equals(poll.is_closed) : poll.is_closed == null;
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return id != null ? id.hashCode() : 0;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Poll{" +
56+
"id='" + id + '\'' +
57+
", question='" + question + '\'' +
58+
", options=" + Arrays.toString(options) +
59+
", is_closed=" + is_closed +
60+
'}';
61+
}
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.pengrad.telegrambot.model;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Stas Parshin
7+
* 17 April 2019
8+
*/
9+
public class PollOption implements Serializable {
10+
private final static long serialVersionUID = 0L;
11+
12+
private String text;
13+
private Integer voter_count;
14+
15+
public String text() {
16+
return text;
17+
}
18+
19+
public Integer voterCount() {
20+
return voter_count;
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (o == null || getClass() != o.getClass()) return false;
27+
28+
PollOption that = (PollOption) o;
29+
30+
if (text != null ? !text.equals(that.text) : that.text != null) return false;
31+
return voter_count != null ? voter_count.equals(that.voter_count) : that.voter_count == null;
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
int result = text != null ? text.hashCode() : 0;
37+
result = 31 * result + (voter_count != null ? voter_count.hashCode() : 0);
38+
return result;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "PollOption{" +
44+
"text='" + text + '\'' +
45+
", voter_count=" + voter_count +
46+
'}';
47+
}
48+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Update implements Serializable {
1919
private CallbackQuery callback_query;
2020
private ShippingQuery shipping_query;
2121
private PreCheckoutQuery pre_checkout_query;
22+
private Poll poll;
2223

2324
public Integer updateId() {
2425
return update_id;
@@ -60,6 +61,10 @@ public PreCheckoutQuery preCheckoutQuery() {
6061
return pre_checkout_query;
6162
}
6263

64+
public Poll poll() {
65+
return poll;
66+
}
67+
6368
@Override
6469
public boolean equals(Object o) {
6570
if (this == o) return true;
@@ -81,7 +86,9 @@ public boolean equals(Object o) {
8186
return false;
8287
if (shipping_query != null ? !shipping_query.equals(update.shipping_query) : update.shipping_query != null)
8388
return false;
84-
return pre_checkout_query != null ? pre_checkout_query.equals(update.pre_checkout_query) : update.pre_checkout_query == null;
89+
if (pre_checkout_query != null ? !pre_checkout_query.equals(update.pre_checkout_query) : update.pre_checkout_query != null)
90+
return false;
91+
return poll != null ? poll.equals(update.poll) : update.poll == null;
8592
}
8693

8794
@Override
@@ -102,6 +109,7 @@ public String toString() {
102109
", callback_query=" + callback_query +
103110
", shipping_query=" + shipping_query +
104111
", pre_checkout_query=" + pre_checkout_query +
112+
", poll=" + poll +
105113
'}';
106114
}
107115
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
/**
4+
* Stas Parshin
5+
* 17 April 2019
6+
*/
7+
public class SendPoll extends AbstractSendRequest<SendPoll> {
8+
9+
public SendPoll(Object chatId, String question, String... options) {
10+
super(chatId);
11+
add("question", question);
12+
add("options", serialize(options));
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
import com.pengrad.telegrambot.model.request.Keyboard;
4+
import com.pengrad.telegrambot.response.PollResponse;
5+
6+
/**
7+
* Stas Parshin
8+
* 17 April 2019
9+
*/
10+
public class StopPoll extends BaseRequest<StopPoll, PollResponse> {
11+
12+
public StopPoll(Object chatId, int messageId) {
13+
super(PollResponse.class);
14+
add("chat_id", chatId).add("message_id", messageId);
15+
}
16+
17+
public StopPoll replyMarkup(Keyboard replyMarkup) {
18+
return add("reply_markup", replyMarkup);
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.pengrad.telegrambot.response;
2+
3+
import com.pengrad.telegrambot.model.Poll;
4+
5+
/**
6+
* Stas Parshin
7+
* 17 April 2019
8+
*/
9+
public class PollResponse extends BaseResponse {
10+
11+
private Poll result;
12+
13+
public Poll poll() {
14+
return result;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "PollResponse{" +
20+
"result=" + result +
21+
'}';
22+
}
23+
}

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void getMe() {
107107
@Test
108108
public void getUpdates() {
109109
GetUpdates getUpdates = new GetUpdates()
110-
.offset(864855364)
110+
.offset(870644071)
111111
.allowedUpdates("")
112112
.timeout(0)
113113
.limit(100);
@@ -1224,4 +1224,41 @@ public void decryptPassport() throws Exception {
12241224
}
12251225
}
12261226
}
1227+
1228+
@Test
1229+
public void sendPoll() {
1230+
String question = "Question ?";
1231+
String[] answers = {"Answer 1", "Answer 2"};
1232+
SendResponse sendResponse = bot.execute(new SendPoll(groupId, question, answers));
1233+
Poll poll = sendResponse.message().poll();
1234+
assertFalse(poll.isClosed());
1235+
assertEquals(question, poll.question());
1236+
assertEquals(answers.length, poll.options().length);
1237+
for (int i = 0; i < answers.length; i++) {
1238+
PollOption option = poll.options()[i];
1239+
assertEquals(answers[i], option.text());
1240+
assertEquals(Integer.valueOf(0), option.voterCount());
1241+
}
1242+
}
1243+
1244+
@Test
1245+
public void stopPoll() throws InterruptedException {
1246+
String question = "Question ?";
1247+
String[] answers = {"Answer 1", "Answer 2"};
1248+
SendResponse sendResponse = bot.execute(new SendPoll(groupId, question, answers));
1249+
Integer messageId = sendResponse.message().messageId();
1250+
1251+
Thread.sleep(1000);
1252+
1253+
PollResponse response = bot.execute(new StopPoll(groupId, messageId));
1254+
Poll poll = response.poll();
1255+
assertTrue(poll.isClosed());
1256+
assertEquals(question, poll.question());
1257+
assertEquals(answers.length, poll.options().length);
1258+
for (int i = 0; i < answers.length; i++) {
1259+
PollOption option = poll.options()[i];
1260+
assertEquals(answers[i], option.text());
1261+
assertEquals(Integer.valueOf(0), option.voterCount());
1262+
}
1263+
}
12271264
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static void check(List<Update> updates) {
2525
if (update.callbackQuery() != null) assertNotNull(update.callbackQuery().id());
2626
if (update.shippingQuery() != null) assertNotNull(update.shippingQuery().id());
2727
if (update.preCheckoutQuery() != null) assertNotNull(update.preCheckoutQuery().id());
28+
if (update.poll() != null) assertNotNull(update.poll().id());
2829
}
2930
}
3031

0 commit comments

Comments
 (0)