Skip to content

Commit fe06b81

Browse files
authored
Merge pull request #529 from Vonage/messages-updates
2 parents d1a9cd6 + a15b2a1 commit fe06b81

File tree

15 files changed

+156
-41
lines changed

15 files changed

+156
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
# [8.7.0] - 2024-05-??
7+
# [8.7.0] - 2024-05-16
88
- Added missing supported languages to `TextToSpeechLanguage` enum
9+
- Added `ttl` field to outbound MMS messages
10+
- Added message reply context to Whatsapp outbound requests
11+
- Added network code to `InboundMessage`
912

1013
# [8.6.0] - 2024-04-18
1114
- Added Experience Composer to Video API

src/main/java/com/vonage/client/messages/InboundMessage.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class InboundMessage extends JsonableBaseObject {
3838

3939
protected static class UrlWrapper extends JsonableBaseObject {
4040
@JsonProperty("url") protected URI url;
41+
@JsonProperty("name") protected String name;
4142
}
4243

4344
protected static class UrlWrapperWithCaption extends UrlWrapper {
@@ -48,6 +49,10 @@ protected static class Whatsapp extends JsonableBaseObject {
4849
@JsonProperty("referral") protected Referral referral;
4950
}
5051

52+
protected static class Origin extends JsonableBaseObject {
53+
@JsonProperty("network_code") protected String networkCode;
54+
}
55+
5156
protected InboundMessage() {}
5257

5358
@JsonAnySetter protected Map<String, Object> unknownProperties;
@@ -79,6 +84,7 @@ protected InboundMessage() {}
7984
@JsonProperty("order") protected Order whatsappOrder;
8085
@JsonProperty("usage") protected MessageStatus.Usage usage;
8186
@JsonProperty("sms") protected SmsInboundMetadata smsMetadata;
87+
@JsonProperty("origin") protected Origin origin;
8288

8389
/**
8490
* This is a catch-all method which encapsulates all fields in the response JSON
@@ -330,6 +336,19 @@ public SmsInboundMetadata getSmsMetadata() {
330336
return smsMetadata;
331337
}
332338

339+
/**
340+
* If the {@linkplain #getChannel()} is {@linkplain Channel#SMS} or {@linkplain Channel#MMS},
341+
* return the network code from which the message originated (if available).
342+
*
343+
* @return The origin network code if applicable, or {@code null} if unknown.
344+
*
345+
* @since 8.7.0
346+
*/
347+
@JsonIgnore
348+
public String getNetworkCode() {
349+
return origin != null ? origin.networkCode : null;
350+
}
351+
333352
/**
334353
* If the {@linkplain #getChannel()} is {@linkplain Channel#WHATSAPP} and a content referral is present in
335354
* the message, returns the metadata related to the post or advertisement that the user clicked on.

src/main/java/com/vonage/client/messages/mms/MmsAudioRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public final class MmsAudioRequest extends MmsRequest {
2323

2424
MmsAudioRequest(Builder builder) {
2525
super(builder, MessageType.AUDIO);
26-
payload = new MessagePayload(builder.url, builder.caption);
27-
payload.validateCaptionLength(2000);
2826
}
2927

3028
@JsonProperty("audio")

src/main/java/com/vonage/client/messages/mms/MmsImageRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public final class MmsImageRequest extends MmsRequest {
2323

2424
MmsImageRequest(Builder builder) {
2525
super(builder, MessageType.IMAGE);
26-
payload = new MessagePayload(builder.url, builder.caption);
2726
payload.validateUrlExtension("jpg", "jpeg", "png", "gif");
28-
payload.validateCaptionLength(2000);
2927
}
3028

3129
@JsonProperty("image")

src/main/java/com/vonage/client/messages/mms/MmsRequest.java

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

18+
import com.fasterxml.jackson.annotation.JsonProperty;
1819
import com.vonage.client.messages.Channel;
1920
import com.vonage.client.messages.MessageRequest;
2021
import com.vonage.client.messages.MessageType;
2122
import com.vonage.client.messages.internal.MessagePayload;
2223

2324
public abstract class MmsRequest extends MessageRequest {
2425
MessagePayload payload;
26+
final Integer ttl;
2527

2628
protected MmsRequest(Builder<?, ?> builder, MessageType messageType) {
2729
super(builder, Channel.MMS, messageType);
30+
payload = new MessagePayload(builder.url, builder.caption);
31+
payload.validateCaptionLength(2000);
32+
int min = 300, max = 259200;
33+
if ((this.ttl = builder.ttl) != null && (ttl < min || ttl > max)) {
34+
throw new IllegalArgumentException("TTL must be between "+min+" and "+max+" seconds.");
35+
}
36+
}
37+
38+
@JsonProperty("ttl")
39+
public Integer getTtl() {
40+
return ttl;
2841
}
2942

3043
@SuppressWarnings("unchecked")
3144
protected abstract static class Builder<M extends MmsRequest, B extends Builder<? extends M, ? extends B>> extends MessageRequest.Builder<M, B> {
3245
String url, caption;
46+
Integer ttl;
3347

34-
protected B url(String url) {
48+
/**
49+
* (REQUIRED)
50+
* Sets the media URL.
51+
*
52+
* @param url The URL as a string.
53+
* @return This builder.
54+
*/
55+
public B url(String url) {
3556
this.url = url;
3657
return (B) this;
3758
}
3859

60+
/**
61+
* (OPTIONAL)
62+
* Additional text to accompany the media. Must be between 1 and 2000 characters.
63+
*
64+
* @param caption The caption string.
65+
* @return This builder.
66+
*/
3967
protected B caption(String caption) {
4068
this.caption = caption;
4169
return (B) this;
4270
}
71+
72+
/**
73+
* (OPTIONAL)
74+
* Time-To-Live (how long a message should exist before it is delivered successfully) in seconds.
75+
* If a message is not delivered successfully within the TTL time, the message is considered expired
76+
* and will be rejected if TTL is supported. Must be between 300 and 259200. Default is 600.
77+
*
78+
* @param ttl The message time-to-live in seconds.
79+
*
80+
* @return This builder.
81+
* @since 8.7.0
82+
*/
83+
public B ttl(int ttl) {
84+
this.ttl = ttl;
85+
return (B) this;
86+
}
4387
}
4488
}

src/main/java/com/vonage/client/messages/mms/MmsVcardRequest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public final class MmsVcardRequest extends MmsRequest {
2323

2424
MmsVcardRequest(Builder builder) {
2525
super(builder, MessageType.VCARD);
26-
payload = new MessagePayload(builder.url, builder.caption);
2726
payload.validateUrlExtension("vcf");
2827
}
2928

src/main/java/com/vonage/client/messages/mms/MmsVideoRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public final class MmsVideoRequest extends MmsRequest {
2323

2424
MmsVideoRequest(Builder builder) {
2525
super(builder, MessageType.VIDEO);
26-
payload = new MessagePayload(builder.url, builder.caption);
27-
payload.validateCaptionLength(2000);
2826
}
2927

3028
@JsonProperty("video")

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/InboundMessageTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,15 @@ public void testWhatsappReferralOnly() {
358358
assertEquals(sourceType, referral.getSourceType());
359359
assertEquals(URI.create(sourceUrl), referral.getSourceUrl());
360360
}
361+
362+
@Test
363+
public void testOriginOnly() {
364+
String networkCode = "12534", json = "{\n" +
365+
" \"origin\": {\n" +
366+
" \"network_code\": \""+networkCode+"\"\n" +
367+
" }\n" +
368+
"}";
369+
var im = InboundMessage.fromJson(json);
370+
assertEquals(networkCode, im.getNetworkCode());
371+
}
361372
}

0 commit comments

Comments
 (0)