|
| 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