Skip to content

Commit 614e49f

Browse files
authored
Merge pull request #246 from AlexL-mb/Added_support_carousel_coupon_code_whatsapp_messages
Added support for Carousel and Coupon code WhatsApp template messages.
2 parents 3b5b604 + 31f90a4 commit 614e49f

9 files changed

Lines changed: 326 additions & 3 deletions

File tree

api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class HSMComponent {
2020
@JsonProperty("code_expiration_minutes")
2121
private Integer codeExpirationMinutes;
2222
private List<HSMComponentButton> buttons;
23+
24+
private List<HSMComponentCard> cards;
25+
2326
private HSMExample example;
2427

2528
public HSMComponentType getType() {
@@ -54,6 +57,14 @@ public void setButtons(List<HSMComponentButton> buttons) {
5457
this.buttons = buttons;
5558
}
5659

60+
public List<HSMComponentCard> getCards() {
61+
return cards;
62+
}
63+
64+
public void setCards(List<HSMComponentCard> cards) {
65+
this.cards = cards;
66+
}
67+
5768
public HSMExample getExample() {
5869
return example;
5970
}

api/src/main/java/com/messagebird/objects/integrations/HSMComponentButton.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ public String toString() {
117117
public void validateButtonExample() throws IllegalArgumentException {
118118
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
119119
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
120-
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));
120+
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY)
121+
|| this.type.equals(HSMComponentButtonType.COPY_CODE)
122+
);
121123

122124
if (isExampleEmpty) {
123125
return;

api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public enum HSMComponentButtonType {
1414
PHONE_NUMBER("PHONE_NUMBER"),
1515
URL("URL"),
1616
QUICK_REPLY("QUICK_REPLY"),
17-
OTP("OTP");
17+
OTP("OTP"),
18+
COPY_CODE("COPY_CODE");
1819

1920
private final String type;
2021

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.messagebird.objects.integrations;
2+
3+
import java.util.List;
4+
5+
/**
6+
* HSMComponentCard
7+
*
8+
* @author AlexL-mb
9+
* @see <a href="https://developers.messagebird.com/api/integrations/#hsmcomponentcard-object">HSMComponentCard</a>
10+
*/
11+
public class HSMComponentCard {
12+
private List<HSMComponent> components;
13+
14+
public List<HSMComponent> getComponents() {
15+
return components;
16+
}
17+
18+
public void setComponents(List<HSMComponent> components) {
19+
this.components = components;
20+
}
21+
}

api/src/main/java/com/messagebird/objects/integrations/HSMComponentType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public enum HSMComponentType {
1212
BODY("BODY"),
1313
HEADER("HEADER"),
1414
FOOTER("FOOTER"),
15-
BUTTONS("BUTTONS");
15+
BUTTONS("BUTTONS"),
16+
CAROUSEL("CAROUSEL");
1617

1718
private final String type;
1819

api/src/test/java/com/messagebird/MessageBirdClientTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,44 @@ public void testCreateWhatsAppTemplate() throws UnauthorizedException, GeneralEx
10771077
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
10781078
}
10791079
}
1080+
@Test
1081+
public void testCreateWhatsAppCarouselTemplate() throws UnauthorizedException, GeneralException {
1082+
final TemplateResponse templateResponse = TestUtil.createWhatsAppCarouselTemplateResponse("sample_template_name", "ko");
1083+
final Template template = TestUtil.createWhatsAppCarouselTemplate("sample_template_name", "ko");
1084+
1085+
MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
1086+
MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock);
1087+
1088+
String url = String.format(
1089+
"%s%s%s",
1090+
INTEGRATIONS_BASE_URL_V2,
1091+
INTEGRATIONS_WHATSAPP_PATH,
1092+
TEMPLATES_PATH
1093+
);
1094+
1095+
when(messageBirdServiceMock.sendPayLoad(url, template, TemplateResponse.class))
1096+
.thenReturn(templateResponse);
1097+
1098+
final TemplateResponse response = messageBirdClientInjectMock.createWhatsAppTemplate(template);
1099+
1100+
verify(messageBirdServiceMock, times(1)).sendPayLoad(url, template, TemplateResponse.class);
1101+
assertNotNull(response);
1102+
assertEquals(response.getName(), templateResponse.getName());
1103+
assertEquals(response.getLanguage(), templateResponse.getLanguage());
1104+
assertEquals(response.getCategory(), templateResponse.getCategory());
1105+
assertEquals(response.getStatus(), templateResponse.getStatus());
1106+
assertEquals(response.getWabaID(), templateResponse.getWabaID());
1107+
assertEquals(response.getCreatedAt(), templateResponse.getCreatedAt());
1108+
assertEquals(response.getUpdatedAt(), templateResponse.getUpdatedAt());
1109+
1110+
/* verify components */
1111+
for (int i = 0; i < response.getComponents().size(); i++) {
1112+
assertEquals(response.getComponents().get(i).getType(), templateResponse.getComponents().get(i).getType());
1113+
assertEquals(response.getComponents().get(i).getFormat(), templateResponse.getComponents().get(i).getFormat());
1114+
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
1115+
}
1116+
}
1117+
10801118
@Test
10811119
public void testUpdateWhatsAppTemplate() throws UnauthorizedException, GeneralException {
10821120
final TemplateResponse templateResponse = TestUtil.createWhatsAppTemplateResponse("sample_template_name", "ko");

api/src/test/java/com/messagebird/TestUtil.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.messagebird.objects.integrations.HSMComponent;
77
import com.messagebird.objects.integrations.HSMComponentButton;
88
import com.messagebird.objects.integrations.HSMComponentButtonType;
9+
import com.messagebird.objects.integrations.HSMComponentCard;
910
import com.messagebird.objects.integrations.HSMComponentFormat;
1011
import com.messagebird.objects.integrations.HSMComponentType;
1112
import com.messagebird.objects.integrations.HSMExample;
@@ -301,6 +302,24 @@ private static HSMComponent createHSMComponentButton() {
301302
return buttonComponent;
302303
}
303304

305+
private static HSMComponent createHSMComponentCarousel() {
306+
final HSMComponent carouselComponent = new HSMComponent();
307+
carouselComponent.setType(HSMComponentType.CAROUSEL);
308+
309+
final List<HSMComponentCard> cards = new ArrayList<>();
310+
final HSMComponentCard card = new HSMComponentCard();
311+
final List<HSMComponent> cardComponents = new ArrayList<>();
312+
cardComponents.add(createHSMComponentHeader());
313+
cardComponents.add(createHSMComponentBody());
314+
cardComponents.add(createHSMComponentButton());
315+
card.setComponents(cardComponents);
316+
cards.add(card);
317+
318+
carouselComponent.setCards(cards);
319+
320+
return carouselComponent;
321+
}
322+
304323
public static TemplateResponse createWhatsAppTemplateResponse(final String templateName, final String language) {
305324
final TemplateResponse templateResponse = new TemplateResponse();
306325
templateResponse.setName(templateName);
@@ -341,6 +360,43 @@ public static Template createWhatsAppTemplate(final String templateName, final S
341360
return template;
342361
}
343362

363+
public static TemplateResponse createWhatsAppCarouselTemplateResponse(final String templateName, final String language) {
364+
final TemplateResponse templateResponse = new TemplateResponse();
365+
templateResponse.setName(templateName);
366+
templateResponse.setLanguage(language);
367+
templateResponse.setCategory(HSMCategory.MARKETING);
368+
templateResponse.setStatus(HSMStatus.NEW);
369+
templateResponse.setCreatedAt(new Date());
370+
templateResponse.setUpdatedAt(new Date());
371+
372+
final List<HSMComponent> components = new ArrayList<>();
373+
components.add(createHSMComponentBody());
374+
components.add(createHSMComponentCarousel());
375+
templateResponse.setComponents(components);
376+
377+
templateResponse.setWabaID("testWABAID");
378+
templateResponse.setNamespace("testNamespace");
379+
380+
return templateResponse;
381+
}
382+
383+
public static Template createWhatsAppCarouselTemplate(final String templateName, final String language) {
384+
final Template template = new Template();
385+
template.setName(templateName);
386+
template.setLanguage(language);
387+
template.setCategory(HSMCategory.MARKETING);
388+
389+
final List<HSMComponent> components = new ArrayList<>();
390+
components.add(createHSMComponentBody());
391+
components.add(createHSMComponentCarousel());
392+
template.setComponents(components);
393+
394+
template.setWABAID("testWABAID");
395+
396+
return template;
397+
}
398+
399+
344400
public static TemplateList createWhatsAppTemplateList(final String templateName) {
345401
final TemplateResponse template1 = TestUtil.createWhatsAppTemplateResponse(templateName, "en_US");
346402
final TemplateResponse template2 = TestUtil.createWhatsAppTemplateResponse(templateName, "ko");
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.integrations.*;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
/**
13+
* Create template.
14+
*
15+
* @see <a href="https://developers.messagebird.com/api/integrations/#create-template">Doc - Create template</a>
16+
* @author AlexL-mb
17+
*/
18+
public class ExampleCreateCarouselTemplate {
19+
20+
public static void main(String[] args) {
21+
if (args.length < 3) {
22+
System.out.println("Please specify your access key and a template name and WABA ID example : java -jar <this jar file> test_accesskey \"My template name\" \"WABA ID\"");
23+
return;
24+
}
25+
26+
// First create your service object
27+
MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
28+
29+
// Add the service to the client
30+
MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
31+
32+
/* body */
33+
HSMComponent bodyComponent = new HSMComponent();
34+
final HSMExample bodyExample = new HSMExample();
35+
final List<List<String>> bodyText = new ArrayList<>();
36+
bodyText.add(Arrays.asList("John"));
37+
bodyExample.setBody_text(bodyText);
38+
bodyComponent.setType(HSMComponentType.BODY);
39+
bodyComponent.setText("Hey {{1}}! This is a sample template from Java.");
40+
bodyComponent.setExample(bodyExample);
41+
42+
/* carousel */
43+
final HSMComponent carouselComponent = new HSMComponent();
44+
carouselComponent.setType(HSMComponentType.CAROUSEL);
45+
46+
/* cards */
47+
final List<HSMComponentCard> cards = new ArrayList<>();
48+
/* card 0 */
49+
final HSMComponentCard card = new HSMComponentCard();
50+
final List<HSMComponent> cardComponents = new ArrayList<>();
51+
52+
/* card header */
53+
final HSMComponent headerComponent = new HSMComponent();
54+
final HSMExample headerExample = new HSMExample();
55+
headerExample.setHeader_url(Arrays.asList("https://images.freeimages.com/images/small-previews/c5a/colourful-paper-rip-1-1195879.jpg"));
56+
57+
headerComponent.setType(HSMComponentType.HEADER);
58+
headerComponent.setFormat(HSMComponentFormat.IMAGE);
59+
headerComponent.setExample(headerExample);
60+
cardComponents.add(headerComponent);
61+
62+
/* card body */
63+
final HSMComponent cardBodyComponent = new HSMComponent();
64+
final HSMExample cardBodyExample = new HSMExample();
65+
final List<List<String>> cardBodyText = new ArrayList<>();
66+
cardBodyText.add(Arrays.asList("John"));
67+
cardBodyExample.setBody_text(cardBodyText);
68+
69+
cardBodyComponent.setType(HSMComponentType.BODY);
70+
cardBodyComponent.setText("Hey {{1}}! This is a sample template from Java.");
71+
cardBodyComponent.setExample(cardBodyExample);
72+
cardComponents.add(cardBodyComponent);
73+
74+
/* card buttons */
75+
final HSMComponent buttonComponent = new HSMComponent();
76+
final List<HSMComponentButton> buttons = new ArrayList<>();
77+
final HSMComponentButton button = new HSMComponentButton();
78+
button.setType(HSMComponentButtonType.URL);
79+
button.setText("Touch it");
80+
button.setUrl("https://www.messagebird.com");
81+
button.setExample(Arrays.asList("https://developers.messagebird.com"));
82+
buttons.add(button);
83+
buttonComponent.setType(HSMComponentType.BUTTONS);
84+
buttonComponent.setButtons(buttons);
85+
cardComponents.add(buttonComponent);
86+
87+
card.setComponents(cardComponents);
88+
89+
cards.add(card);
90+
91+
carouselComponent.setCards(cards);
92+
93+
/* set components */
94+
Template template = new Template();
95+
List<HSMComponent> components = new ArrayList<>();
96+
components.add(bodyComponent);
97+
components.add(carouselComponent);
98+
99+
template.setName(args[1]);
100+
template.setLanguage("en_US");
101+
template.setWABAID(args[2]);
102+
template.setComponents(components);
103+
template.setCategory(HSMCategory.MARKETING);
104+
105+
try {
106+
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
107+
System.out.println(response.toString());
108+
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
109+
exception.printStackTrace();
110+
}
111+
112+
}
113+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.integrations.*;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
/**
13+
* Create template.
14+
*
15+
* @see <a href="https://developers.messagebird.com/api/integrations/#create-template">Doc - Create template</a>
16+
* @author AlexL-mb
17+
*/
18+
public class ExampleCreateCouponTemplate {
19+
20+
public static void main(String[] args) {
21+
if (args.length < 3) {
22+
System.out.println("Please specify your access key and a template name and WABA ID example : java -jar <this jar file> test_accesskey \"My template name\" \"WABA ID\"");
23+
return;
24+
}
25+
26+
// First create your service object
27+
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
28+
29+
// Add the service to the client
30+
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
31+
32+
/* header */
33+
final HSMComponent headerComponent = new HSMComponent();
34+
headerComponent.setType(HSMComponentType.HEADER);
35+
headerComponent.setFormat(HSMComponentFormat.TEXT);
36+
headerComponent.setText("Our Fall Sale is on!");
37+
38+
/* body */
39+
final HSMComponent bodyComponent = new HSMComponent();
40+
final HSMExample bodyExample = new HSMExample();
41+
final List<List<String>> bodyText = new ArrayList<>();
42+
bodyText.add(Arrays.asList("25OFF", "25%"));
43+
bodyExample.setBody_text(bodyText);
44+
45+
bodyComponent.setType(HSMComponentType.BODY);
46+
bodyComponent.setText("Shop now through November and use code {{1}} to get {{2}} off of all merchandise!");
47+
bodyComponent.setExample(bodyExample);
48+
49+
50+
/* button */
51+
final HSMComponent buttonComponent = new HSMComponent();
52+
final List<HSMComponentButton> buttons = new ArrayList<>();
53+
final HSMComponentButton button = new HSMComponentButton();
54+
button.setType(HSMComponentButtonType.COPY_CODE);
55+
button.setExample(Arrays.asList("CODE25"));
56+
buttons.add(button);
57+
buttonComponent.setType(HSMComponentType.BUTTONS);
58+
buttonComponent.setButtons(buttons);
59+
60+
/* set components */
61+
final Template template = new Template();
62+
final List<HSMComponent> components = new ArrayList<>();
63+
components.add(headerComponent);
64+
components.add(bodyComponent);
65+
components.add(buttonComponent);
66+
67+
template.setName(args[1]);
68+
template.setLanguage("en_US");
69+
template.setWABAID(args[2]);
70+
template.setComponents(components);
71+
template.setCategory(HSMCategory.MARKETING);
72+
73+
try {
74+
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
75+
System.out.println(response.toString());
76+
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
77+
exception.printStackTrace();
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)