Skip to content

Commit

Permalink
Issue #1216 - imcoming wabp image message
Browse files Browse the repository at this point in the history
  • Loading branch information
nbartels committed Jul 2, 2022
1 parent 8d45064 commit 3e4aea7
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
Expand Up @@ -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;

Expand Down Expand Up @@ -61,7 +62,10 @@ public class Message extends AbstractFacebookType {

private JsonObject identity;

private JsonObject image;
@Getter
@Setter
@Facebook
private Image image;

private JsonObject interactive;

Expand Down Expand Up @@ -89,4 +93,6 @@ public class Message extends AbstractFacebookType {
public boolean isText() {
return text != null;
}

public boolean isImage() { return image != null; }
}
@@ -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;
}
36 changes: 36 additions & 0 deletions src/test/java/com/restfb/types/WebhookWhatsappTest.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 extends ChangeValue> T getWHObjectFromJson(String jsonName, Class<T> clazz) {
WebhookObject webhookObject =
createJsonMapper().toJavaObject(jsonFromClasspath("whatsapp/" + jsonName), WebhookObject.class);
Expand Down
@@ -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"
}
]
}
]
}

0 comments on commit 3e4aea7

Please sign in to comment.