|
| 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.Collections; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class ExampleConversationSendCarouselTemplate { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + |
| 16 | + if (args.length < 3) { |
| 17 | + System.out.println("Please at least specify your access key, the channel id and destination address.\n" + |
| 18 | + "Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) to(Required)"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + //First create your service object |
| 23 | + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); |
| 24 | + //Add the service to the client |
| 25 | + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); |
| 26 | + |
| 27 | + ConversationContent conversationContent = new ConversationContent(); |
| 28 | + ConversationContentHsm conversationContentHsm = new ConversationContentHsm(); |
| 29 | + conversationContentHsm.setNamespace("c663a566_a4de_492f_86b2_028cbf612345"); |
| 30 | + conversationContentHsm.setTemplateName("carousel_template_test_hello"); |
| 31 | + ConversationHsmLanguage language = new ConversationHsmLanguage(); |
| 32 | + language.setCode("en_US"); |
| 33 | + conversationContentHsm.setLanguage(language); |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +// card0 components |
| 38 | + List<MessageComponent> card0Components = new ArrayList<>(); |
| 39 | +// card0 header |
| 40 | + MessageComponent card0HeaderComponent = new MessageComponent(); |
| 41 | + card0HeaderComponent.setType(MessageComponentType.HEADER); |
| 42 | + MessageParam imageParam = new MessageParam(); |
| 43 | + imageParam.setType(TemplateMediaType.IMAGE); |
| 44 | + Media media = new Media(); |
| 45 | + media.setUrl("https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg"); |
| 46 | + imageParam.setImage(media); |
| 47 | + card0HeaderComponent.setParameters(Collections.singletonList(imageParam)); |
| 48 | + card0Components.add(card0HeaderComponent); |
| 49 | +// card0 body |
| 50 | + MessageComponent card0BodyComponent = new MessageComponent(); |
| 51 | + card0BodyComponent.setType(MessageComponentType.BODY); |
| 52 | + MessageParam textParam = new MessageParam(); |
| 53 | + textParam.setType(TemplateMediaType.TEXT); |
| 54 | + textParam.setText("dummy text"); |
| 55 | + card0BodyComponent.setParameters(Collections.singletonList(textParam)); |
| 56 | + card0Components.add(card0BodyComponent); |
| 57 | +// card0 button |
| 58 | + MessageComponent card0ButtonComponent = new MessageComponent(); |
| 59 | + card0ButtonComponent.setType(MessageComponentType.BUTTON); |
| 60 | + card0ButtonComponent.setSub_type("quick_reply"); |
| 61 | + card0ButtonComponent.setIndex(0); |
| 62 | + MessageParam buttonParam = new MessageParam(); |
| 63 | + buttonParam.setType(TemplateMediaType.PAYLOAD); |
| 64 | + buttonParam.setPayload("dummy button"); |
| 65 | + card0ButtonComponent.setParameters(Collections.singletonList(buttonParam)); |
| 66 | + card0Components.add(card0ButtonComponent); |
| 67 | + |
| 68 | +// card0 |
| 69 | + MessageComponent card0 = new MessageComponent(); |
| 70 | + card0.setType(MessageComponentType.CARD); |
| 71 | + card0.setCard_index(0); |
| 72 | + card0.setComponents(card0Components); |
| 73 | + |
| 74 | + |
| 75 | +// cards list |
| 76 | + List<MessageComponent> cards = new ArrayList<>(); |
| 77 | + cards.add(card0); |
| 78 | + |
| 79 | +// carousel component |
| 80 | + MessageComponent carousel = new MessageComponent(); |
| 81 | + carousel.setType(MessageComponentType.CAROUSEL); |
| 82 | + carousel.setCards(cards); |
| 83 | +// body component |
| 84 | + MessageComponent body = new MessageComponent(); |
| 85 | + body.setType(MessageComponentType.BODY); |
| 86 | + MessageParam bodyParam = new MessageParam(); |
| 87 | + bodyParam.setType(TemplateMediaType.TEXT); |
| 88 | + bodyParam.setText("Jackson"); |
| 89 | + body.setParameters(Collections.singletonList(bodyParam)); |
| 90 | + |
| 91 | +// set components to message |
| 92 | + List<MessageComponent> messageComponents = new ArrayList<>(); |
| 93 | + messageComponents.add(body); |
| 94 | + messageComponents.add(carousel); |
| 95 | + conversationContentHsm.setComponents(messageComponents); |
| 96 | + |
| 97 | + conversationContent.setHsm(conversationContentHsm); |
| 98 | + ConversationSendRequest request = new ConversationSendRequest( |
| 99 | + args[2], |
| 100 | + ConversationContentType.HSM, |
| 101 | + conversationContent, |
| 102 | + args[1], |
| 103 | + "", |
| 104 | + null, |
| 105 | + null, |
| 106 | + null); |
| 107 | + |
| 108 | + try { |
| 109 | + ConversationSendResponse sendResponse = messageBirdClient.sendMessage(request); |
| 110 | + System.out.println(sendResponse.toString()); |
| 111 | + |
| 112 | + } catch (GeneralException | UnauthorizedException exception) { |
| 113 | + exception.printStackTrace(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments