Skip to content

Commit

Permalink
Merge pull request #74 from Bindambc/72-add-support-for-responding-to…
Browse files Browse the repository at this point in the history
…-whatsapp-messages-with-reactions

Added support for responding to messages with reactions (using emojis)
  • Loading branch information
Bindambc authored Mar 28, 2023
2 parents de98ef2 + 98fccd1 commit dd8b450
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class Message {
@JsonProperty("video")
private VideoMessage videoMessage;

@JsonProperty("reaction")
private ReactionMessage reactionMessage;


private Message() {
}
Expand Down Expand Up @@ -238,6 +241,20 @@ public Message buildVideoMessage(VideoMessage videoMessage) {

}

/**
* Build reaction message
*
* @param reactionMessage the reaction message
* @return the message
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#location-object">API documentation</a>
*/
public Message buildReactionMessage(ReactionMessage reactionMessage) {
var message = new Message(to, MessageType.REACTION);
message.reactionMessage = reactionMessage;
return message;

}


}

Expand Down
108 changes: 108 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/ReactionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.whatsapp.api.domain.messages;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The type Reaction message.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ReactionMessage {
@JsonProperty("message_id")
private String messageId;
@JsonProperty("emoji")
private String emoji;

/**
* Instantiates a new Reaction message.
*
* @param messageId <b>required</b>. The WhatsApp Message ID (wamid) of the message on which the reaction should appear. The reaction will not be sent if:
* <ul>
* <li>
* The message is older than 30 days
* </li>
* <li>
* The message is a reaction message
* </li>
* <li>The message has been deleted</li>
* </ul>
* If the ID is of a message that has been deleted, the message will not be delivered.
* @param emoji <b>required</b>. Emoji to appear on the message.
* <ul>
* <li>All emojis supported by Android and iOS devices are supported.</li>
* <li>Rendered-emojis are supported.</li>
* <li>If using emoji unicode values, values must be Java- or JavaScript-escape encoded.</li>
* <li>Only one emoji can be sent in a reaction message</li>
* <li>Use an empty string to remove a previously sent emoji.</li>
*
* </ul>
*/
public ReactionMessage(String messageId, String emoji) {
this.messageId = messageId;
this.emoji = emoji;
}

/**
* Instantiates a new Reaction message.
*/
public ReactionMessage() {
}

/**
* Gets message id.
*
* @return the message id
*/
public String getMessageId() {
return messageId;
}

/**
* Sets message id.
*
* @param messageId <b>required</b>. The WhatsApp Message ID (wamid) of the message on which the reaction should appear. The reaction will not be sent if:
* <ul>
* <li>
* The message is older than 30 days
* </li>
* <li>
* The message is a reaction message
* </li>
* <li>The message has been deleted</li>
* </ul>
* If the ID is of a message that has been deleted, the message will not be delivered.
* @return ReactionMessage
*/
public ReactionMessage setMessageId(String messageId) {
this.messageId = messageId;
return this;
}

/**
* Gets emoji.
*
* @return the emoji
*/
public String getEmoji() {
return emoji;
}

/**
* Sets emoji.
*
* @param emoji <b>required</b>. Emoji to appear on the message.
* <ul>
* <li>All emojis supported by Android and iOS devices are supported.</li>
* <li>Rendered-emojis are supported.</li>
* <li>If using emoji unicode values, values must be Java- or JavaScript-escape encoded.</li>
* <li>Only one emoji can be sent in a reaction message</li>
* <li>Use an empty string to remove a previously sent emoji.</li>
*
* </ul>
* @return ReactionMessage
*/
public ReactionMessage setEmoji(String emoji) {
this.emoji = emoji;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.whatsapp.api.examples;

import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.ReactionMessage;
import com.whatsapp.api.domain.messages.response.MessageResponse;
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;

import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1;
import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID;
import static com.whatsapp.api.TestConstants.TOKEN;


public class SendIReactionMessageExample {

public static void main(String[] args) {

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);

WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
var emojiThumbsUp = "\uD83D\uDC4D";
var reactionMessage = new ReactionMessage()//
.setMessageId("wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==")//
.setEmoji(emojiThumbsUp);//

var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildReactionMessage(reactionMessage);

MessageResponse messageResponse = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);

System.out.println(messageResponse);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.whatsapp.api.domain.messages.Name;
import com.whatsapp.api.domain.messages.Org;
import com.whatsapp.api.domain.messages.Phone;
import com.whatsapp.api.domain.messages.ReactionMessage;
import com.whatsapp.api.domain.messages.Reply;
import com.whatsapp.api.domain.messages.Row;
import com.whatsapp.api.domain.messages.Section;
Expand Down Expand Up @@ -823,6 +824,74 @@ void testSendInteractiveMessageWithList() throws InterruptedException, JSONExcep

}

@Test
void testSendReactionMessage() throws InterruptedException, JSONException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE));
var emojiThumbsUp = "\uD83D\uDC4D";
var expectedJson = """
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "121212121212",
"type": "reaction",
"reaction": {
"message_id": "wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==",
"emoji": "\\uD83D\\uDC4D"
}
}
""";

var reactionMessage = new ReactionMessage()//
.setMessageId("wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==")//
.setEmoji(emojiThumbsUp);//

var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildReactionMessage(reactionMessage);

var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
Assertions.assertNotNull(response);
RecordedRequest recordedRequest = mockWebServer.takeRequest();
Assertions.assertEquals("POST", recordedRequest.getMethod());
Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath());

JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT);

}

@Test
void testSendReactionMessage2() throws InterruptedException, JSONException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(DEFAULT_SEND_MESSAGE_RESPONSE));
var emojiThumbsUp = "\uD83D\uDC4D";
var expectedJson = """
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "121212121212",
"type": "reaction",
"reaction": {
"message_id": "wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==",
"emoji": "\\uD83D\\uDC4D"
}
}
""";

var reactionMessage = new ReactionMessage("wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==", emojiThumbsUp);

var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildReactionMessage(reactionMessage);

var response = whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
Assertions.assertNotNull(response);
RecordedRequest recordedRequest = mockWebServer.takeRequest();
Assertions.assertEquals("POST", recordedRequest.getMethod());
Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath());

JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT);

}

@Test
void testUploadMedia() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/uploadResponse.json")));
Expand Down

0 comments on commit dd8b450

Please sign in to comment.