Skip to content

Commit

Permalink
Issue #1182 - Product template added to messaging webhook object
Browse files Browse the repository at this point in the history
  • Loading branch information
nbartels committed Nov 12, 2021
1 parent 0ab4451 commit 760472b
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 2 deletions.
Expand Up @@ -61,6 +61,11 @@ public class MessagingAttachment {
*/
public static final String VIDEO = "video";

/**
* The "template" attachment type.
*/
public static final String TEMPLATE = "template";

@Getter
@Setter
@Facebook
Expand Down Expand Up @@ -145,4 +150,13 @@ public boolean isLocation() {
public boolean isVideo() {
return VIDEO.equals(type);
}

/**
* convenience method to check if the attachment type is template
*
* @return {@code true} if template, {@code false} if not template
*/
public boolean isTemplate() {
return TEMPLATE.equals(type);
}
}
Expand Up @@ -183,6 +183,11 @@ public class MessagingPayload {
@Facebook("boarding_pass")
private List<BoardingPassItem> boardingPassItems;

@Getter
@Setter
@Facebook
private ProductTemplateItem product;

@Getter
@Setter
private String fallback;
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2010-2021 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.webhook.messaging;

import com.restfb.Facebook;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class ProductItem {

@Getter
@Setter
@Facebook
private String id;

@Getter
@Setter
@Facebook("retailer_id")
private String retailerId;

@Getter
@Setter
@Facebook("image_url")
private String imageUrl;

@Getter
@Setter
@Facebook
private String title;

@Getter
@Setter
@Facebook
private String subtitle;
}
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010-2021 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.webhook.messaging;

import java.util.ArrayList;
import java.util.List;

import com.restfb.Facebook;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class ProductTemplateItem {

@Getter
@Setter
@Facebook
private List<ProductItem> elements = new ArrayList<>();
}
46 changes: 44 additions & 2 deletions src/test/java/com/restfb/types/WebhookMessagingTest.java
Expand Up @@ -67,8 +67,10 @@ public void delivery(DeliveryItem delivery, MessagingParticipant recipient, Mess
assertEquals("1458668856253", delivery.getWatermark());
foundDeprecated.set(true);
}

@Override
public void delivery(DeliveryItem delivery, MessagingParticipant recipient, MessagingParticipant sender, Date timestamp) {
public void delivery(DeliveryItem delivery, MessagingParticipant recipient, MessagingParticipant sender,
Date timestamp) {
assertNotNull(delivery);
assertEquals("1458668856253", delivery.getWatermark());
found.set(true);
Expand Down Expand Up @@ -571,7 +573,7 @@ void messagingPostbackWithReferral() {
@Test
void messagingPostbackFromChat() {
WebhookObject webhookObject =
createJsonMapper().toJavaObject(jsonFromClasspath("webhooks/messaging-postback-chat"), WebhookObject.class);
createJsonMapper().toJavaObject(jsonFromClasspath("webhooks/messaging-postback-chat"), WebhookObject.class);
assertThat(webhookObject).isNotNull();
assertThat(webhookObject.getEntryList()).isNotEmpty();
WebhookEntry entry = webhookObject.getEntryList().get(0);
Expand Down Expand Up @@ -786,4 +788,44 @@ public void requestThreadControl(RequestThreadControlItem requestThreadControl,
webhookListener.process(webhookObject);
assertTrue(found.get());
}

@Test
void productTemplate() {
WebhookObject webhookObject = createJsonMapper()
.toJavaObject(jsonFromClasspath("webhooks/messaging-message-product-template"), WebhookObject.class);
assertNotNull(webhookObject);
ProductItem productItem = extractProductItem(webhookObject);
assertThat(productItem.getId()).isEqualTo("<PRODUCT_ID>");
assertThat(productItem.getImageUrl()).endsWith("sdsd");
assertThat(productItem.getTitle()).isEqualTo("Some product title");
assertThat(productItem.getRetailerId()).isEqualTo("<EXTERNAL_ID>");
assertThat(productItem.getSubtitle()).isEqualTo("$40");
}

@Test
void productTemplateLive() {
WebhookObject webhookObject = createJsonMapper()
.toJavaObject(jsonFromClasspath("webhooks/messaging-message-product-template-2"), WebhookObject.class);
assertNotNull(webhookObject);
ProductItem productItem = extractProductItem(webhookObject);
assertThat(productItem.getId()).isEqualTo("12345678910");
assertThat(productItem.getImageUrl()).contains("andSoOn.png");
assertThat(productItem.getTitle()).isEqualTo("product name");
assertThat(productItem.getRetailerId()).isEqualTo("123456");
assertThat(productItem.getSubtitle()).isEqualTo("product price as a string");
}

private ProductItem extractProductItem(WebhookObject webhookObject) {
MessagingItem item = webhookObject.getEntryList().get(0).getMessaging().get(0);
assertNotNull(item);
assertTrue(item.getMessage().hasAttachment());
MessagingAttachment messagingAttachment = item.getMessage().getAttachments().get(0);
assertNotNull(messagingAttachment);
assertTrue(messagingAttachment.isTemplate());
final ProductTemplateItem product = messagingAttachment.getPayload().getProduct();
assertFalse(product.getElements().isEmpty());
ProductItem productItem = product.getElements().get(0);
assertNotNull(productItem);
return productItem;
}
}
@@ -0,0 +1,42 @@
{
"object": "page",
"entry": [
{
"id": "page id",
"time": 1636729432317,
"messaging": [
{
"sender": {
"id": "send id"
},
"recipient": {
"id": "page id"
},
"timestamp": 1636729431949,
"message": {
"mid": "m_id",
"text": "text sent along with the product",
"attachments": [
{
"type": "template",
"payload": {
"product": {
"elements": [
{
"id": "12345678910",
"retailer_id": "123456",
"image_url": "https://scontent.xx.fbcdn.net/v/andSoOn.png",
"title": "product name",
"subtitle": "product price as a string"
}
]
}
}
}
]
}
}
]
}
]
}
@@ -0,0 +1,41 @@
{
"object": "page",
"entry": [
{
"id": "682498302938465",
"time": 1518479195594,
"messaging": [
{
"sender": {
"id": "<PSID>"
},
"recipient": {
"id": "<PAGE_ID>"
},
"timestamp": 1518479195308,
"message": {
"mid": "mid.$cAAJdkrCd2ORnva8ErFhjGm0X_Q_c",
"attachments": [
{
"type": "template",
"payload": {
"product": {
"elements": [
{
"id": "<PRODUCT_ID>",
"retailer_id": "<EXTERNAL_ID>",
"image_url": "https://fb.cdn.com/sdsd",
"title": "Some product title",
"subtitle": "$40"
}
]
}
}
}
]
}
}
]
}
]
}

0 comments on commit 760472b

Please sign in to comment.