From 0b79a838b00e9e09efa95ee0516ef0df1ce6c3cc Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Mon, 22 May 2017 22:55:15 +0700 Subject: [PATCH 1/8] Replaced the field new_chat_member in Message with new_chat_members --- .../pengrad/telegrambot/model/Message.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Message.java b/library/src/main/java/com/pengrad/telegrambot/model/Message.java index 41e7bde5..814533f2 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Message.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Message.java @@ -34,6 +34,7 @@ public class Message implements Serializable { private Location location; private Venue venue; private User new_chat_member; + private User[] new_chat_members; private User left_chat_member; private String new_chat_title; private PhotoSize[] new_chat_photo; @@ -137,10 +138,18 @@ public Venue venue() { return venue; } + /** + * @deprecated Replaced with new_chat_members + */ + @Deprecated public User newChatMember() { return new_chat_member; } + public User[] newChatMembers() { + return new_chat_members; + } + public User leftChatMember() { return left_chat_member; } @@ -192,14 +201,12 @@ public boolean equals(Object o) { if (from != null ? !from.equals(message.from) : message.from != null) return false; if (date != null ? !date.equals(message.date) : message.date != null) return false; if (chat != null ? !chat.equals(message.chat) : message.chat != null) return false; - if (forward_from != null ? !forward_from.equals(message.forward_from) : message.forward_from != null) - return false; + if (forward_from != null ? !forward_from.equals(message.forward_from) : message.forward_from != null) return false; if (forward_from_chat != null ? !forward_from_chat.equals(message.forward_from_chat) : message.forward_from_chat != null) return false; if (forward_from_message_id != null ? !forward_from_message_id.equals(message.forward_from_message_id) : message.forward_from_message_id != null) return false; - if (forward_date != null ? !forward_date.equals(message.forward_date) : message.forward_date != null) - return false; + if (forward_date != null ? !forward_date.equals(message.forward_date) : message.forward_date != null) return false; if (reply_to_message != null ? !reply_to_message.equals(message.reply_to_message) : message.reply_to_message != null) return false; if (edit_date != null ? !edit_date.equals(message.edit_date) : message.edit_date != null) return false; @@ -218,8 +225,8 @@ public boolean equals(Object o) { if (contact != null ? !contact.equals(message.contact) : message.contact != null) return false; if (location != null ? !location.equals(message.location) : message.location != null) return false; if (venue != null ? !venue.equals(message.venue) : message.venue != null) return false; - if (new_chat_member != null ? !new_chat_member.equals(message.new_chat_member) : message.new_chat_member != null) - return false; + // Probably incorrect - comparing Object[] arrays with Arrays.equals + if (!Arrays.equals(new_chat_members, message.new_chat_members)) return false; if (left_chat_member != null ? !left_chat_member.equals(message.left_chat_member) : message.left_chat_member != null) return false; if (new_chat_title != null ? !new_chat_title.equals(message.new_chat_title) : message.new_chat_title != null) @@ -273,7 +280,7 @@ public String toString() { ", contact=" + contact + ", location=" + location + ", venue=" + venue + - ", new_chat_member=" + new_chat_member + + ", new_chat_members=" + Arrays.toString(new_chat_members) + ", left_chat_member=" + left_chat_member + ", new_chat_title='" + new_chat_title + '\'' + ", new_chat_photo=" + Arrays.toString(new_chat_photo) + From 091b0a4a041a9d4e067e8fca025d0df3e3e42536 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Mon, 22 May 2017 23:06:18 +0700 Subject: [PATCH 2/8] New fields gif_duration in InlineQueryResultGif and mpeg4_duration in InlineQueryResultMpeg4Gif --- .../telegrambot/model/request/InlineQueryResultGif.java | 6 ++++++ .../model/request/InlineQueryResultMpeg4Gif.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineQueryResultGif.java b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineQueryResultGif.java index fc47994a..a0116a91 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineQueryResultGif.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineQueryResultGif.java @@ -14,6 +14,7 @@ public class InlineQueryResultGif extends InlineQueryResult Date: Mon, 22 May 2017 23:39:33 +0700 Subject: [PATCH 3/8] New method deleteMessage --- .../telegrambot/request/DeleteMessage.java | 15 +++++++++++++++ .../com/pengrad/telegrambot/TelegramBotTest.java | 7 +++++++ 2 files changed, 22 insertions(+) create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/DeleteMessage.java diff --git a/library/src/main/java/com/pengrad/telegrambot/request/DeleteMessage.java b/library/src/main/java/com/pengrad/telegrambot/request/DeleteMessage.java new file mode 100644 index 00000000..db33176d --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/DeleteMessage.java @@ -0,0 +1,15 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 22 May 2017 + */ +public class DeleteMessage extends BaseRequest { + + public DeleteMessage(Object chatId, int messageId) { + super(BaseResponse.class); + add("chat_id", chatId).add("message_id", messageId); + } +} diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 61fa4db4..8dde5bcf 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -274,4 +274,11 @@ public void sendGame() { SendResponse response = bot.execute(new SendGame(chatId, "pengrad_test_game")); MessageTest.checkGameMessage(response.message()); } + + @Test + public void deleteMessage() { + Message message = bot.execute(new SendMessage(chatId, "message for delete")).message(); + BaseResponse response = bot.execute(new DeleteMessage(chatId, message.messageId())); + assertTrue(response.isOk()); + } } From 7a475d03bb55c70fcacbaa89d5d03486a002f085 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Tue, 23 May 2017 23:38:37 +0700 Subject: [PATCH 4/8] User object now may have a language_code field --- .../main/java/com/pengrad/telegrambot/model/User.java | 9 +++++++-- .../src/test/java/com/pengrad/telegrambot/UserTest.java | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/User.java b/library/src/main/java/com/pengrad/telegrambot/model/User.java index 4c61af2f..c3630b0b 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/User.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/User.java @@ -13,6 +13,7 @@ public class User implements Serializable { private String first_name; private String last_name; private String username; + private String language_code; public Integer id() { return id; @@ -30,6 +31,10 @@ public String username() { return username; } + public String languageCode() { + return language_code; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -41,8 +46,7 @@ public boolean equals(Object o) { if (first_name != null ? !first_name.equals(user.first_name) : user.first_name != null) return false; if (last_name != null ? !last_name.equals(user.last_name) : user.last_name != null) return false; if (username != null ? !username.equals(user.username) : user.username != null) return false; - - return true; + return language_code != null ? language_code.equals(user.language_code) : user.language_code == null; } @Override @@ -57,6 +61,7 @@ public String toString() { ", first_name='" + first_name + '\'' + ", last_name='" + last_name + '\'' + ", username='" + username + '\'' + + ", language_code='" + language_code + '\'' + '}'; } } \ No newline at end of file diff --git a/library/src/test/java/com/pengrad/telegrambot/UserTest.java b/library/src/test/java/com/pengrad/telegrambot/UserTest.java index d5d61928..785815ab 100644 --- a/library/src/test/java/com/pengrad/telegrambot/UserTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/UserTest.java @@ -15,6 +15,7 @@ public static void checkUser(User user) { assertNotNull(user.id()); assertNotNull(user.firstName()); assertNotNull(user.username()); + assertNotNull(user.languageCode()); } } From 3a0d9c8e91976f679d4a446211c8f176673a27a6 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Wed, 24 May 2017 01:04:27 +0700 Subject: [PATCH 5/8] Added the sendVideoNote method, the new field video_note to Message, the fields record_video_note or upload_video_note to sendChatAction --- .../pengrad/telegrambot/model/Message.java | 27 +++++--- .../pengrad/telegrambot/model/VideoNote.java | 67 +++++++++++++++++++ .../telegrambot/model/request/ChatAction.java | 3 +- .../telegrambot/request/SendChatAction.java | 6 ++ .../telegrambot/request/SendVideoNote.java | 45 +++++++++++++ .../pengrad/telegrambot/TelegramBotTest.java | 22 +++++- .../pengrad/telegrambot/VideoNoteCheck.java | 21 ++++++ 7 files changed, 180 insertions(+), 11 deletions(-) create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/VideoNote.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/SendVideoNote.java create mode 100644 library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Message.java b/library/src/main/java/com/pengrad/telegrambot/model/Message.java index 814533f2..4471a866 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Message.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Message.java @@ -29,12 +29,13 @@ public class Message implements Serializable { private Sticker sticker; private Video video; private Voice voice; + private VideoNote video_note; + private User[] new_chat_members; private String caption; private Contact contact; private Location location; private Venue venue; private User new_chat_member; - private User[] new_chat_members; private User left_chat_member; private String new_chat_title; private PhotoSize[] new_chat_photo; @@ -122,6 +123,14 @@ public Voice voice() { return voice; } + public VideoNote videoNote() { + return video_note; + } + + public User[] newChatMembers() { + return new_chat_members; + } + public String caption() { return caption; } @@ -146,10 +155,6 @@ public User newChatMember() { return new_chat_member; } - public User[] newChatMembers() { - return new_chat_members; - } - public User leftChatMember() { return left_chat_member; } @@ -221,12 +226,15 @@ public boolean equals(Object o) { if (sticker != null ? !sticker.equals(message.sticker) : message.sticker != null) return false; if (video != null ? !video.equals(message.video) : message.video != null) return false; if (voice != null ? !voice.equals(message.voice) : message.voice != null) return false; + if (video_note != null ? !video_note.equals(message.video_note) : message.video_note != null) return false; + // Probably incorrect - comparing Object[] arrays with Arrays.equals + if (!Arrays.equals(new_chat_members, message.new_chat_members)) return false; if (caption != null ? !caption.equals(message.caption) : message.caption != null) return false; if (contact != null ? !contact.equals(message.contact) : message.contact != null) return false; if (location != null ? !location.equals(message.location) : message.location != null) return false; if (venue != null ? !venue.equals(message.venue) : message.venue != null) return false; - // Probably incorrect - comparing Object[] arrays with Arrays.equals - if (!Arrays.equals(new_chat_members, message.new_chat_members)) return false; + if (new_chat_member != null ? !new_chat_member.equals(message.new_chat_member) : message.new_chat_member != null) + return false; if (left_chat_member != null ? !left_chat_member.equals(message.left_chat_member) : message.left_chat_member != null) return false; if (new_chat_title != null ? !new_chat_title.equals(message.new_chat_title) : message.new_chat_title != null) @@ -246,7 +254,6 @@ public boolean equals(Object o) { if (migrate_from_chat_id != null ? !migrate_from_chat_id.equals(message.migrate_from_chat_id) : message.migrate_from_chat_id != null) return false; return pinned_message != null ? pinned_message.equals(message.pinned_message) : message.pinned_message == null; - } @Override @@ -276,11 +283,13 @@ public String toString() { ", sticker=" + sticker + ", video=" + video + ", voice=" + voice + + ", video_note=" + video_note + + ", new_chat_members=" + Arrays.toString(new_chat_members) + ", caption='" + caption + '\'' + ", contact=" + contact + ", location=" + location + ", venue=" + venue + - ", new_chat_members=" + Arrays.toString(new_chat_members) + + ", new_chat_member=" + new_chat_member + ", left_chat_member=" + left_chat_member + ", new_chat_title='" + new_chat_title + '\'' + ", new_chat_photo=" + Arrays.toString(new_chat_photo) + diff --git a/library/src/main/java/com/pengrad/telegrambot/model/VideoNote.java b/library/src/main/java/com/pengrad/telegrambot/model/VideoNote.java new file mode 100644 index 00000000..9a486375 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/VideoNote.java @@ -0,0 +1,67 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 23 May 2017 + */ +public class VideoNote implements Serializable { + private final static long serialVersionUID = 0L; + + private String file_id; + private Integer length; + private Integer duration; + private PhotoSize thumb; + private Integer file_size; + + public String fileId() { + return file_id; + } + + public Integer length() { + return length; + } + + public Integer duration() { + return duration; + } + + public PhotoSize thumb() { + return thumb; + } + + public Integer fileSize() { + return file_size; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + VideoNote videoNote = (VideoNote) o; + + if (file_id != null ? !file_id.equals(videoNote.file_id) : videoNote.file_id != null) return false; + if (length != null ? !length.equals(videoNote.length) : videoNote.length != null) return false; + if (duration != null ? !duration.equals(videoNote.duration) : videoNote.duration != null) return false; + if (thumb != null ? !thumb.equals(videoNote.thumb) : videoNote.thumb != null) return false; + return file_size != null ? file_size.equals(videoNote.file_size) : videoNote.file_size == null; + } + + @Override + public int hashCode() { + return file_id != null ? file_id.hashCode() : 0; + } + + @Override + public String toString() { + return "VideoNote{" + + "file_id='" + file_id + '\'' + + ", length=" + length + + ", duration=" + duration + + ", thumb=" + thumb + + ", file_size=" + file_size + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/ChatAction.java b/library/src/main/java/com/pengrad/telegrambot/model/request/ChatAction.java index ca2776d5..af6cc89a 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/request/ChatAction.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/ChatAction.java @@ -5,5 +5,6 @@ * 10/21/15. */ public enum ChatAction { - typing, upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location + typing, upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location, + record_video_note, upload_video_note } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendChatAction.java b/library/src/main/java/com/pengrad/telegrambot/request/SendChatAction.java index 20143814..e910e106 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/SendChatAction.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendChatAction.java @@ -1,5 +1,6 @@ package com.pengrad.telegrambot.request; +import com.pengrad.telegrambot.model.request.ChatAction; import com.pengrad.telegrambot.response.BaseResponse; /** @@ -12,4 +13,9 @@ public SendChatAction(Object chatId, String action) { super(BaseResponse.class); add("chat_id", chatId).add("action", action); } + + public SendChatAction(Object chatId, ChatAction action) { + super(BaseResponse.class); + add("chat_id", chatId).add("action", action.name()); + } } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendVideoNote.java b/library/src/main/java/com/pengrad/telegrambot/request/SendVideoNote.java new file mode 100644 index 00000000..f618ec27 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendVideoNote.java @@ -0,0 +1,45 @@ +package com.pengrad.telegrambot.request; + +import java.io.File; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class SendVideoNote extends AbstractMultipartRequest { + + public SendVideoNote(Object chatId, String videoNote) { + super(chatId, videoNote); + } + + public SendVideoNote(Object chatId, File videoNote) { + super(chatId, videoNote); + } + + public SendVideoNote(Object chatId, byte[] videoNote) { + super(chatId, videoNote); + } + + public SendVideoNote duration(int duration) { + return add("duration", duration); + } + + public SendVideoNote length(int length) { + return add("length", length); + } + + @Override + protected String getFileParamName() { + return "video_note"; + } + + @Override + public String getContentType() { + return ContentTypes.VIDEO_MIME_TYPE; + } + + @Override + protected String getDefaultFileName() { + return ContentTypes.VIDEO_FILE_NAME; + } +} diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 8dde5bcf..3b611993 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -64,7 +64,7 @@ public void getMe() { @Test public void getUpdates() { - GetUpdatesResponse response = bot.execute(new GetUpdates().offset(279824711).allowedUpdates("callback_query")); + GetUpdatesResponse response = bot.execute(new GetUpdates()); System.out.println(response); } @@ -281,4 +281,24 @@ public void deleteMessage() { BaseResponse response = bot.execute(new DeleteMessage(chatId, message.messageId())); assertTrue(response.isOk()); } + + @Test + public void sendChatAction() { + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.typing)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_photo)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_video)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_video)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_audio)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_audio)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_document)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.find_location)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.record_video_note)).isOk()); + assertTrue(bot.execute(new SendChatAction(chatId, ChatAction.upload_video_note)).isOk()); + } + + @Test + public void sendVideoNote() { + SendResponse response = bot.execute(new SendVideoNote(chatId, "DQADAgADmQADYgwpSbum1JrxPsbmAg").length(240)); + VideoNoteCheck.check(response.message().videoNote()); + } } diff --git a/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java b/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java new file mode 100644 index 00000000..810cc20c --- /dev/null +++ b/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java @@ -0,0 +1,21 @@ +package com.pengrad.telegrambot; + +import com.pengrad.telegrambot.model.VideoNote; + +import static org.junit.Assert.assertNotNull; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class VideoNoteCheck { + + public static void check(VideoNote videoNote) { + assertNotNull(videoNote.fileId()); + assertNotNull(videoNote.length()); + assertNotNull(videoNote.duration()); + assertNotNull(videoNote.fileSize()); + PhotoSizeTest.checkPhotos(videoNote.thumb()); + } + +} From 884670e4791d33cd42b2d5fd51f3b04e755521a4 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Wed, 24 May 2017 01:29:37 +0700 Subject: [PATCH 6/8] Fix test User check --- .../java/com/pengrad/telegrambot/ChatMemberTest.java | 2 +- .../src/test/java/com/pengrad/telegrambot/UserTest.java | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/library/src/test/java/com/pengrad/telegrambot/ChatMemberTest.java b/library/src/test/java/com/pengrad/telegrambot/ChatMemberTest.java index 3b2dd8d4..7256c8a1 100644 --- a/library/src/test/java/com/pengrad/telegrambot/ChatMemberTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/ChatMemberTest.java @@ -13,7 +13,7 @@ public class ChatMemberTest { public static void check(ChatMember chatMember) { assertNotNull(chatMember.user()); assertNotNull(chatMember.status()); - UserTest.checkUser(chatMember.user()); + UserTest.checkUser(chatMember.user(), chatMember.status() == ChatMember.Status.creator); } diff --git a/library/src/test/java/com/pengrad/telegrambot/UserTest.java b/library/src/test/java/com/pengrad/telegrambot/UserTest.java index 785815ab..491858d6 100644 --- a/library/src/test/java/com/pengrad/telegrambot/UserTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/UserTest.java @@ -12,10 +12,17 @@ public class UserTest { public static void checkUser(User user) { + checkUser(user, false); + } + + public static void checkUser(User user, boolean full) { assertNotNull(user.id()); assertNotNull(user.firstName()); assertNotNull(user.username()); - assertNotNull(user.languageCode()); + if (full) { + assertNotNull(user.lastName()); + assertNotNull(user.languageCode()); + } } } From bffe4ba2d2e3c8bc8d84a7524bc2d538e38bbdfd Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Fri, 26 May 2017 01:37:47 +0700 Subject: [PATCH 7/8] Added new kinds of updates, shipping_query and pre_checkout_query, and new types of message content, invoice and successful_payment. Added new methods for payments: sendInvoice, answerShippingQuery, and answerPreCheckoutQuery. Added a new type of button, the pay button to InlineKeyboardButton. --- .../pengrad/telegrambot/model/Invoice.java | 71 ++++++++++++++ .../pengrad/telegrambot/model/Message.java | 17 +++- .../pengrad/telegrambot/model/OrderInfo.java | 63 +++++++++++++ .../telegrambot/model/PreCheckoutQuery.java | 83 +++++++++++++++++ .../telegrambot/model/ShippingAddress.java | 76 +++++++++++++++ .../telegrambot/model/ShippingQuery.java | 61 ++++++++++++ .../telegrambot/model/SuccessfulPayment.java | 92 +++++++++++++++++++ .../com/pengrad/telegrambot/model/Update.java | 25 +++-- .../model/request/InlineKeyboardButton.java | 8 ++ .../model/request/LabeledPrice.java | 19 ++++ .../model/request/ShippingOption.java | 20 ++++ .../request/AnswerPreCheckoutQuery.java | 20 ++++ .../request/AnswerShippingQuery.java | 24 +++++ .../telegrambot/request/SendInvoice.java | 57 ++++++++++++ .../com/pengrad/telegrambot/InvoiceCheck.java | 21 +++++ .../pengrad/telegrambot/TelegramBotTest.java | 82 ++++++++++++++++- 16 files changed, 731 insertions(+), 8 deletions(-) create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/Invoice.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/OrderInfo.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/PreCheckoutQuery.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/ShippingAddress.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/ShippingQuery.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/request/LabeledPrice.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/request/ShippingOption.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/AnswerPreCheckoutQuery.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java create mode 100644 library/src/test/java/com/pengrad/telegrambot/InvoiceCheck.java diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Invoice.java b/library/src/main/java/com/pengrad/telegrambot/model/Invoice.java new file mode 100644 index 00000000..f4d69120 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/Invoice.java @@ -0,0 +1,71 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class Invoice implements Serializable { + private final static long serialVersionUID = 0L; + + private String title, description, start_parameter, currency; + private Integer total_amount; + + public String title() { + return title; + } + + public String description() { + return description; + } + + public String startParameter() { + return start_parameter; + } + + public String currency() { + return currency; + } + + public Integer totalAmount() { + return total_amount; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Invoice invoice = (Invoice) o; + + if (title != null ? !title.equals(invoice.title) : invoice.title != null) return false; + if (description != null ? !description.equals(invoice.description) : invoice.description != null) return false; + if (start_parameter != null ? !start_parameter.equals(invoice.start_parameter) : invoice.start_parameter != null) + return false; + if (currency != null ? !currency.equals(invoice.currency) : invoice.currency != null) return false; + return total_amount != null ? total_amount.equals(invoice.total_amount) : invoice.total_amount == null; + + } + + @Override + public int hashCode() { + int result = title != null ? title.hashCode() : 0; + result = 31 * result + (description != null ? description.hashCode() : 0); + result = 31 * result + (start_parameter != null ? start_parameter.hashCode() : 0); + result = 31 * result + (currency != null ? currency.hashCode() : 0); + result = 31 * result + (total_amount != null ? total_amount.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Invoice{" + + "title='" + title + '\'' + + ", description='" + description + '\'' + + ", start_parameter='" + start_parameter + '\'' + + ", currency='" + currency + '\'' + + ", total_amount=" + total_amount + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Message.java b/library/src/main/java/com/pengrad/telegrambot/model/Message.java index 4471a866..1b655551 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Message.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Message.java @@ -46,6 +46,8 @@ public class Message implements Serializable { private Long migrate_to_chat_id; private Long migrate_from_chat_id; private Message pinned_message; + private Invoice invoice; + private SuccessfulPayment successful_payment; public Integer messageId() { return message_id; @@ -195,6 +197,14 @@ public Message pinnedMessage() { return pinned_message; } + public Invoice invoice() { + return invoice; + } + + public SuccessfulPayment successfulPayment() { + return successful_payment; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -253,7 +263,10 @@ public boolean equals(Object o) { return false; if (migrate_from_chat_id != null ? !migrate_from_chat_id.equals(message.migrate_from_chat_id) : message.migrate_from_chat_id != null) return false; - return pinned_message != null ? pinned_message.equals(message.pinned_message) : message.pinned_message == null; + if (pinned_message != null ? !pinned_message.equals(message.pinned_message) : message.pinned_message != null) + return false; + if (invoice != null ? !invoice.equals(message.invoice) : message.invoice != null) return false; + return successful_payment != null ? successful_payment.equals(message.successful_payment) : message.successful_payment == null; } @Override @@ -300,6 +313,8 @@ public String toString() { ", migrate_to_chat_id=" + migrate_to_chat_id + ", migrate_from_chat_id=" + migrate_from_chat_id + ", pinned_message=" + pinned_message + + ", invoice=" + invoice + + ", successful_payment=" + successful_payment + '}'; } } diff --git a/library/src/main/java/com/pengrad/telegrambot/model/OrderInfo.java b/library/src/main/java/com/pengrad/telegrambot/model/OrderInfo.java new file mode 100644 index 00000000..fd057391 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/OrderInfo.java @@ -0,0 +1,63 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class OrderInfo implements Serializable { + private final static long serialVersionUID = 0L; + + private String name, phone_number, email; + private ShippingAddress shipping_address; + + public String name() { + return name; + } + + public String phoneNumber() { + return phone_number; + } + + public String email() { + return email; + } + + public ShippingAddress shippingAddress() { + return shipping_address; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + OrderInfo orderInfo = (OrderInfo) o; + + if (name != null ? !name.equals(orderInfo.name) : orderInfo.name != null) return false; + if (phone_number != null ? !phone_number.equals(orderInfo.phone_number) : orderInfo.phone_number != null) return false; + if (email != null ? !email.equals(orderInfo.email) : orderInfo.email != null) return false; + return shipping_address != null ? shipping_address.equals(orderInfo.shipping_address) : orderInfo.shipping_address == null; + + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (phone_number != null ? phone_number.hashCode() : 0); + result = 31 * result + (email != null ? email.hashCode() : 0); + result = 31 * result + (shipping_address != null ? shipping_address.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "OrderInfo{" + + "name='" + name + '\'' + + ", phone_number='" + phone_number + '\'' + + ", email='" + email + '\'' + + ", shipping_address=" + shipping_address + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/PreCheckoutQuery.java b/library/src/main/java/com/pengrad/telegrambot/model/PreCheckoutQuery.java new file mode 100644 index 00000000..d956397a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/PreCheckoutQuery.java @@ -0,0 +1,83 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class PreCheckoutQuery implements Serializable { + private final static long serialVersionUID = 0L; + + private String id; + private User from; + private String currency; + private Integer total_amount; + private String invoice_payload; + private String shipping_option_id; + private OrderInfo order_info; + + public String id() { + return id; + } + + public User from() { + return from; + } + + public String currency() { + return currency; + } + + public Integer totalAmount() { + return total_amount; + } + + public String invoicePayload() { + return invoice_payload; + } + + public String shippingOptionId() { + return shipping_option_id; + } + + public OrderInfo orderInfo() { + return order_info; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + PreCheckoutQuery that = (PreCheckoutQuery) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (from != null ? !from.equals(that.from) : that.from != null) return false; + if (currency != null ? !currency.equals(that.currency) : that.currency != null) return false; + if (total_amount != null ? !total_amount.equals(that.total_amount) : that.total_amount != null) return false; + if (invoice_payload != null ? !invoice_payload.equals(that.invoice_payload) : that.invoice_payload != null) + return false; + if (shipping_option_id != null ? !shipping_option_id.equals(that.shipping_option_id) : that.shipping_option_id != null) + return false; + return order_info != null ? order_info.equals(that.order_info) : that.order_info == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "PreCheckoutQuery{" + + "id='" + id + '\'' + + ", from=" + from + + ", currency='" + currency + '\'' + + ", total_amount=" + total_amount + + ", invoice_payload='" + invoice_payload + '\'' + + ", shipping_option_id='" + shipping_option_id + '\'' + + ", order_info=" + order_info + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/ShippingAddress.java b/library/src/main/java/com/pengrad/telegrambot/model/ShippingAddress.java new file mode 100644 index 00000000..f2e5a609 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/ShippingAddress.java @@ -0,0 +1,76 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class ShippingAddress implements Serializable { + private final static long serialVersionUID = 0L; + + private String country_code, state, city, street_line1, street_line2, post_code; + + public String countryCode() { + return country_code; + } + + public String state() { + return state; + } + + public String city() { + return city; + } + + public String streetLine1() { + return street_line1; + } + + public String streetLine2() { + return street_line2; + } + + public String postCode() { + return post_code; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ShippingAddress that = (ShippingAddress) o; + + if (country_code != null ? !country_code.equals(that.country_code) : that.country_code != null) return false; + if (state != null ? !state.equals(that.state) : that.state != null) return false; + if (city != null ? !city.equals(that.city) : that.city != null) return false; + if (street_line1 != null ? !street_line1.equals(that.street_line1) : that.street_line1 != null) return false; + if (street_line2 != null ? !street_line2.equals(that.street_line2) : that.street_line2 != null) return false; + return post_code != null ? post_code.equals(that.post_code) : that.post_code == null; + + } + + @Override + public int hashCode() { + int result = country_code != null ? country_code.hashCode() : 0; + result = 31 * result + (state != null ? state.hashCode() : 0); + result = 31 * result + (city != null ? city.hashCode() : 0); + result = 31 * result + (street_line1 != null ? street_line1.hashCode() : 0); + result = 31 * result + (street_line2 != null ? street_line2.hashCode() : 0); + result = 31 * result + (post_code != null ? post_code.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "ShippingAddress{" + + "country_code='" + country_code + '\'' + + ", state='" + state + '\'' + + ", city='" + city + '\'' + + ", street_line1='" + street_line1 + '\'' + + ", street_line2='" + street_line2 + '\'' + + ", post_code='" + post_code + '\'' + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/ShippingQuery.java b/library/src/main/java/com/pengrad/telegrambot/model/ShippingQuery.java new file mode 100644 index 00000000..3fc95f6a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/ShippingQuery.java @@ -0,0 +1,61 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class ShippingQuery implements Serializable { + private final static long serialVersionUID = 0L; + + private String id; + private User from; + private String invoice_payload; + private ShippingAddress shipping_address; + + public String id() { + return id; + } + + public User from() { + return from; + } + + public String invoicePayload() { + return invoice_payload; + } + + public ShippingAddress shippingAddress() { + return shipping_address; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ShippingQuery that = (ShippingQuery) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (from != null ? !from.equals(that.from) : that.from != null) return false; + if (invoice_payload != null ? !invoice_payload.equals(that.invoice_payload) : that.invoice_payload != null) + return false; + return shipping_address != null ? shipping_address.equals(that.shipping_address) : that.shipping_address == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + + @Override + public String toString() { + return "ShippingQuery{" + + "id='" + id + '\'' + + ", from=" + from + + ", invoice_payload='" + invoice_payload + '\'' + + ", shipping_address=" + shipping_address + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java b/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java new file mode 100644 index 00000000..64e81c31 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java @@ -0,0 +1,92 @@ +package com.pengrad.telegrambot.model; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class SuccessfulPayment implements Serializable { + private final static long serialVersionUID = 0L; + + private String currency; + private Integer total_amount; + private String invoice_payload; + private String shipping_option_id; + private OrderInfo order_info; + private String telegram_payment_charge_id; + private String provider_payment_charge_id; + + public String currency() { + return currency; + } + + public Integer totalAmount() { + return total_amount; + } + + public String invoicePayload() { + return invoice_payload; + } + + public String shippingOptionId() { + return shipping_option_id; + } + + public OrderInfo orderInfo() { + return order_info; + } + + public String telegramPaymentChargeId() { + return telegram_payment_charge_id; + } + + public String providerPaymentChargeId() { + return provider_payment_charge_id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SuccessfulPayment that = (SuccessfulPayment) o; + + if (currency != null ? !currency.equals(that.currency) : that.currency != null) return false; + if (total_amount != null ? !total_amount.equals(that.total_amount) : that.total_amount != null) return false; + if (invoice_payload != null ? !invoice_payload.equals(that.invoice_payload) : that.invoice_payload != null) + return false; + if (shipping_option_id != null ? !shipping_option_id.equals(that.shipping_option_id) : that.shipping_option_id != null) + return false; + if (order_info != null ? !order_info.equals(that.order_info) : that.order_info != null) return false; + if (telegram_payment_charge_id != null ? !telegram_payment_charge_id.equals(that.telegram_payment_charge_id) : that.telegram_payment_charge_id != null) + return false; + return provider_payment_charge_id != null ? provider_payment_charge_id.equals(that.provider_payment_charge_id) : that.provider_payment_charge_id == null; + + } + + @Override + public int hashCode() { + int result = currency != null ? currency.hashCode() : 0; + result = 31 * result + (total_amount != null ? total_amount.hashCode() : 0); + result = 31 * result + (invoice_payload != null ? invoice_payload.hashCode() : 0); + result = 31 * result + (shipping_option_id != null ? shipping_option_id.hashCode() : 0); + result = 31 * result + (order_info != null ? order_info.hashCode() : 0); + result = 31 * result + (telegram_payment_charge_id != null ? telegram_payment_charge_id.hashCode() : 0); + result = 31 * result + (provider_payment_charge_id != null ? provider_payment_charge_id.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "SuccessfulPayment{" + + "currency='" + currency + '\'' + + ", total_amount=" + total_amount + + ", invoice_payload='" + invoice_payload + '\'' + + ", shipping_option_id='" + shipping_option_id + '\'' + + ", order_info=" + order_info + + ", telegram_payment_charge_id='" + telegram_payment_charge_id + '\'' + + ", provider_payment_charge_id='" + provider_payment_charge_id + '\'' + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Update.java b/library/src/main/java/com/pengrad/telegrambot/model/Update.java index 17bc1e44..d20770c1 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Update.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Update.java @@ -17,6 +17,8 @@ public class Update implements Serializable { private InlineQuery inline_query; private ChosenInlineResult chosen_inline_result; private CallbackQuery callback_query; + private ShippingQuery shipping_query; + private PreCheckoutQuery pre_checkout_query; public Integer updateId() { return update_id; @@ -50,6 +52,14 @@ public CallbackQuery callbackQuery() { return callback_query; } + public ShippingQuery shippingQuery() { + return shipping_query; + } + + public PreCheckoutQuery preCheckoutQuery() { + return pre_checkout_query; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -61,16 +71,17 @@ public boolean equals(Object o) { if (message != null ? !message.equals(update.message) : update.message != null) return false; if (edited_message != null ? !edited_message.equals(update.edited_message) : update.edited_message != null) return false; - if (channel_post != null ? !channel_post.equals(update.channel_post) : update.channel_post != null) - return false; + if (channel_post != null ? !channel_post.equals(update.channel_post) : update.channel_post != null) return false; if (edited_channel_post != null ? !edited_channel_post.equals(update.edited_channel_post) : update.edited_channel_post != null) return false; - if (inline_query != null ? !inline_query.equals(update.inline_query) : update.inline_query != null) - return false; + if (inline_query != null ? !inline_query.equals(update.inline_query) : update.inline_query != null) return false; if (chosen_inline_result != null ? !chosen_inline_result.equals(update.chosen_inline_result) : update.chosen_inline_result != null) return false; - return callback_query != null ? callback_query.equals(update.callback_query) : update.callback_query == null; - + if (callback_query != null ? !callback_query.equals(update.callback_query) : update.callback_query != null) + return false; + if (shipping_query != null ? !shipping_query.equals(update.shipping_query) : update.shipping_query != null) + return false; + return pre_checkout_query != null ? pre_checkout_query.equals(update.pre_checkout_query) : update.pre_checkout_query == null; } @Override @@ -89,6 +100,8 @@ public String toString() { ", inline_query=" + inline_query + ", chosen_inline_result=" + chosen_inline_result + ", callback_query=" + callback_query + + ", shipping_query=" + shipping_query + + ", pre_checkout_query=" + pre_checkout_query + '}'; } } diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.java b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.java index 4b01141b..de0988d9 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.java @@ -15,6 +15,9 @@ public class InlineKeyboardButton implements Serializable { private String switch_inline_query; private String switch_inline_query_current_chat; private String callback_game; + private Boolean pay; + + //todo can use only one optional field, make different constructors or static methods public InlineKeyboardButton(String text) { this.text = text; @@ -44,4 +47,9 @@ public InlineKeyboardButton callbackGame(String callbackGame) { callback_game = callbackGame; return this; } + + public InlineKeyboardButton pay() { + this.pay = true; + return this; + } } diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/LabeledPrice.java b/library/src/main/java/com/pengrad/telegrambot/model/request/LabeledPrice.java new file mode 100644 index 00000000..2f6d3524 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/LabeledPrice.java @@ -0,0 +1,19 @@ +package com.pengrad.telegrambot.model.request; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class LabeledPrice implements Serializable { + private final static long serialVersionUID = 0L; + + private String label; + private Integer amount; + + public LabeledPrice(String label, Integer amount) { + this.label = label; + this.amount = amount; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/ShippingOption.java b/library/src/main/java/com/pengrad/telegrambot/model/request/ShippingOption.java new file mode 100644 index 00000000..b8364ba9 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/ShippingOption.java @@ -0,0 +1,20 @@ +package com.pengrad.telegrambot.model.request; + +import java.io.Serializable; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class ShippingOption implements Serializable { + private final static long serialVersionUID = 0L; + + private String id, title; + private LabeledPrice[] prices; + + public ShippingOption(String id, String title, LabeledPrice... prices) { + this.id = id; + this.title = title; + this.prices = prices; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AnswerPreCheckoutQuery.java b/library/src/main/java/com/pengrad/telegrambot/request/AnswerPreCheckoutQuery.java new file mode 100644 index 00000000..9a488c3c --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/AnswerPreCheckoutQuery.java @@ -0,0 +1,20 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 26 May 2017 + */ +public class AnswerPreCheckoutQuery extends BaseRequest { + + public AnswerPreCheckoutQuery(String preCheckoutQueryId) { + super(BaseResponse.class); + add("pre_checkout_query_id", preCheckoutQueryId).add("ok", true); + } + + public AnswerPreCheckoutQuery(String preCheckoutQueryId, String errorMessage) { + super(BaseResponse.class); + add("pre_checkout_query_id", preCheckoutQueryId).add("ok", false).add("error_message", errorMessage); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java b/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java new file mode 100644 index 00000000..6bca9b0f --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java @@ -0,0 +1,24 @@ +package com.pengrad.telegrambot.request; + +import com.google.gson.Gson; +import com.pengrad.telegrambot.model.request.ShippingOption; +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 25 May 2017 + */ +public class AnswerShippingQuery extends BaseRequest { + + private static Gson gson = new Gson(); + + public AnswerShippingQuery(String shippingQueryId, ShippingOption... shippingOptions) { + super(BaseResponse.class); + add("shipping_query_id", shippingQueryId).add("ok", true).add("shipping_options", gson.toJson(shippingOptions)); + } + + public AnswerShippingQuery(String shippingQueryId, String errorMessage) { + super(BaseResponse.class); + add("shipping_query_id", shippingQueryId).add("ok", false).add("error_message", errorMessage); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java b/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java new file mode 100644 index 00000000..0581014a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java @@ -0,0 +1,57 @@ +package com.pengrad.telegrambot.request; + +import com.google.gson.Gson; +import com.pengrad.telegrambot.model.request.LabeledPrice; + +/** + * Stas Parshin + * 24 May 2017 + */ +public class SendInvoice extends AbstractSendRequest { + + // todo remove gson + private static Gson gson = new Gson(); + + public SendInvoice(Integer chatId, String title, String description, String payload, String providerToken, + String startParameter, String currency, LabeledPrice... prices) { + super(chatId); + add("title", title).add("description", description).add("payload", payload).add("provider_token", providerToken) + .add("start_parameter", startParameter).add("currency", currency).add("prices", gson.toJson(prices)); + } + + public SendInvoice photoUrl(String photoUrl) { + return add("photo_url", photoUrl); + } + + public SendInvoice photoSize(Integer photoSize) { + return add("photo_size", photoSize); + } + + public SendInvoice photoWidth(Integer photoWidth) { + return add("photo_width", photoWidth); + } + + public SendInvoice photoHeight(Integer photoHeight) { + return add("photo_height", photoHeight); + } + + public SendInvoice needName(boolean needName) { + return add("need_name", needName); + } + + public SendInvoice needPhoneNumber(boolean needPhoneNumber) { + return add("need_phone_number", needPhoneNumber); + } + + public SendInvoice needEmail(boolean needEmail) { + return add("need_email", needEmail); + } + + public SendInvoice needShippingAddress(boolean needShippingAddress) { + return add("need_shipping_address", needShippingAddress); + } + + public SendInvoice isFlexible(boolean isFlexible) { + return add("is_flexible", isFlexible); + } +} diff --git a/library/src/test/java/com/pengrad/telegrambot/InvoiceCheck.java b/library/src/test/java/com/pengrad/telegrambot/InvoiceCheck.java new file mode 100644 index 00000000..a3390b2a --- /dev/null +++ b/library/src/test/java/com/pengrad/telegrambot/InvoiceCheck.java @@ -0,0 +1,21 @@ +package com.pengrad.telegrambot; + +import com.pengrad.telegrambot.model.Invoice; + +import static org.junit.Assert.assertNotNull; + +/** + * Stas Parshin + * 25 May 2017 + */ +public class InvoiceCheck { + + public static void check(Invoice invoice) { + assertNotNull(invoice.title()); + assertNotNull(invoice.description()); + assertNotNull(invoice.startParameter()); + assertNotNull(invoice.currency()); + assertNotNull(invoice.totalAmount()); + } + +} diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 3b611993..e428b17a 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -64,7 +64,7 @@ public void getMe() { @Test public void getUpdates() { - GetUpdatesResponse response = bot.execute(new GetUpdates()); + GetUpdatesResponse response = bot.execute(new GetUpdates().allowedUpdates("")); System.out.println(response); } @@ -301,4 +301,84 @@ public void sendVideoNote() { SendResponse response = bot.execute(new SendVideoNote(chatId, "DQADAgADmQADYgwpSbum1JrxPsbmAg").length(240)); VideoNoteCheck.check(response.message().videoNote()); } + + @Test + public void sendInvoice() { + SendResponse response = bot.execute(new SendInvoice(chatId, "title", "desc", "my_payload", + "284685063:TEST:NThlNWQ3NDk0ZDQ5", "my_start_param", "USD", new LabeledPrice("label", 200)) + .needPhoneNumber(true) + .needShippingAddress(true) + .isFlexible(true) + .replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton[]{ + new InlineKeyboardButton("just pay").pay(), + new InlineKeyboardButton("google it").url("www.google.com") + + })) + ); + InvoiceCheck.check(response.message().invoice()); + } + + @Test + public void answerShippingQuery() { + ShippingQuery shippingQuery = getLastShippingQuery(); + if (shippingQuery == null) return; + + String shippingQueryId = shippingQuery.id(); + BaseResponse response = bot.execute(new AnswerShippingQuery(shippingQueryId, + new ShippingOption("1", "VNPT", new LabeledPrice("delivery", 100), new LabeledPrice("tips", 50)), + new ShippingOption("2", "FREE", new LabeledPrice("free delivery", 0)) + )); + } + + @Test + public void answerShippingQueryError() { + ShippingQuery shippingQuery = getLastShippingQuery(); + if (shippingQuery == null) return; + + String shippingQueryId = shippingQuery.id(); + BaseResponse response = bot.execute(new AnswerShippingQuery(shippingQueryId, "cant delivery so far")); + } + + private ShippingQuery getLastShippingQuery() { + GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates()); + List updates = updatesResponse.updates(); + Collections.reverse(updates); + for (Update update : updates) { + if (update.shippingQuery() != null) { + return update.shippingQuery(); + } + } + return null; + } + + @Test + public void answerPreCheckoutQuery() { + PreCheckoutQuery preCheckoutQuery = getLastPreCheckoutQuery(); + if (preCheckoutQuery == null) return; + + String preCheckoutQueryId = preCheckoutQuery.id(); + BaseResponse response = bot.execute(new AnswerPreCheckoutQuery(preCheckoutQueryId)); + } + + @Test + public void answerPreCheckoutQueryError() { + PreCheckoutQuery preCheckoutQuery = getLastPreCheckoutQuery(); + if (preCheckoutQuery == null) return; + + String preCheckoutQueryId = preCheckoutQuery.id(); + BaseResponse response = bot.execute(new AnswerPreCheckoutQuery(preCheckoutQueryId, "cant sell to you")); + } + + private PreCheckoutQuery getLastPreCheckoutQuery() { + GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates()); + List updates = updatesResponse.updates(); + Collections.reverse(updates); + for (Update update : updates) { + if (update.preCheckoutQuery() != null) { + return update.preCheckoutQuery(); + } + } + return null; + } + } From 9d9a10cf7c348b1202193a712053eea030912f70 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Sat, 27 May 2017 16:59:27 +0700 Subject: [PATCH 8/8] Fix sendVideoNote test --- .../java/com/pengrad/telegrambot/TelegramBotTest.java | 8 +++++++- .../test/java/com/pengrad/telegrambot/VideoNoteCheck.java | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index e428b17a..18a82ff1 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -298,10 +298,16 @@ public void sendChatAction() { @Test public void sendVideoNote() { - SendResponse response = bot.execute(new SendVideoNote(chatId, "DQADAgADmQADYgwpSbum1JrxPsbmAg").length(240)); + SendResponse response = bot.execute(new SendVideoNote(chatId, "DQADAgADmQADYgwpSbum1JrxPsbmAg")); VideoNoteCheck.check(response.message().videoNote()); } + @Test + public void sendVideoNoteFile() { + SendResponse response = bot.execute(new SendVideoNote(chatId, new File(videoFile)).length(20).duration(30)); + VideoNoteCheck.check(response.message().videoNote(), true); + } + @Test public void sendInvoice() { SendResponse response = bot.execute(new SendInvoice(chatId, "title", "desc", "my_payload", diff --git a/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java b/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java index 810cc20c..b7f2c6a1 100644 --- a/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java +++ b/library/src/test/java/com/pengrad/telegrambot/VideoNoteCheck.java @@ -11,11 +11,17 @@ public class VideoNoteCheck { public static void check(VideoNote videoNote) { + check(videoNote, false); + } + + public static void check(VideoNote videoNote, boolean full) { assertNotNull(videoNote.fileId()); assertNotNull(videoNote.length()); assertNotNull(videoNote.duration()); - assertNotNull(videoNote.fileSize()); PhotoSizeTest.checkPhotos(videoNote.thumb()); + if (full) { + assertNotNull(videoNote.fileSize()); + } } }