Skip to content

Commit

Permalink
Issue #1238 - incoming document message
Browse files Browse the repository at this point in the history
  • Loading branch information
nbartels committed Jul 4, 2022
1 parent fe8fef3 commit f495000
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
Expand Up @@ -50,7 +50,10 @@ public class Message extends AbstractFacebookType {

private JsonObject context;

private JsonObject document;
@Getter
@Setter
@Facebook
private Document document;

private List<JsonObject> errors = new ArrayList<>();

Expand Down Expand Up @@ -142,6 +145,8 @@ public boolean isButton() {
return button != null;
}

public boolean isDocument() { return document != null; }

public boolean hasReferral() {
return referral != null;
}
Expand Down
@@ -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;
}
35 changes: 35 additions & 0 deletions src/test/java/com/restfb/types/WABPwebhookTest.java
Expand Up @@ -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);
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",
"type": "document",
"document": {
"caption": "RESTFB ANNIVERSARY",
"filename": "RESTFB ANNIVERSARY.zip",
"mime_type": "application\\/zip",
"sha256": "FzonbuoHILCEbNUvfvddy77Ef\\/vLBuZvfrM8W2kd4srXrk=",
"id": "4937353526352"
}
}
]
},
"field": "messages"
}
]
}
]
}

0 comments on commit f495000

Please sign in to comment.