From 6f5576245600bcf01b5d624dfcc77d28f419ee13 Mon Sep 17 00:00:00 2001 From: "Leonardo Demartino (Delek)" Date: Fri, 15 Jul 2016 23:04:15 -0300 Subject: [PATCH 1/3] Integers-to-Long The official API says that all msgs ID can be greater than 32bits at any moment. --- src/main/java/de/vivistra/telegrambot/model/User.java | 8 ++++---- .../vivistra/telegrambot/model/message/AudioMessage.java | 4 ++-- .../telegrambot/model/message/ContactMessage.java | 2 +- .../telegrambot/model/message/DocumentMessage.java | 4 ++-- .../vivistra/telegrambot/model/message/ImageMessage.java | 4 ++-- .../telegrambot/model/message/LocationMessage.java | 2 +- .../de/vivistra/telegrambot/model/message/Message.java | 6 +++--- .../telegrambot/model/message/MessageFactory.java | 2 +- .../telegrambot/model/message/StickerMessage.java | 4 ++-- .../vivistra/telegrambot/model/message/TextMessage.java | 2 +- .../telegrambot/model/message/UnhandledMessage.java | 2 +- .../vivistra/telegrambot/model/message/VideoMessage.java | 4 ++-- 12 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/vivistra/telegrambot/model/User.java b/src/main/java/de/vivistra/telegrambot/model/User.java index 90d4700..642486d 100644 --- a/src/main/java/de/vivistra/telegrambot/model/User.java +++ b/src/main/java/de/vivistra/telegrambot/model/User.java @@ -10,7 +10,7 @@ */ public class User { // Unique identifier for this user or bot - private Integer id; + private Long id; // User's or bot's first name private String firstName; // Optional. User's or bot's last name @@ -26,7 +26,7 @@ public class User { * @param lastName * @param userName */ - public User(Integer id, String firstName, String lastName, String userName) { + public User(Long id, String firstName, String lastName, String userName) { Assert.notNull(id); Assert.notEmpty(firstName); @@ -44,7 +44,7 @@ public User(Integer id, String firstName, String lastName, String userName) { */ public static User fromJSON(JSONObject userObject) { // - Integer id = userObject.getInt("id"); + Long id = userObject.getLong("id"); String firstName = userObject.getString("first_name"); // Optional @@ -59,7 +59,7 @@ public static User fromJSON(JSONObject userObject) { * * @return */ - public Integer getId() { + public Long getId() { return id; } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/AudioMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/AudioMessage.java index a07ee2d..105c748 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/AudioMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/AudioMessage.java @@ -15,7 +15,7 @@ public class AudioMessage extends Message { * @param recipient * @param message */ - public AudioMessage(Integer recipient, File message) { + public AudioMessage(Long recipient, File message) { super(recipient, message); } @@ -25,7 +25,7 @@ public AudioMessage(Integer recipient, File message) { * @param recipient * @param message */ - public AudioMessage(Integer recipient, Audio message) { + public AudioMessage(Long recipient, Audio message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/ContactMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/ContactMessage.java index 028f6c2..e2612b5 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/ContactMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/ContactMessage.java @@ -15,7 +15,7 @@ public class ContactMessage extends Message { * @param recipient * @param message */ - public ContactMessage(Integer recipient, Contact contact) { + public ContactMessage(Long recipient, Contact contact) { super(recipient, contact); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/DocumentMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/DocumentMessage.java index a0e8805..1f7fb73 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/DocumentMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/DocumentMessage.java @@ -15,7 +15,7 @@ public class DocumentMessage extends Message { * @param recipient * @param message */ - public DocumentMessage(Integer recipient, File message) { + public DocumentMessage(Long recipient, File message) { super(recipient, message); } @@ -25,7 +25,7 @@ public DocumentMessage(Integer recipient, File message) { * @param recipient * @param message */ - public DocumentMessage(Integer recipient, Document message) { + public DocumentMessage(Long recipient, Document message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/ImageMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/ImageMessage.java index cb4974e..94b85e9 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/ImageMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/ImageMessage.java @@ -15,7 +15,7 @@ public class ImageMessage extends Message { * @param recipient * @param message */ - public ImageMessage(Integer recipient, File message) { + public ImageMessage(Long recipient, File message) { super(recipient, message); } @@ -25,7 +25,7 @@ public ImageMessage(Integer recipient, File message) { * @param recipient * @param message */ - public ImageMessage(Integer recipient, PhotoSize[] message) { + public ImageMessage(Long recipient, PhotoSize[] message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/LocationMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/LocationMessage.java index 1223095..0998814 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/LocationMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/LocationMessage.java @@ -16,7 +16,7 @@ public class LocationMessage extends Message { * @param recipient * @param message */ - public LocationMessage(Integer recipient, Location message) { + public LocationMessage(Long recipient, Location message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/Message.java b/src/main/java/de/vivistra/telegrambot/model/message/Message.java index cf3a89a..b81f3c7 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/Message.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/Message.java @@ -10,7 +10,7 @@ public abstract class Message { private static final Logger LOG = LogManager.getLogger(); // The recipient of the message - protected Integer recipient; + protected Long recipient; // The content of the message protected Object message; // Type of the message @@ -39,7 +39,7 @@ public abstract class Message { * @param recipient * @param message */ - public Message(Integer recipient, Object message) { + public Message(Long recipient, Object message) { this.recipient = recipient; this.message = message; } @@ -69,7 +69,7 @@ public Message(Integer recipient, Object message) { * * @return */ - public Integer getRecipient() { + public Long getRecipient() { return this.recipient; } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/MessageFactory.java b/src/main/java/de/vivistra/telegrambot/model/message/MessageFactory.java index adbc53d..256d268 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/MessageFactory.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/MessageFactory.java @@ -18,7 +18,7 @@ public class MessageFactory { */ public static Message fromJSON(JSONObject messageObject) { - Integer recipient = null; + Long recipient = null; Message message = null; // Text diff --git a/src/main/java/de/vivistra/telegrambot/model/message/StickerMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/StickerMessage.java index bc59c4d..b721626 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/StickerMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/StickerMessage.java @@ -15,7 +15,7 @@ public class StickerMessage extends Message { * @param recipient * @param message */ - public StickerMessage(Integer recipient, File message) { + public StickerMessage(Long recipient, File message) { super(recipient, message); } @@ -25,7 +25,7 @@ public StickerMessage(Integer recipient, File message) { * @param recipient * @param message */ - public StickerMessage(Integer recipient, Sticker message) { + public StickerMessage(Long recipient, Sticker message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/TextMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/TextMessage.java index 96ac83e..2741eae 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/TextMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/TextMessage.java @@ -11,7 +11,7 @@ public class TextMessage extends Message { * @param recipient * @param message */ - public TextMessage(Integer recipient, String message) { + public TextMessage(Long recipient, String message) { super(recipient, message); } diff --git a/src/main/java/de/vivistra/telegrambot/model/message/UnhandledMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/UnhandledMessage.java index 94ad59a..865e5c2 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/UnhandledMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/UnhandledMessage.java @@ -9,7 +9,7 @@ public class UnhandledMessage extends Message { * @param message */ public UnhandledMessage(Object message) { - super(0, message); + super((long) 0, message); } /** diff --git a/src/main/java/de/vivistra/telegrambot/model/message/VideoMessage.java b/src/main/java/de/vivistra/telegrambot/model/message/VideoMessage.java index a34854c..5fde08e 100644 --- a/src/main/java/de/vivistra/telegrambot/model/message/VideoMessage.java +++ b/src/main/java/de/vivistra/telegrambot/model/message/VideoMessage.java @@ -13,7 +13,7 @@ public class VideoMessage extends Message { * @param recipient * @param message */ - public VideoMessage(Integer recipient, File message) { + public VideoMessage(Long recipient, File message) { super(recipient, message); } @@ -23,7 +23,7 @@ public VideoMessage(Integer recipient, File message) { * @param recipient * @param message */ - public VideoMessage(Integer recipient, String message) { + public VideoMessage(Long recipient, String message) { super(recipient, message); } From 58794d76cd2bd47ac8cefdb10f4d85b98f9b244e Mon Sep 17 00:00:00 2001 From: "Leonardo Demartino (Delek)" Date: Sat, 16 Jul 2016 21:56:24 -0300 Subject: [PATCH 2/3] When you "edit" a message there's no "message" in the JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since some months ago Telegram support the edition of messages, this event don’t come with a “message” in the JSON so the API was crashing because of the exception not being handled. TODO: Support for EDIT MESSAGE events. --- .../de/vivistra/telegrambot/client/BotResponse.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/vivistra/telegrambot/client/BotResponse.java b/src/main/java/de/vivistra/telegrambot/client/BotResponse.java index 7f5f48d..e1c4db2 100644 --- a/src/main/java/de/vivistra/telegrambot/client/BotResponse.java +++ b/src/main/java/de/vivistra/telegrambot/client/BotResponse.java @@ -48,9 +48,13 @@ public Message[] getMessages() { for (int i = 0; i < length; i++) { JSONObject result = resultArray.getJSONObject(i); - - messages[i] = MessageFactory.fromJSON(result.getJSONObject("message")); - + try{ + messages[i] = MessageFactory.fromJSON(result.getJSONObject("message")); + } + catch(Exception e){ + messages[i] = MessageFactory.fromJSON(new JSONObject()); + } + if (this.updateID < result.getInt("update_id")) { this.updateID = result.getInt("update_id"); } From b45b461a8fd4dc1aa6359979badf2a19693b86b8 Mon Sep 17 00:00:00 2001 From: "Leonardo Demartino (Delek)" Date: Mon, 18 Jul 2016 11:41:22 -0300 Subject: [PATCH 3/3] Added Long conversion to some test units Fixed the long issue on Test units too. --- .../java/de/vivistra/telegrambot/model/TestUser.java | 10 +++++----- .../telegrambot/model/message/TestMessageFactory.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/de/vivistra/telegrambot/model/TestUser.java b/src/test/java/de/vivistra/telegrambot/model/TestUser.java index 96a8426..7ae8288 100644 --- a/src/test/java/de/vivistra/telegrambot/model/TestUser.java +++ b/src/test/java/de/vivistra/telegrambot/model/TestUser.java @@ -8,14 +8,14 @@ public class TestUser { @Test public void testConstructorAndGetter() { - User peter = new User(1, "Peter", "Mueller", "PeMu"); + User peter = new User(1L, "Peter", "Mueller", "PeMu"); assertTrue(1 == peter.getId()); assertEquals("Peter", peter.getFirstName()); assertEquals("Mueller", peter.getLastName()); assertEquals("PeMu", peter.getUserName()); - User bot = new User(-1, "Bot", null, null); + User bot = new User(-1L, "Bot", null, null); assertTrue(-1 == bot.getId()); assertEquals("Bot", bot.getFirstName()); @@ -25,7 +25,7 @@ public void testConstructorAndGetter() { @Test(expected = IllegalArgumentException.class) public void testConstructorWithIllegalFirstName() { - new User(0, null, null, null); + new User(0L, null, null, null); } @Test(expected = IllegalArgumentException.class) @@ -35,11 +35,11 @@ public void testConstructorWithIllegalId() { @Test public void testToString() { - User peter = new User(1, "Peter", "Mueller", "PeMu"); + User peter = new User(1L, "Peter", "Mueller", "PeMu"); assertEquals("Peter Mueller [PeMu@1]", peter.toString()); - User bot = new User(-1, "Bot", null, null); + User bot = new User(-1L, "Bot", null, null); assertEquals("Bot [-1]", bot.toString()); } diff --git a/src/test/java/de/vivistra/telegrambot/model/message/TestMessageFactory.java b/src/test/java/de/vivistra/telegrambot/model/message/TestMessageFactory.java index ded4077..109dcf2 100644 --- a/src/test/java/de/vivistra/telegrambot/model/message/TestMessageFactory.java +++ b/src/test/java/de/vivistra/telegrambot/model/message/TestMessageFactory.java @@ -47,7 +47,7 @@ public void testFromJSON() { assertNull(message.getGroupChat()); - User sender = new User(7872355, "Marcel", "", "Marcn"); + User sender = new User(7872355L, "Marcel", "", "Marcn"); assertEquals(sender, message.getSender()); }