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 00000000..5f436888 Binary files /dev/null and b/library/src/test/resources/imageSticker.png differ