From d77fa4dd7823df7609ca1e2508dc1f33693b85d4 Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Sun, 23 Jul 2017 23:21:26 +0700 Subject: [PATCH] Added new methods for working with stickers: getStickerSet, uploadStickerFile, createNewStickerSet, addStickerToSet, setStickerPositionInSet, and deleteStickerFromSet --- .../request/AbstractUploadRequest.java | 33 ++++++++ .../telegrambot/request/AddStickerToSet.java | 17 ++++ .../request/AnswerInlineQuery.java | 6 +- .../request/AnswerShippingQuery.java | 2 +- .../telegrambot/request/BaseRequest.java | 9 +++ .../request/CreateNewStickerSet.java | 27 +++++++ .../request/DeleteStickerFromSet.java | 14 ++++ .../telegrambot/request/GetStickerSet.java | 15 ++++ .../telegrambot/request/GetUpdates.java | 6 +- .../telegrambot/request/SendInvoice.java | 6 +- .../request/SetStickerPositionInSet.java | 15 ++++ .../telegrambot/request/SetWebhook.java | 6 +- .../request/UploadStickerFile.java | 15 ++++ .../response/GetStickerSetResponse.java | 23 ++++++ .../com/pengrad/telegrambot/FileTest.java | 6 +- .../pengrad/telegrambot/TelegramBotTest.java | 74 +++++++++++++++++- library/src/test/resources/imageSticker.png | Bin 0 -> 9143 bytes 17 files changed, 251 insertions(+), 23 deletions(-) create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/AbstractUploadRequest.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/AddStickerToSet.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/CreateNewStickerSet.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/DeleteStickerFromSet.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/GetStickerSet.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/SetStickerPositionInSet.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/request/UploadStickerFile.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/response/GetStickerSetResponse.java create mode 100644 library/src/test/resources/imageSticker.png diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AbstractUploadRequest.java b/library/src/main/java/com/pengrad/telegrambot/request/AbstractUploadRequest.java new file mode 100644 index 00000000..83d22e8e --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/AbstractUploadRequest.java @@ -0,0 +1,33 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +import java.io.File; + +/** + * Stas Parshin + * 23 July 2017 + */ +abstract public class AbstractUploadRequest extends BaseRequest { + + private final boolean isMultipart; + + public AbstractUploadRequest(Class responseClass, String paramName, Object data) { + super(responseClass); + if (data instanceof String) { + isMultipart = false; + } else if (data instanceof File) { + isMultipart = true; + } else if (data instanceof byte[]) { + isMultipart = true; + } else { + throw new IllegalArgumentException("Sending data should be String, File or byte[]"); + } + add(paramName, data); + } + + @Override + public boolean isMultipart() { + return isMultipart; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AddStickerToSet.java b/library/src/main/java/com/pengrad/telegrambot/request/AddStickerToSet.java new file mode 100644 index 00000000..b6cc7879 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/AddStickerToSet.java @@ -0,0 +1,17 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class AddStickerToSet extends AbstractUploadRequest { + + public AddStickerToSet(Integer userId, String name, Object pngSticker, String emojis) { + super(BaseResponse.class, "png_sticker", pngSticker); + add("user_id", userId); + add("name", name); + add("emojis", emojis); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AnswerInlineQuery.java b/library/src/main/java/com/pengrad/telegrambot/request/AnswerInlineQuery.java index 8ce68843..f23a1722 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/AnswerInlineQuery.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/AnswerInlineQuery.java @@ -1,6 +1,5 @@ package com.pengrad.telegrambot.request; -import com.google.gson.Gson; import com.pengrad.telegrambot.model.request.InlineQueryResult; import com.pengrad.telegrambot.response.BaseResponse; @@ -10,12 +9,9 @@ */ public class AnswerInlineQuery extends BaseRequest { - // todo remove gson - private static Gson gson = new Gson(); - public AnswerInlineQuery(String inlineQueryId, InlineQueryResult... results) { super(BaseResponse.class); - add("inline_query_id", inlineQueryId).add("results", gson.toJson(results)); + add("inline_query_id", inlineQueryId).add("results", serialize(results)); } public AnswerInlineQuery cacheTime(int cacheTime) { diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java b/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java index 6bca9b0f..89c67d5e 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/AnswerShippingQuery.java @@ -14,7 +14,7 @@ public class AnswerShippingQuery extends BaseRequest { + private static final Gson gson = new Gson(); + @SuppressWarnings("unchecked") protected final T thisAsT = (T) this; @@ -56,4 +59,10 @@ public String getContentType() { public int getTimeoutSeconds() { return 0; } + + // Serialize model objects. Basically convert to json + // todo move to TelegramBotClient, let it serialize everything in request time + protected String serialize(Object o) { + return gson.toJson(o); + } } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/CreateNewStickerSet.java b/library/src/main/java/com/pengrad/telegrambot/request/CreateNewStickerSet.java new file mode 100644 index 00000000..b4ff7ac7 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/CreateNewStickerSet.java @@ -0,0 +1,27 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.model.MaskPosition; +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class CreateNewStickerSet extends AbstractUploadRequest { + + public CreateNewStickerSet(Integer userId, String name, String title, Object pngSticker, String emojis) { + super(BaseResponse.class, "png_sticker", pngSticker); + add("user_id", userId); + add("name", name); + add("title", title); + add("emojis", emojis); + } + + public CreateNewStickerSet containsMasks(boolean containsMasks) { + return add("contains_masks", containsMasks); + } + + public CreateNewStickerSet maskPosition(MaskPosition maskPosition) { + return add("mask_position", serialize(maskPosition)).containsMasks(true); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/DeleteStickerFromSet.java b/library/src/main/java/com/pengrad/telegrambot/request/DeleteStickerFromSet.java new file mode 100644 index 00000000..349afb43 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/DeleteStickerFromSet.java @@ -0,0 +1,14 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class DeleteStickerFromSet extends BaseRequest { + public DeleteStickerFromSet(String sticker) { + super(BaseResponse.class); + add("sticker", sticker); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/GetStickerSet.java b/library/src/main/java/com/pengrad/telegrambot/request/GetStickerSet.java new file mode 100644 index 00000000..c571ada8 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/GetStickerSet.java @@ -0,0 +1,15 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.GetStickerSetResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class GetStickerSet extends BaseRequest { + + public GetStickerSet(String name) { + super(GetStickerSetResponse.class); + add("name", name); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/GetUpdates.java b/library/src/main/java/com/pengrad/telegrambot/request/GetUpdates.java index 13aac28d..82c9c7dd 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/GetUpdates.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/GetUpdates.java @@ -1,6 +1,5 @@ package com.pengrad.telegrambot.request; -import com.google.gson.Gson; import com.pengrad.telegrambot.response.GetUpdatesResponse; /** @@ -9,9 +8,6 @@ */ public class GetUpdates extends BaseRequest { - // todo remove gson - private static Gson gson = new Gson(); - private int timeout = 0; private int limit = 100; @@ -34,7 +30,7 @@ public GetUpdates timeout(int timeout) { } public GetUpdates allowedUpdates(String... allowedUpdates) { - return add("allowed_updates", gson.toJson(allowedUpdates)); + return add("allowed_updates", serialize(allowedUpdates)); } @Override diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java b/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java index 0581014a..3ff5cefb 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendInvoice.java @@ -1,6 +1,5 @@ package com.pengrad.telegrambot.request; -import com.google.gson.Gson; import com.pengrad.telegrambot.model.request.LabeledPrice; /** @@ -9,14 +8,11 @@ */ 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)); + .add("start_parameter", startParameter).add("currency", currency).add("prices", serialize(prices)); } public SendInvoice photoUrl(String photoUrl) { diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SetStickerPositionInSet.java b/library/src/main/java/com/pengrad/telegrambot/request/SetStickerPositionInSet.java new file mode 100644 index 00000000..a21da26f --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SetStickerPositionInSet.java @@ -0,0 +1,15 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.BaseResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class SetStickerPositionInSet extends BaseRequest { + + public SetStickerPositionInSet(String sticker, int position) { + super(BaseResponse.class); + add("sticker", sticker).add("position", position); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SetWebhook.java b/library/src/main/java/com/pengrad/telegrambot/request/SetWebhook.java index 4b2a16b8..c4c733b2 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/SetWebhook.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/SetWebhook.java @@ -1,6 +1,5 @@ package com.pengrad.telegrambot.request; -import com.google.gson.Gson; import com.pengrad.telegrambot.response.BaseResponse; import java.io.File; @@ -11,9 +10,6 @@ */ public class SetWebhook extends BaseRequest { - // todo remove gson - private static Gson gson = new Gson(); - private boolean isMultipart = false; public SetWebhook() { @@ -39,7 +35,7 @@ public SetWebhook maxConnections(int maxConnections) { } public SetWebhook allowedUpdates(String... allowedUpdates) { - return add("allowed_updates", gson.toJson(allowedUpdates)); + return add("allowed_updates", serialize(allowedUpdates)); } @Override diff --git a/library/src/main/java/com/pengrad/telegrambot/request/UploadStickerFile.java b/library/src/main/java/com/pengrad/telegrambot/request/UploadStickerFile.java new file mode 100644 index 00000000..4c14441c --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/UploadStickerFile.java @@ -0,0 +1,15 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.response.GetFileResponse; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class UploadStickerFile extends AbstractUploadRequest { + + public UploadStickerFile(Integer userId, Object pngSticker) { + super(GetFileResponse.class, "png_sticker", pngSticker); + add("user_id", userId); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/response/GetStickerSetResponse.java b/library/src/main/java/com/pengrad/telegrambot/response/GetStickerSetResponse.java new file mode 100644 index 00000000..d2ed8878 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/response/GetStickerSetResponse.java @@ -0,0 +1,23 @@ +package com.pengrad.telegrambot.response; + +import com.pengrad.telegrambot.model.StickerSet; + +/** + * Stas Parshin + * 23 July 2017 + */ +public class GetStickerSetResponse extends BaseResponse { + + private StickerSet result; + + public StickerSet stickerSet() { + return result; + } + + @Override + public String toString() { + return "GetStickerSetResponse{" + + "result=" + result + + '}'; + } +} diff --git a/library/src/test/java/com/pengrad/telegrambot/FileTest.java b/library/src/test/java/com/pengrad/telegrambot/FileTest.java index 9ce038c1..5ee1c4b3 100644 --- a/library/src/test/java/com/pengrad/telegrambot/FileTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/FileTest.java @@ -11,8 +11,12 @@ public class FileTest { public static void check(File file) { + check(file, true); + } + + public static void check(File file, boolean path) { assertNotNull(file.fileId()); assertNotNull(file.fileSize()); - assertNotNull(file.filePath()); + if (path) assertNotNull(file.filePath()); } } diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 256956d2..3709a9eb 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -43,6 +43,7 @@ public class TelegramBotTest { Path resourcePath = Paths.get("src/test/resources"); String imagefile = resourcePath.resolve("image.png").toString(); File imageFile = resourcePath.resolve("image.jpg").toFile(); + File stickerFile = resourcePath.resolve("imageSticker.png").toFile(); String audioFile = resourcePath.resolve("beep.mp3").toString(); String docFile = resourcePath.resolve("doc.txt").toString(); String videoFile = resourcePath.resolve("tabs.mp4").toString(); @@ -56,6 +57,7 @@ public class TelegramBotTest { String photoFileId = "AgADAgADDKgxG7mDWUlvyFIJ9XfF9yszSw0ABBhVadWwbAK1z-wIAAEC"; String gifFileId = "CgADAgADfQADgNqgSTt9SzatJhc3Ag"; String withSpaceFileId = "BAADAgADZwADkg-4SQI5WM0SPNHrAg"; + String stickerSet = "testset_by_pengrad_test_bot"; public TelegramBotTest() throws IOException { String token, chat, group; @@ -816,7 +818,10 @@ public void setChatPhoto() throws IOException { @Test public void deleteChatPhoto() { BaseResponse response = bot.execute(new DeleteChatPhoto(groupId)); - assertTrue(response.isOk()); + if (!response.isOk()) { + assertEquals(400, response.errorCode()); + assertEquals("Bad Request: CHAT_NOT_MODIFIED", response.description()); + } } @Test @@ -848,4 +853,71 @@ public void unpinChatMessage() { assertEquals("Bad Request: CHAT_NOT_MODIFIED", response.description()); } } + + @Test + public void getStickerSet() { + GetStickerSetResponse response = bot.execute(new GetStickerSet(stickerSet)); + StickerSet stickerSet = response.stickerSet(); + for (Sticker sticker : response.stickerSet().stickers()) { + StickerTest.check(sticker, true, true); + } + assertTrue(stickerSet.containsMasks()); + assertEquals(this.stickerSet, stickerSet.name()); + assertEquals("test1", stickerSet.title()); + + Sticker sticker = stickerSet.stickers()[0]; + assertEquals(this.stickerSet, sticker.setName()); + MaskPosition maskPosition = sticker.maskPosition(); + assertEquals(MaskPosition.Point.forehead.name(), maskPosition.point()); + assertEquals(0f, maskPosition.xShift(), 0); + assertEquals(0f, maskPosition.yShift(), 0); + assertEquals(1f, maskPosition.scale(), 0); + System.out.println(response); + } + + @Test + public void uploadStickerFile() throws IOException { + byte[] bytes = Files.readAllBytes(stickerFile.toPath()); + GetFileResponse response = bot.execute(new UploadStickerFile(chatId, bytes)); + FileTest.check(response.file(), false); + } + + @Test + public void createNewStickerSet() throws IOException { + BaseResponse response = bot.execute( + new CreateNewStickerSet(chatId, "test" + System.currentTimeMillis() + "_by_pengrad_test_bot", + "test1", stickerFile, "\uD83D\uDE00") + .containsMasks(true) + .maskPosition(new MaskPosition(MaskPosition.Point.forehead, 0f, 0f, 1f))); + assertTrue(response.isOk()); + } + + @Test + public void addStickerToSet() { + BaseResponse response = bot.execute( + new AddStickerToSet(chatId, stickerSet, "BQADAgADuAAD7yupS4eB23UmZhGuAg", "\uD83D\uDE15")); + assertTrue(response.isOk()); + } + + @Test + public void setStickerPositionInSet() { + GetStickerSetResponse setResponse = bot.execute(new GetStickerSet(stickerSet)); + Sticker sticker = setResponse.stickerSet().stickers()[0]; + + BaseResponse response = bot.execute(new SetStickerPositionInSet(sticker.fileId(), 0)); + assertTrue(response.isOk()); + } + + @Test + public void deleteStickerFromSet() { + BaseResponse response = bot.execute(new AddStickerToSet(chatId, stickerSet, stickerFile, "\uD83D\uDE15")); + assertTrue(response.isOk()); + + GetStickerSetResponse setResponse = bot.execute(new GetStickerSet(stickerSet)); + int size = setResponse.stickerSet().stickers().length; + Sticker sticker = setResponse.stickerSet().stickers()[size - 1]; + + response = bot.execute(new DeleteStickerFromSet(sticker.fileId())); + assertTrue(response.isOk()); + } } diff --git a/library/src/test/resources/imageSticker.png b/library/src/test/resources/imageSticker.png new file mode 100644 index 0000000000000000000000000000000000000000..5f4368882be14578ea9689a1696e41d893cdb2b9 GIT binary patch literal 9143 zcmeHNU5Fc16rSvE>vn0k5f$rCWr$D&N#-Y+q&sBCWb@NTHrpkM?e;;~N#-WG?PO*; zGjX$hkhBOQK8S)3eJl8EUlkE-p$`?o9}pj_=tJv+DAETJ5%0Z|nM`JruD zS>j+dMi3(UjKxfhCJ_7LYHX{zNf&ez&_W`WU}Mq}P19LZX^XXDc|=D~tFf+a8zRH( z?Cd0Vk_p}HFx<+@3d8aY&&LrWZtZHeJcw)7^NtbMP7zow6B;(uH5%KMn|j|~jm7Xn z-jCxa7{KvLnl%aw1;}6x!zEZ|8qJ3788ob!NkcjE!kSoYAR9AD*pbqf$7?}DPFye^ z?w`@sZQbhXGnVtzGZ#gkR;%8cQ+@6A7%%N8t6A))SkP77tQ&F*NL-fq=ogJQz(Lph z%?qGq=S?7!D)2UkltUd{nv~*I@P4XL;xa-y%ZwhzW*q}AD9s}u_QS9%`(rqMI>wPs z`ZDF^zJW+s=)5*iiDCt_IOen+m#f&_Zco=oZc_vLSH{?K#+=d9k`ASH71A z+h>7E^2+kK-bji{s25yukg{gNPS@6eWu2APK9Eu;Q+T2YZjyrfAitpZ3|#}7Z54E_ z4Lf~P#*=QcRrYV7=@sp3u^Bb*LnQaLfpR~s{~ppe`$y+=!nrQA!J4ef8X7h37EcI+ zc^lWx8MFgXlhvt(-%2IW!ju*|sQg1R6(ZvYcdT(9c{ zbf4hd$ug-8a><}PKXZioG(jesoxsF16ju#u7!t^YFvzIX&(sY7lp1Pltz-ET=C_?L$Hx*msN8IiB|+~=&7*X=(~l(%ImM0l-=_*v>)pPMl0|}d zHi^$VNMXBG1irALhLQ_$McvDU)agI~uxU3|(xROjN8G$>= zx4971M(G@V zVw<|XoJd6Q;~xhOQ>%1tNJZxIk$HMxzk3KlhQrA9c?bN$Hns3hf+t{>vN3RBapAz> z;Sb-GP7vO~;WDOBSFR5u(d;L82=6BKHKEYa$64y?sVA=y-Z4rfl!dEv2XnvOk3B|K zp3gmk{Tk*rZ&HiTy{Zs5x2gMt5?x$6M&0`0?RB#9Vd^-cgw968%!W0KTP