Skip to content

Commit

Permalink
Issue #1234 - Interactive object added to incoming WABP webhook message
Browse files Browse the repository at this point in the history
  • Loading branch information
nbartels committed Jul 27, 2022
1 parent 2770d2c commit 0a3a8a5
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 1 deletion.
Expand Up @@ -75,7 +75,10 @@ public class Message extends AbstractFacebookType {
@Facebook
private Image image;

private JsonObject interactive;
@Getter
@Setter
@Facebook
private Interactive interactive;

@Getter
@Setter
Expand Down Expand Up @@ -160,4 +163,8 @@ public boolean hasReferral() {
public boolean isSystem() {
return system != null;
}

public boolean isInteractive() {
return interactive != null;
}
}
@@ -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
}
}
70 changes: 70 additions & 0 deletions src/test/java/com/restfb/types/WABPwebhookTest.java
Expand Up @@ -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();
Expand Down
@@ -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"
}
]
}
]
}
@@ -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"
}
]
}
]
}

0 comments on commit 0a3a8a5

Please sign in to comment.