Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class HSMComponent {
@JsonProperty("code_expiration_minutes")
private Integer codeExpirationMinutes;
private List<HSMComponentButton> buttons;

private List<HSMComponentCard> cards;

private HSMExample example;

public HSMComponentType getType() {
Expand Down Expand Up @@ -54,6 +57,14 @@ public void setButtons(List<HSMComponentButton> buttons) {
this.buttons = buttons;
}

public List<HSMComponentCard> getCards() {
return cards;
}

public void setCards(List<HSMComponentCard> cards) {
this.cards = cards;
}

public HSMExample getExample() {
return example;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public String toString() {
public void validateButtonExample() throws IllegalArgumentException {
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY)
|| this.type.equals(HSMComponentButtonType.COPY_CODE)
);

if (isExampleEmpty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public enum HSMComponentButtonType {
PHONE_NUMBER("PHONE_NUMBER"),
URL("URL"),
QUICK_REPLY("QUICK_REPLY"),
OTP("OTP");
OTP("OTP"),
COPY_CODE("COPY_CODE");

private final String type;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.messagebird.objects.integrations;

import java.util.List;

/**
* HSMComponentCard
*
* @author AlexL-mb
* @see <a href="https://developers.messagebird.com/api/integrations/#hsmcomponentcard-object">HSMComponentCard</a>
*/
public class HSMComponentCard {
private List<HSMComponent> components;

public List<HSMComponent> getComponents() {
return components;
}

public void setComponents(List<HSMComponent> components) {
this.components = components;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum HSMComponentType {
BODY("BODY"),
HEADER("HEADER"),
FOOTER("FOOTER"),
BUTTONS("BUTTONS");
BUTTONS("BUTTONS"),
CAROUSEL("CAROUSEL");

private final String type;

Expand Down
38 changes: 38 additions & 0 deletions api/src/test/java/com/messagebird/MessageBirdClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,44 @@ public void testCreateWhatsAppTemplate() throws UnauthorizedException, GeneralEx
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
}
}
@Test
public void testCreateWhatsAppCarouselTemplate() throws UnauthorizedException, GeneralException {
final TemplateResponse templateResponse = TestUtil.createWhatsAppCarouselTemplateResponse("sample_template_name", "ko");
final Template template = TestUtil.createWhatsAppCarouselTemplate("sample_template_name", "ko");

MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class);
MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock);

String url = String.format(
"%s%s%s",
INTEGRATIONS_BASE_URL_V2,
INTEGRATIONS_WHATSAPP_PATH,
TEMPLATES_PATH
);

when(messageBirdServiceMock.sendPayLoad(url, template, TemplateResponse.class))
.thenReturn(templateResponse);

final TemplateResponse response = messageBirdClientInjectMock.createWhatsAppTemplate(template);

verify(messageBirdServiceMock, times(1)).sendPayLoad(url, template, TemplateResponse.class);
assertNotNull(response);
assertEquals(response.getName(), templateResponse.getName());
assertEquals(response.getLanguage(), templateResponse.getLanguage());
assertEquals(response.getCategory(), templateResponse.getCategory());
assertEquals(response.getStatus(), templateResponse.getStatus());
assertEquals(response.getWabaID(), templateResponse.getWabaID());
assertEquals(response.getCreatedAt(), templateResponse.getCreatedAt());
assertEquals(response.getUpdatedAt(), templateResponse.getUpdatedAt());

/* verify components */
for (int i = 0; i < response.getComponents().size(); i++) {
assertEquals(response.getComponents().get(i).getType(), templateResponse.getComponents().get(i).getType());
assertEquals(response.getComponents().get(i).getFormat(), templateResponse.getComponents().get(i).getFormat());
assertEquals(response.getComponents().get(i).getText(), templateResponse.getComponents().get(i).getText());
}
}

@Test
public void testUpdateWhatsAppTemplate() throws UnauthorizedException, GeneralException {
final TemplateResponse templateResponse = TestUtil.createWhatsAppTemplateResponse("sample_template_name", "ko");
Expand Down
56 changes: 56 additions & 0 deletions api/src/test/java/com/messagebird/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.messagebird.objects.integrations.HSMComponent;
import com.messagebird.objects.integrations.HSMComponentButton;
import com.messagebird.objects.integrations.HSMComponentButtonType;
import com.messagebird.objects.integrations.HSMComponentCard;
import com.messagebird.objects.integrations.HSMComponentFormat;
import com.messagebird.objects.integrations.HSMComponentType;
import com.messagebird.objects.integrations.HSMExample;
Expand Down Expand Up @@ -301,6 +302,24 @@ private static HSMComponent createHSMComponentButton() {
return buttonComponent;
}

private static HSMComponent createHSMComponentCarousel() {
final HSMComponent carouselComponent = new HSMComponent();
carouselComponent.setType(HSMComponentType.CAROUSEL);

final List<HSMComponentCard> cards = new ArrayList<>();
final HSMComponentCard card = new HSMComponentCard();
final List<HSMComponent> cardComponents = new ArrayList<>();
cardComponents.add(createHSMComponentHeader());
cardComponents.add(createHSMComponentBody());
cardComponents.add(createHSMComponentButton());
card.setComponents(cardComponents);
cards.add(card);

carouselComponent.setCards(cards);

return carouselComponent;
}

public static TemplateResponse createWhatsAppTemplateResponse(final String templateName, final String language) {
final TemplateResponse templateResponse = new TemplateResponse();
templateResponse.setName(templateName);
Expand Down Expand Up @@ -341,6 +360,43 @@ public static Template createWhatsAppTemplate(final String templateName, final S
return template;
}

public static TemplateResponse createWhatsAppCarouselTemplateResponse(final String templateName, final String language) {
final TemplateResponse templateResponse = new TemplateResponse();
templateResponse.setName(templateName);
templateResponse.setLanguage(language);
templateResponse.setCategory(HSMCategory.MARKETING);
templateResponse.setStatus(HSMStatus.NEW);
templateResponse.setCreatedAt(new Date());
templateResponse.setUpdatedAt(new Date());

final List<HSMComponent> components = new ArrayList<>();
components.add(createHSMComponentBody());
components.add(createHSMComponentCarousel());
templateResponse.setComponents(components);

templateResponse.setWabaID("testWABAID");
templateResponse.setNamespace("testNamespace");

return templateResponse;
}

public static Template createWhatsAppCarouselTemplate(final String templateName, final String language) {
final Template template = new Template();
template.setName(templateName);
template.setLanguage(language);
template.setCategory(HSMCategory.MARKETING);

final List<HSMComponent> components = new ArrayList<>();
components.add(createHSMComponentBody());
components.add(createHSMComponentCarousel());
template.setComponents(components);

template.setWABAID("testWABAID");

return template;
}


public static TemplateList createWhatsAppTemplateList(final String templateName) {
final TemplateResponse template1 = TestUtil.createWhatsAppTemplateResponse(templateName, "en_US");
final TemplateResponse template2 = TestUtil.createWhatsAppTemplateResponse(templateName, "ko");
Expand Down
113 changes: 113 additions & 0 deletions examples/src/main/java/ExampleCreateCarouselTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import com.messagebird.MessageBirdClient;
import com.messagebird.MessageBirdService;
import com.messagebird.MessageBirdServiceImpl;
import com.messagebird.exceptions.GeneralException;
import com.messagebird.exceptions.UnauthorizedException;
import com.messagebird.objects.integrations.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Create template.
*
* @see <a href="https://developers.messagebird.com/api/integrations/#create-template">Doc - Create template</a>
* @author AlexL-mb
*/
public class ExampleCreateCarouselTemplate {

public static void main(String[] args) {
if (args.length < 3) {
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\"");
return;
}

// First create your service object
MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);

// Add the service to the client
MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);

/* body */
HSMComponent bodyComponent = new HSMComponent();
final HSMExample bodyExample = new HSMExample();
final List<List<String>> bodyText = new ArrayList<>();
bodyText.add(Arrays.asList("John"));
bodyExample.setBody_text(bodyText);
bodyComponent.setType(HSMComponentType.BODY);
bodyComponent.setText("Hey {{1}}! This is a sample template from Java.");
bodyComponent.setExample(bodyExample);

/* carousel */
final HSMComponent carouselComponent = new HSMComponent();
carouselComponent.setType(HSMComponentType.CAROUSEL);

/* cards */
final List<HSMComponentCard> cards = new ArrayList<>();
/* card 0 */
final HSMComponentCard card = new HSMComponentCard();
final List<HSMComponent> cardComponents = new ArrayList<>();

/* card header */
final HSMComponent headerComponent = new HSMComponent();
final HSMExample headerExample = new HSMExample();
headerExample.setHeader_url(Arrays.asList("https://images.freeimages.com/images/small-previews/c5a/colourful-paper-rip-1-1195879.jpg"));

headerComponent.setType(HSMComponentType.HEADER);
headerComponent.setFormat(HSMComponentFormat.IMAGE);
headerComponent.setExample(headerExample);
cardComponents.add(headerComponent);

/* card body */
final HSMComponent cardBodyComponent = new HSMComponent();
final HSMExample cardBodyExample = new HSMExample();
final List<List<String>> cardBodyText = new ArrayList<>();
cardBodyText.add(Arrays.asList("John"));
cardBodyExample.setBody_text(cardBodyText);

cardBodyComponent.setType(HSMComponentType.BODY);
cardBodyComponent.setText("Hey {{1}}! This is a sample template from Java.");
cardBodyComponent.setExample(cardBodyExample);
cardComponents.add(cardBodyComponent);

/* card buttons */
final HSMComponent buttonComponent = new HSMComponent();
final List<HSMComponentButton> buttons = new ArrayList<>();
final HSMComponentButton button = new HSMComponentButton();
button.setType(HSMComponentButtonType.URL);
button.setText("Touch it");
button.setUrl("https://www.messagebird.com");
button.setExample(Arrays.asList("https://developers.messagebird.com"));
buttons.add(button);
buttonComponent.setType(HSMComponentType.BUTTONS);
buttonComponent.setButtons(buttons);
cardComponents.add(buttonComponent);

card.setComponents(cardComponents);

cards.add(card);

carouselComponent.setCards(cards);

/* set components */
Template template = new Template();
List<HSMComponent> components = new ArrayList<>();
components.add(bodyComponent);
components.add(carouselComponent);

template.setName(args[1]);
template.setLanguage("en_US");
template.setWABAID(args[2]);
template.setComponents(components);
template.setCategory(HSMCategory.MARKETING);

try {
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
System.out.println(response.toString());
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
exception.printStackTrace();
}

}
}
80 changes: 80 additions & 0 deletions examples/src/main/java/ExampleCreateCouponTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import com.messagebird.MessageBirdClient;
import com.messagebird.MessageBirdService;
import com.messagebird.MessageBirdServiceImpl;
import com.messagebird.exceptions.GeneralException;
import com.messagebird.exceptions.UnauthorizedException;
import com.messagebird.objects.integrations.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Create template.
*
* @see <a href="https://developers.messagebird.com/api/integrations/#create-template">Doc - Create template</a>
* @author AlexL-mb
*/
public class ExampleCreateCouponTemplate {

public static void main(String[] args) {
if (args.length < 3) {
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\"");
return;
}

// First create your service object
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);

// Add the service to the client
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);

/* header */
final HSMComponent headerComponent = new HSMComponent();
headerComponent.setType(HSMComponentType.HEADER);
headerComponent.setFormat(HSMComponentFormat.TEXT);
headerComponent.setText("Our Fall Sale is on!");

/* body */
final HSMComponent bodyComponent = new HSMComponent();
final HSMExample bodyExample = new HSMExample();
final List<List<String>> bodyText = new ArrayList<>();
bodyText.add(Arrays.asList("25OFF", "25%"));
bodyExample.setBody_text(bodyText);

bodyComponent.setType(HSMComponentType.BODY);
bodyComponent.setText("Shop now through November and use code {{1}} to get {{2}} off of all merchandise!");
bodyComponent.setExample(bodyExample);


/* button */
final HSMComponent buttonComponent = new HSMComponent();
final List<HSMComponentButton> buttons = new ArrayList<>();
final HSMComponentButton button = new HSMComponentButton();
button.setType(HSMComponentButtonType.COPY_CODE);
button.setExample(Arrays.asList("CODE25"));
buttons.add(button);
buttonComponent.setType(HSMComponentType.BUTTONS);
buttonComponent.setButtons(buttons);

/* set components */
final Template template = new Template();
final List<HSMComponent> components = new ArrayList<>();
components.add(headerComponent);
components.add(bodyComponent);
components.add(buttonComponent);

template.setName(args[1]);
template.setLanguage("en_US");
template.setWABAID(args[2]);
template.setComponents(components);
template.setCategory(HSMCategory.MARKETING);

try {
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
System.out.println(response.toString());
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
exception.printStackTrace();
}
}
}