|
| 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 | +} |
0 commit comments