From 3e4aea775c6fa3d0aa9f804e0ce2ac9a87c3b1b1 Mon Sep 17 00:00:00 2001 From: Norbert Bartels Date: Sat, 2 Jul 2022 18:02:27 +0200 Subject: [PATCH] Issue #1216 - imcoming wabp image message --- .../types/whatsapp/platform/Message.java | 8 ++- .../whatsapp/platform/message/Image.java | 62 +++++++++++++++++++ .../com/restfb/types/WebhookWhatsappTest.java | 36 +++++++++++ .../webhook-incoming-message-image.json | 42 +++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/main/lombok/com/restfb/types/whatsapp/platform/message/Image.java create mode 100644 src/test/resources/json/whatsapp/webhook-incoming-message-image.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 ffbb66b45..018312935 100644 --- a/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java @@ -28,6 +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; @@ -61,7 +62,10 @@ public class Message extends AbstractFacebookType { private JsonObject identity; - private JsonObject image; + @Getter + @Setter + @Facebook + private Image image; private JsonObject interactive; @@ -89,4 +93,6 @@ public class Message extends AbstractFacebookType { public boolean isText() { return text != null; } + + public boolean isImage() { return image != null; } } diff --git a/src/main/lombok/com/restfb/types/whatsapp/platform/message/Image.java b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Image.java new file mode 100644 index 000000000..e9a7e83d1 --- /dev/null +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Image.java @@ -0,0 +1,62 @@ +/* + * 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 Image extends AbstractFacebookType { + + /** + * ID for the image + */ + @Getter + @Setter + @Facebook + private String id; + + /** + * Mime type for the image + */ + @Getter + @Setter + @Facebook("mime_type") + private String mimeType; + + /** + * Image hash + */ + @Getter + @Setter + @Facebook + private String sha256; + + /** + * Caption for the image, if provided + */ + @Getter + @Setter + @Facebook + private String caption; +} diff --git a/src/test/java/com/restfb/types/WebhookWhatsappTest.java b/src/test/java/com/restfb/types/WebhookWhatsappTest.java index c93584362..e21475e11 100644 --- a/src/test/java/com/restfb/types/WebhookWhatsappTest.java +++ b/src/test/java/com/restfb/types/WebhookWhatsappTest.java @@ -34,6 +34,7 @@ import com.restfb.types.webhook.whatsapp.*; 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.MessageType; class WebhookWhatsappTest extends AbstractJsonMapperTests { @@ -129,6 +130,41 @@ void incomingMessageText() { assertThat(message.isText()).isTrue(); } + @Test + void incomingMessageImage() { + WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-image", WhatsappMessagesValue.class); + assertThat(change).isInstanceOf(WhatsappMessagesValue.class); + + // check contact + assertThat(change.getContacts()).hasSize(1); + Contact contact = change.getContacts().get(0); + assertThat(contact.getWaId()).isEqualTo("491234567890"); + assertThat(contact.getProfile()).isNotNull(); + assertThat(contact.getProfile().getName()).isEqualTo("TestUser"); + + // check Metadata + assertThat(change.getMetadata()).isNotNull(); + assertThat(change.getMetadata().getDisplayPhoneNumber()).isEqualTo("1234567891"); + assertThat(change.getMetadata().getPhoneNumberId()).isEqualTo("10634295353625"); + + // check Message + assertThat(change.getMessages()).hasSize(1); + Message message = change.getMessages().get(0); + assertThat(message.getImage()).isNotNull(); + assertThat(message.isImage()).isTrue(); + assertThat(message.isText()).isFalse(); + + Image image = message.getImage(); + assertThat(image.getCaption()).isEqualTo("Some awesome image"); + assertThat(image.getId()).isEqualTo("400962571939895"); + assertThat(image.getMimeType()).isEqualTo("image\\/jpeg"); + assertThat(image.getSha256()).isEqualTo("law0CgE277wMMnIJU0XIxhDne6Ptmwaek\\/thVM7mVtg="); + + assertThat(message.getTimestamp()).isEqualTo(new Date(1653253313000L)); + assertThat(message.getType()).isEqualTo(MessageType.image); + assertThat(message.getFrom()).isEqualTo("491234567890"); + } + private T getWHObjectFromJson(String jsonName, Class clazz) { WebhookObject webhookObject = createJsonMapper().toJavaObject(jsonFromClasspath("whatsapp/" + jsonName), WebhookObject.class); diff --git a/src/test/resources/json/whatsapp/webhook-incoming-message-image.json b/src/test/resources/json/whatsapp/webhook-incoming-message-image.json new file mode 100644 index 000000000..95d99c989 --- /dev/null +++ b/src/test/resources/json/whatsapp/webhook-incoming-message-image.json @@ -0,0 +1,42 @@ +{ + "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": "image", + "image": { + "caption": "Some awesome image", + "mime_type": "image\\/jpeg", + "sha256": "law0CgE277wMMnIJU0XIxhDne6Ptmwaek\\/thVM7mVtg=", + "id": "400962571939895" + } + } + ] + }, + "field": "messages" + } + ] + } + ] +} \ No newline at end of file