From 7a08e13be5f365c32be2405afb5ef398c8f606cb Mon Sep 17 00:00:00 2001 From: Norbert Bartels Date: Sun, 3 Jul 2022 16:29:23 +0200 Subject: [PATCH] Issue #1230 - incoming wabp sticker --- .../types/whatsapp/platform/Message.java | 12 +++-- .../whatsapp/platform/message/Sticker.java | 54 +++++++++++++++++++ .../com/restfb/types/WABPwebhookTest.java | 42 ++++++++++++--- .../webhook-incoming-message-sticker.json | 41 ++++++++++++++ 4 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 src/main/lombok/com/restfb/types/whatsapp/platform/message/Sticker.java create mode 100644 src/test/resources/json/whatsapp/webhook-incoming-message-sticker.json diff --git a/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java b/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java index a6b18ae35..5353555eb 100644 --- a/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java @@ -28,10 +28,7 @@ import com.restfb.Facebook; import com.restfb.json.JsonObject; import com.restfb.types.AbstractFacebookType; -import com.restfb.types.whatsapp.platform.message.Image; -import com.restfb.types.whatsapp.platform.message.MessageType; -import com.restfb.types.whatsapp.platform.message.Text; -import com.restfb.types.whatsapp.platform.message.Video; +import com.restfb.types.whatsapp.platform.message.*; import lombok.Getter; import lombok.Setter; @@ -74,6 +71,11 @@ public class Message extends AbstractFacebookType { private JsonObject system; + @Getter + @Setter + @Facebook + private Sticker sticker; + @Getter @Setter @Facebook @@ -101,4 +103,6 @@ public boolean isText() { public boolean isImage() { return image != null; } public boolean isVideo() { return video != null; } + + public boolean isSticker() { return sticker != null; } } diff --git a/src/main/lombok/com/restfb/types/whatsapp/platform/message/Sticker.java b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Sticker.java new file mode 100644 index 000000000..6a15aa2ca --- /dev/null +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Sticker.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2010-2022 Mark Allen, Norbert Bartels. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.restfb.types.whatsapp.platform.message; + +import com.restfb.Facebook; +import com.restfb.types.AbstractFacebookType; +import lombok.Getter; +import lombok.Setter; + +public class Sticker extends AbstractFacebookType { + + /** + * ID for the sticker + */ + @Getter + @Setter + @Facebook + private String id; + + /** + * image/webp + */ + @Getter + @Setter + @Facebook("mime_type") + private String mimeType; + + /** + * Hash for the sticker + */ + @Getter + @Setter + @Facebook + private String sha256; +} diff --git a/src/test/java/com/restfb/types/WABPwebhookTest.java b/src/test/java/com/restfb/types/WABPwebhookTest.java index 71a5a711c..d8766333b 100644 --- a/src/test/java/com/restfb/types/WABPwebhookTest.java +++ b/src/test/java/com/restfb/types/WABPwebhookTest.java @@ -21,6 +21,12 @@ */ package com.restfb.types; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Date; + +import org.junit.jupiter.api.Test; + import com.restfb.AbstractJsonMapperTests; import com.restfb.types.webhook.ChangeValue; import com.restfb.types.webhook.WebhookEntry; @@ -29,13 +35,9 @@ import com.restfb.types.whatsapp.platform.Contact; import com.restfb.types.whatsapp.platform.Message; import com.restfb.types.whatsapp.platform.message.Image; -import com.restfb.types.whatsapp.platform.message.Video; import com.restfb.types.whatsapp.platform.message.MessageType; -import org.junit.jupiter.api.Test; - -import java.util.Date; - -import static org.assertj.core.api.Assertions.assertThat; +import com.restfb.types.whatsapp.platform.message.Sticker; +import com.restfb.types.whatsapp.platform.message.Video; class WABPwebhookTest extends AbstractJsonMapperTests { @@ -118,6 +120,34 @@ void incomingMessageVideo() { assertThat(message.getFrom()).isEqualTo("491234567890"); } + @Test + void incomingMessageSticker() { + WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-sticker", WhatsappMessagesValue.class); + assertThat(change).isInstanceOf(WhatsappMessagesValue.class); + + checkContact(change); + + checkMetaData(change); + + // check Message + assertThat(change.getMessages()).hasSize(1); + Message message = change.getMessages().get(0); + assertThat(message.getSticker()).isNotNull(); + assertThat(message.isImage()).isFalse(); + assertThat(message.isText()).isFalse(); + assertThat(message.isVideo()).isFalse(); + assertThat(message.isSticker()).isTrue(); + + Sticker image = message.getSticker(); + assertThat(image.getId()).isEqualTo("1122990445227715"); + assertThat(image.getMimeType()).isEqualTo("image\\/webp"); + assertThat(image.getSha256()).isEqualTo("RZEEl5HVWT4S6C0PoOgjYCQVDWs5esIMJsjcDYI80ZE="); + + assertThat(message.getTimestamp()).isEqualTo(new Date(1653253313000L)); + assertThat(message.getType()).isEqualTo(MessageType.sticker); + assertThat(message.getFrom()).isEqualTo("491234567890"); + } + private void checkMetaData(WhatsappMessagesValue change) { // check Metadata assertThat(change.getMetadata()).isNotNull(); diff --git a/src/test/resources/json/whatsapp/webhook-incoming-message-sticker.json b/src/test/resources/json/whatsapp/webhook-incoming-message-sticker.json new file mode 100644 index 000000000..0762fe64d --- /dev/null +++ b/src/test/resources/json/whatsapp/webhook-incoming-message-sticker.json @@ -0,0 +1,41 @@ +{ + "object": "whatsapp_business_account", + "entry": [ + { + "id": "101809759215303", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "1234567891", + "phone_number_id": "10634295353625" + }, + "contacts": [ + { + "profile": { + "name": "TestUser" + }, + "wa_id": "491234567890" + } + ], + "messages": [ + { + "from": "491234567890", + "id": "wamid.HBgNNDkxNTIwOTgzMzM5OBUCABIYHGDEHJVFHJEVCFHJEVBCMTkyODk1OEIyAA==", + "timestamp": "1653253313", + "type": "sticker", + "sticker": { + "mime_type": "image\\/webp", + "sha256": "RZEEl5HVWT4S6C0PoOgjYCQVDWs5esIMJsjcDYI80ZE=", + "id": "1122990445227715" + } + } + ] + }, + "field": "messages" + } + ] + } + ] +} \ No newline at end of file