Skip to content

Commit c99ac4c

Browse files
authored
Merge pull request #270 from 17twentynine/chore/support-copy-code
feat(copy-code): Adding support for sending copy code template messages
2 parents ec2826c + be42898 commit c99ac4c

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

api/src/main/java/com/messagebird/objects/conversations/MessageComponentType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum MessageComponentType {
1313
BUTTON("button"),
1414
CARD("card"),
1515
CAROUSEL("carousel"),
16-
LIMITED_TIME_OFFER("limited_time_offer");
16+
LIMITED_TIME_OFFER("limited_time_offer"),
17+
COPY_CODE("copy_code");
1718

1819
private static final Map<String, MessageComponentType> TYPE_MAP;
1920

api/src/main/java/com/messagebird/objects/conversations/MessageParam.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class MessageParam {
1515
private Media video;
1616
@JsonProperty("expiration_time")
1717
private String expirationTime;
18+
@JsonProperty("coupon_code")
19+
private String couponCode;
1820

1921
public TemplateMediaType getType() {
2022
return type;
@@ -93,6 +95,13 @@ public void setExpirationTime(String expirationTime) {
9395
this.expirationTime = expirationTime;
9496
}
9597

98+
public void setCouponCode(String couponCode) {
99+
if (StringUtils.isBlank(couponCode)) {
100+
throw new IllegalArgumentException("couponCode cannot be null or empty");
101+
}
102+
this.couponCode = couponCode;
103+
}
104+
96105
@Override
97106
public String toString() {
98107
StringBuilder sb = new StringBuilder("MessageParam{");

api/src/main/java/com/messagebird/objects/conversations/TemplateMediaType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public enum TemplateMediaType {
1515
CURRENCY("currency"),
1616
DATETIME("date_time"),
1717
PAYLOAD("payload"),
18-
EXPIRATION_TIME("expiration_time");
18+
EXPIRATION_TIME("expiration_time"),
19+
COUPON_CODE("coupon_code");
1920

2021
private static final Map<String, TemplateMediaType> TYPE_MAP;
2122

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import com.messagebird.MessageBirdClient;
2+
import com.messagebird.MessageBirdService;
3+
import com.messagebird.MessageBirdServiceImpl;
4+
import com.messagebird.exceptions.GeneralException;
5+
import com.messagebird.exceptions.UnauthorizedException;
6+
import com.messagebird.objects.conversations.*;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class ExampleConversationSendHSMCopyCodeTemplate {
12+
13+
public static void main(String[] args) {
14+
if (args.length < 6) {
15+
System.out.println("Please at least specify your access key, the channel id and destination address.\n" +
16+
"Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) from(Required) destination(Required) templateName(Required) namespace(Required) expirationTimeInput(Required)");
17+
return;
18+
}
19+
20+
final String accessKey = args[0];
21+
final String from = args[1];
22+
final String destination = args[2];
23+
final String templateName = args[3];
24+
final String namespace = args[4];
25+
final String couponCodeInput = args[5];
26+
27+
//First create your service object
28+
final MessageBirdService wsr = new MessageBirdServiceImpl(accessKey);
29+
//Add the service to the client
30+
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
31+
32+
ConversationContent conversationContent = new ConversationContent();
33+
ConversationContentHsm conversationContentHsm = new ConversationContentHsm();
34+
conversationContentHsm.setNamespace(namespace);
35+
conversationContentHsm.setTemplateName(templateName);
36+
ConversationHsmLanguage language = new ConversationHsmLanguage();
37+
language.setCode("en");
38+
conversationContentHsm.setLanguage(language);
39+
List<MessageComponent> messageComponents = new ArrayList<>();
40+
41+
// Add LTO component
42+
MessageComponent messageCopyCodeComponent = new MessageComponent();
43+
messageCopyCodeComponent.setType(MessageComponentType.BUTTON);
44+
messageCopyCodeComponent.setSub_type(MessageComponentType.COPY_CODE.toString());
45+
List<MessageParam> messageCCParams = new ArrayList<>();
46+
47+
MessageParam couponCodeParam = new MessageParam();
48+
couponCodeParam.setType(TemplateMediaType.COUPON_CODE);
49+
couponCodeParam.setCouponCode(couponCodeInput);
50+
messageCCParams.add(couponCodeParam);
51+
52+
messageCopyCodeComponent.setParameters(messageCCParams);
53+
54+
// Add body component
55+
MessageComponent messageBodyComponent = new MessageComponent();
56+
messageBodyComponent.setType(MessageComponentType.BODY);
57+
List<MessageParam> messageBodyParams = new ArrayList<>();
58+
59+
MessageParam text = new MessageParam();
60+
text.setType(TemplateMediaType.TEXT);
61+
text.setText("Bob");
62+
messageBodyParams.add(text);
63+
64+
messageBodyComponent.setParameters(messageBodyParams);
65+
66+
messageComponents.add(messageCopyCodeComponent);
67+
messageComponents.add(messageBodyComponent);
68+
conversationContentHsm.setComponents(messageComponents);
69+
conversationContent.setHsm(conversationContentHsm);
70+
ConversationSendRequest request = new ConversationSendRequest(
71+
destination,
72+
ConversationContentType.HSM,
73+
conversationContent,
74+
from,
75+
"",
76+
null,
77+
null,
78+
null);
79+
80+
try {
81+
System.out.println(request.toString());
82+
ConversationSendResponse sendResponse = messageBirdClient.sendMessage(request);
83+
System.out.println(sendResponse.toString());
84+
85+
} catch (UnauthorizedException e) {
86+
System.err.println("Authorization failed. Please check your access key: " + e.getMessage());
87+
} catch (GeneralException e) {
88+
System.err.println("An error occurred while sending the message: " + e.getMessage());
89+
}
90+
}
91+
92+
}

0 commit comments

Comments
 (0)