Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to send a message as a reply of another #196

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.whatsapp.api.domain.messages;

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

/**
* Object that allow messages to be replied
*
*
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#replies">Replies</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Context {

@JsonProperty("message_id")
private String messageId;

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

/**
* Sets messageId.
*/
public void setMessageId(String messageId) {
this.messageId = messageId;
}
}
48 changes: 36 additions & 12 deletions src/main/java/com/whatsapp/api/domain/messages/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Message {
private final String messagingProduct = "whatsapp";
@JsonProperty("recipient_type")
private final String recipientType = "individual";
@JsonProperty("context")
private Context context;
/**
* The Interactive message.
*/
Expand Down Expand Up @@ -58,9 +60,10 @@ public class Message {
private Message() {
}

private Message(String to, MessageType type) {
private Message(String to, MessageType type, Context context) {
this.to = to;
this.type = type;
this.context = context;
}

/**
Expand All @@ -81,12 +84,22 @@ public String getRecipientType() {
return recipientType;
}

/**
* Gets context.
*
* @return the context
*/
public Context getContext() {
return context;
}

/**
* The type Message builder.
*/
public static class MessageBuilder {

private String to;
private Context context;


private MessageBuilder() {
Expand All @@ -112,6 +125,17 @@ public MessageBuilder setTo(String to) {
return this;
}

/**
* Sets context.
*
* @param context. the messsage's context.
* @return the context
*/
public MessageBuilder setContext(Context context) {
this.context = context;
return this;
}

/**
* Build a text objetc, with:
* <ul>
Expand All @@ -123,7 +147,7 @@ public MessageBuilder setTo(String to) {
* @return the message
*/
public Message buildTextMessage(TextMessage textMessage) {
var message = new Message(to, MessageType.TEXT);
var message = new Message(to, MessageType.TEXT, context);
message.textMessage = textMessage;
return message;

Expand All @@ -139,7 +163,7 @@ public Message buildTextMessage(TextMessage textMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#contacts-object">API documentation</a>
*/
public Message buildContactMessage(ContactMessage contactMessage) {
var message = new Message(to, MessageType.CONTACTS);
var message = new Message(to, MessageType.CONTACTS, context);
message.contactMessage = contactMessage.getContacts();
return message;

Expand All @@ -153,7 +177,7 @@ public Message buildContactMessage(ContactMessage contactMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#template-object">API documentation</a>
*/
public Message buildTemplateMessage(TemplateMessage templateMessage) {
var message = new Message(to, MessageType.TEMPLATE);
var message = new Message(to, MessageType.TEMPLATE, context);
message.templateMessage = templateMessage;
return message;

Expand All @@ -167,7 +191,7 @@ public Message buildTemplateMessage(TemplateMessage templateMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#interactive-object">API documentation</a>
*/
public Message buildInteractiveMessage(InteractiveMessage interactiveMessage) {
var message = new Message(to, MessageType.INTERACTIVE);
var message = new Message(to, MessageType.INTERACTIVE, context);
message.interactiveMessage = interactiveMessage;
return message;

Expand All @@ -182,7 +206,7 @@ public Message buildInteractiveMessage(InteractiveMessage interactiveMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#media-object">API documentation</a>
*/
public Message buildAudioMessage(AudioMessage audioMessage) {
var message = new Message(to, MessageType.AUDIO);
var message = new Message(to, MessageType.AUDIO, context);
message.audioMessage = audioMessage;
return message;

Expand All @@ -196,7 +220,7 @@ public Message buildAudioMessage(AudioMessage audioMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#media-object">API documentation</a>
*/
public Message buildDocumentMessage(DocumentMessage documentMessage) {
var message = new Message(to, MessageType.DOCUMENT);
var message = new Message(to, MessageType.DOCUMENT, context);
message.documentMessage = documentMessage;
return message;

Expand All @@ -210,7 +234,7 @@ public Message buildDocumentMessage(DocumentMessage documentMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#media-object">API documentation</a>
*/
public Message buildImageMessage(ImageMessage imageMessage) {
var message = new Message(to, MessageType.IMAGE);
var message = new Message(to, MessageType.IMAGE, context);
message.imageMessage = imageMessage;
return message;

Expand All @@ -224,7 +248,7 @@ public Message buildImageMessage(ImageMessage imageMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#media-object">API documentation</a>
*/
public Message buildStickerMessage(StickerMessage stickerMessage) {
var message = new Message(to, MessageType.STICKER);
var message = new Message(to, MessageType.STICKER, context);
message.stickerMessage = stickerMessage;
return message;

Expand All @@ -238,7 +262,7 @@ public Message buildStickerMessage(StickerMessage stickerMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#media-object">API documentation</a>
*/
public Message buildVideoMessage(VideoMessage videoMessage) {
var message = new Message(to, MessageType.VIDEO);
var message = new Message(to, MessageType.VIDEO, context);
message.videoMessage = videoMessage;
return message;

Expand All @@ -252,7 +276,7 @@ public Message buildVideoMessage(VideoMessage videoMessage) {
* @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);
var message = new Message(to, MessageType.REACTION, context);
message.reactionMessage = reactionMessage;
return message;

Expand All @@ -266,7 +290,7 @@ public Message buildReactionMessage(ReactionMessage reactionMessage) {
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#location-messages">API documentation</a>
*/
public Message buildLocationMessage(LocationMessage locationMessage) {
var message = new Message(to, MessageType.LOCATION);
var message = new Message(to, MessageType.LOCATION, context);
message.locationMessage = locationMessage;
return message;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

public class MessageTest {

@Test
public void testMessageBuilderWithContext() {
String messageId = "wamid.HBgNNTUyNzk5NzAzMDkzNhUCABIYFDNBRjE2OTUyOTNCNTlCM0IzRDQ0AA==";
Context context = new Context();
context.setMessageId(messageId);

Message message = Message.MessageBuilder.builder()
.setTo("123456789")
.setContext(context)
.buildTextMessage(new TextMessage());

assertNotNull(message.getContext(), "Context should not be null");
assertEquals(messageId, message.getContext().getMessageId(), "Context field value mismatch");
}

@Test
public void testMessageWithoutContext() {
Message message = Message.MessageBuilder.builder()
.setTo("123456789")
.buildTextMessage(new TextMessage());

assertEquals(null, message.getContext(), "Context should be null");
}
}
Loading