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 97026d8ab..c28888070 100644 --- a/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/Message.java @@ -75,7 +75,10 @@ public class Message extends AbstractFacebookType { @Facebook private Image image; - private JsonObject interactive; + @Getter + @Setter + @Facebook + private Interactive interactive; @Getter @Setter @@ -160,4 +163,8 @@ public boolean hasReferral() { public boolean isSystem() { return system != null; } + + public boolean isInteractive() { + return interactive != null; + } } diff --git a/src/main/lombok/com/restfb/types/whatsapp/platform/message/Interactive.java b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Interactive.java new file mode 100644 index 000000000..d6bba0283 --- /dev/null +++ b/src/main/lombok/com/restfb/types/whatsapp/platform/message/Interactive.java @@ -0,0 +1,88 @@ +/* + * 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 Interactive extends AbstractFacebookType { + + @Getter + @Setter + @Facebook("list_reply") + private Reply listReply; + + @Getter + @Setter + @Facebook("button_reply") + private Reply buttonReply; + + @Getter + @Setter + @Facebook + private Type type; + + public Reply getReply() { + if (buttonReply != null) { + return buttonReply; + } + + if (listReply != null) { + return listReply; + } + + return null; + } + + public boolean isButtonReply() { + return buttonReply != null; + } + + public boolean isListReply() { + return listReply != null; + } + + public static class Reply extends AbstractFacebookType { + + @Getter + @Setter + @Facebook + private String id; + + @Getter + @Setter + @Facebook + private String title; + + @Getter + @Setter + @Facebook + private String description; + } + + public enum Type { + list_reply, button_reply + } +} diff --git a/src/test/java/com/restfb/types/WABPwebhookTest.java b/src/test/java/com/restfb/types/WABPwebhookTest.java index a6d915724..6088403ed 100644 --- a/src/test/java/com/restfb/types/WABPwebhookTest.java +++ b/src/test/java/com/restfb/types/WABPwebhookTest.java @@ -336,6 +336,76 @@ void incomingMessageSystem() { assertThat(message.getFrom()).isEqualTo("491234567890"); } + @Test + void incomingMessageInteractiveButton() { + WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-interactive-btn", 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.getInteractive()).isNotNull(); + assertThat(message.isInteractive()).isTrue(); + + Interactive image = message.getInteractive(); + assertThat(image.isListReply()).isFalse(); + assertThat(image.isButtonReply()).isTrue(); + + assertThat(image.getReply()).isNotNull(); + assertThat(image.getButtonReply()).isNotNull(); + assertThat(image.getListReply()).isNull(); + + assertThat(image.getType()).isEqualTo(Interactive.Type.button_reply); + + Interactive.Reply reply = image.getReply(); + assertThat(reply.getDescription()).isNull(); + assertThat(reply.getId()).isEqualTo("unique-button-identifier-here"); + assertThat(reply.getTitle()).isEqualTo("button-text"); + + assertThat(message.getTimestamp()).isEqualTo(new Date(1653253313000L)); + assertThat(message.getType()).isEqualTo(MessageType.interactive); + assertThat(message.getFrom()).isEqualTo("491234567890"); + } + + @Test + void incomingMessageInteractiveList() { + WhatsappMessagesValue change = getWHObjectFromJson("webhook-incoming-message-interactive-list", 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.getInteractive()).isNotNull(); + assertThat(message.isInteractive()).isTrue(); + + Interactive image = message.getInteractive(); + assertThat(image.isListReply()).isTrue(); + assertThat(image.isButtonReply()).isFalse(); + + assertThat(image.getReply()).isNotNull(); + assertThat(image.getButtonReply()).isNull(); + assertThat(image.getListReply()).isNotNull(); + + assertThat(image.getType()).isEqualTo(Interactive.Type.list_reply); + + Interactive.Reply reply = image.getReply(); + assertThat(reply.getDescription()).isEqualTo("list_reply_description"); + assertThat(reply.getId()).isEqualTo("list_reply_id"); + assertThat(reply.getTitle()).isEqualTo("list_reply_title"); + + assertThat(message.getTimestamp()).isEqualTo(new Date(1653253313000L)); + assertThat(message.getType()).isEqualTo(MessageType.interactive); + 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-interactive-btn.json b/src/test/resources/json/whatsapp/webhook-incoming-message-interactive-btn.json new file mode 100644 index 000000000..1735bd91b --- /dev/null +++ b/src/test/resources/json/whatsapp/webhook-incoming-message-interactive-btn.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", + "interactive": { + "button_reply": { + "id": "unique-button-identifier-here", + "title": "button-text" + }, + "type": "button_reply" + }, + "type": "interactive" + } + ] + }, + "field": "messages" + } + ] + } + ] +} \ No newline at end of file diff --git a/src/test/resources/json/whatsapp/webhook-incoming-message-interactive-list.json b/src/test/resources/json/whatsapp/webhook-incoming-message-interactive-list.json new file mode 100644 index 000000000..d856171f3 --- /dev/null +++ b/src/test/resources/json/whatsapp/webhook-incoming-message-interactive-list.json @@ -0,0 +1,44 @@ +{ + "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", + "interactive": { + "list_reply": { + "id": "list_reply_id", + "title": "list_reply_title", + "description": "list_reply_description" + }, + "type": "list_reply" + }, + "type": "interactive" + } + ] + }, + "field": "messages" + } + ] + } + ] +} \ No newline at end of file