From f49500033a25eb63b4ff93bec81dc74c6ca99a76 Mon Sep 17 00:00:00 2001 From: Norbert Bartels Date: Mon, 4 Jul 2022 17:33:49 +0200 Subject: [PATCH] Issue #1238 - incoming document message --- .../types/whatsapp/platform/Message.java | 7 +- .../whatsapp/platform/message/Document.java | 70 +++++++++++++++++++ .../com/restfb/types/WABPwebhookTest.java | 35 ++++++++++ .../webhook-incoming-message-document.json | 43 ++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/main/lombok/com/restfb/types/whatsapp/platform/message/Document.java create mode 100644 src/test/resources/json/whatsapp/webhook-incoming-message-document.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 fb0e0c712..6c2d80f51 100644 --- a/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java @@ -50,7 +50,10 @@ public class Message extends AbstractFacebookType { private JsonObject context; - private JsonObject document; + @Getter + @Setter + @Facebook + private Document document; private List errors = new ArrayList<>(); @@ -142,6 +145,8 @@ public boolean isButton() { return button != null; } + public boolean isDocument() { return document != null; } + public boolean hasReferral() { return referral != null; } diff --git a/src/main/lombok/com/restfb/types/whatsapp/platform/message/Document.java b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Document.java new file mode 100644 index 000000000..c74d38247 --- /dev/null +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Document.java @@ -0,0 +1,70 @@ +/* + * 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 Document extends AbstractFacebookType { + + /** + * ID for the document + */ + @Getter + @Setter + @Facebook + private String id; + + /** + * Mime type for the document + */ + @Getter + @Setter + @Facebook("mime_type") + private String mimeType; + + /** + * Document hash + */ + @Getter + @Setter + @Facebook + private String sha256; + + /** + * Caption for the document, if provided + */ + @Getter + @Setter + @Facebook + private String caption; + + /** + * Name for the file on the sender's device + */ + @Getter + @Setter + @Facebook + private String filename; +} diff --git a/src/test/java/com/restfb/types/WABPwebhookTest.java b/src/test/java/com/restfb/types/WABPwebhookTest.java index ddacf04a0..ed15b65a1 100644 --- a/src/test/java/com/restfb/types/WABPwebhookTest.java +++ b/src/test/java/com/restfb/types/WABPwebhookTest.java @@ -239,6 +239,41 @@ void incomingMessageButton() { assertThat(message.getFrom()).isEqualTo("491234567890"); } + @Test + void incomingMessageDocument() { + WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-document", 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.getDocument()).isNotNull(); + assertThat(message.isImage()).isFalse(); + assertThat(message.isText()).isFalse(); + assertThat(message.isVideo()).isFalse(); + assertThat(message.isSticker()).isFalse(); + assertThat(message.isLocation()).isFalse(); + assertThat(message.isAudio()).isFalse(); + assertThat(message.isVoice()).isFalse(); + assertThat(message.isButton()).isFalse(); + assertThat(message.isDocument()).isTrue(); + + Document image = message.getDocument(); + assertThat(image.getId()).isEqualTo("4937353526352"); + assertThat(image.getMimeType()).isEqualTo("application\\/zip"); + assertThat(image.getSha256()).isEqualTo("FzonbuoHILCEbNUvfvddy77Ef\\/vLBuZvfrM8W2kd4srXrk="); + assertThat(image.getFilename()).isEqualTo("RESTFB ANNIVERSARY.zip"); + assertThat(image.getCaption()).isEqualTo("RESTFB ANNIVERSARY"); + + assertThat(message.getTimestamp()).isEqualTo(new Date(1653253313000L)); + assertThat(message.getType()).isEqualTo(MessageType.document); + assertThat(message.getFrom()).isEqualTo("491234567890"); + } + @Test void incomingMessageAds() { WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-ads", WhatsappMessagesValue.class); diff --git a/src/test/resources/json/whatsapp/webhook-incoming-message-document.json b/src/test/resources/json/whatsapp/webhook-incoming-message-document.json new file mode 100644 index 000000000..9220d9ea2 --- /dev/null +++ b/src/test/resources/json/whatsapp/webhook-incoming-message-document.json @@ -0,0 +1,43 @@ +{ + "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": "document", + "document": { + "caption": "RESTFB ANNIVERSARY", + "filename": "RESTFB ANNIVERSARY.zip", + "mime_type": "application\\/zip", + "sha256": "FzonbuoHILCEbNUvfvddy77Ef\\/vLBuZvfrM8W2kd4srXrk=", + "id": "4937353526352" + } + } + ] + }, + "field": "messages" + } + ] + } + ] +} \ No newline at end of file