Skip to content

Commit 9ad5f58

Browse files
committed
feat: Add contextMessageUuid to WhatsappRequest
1 parent 7a3a45f commit 9ad5f58

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
# [8.7.0] - 2024-05-??
88
- Added missing supported languages to `TextToSpeechLanguage` enum
99
- Added `ttl` field to outbound MMS messages
10-
-
10+
- Added message reply context to Whatsapp outbound requests
1111

1212
# [8.6.0] - 2024-04-18
1313
- Added Experience Composer to Video API

src/main/java/com/vonage/client/messages/whatsapp/Context.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public final class Context extends JsonableBaseObject {
3232

3333
Context() {}
3434

35+
Context(UUID messageUuid) {
36+
this.messageUuid = messageUuid;
37+
}
38+
3539
/**
3640
* The phone number of the original sender of the message being quoted.
3741
*

src/main/java/com/vonage/client/messages/whatsapp/WhatsappRequest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
*/
1616
package com.vonage.client.messages.whatsapp;
1717

18+
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import com.vonage.client.common.E164;
1820
import com.vonage.client.messages.Channel;
1921
import com.vonage.client.messages.MessageRequest;
2022
import com.vonage.client.messages.MessageType;
21-
import com.vonage.client.common.E164;
23+
import java.util.UUID;
2224

2325
public abstract class WhatsappRequest extends MessageRequest {
26+
final Context context;
2427

2528
protected WhatsappRequest(Builder<?, ?> builder, MessageType messageType) {
2629
super(builder, Channel.WHATSAPP, messageType);
30+
context = builder.messageUuid != null ? new Context(builder.messageUuid) : null;
2731
}
2832

2933
@Override
@@ -32,6 +36,29 @@ protected void validateSenderAndRecipient(String from, String to) throws Illegal
3236
this.to = new E164(to).toString();
3337
}
3438

39+
@JsonProperty("context")
40+
public Context getContext() {
41+
return context;
42+
}
43+
44+
@SuppressWarnings("unchecked")
3545
protected abstract static class Builder<M extends WhatsappRequest, B extends Builder<? extends M, ? extends B>> extends MessageRequest.Builder<M, B> {
46+
UUID messageUuid;
47+
48+
/**
49+
* An optional context used for quoting/replying to a specific message in a conversation. When used,
50+
* the WhatsApp UI will display the new message along with a contextual bubble that displays the
51+
* quoted/replied to message's content.<br>
52+
* This field is the UUID of the message being replied to or quoted.
53+
*
54+
* @param messageUuid The context's message UUID as a string.
55+
*
56+
* @return This builder.
57+
* @since 8.7.0
58+
*/
59+
public B contextMessageId(String messageUuid) {
60+
this.messageUuid = UUID.fromString(messageUuid);
61+
return (B) this;
62+
}
3663
}
3764
}

src/test/java/com/vonage/client/messages/whatsapp/WhatsappFileRequestTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,31 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import static org.junit.jupiter.api.Assertions.*;
20+
import java.util.UUID;
2021

2122
public class WhatsappFileRequestTest {
23+
String from = "447900000001", to = "317900000002";
2224

2325
@Test
2426
public void testSerializeValid() {
27+
String messageUuid = UUID.randomUUID().toString();
2528
String url = "file:///path/to/attachment.zip", caption = "Srs bzns", name = "Stuff";
2629
String json = WhatsappFileRequest.builder()
27-
.from("317900000002").to("447900000001")
28-
.url(url).caption(caption).name(name)
29-
.build().toJson();
30+
.from(from).to(to).url(url).contextMessageId(messageUuid)
31+
.caption(caption).name(name).build().toJson();
32+
3033
assertTrue(json.contains(
31-
"\"file\":{\"url\":\""+url+ "\",\"caption\":\""+caption+"\",\"name\":\""+name+"\"}"
34+
"\"file\":{\"url\":\""+url+"\",\"caption\":\"" + caption + "\",\"name\":\""+name+"\"}"
3235
));
36+
assertTrue(json.contains("\"context\":{\"message_uuid\":\""+messageUuid+"\"}"));
3337
assertTrue(json.contains("\"message_type\":\"file\""));
3438
assertTrue(json.contains("\"channel\":\"whatsapp\""));
3539
}
3640

3741
@Test
3842
public void testSerializeNoCaptionOrName() {
3943
String url = "file:///path/to/spec.pdf";
40-
WhatsappFileRequest req = WhatsappFileRequest.builder()
41-
.url(url).from("447900000002").to("447900000001").build();
44+
WhatsappFileRequest req = WhatsappFileRequest.builder().url(url).from(from).to(to).build();
4245
assertNull(req.getFile().getName());
4346
String json = req.toJson();
4447
assertTrue(json.contains("\"file\":{\"url\":\""+url+"\"}"));
@@ -49,7 +52,7 @@ public void testSerializeNoCaptionOrName() {
4952
@Test
5053
public void testConstructNoUrl() {
5154
assertThrows(NullPointerException.class, () -> WhatsappFileRequest.builder()
52-
.caption("Description").from("447900000001").to("317900000002").build()
55+
.caption("Description").from(from).to(to).build()
5356
);
5457
}
5558
}

src/test/java/com/vonage/client/messages/whatsapp/WhatsappImageRequestTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void testSerializeValid() {
3030
assertTrue(json.contains("\"image\":{\"url\":\""+url+"\",\"caption\":\""+caption+"\"}"));
3131
assertTrue(json.contains("\"message_type\":\"image\""));
3232
assertTrue(json.contains("\"channel\":\"whatsapp\""));
33+
assertFalse(json.contains("\"context\""));
3334
}
3435

3536
@Test

src/test/java/com/vonage/client/messages/whatsapp/WhatsappTextRequestTest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,63 @@
1515
*/
1616
package com.vonage.client.messages.whatsapp;
1717

18-
import org.junit.jupiter.api.Test;
19-
2018
import static org.junit.jupiter.api.Assertions.*;
19+
import org.junit.jupiter.api.*;
20+
import java.util.UUID;
2121

2222
public class WhatsappTextRequestTest {
23+
String from = "447900000001", to = "317900000002", txt = "Hello, World!";
2324

2425
@Test
2526
public void testSerializeValid() {
26-
String from = "447900000001", to = "317900000002", txt = "Hello, World!";
27-
String json = WhatsappTextRequest.builder().from(from).to(to).text(txt).build().toJson();
27+
String messageUuid = UUID.randomUUID().toString();
28+
String json = WhatsappTextRequest.builder().from(from).to(to).text(txt)
29+
.contextMessageId(messageUuid).build().toJson();
30+
2831
assertTrue(json.contains("\"text\":\""+txt+"\""));
2932
assertTrue(json.contains("\"from\":\""+from+"\""));
3033
assertTrue(json.contains("\"to\":\""+to+"\""));
34+
assertTrue(json.contains("\"context\":{\"message_uuid\":\""+messageUuid+"\"}"));
3135
assertTrue(json.contains("\"message_type\":\"text\""));
3236
assertTrue(json.contains("\"channel\":\"whatsapp\""));
3337
}
3438

3539
@Test
3640
public void testConstructEmptySender() {
3741
assertThrows(IllegalArgumentException.class, () -> WhatsappTextRequest.builder()
38-
.from("").to("317900000002").text("Hello, World!").build()
42+
.from("").to(to).text(txt).build()
3943
);
4044
}
4145

4246
@Test
4347
public void testConstructNoSender() {
4448
assertThrows(NullPointerException.class, () -> WhatsappTextRequest.builder()
45-
.to("317900000002").text("Hello, World!").build()
49+
.to(to).text(txt).build()
4650
);
4751
}
4852

4953
@Test
5054
public void testConstructNullText() {
5155
assertThrows(NullPointerException.class, () -> WhatsappTextRequest.builder()
52-
.from("447900000001").to("317900000002").build()
56+
.from(from).to(to).build()
5357
);
5458
}
5559

5660
@Test
5761
public void testConstructEmptyText() {
5862
assertThrows(IllegalArgumentException.class, () -> WhatsappTextRequest.builder()
59-
.from("447900000001").to("317900000002").text("").build()
63+
.from(from).to(to).text("").build()
6064
);
6165
}
6266

6367
@Test
6468
public void testConstructLongText() {
6569
StringBuilder text = new StringBuilder(1002);
66-
for (int i = 0; i < 4095; i++) {
67-
text.append('*');
68-
}
70+
text.append("*".repeat(4095));
6971
assertEquals(4095, text.length());
7072

7173
WhatsappTextRequest msg = WhatsappTextRequest.builder()
72-
.text(text.toString())
73-
.from("447900000001")
74-
.to("317900000002")
75-
.build();
74+
.text(text.toString()).from(from).to(to).build();
7675

7776
assertEquals(text.toString(), msg.getText());
7877
text.append("xy");

0 commit comments

Comments
 (0)