|
| 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.Collections; |
| 9 | + |
| 10 | +public class ExampleConversationSendHSMTemplateWithButtons { |
| 11 | + |
| 12 | + // Reference Example: https://developers.messagebird.com/quickstarts/whatsapp/send-message-with-buttons/ |
| 13 | + public static void main(String[] args) { |
| 14 | + |
| 15 | + if (args.length < 4) { |
| 16 | + System.out.println("Please at least specify your access key, the channel id and destination address.\n" + |
| 17 | + "Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) from(Required) to(Required)"); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + //First create your service object |
| 22 | + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); |
| 23 | + //Add the service to the client |
| 24 | + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); |
| 25 | + |
| 26 | + ConversationContent conversationContent = new ConversationContent(); |
| 27 | + ConversationContentHsm conversationContentHsm = new ConversationContentHsm(); |
| 28 | + conversationContentHsm.setNamespace("20332cd4_f095_b080_d255_35677159aaff"); |
| 29 | + conversationContentHsm.setTemplateName("33172012024_ship_img_but_1"); |
| 30 | + ConversationHsmLanguage language = new ConversationHsmLanguage(); |
| 31 | + language.setCode("en"); |
| 32 | + conversationContentHsm.setLanguage(language); |
| 33 | + |
| 34 | + //Define component with button |
| 35 | + MessageComponent messageButtonComponent = new MessageComponent(); |
| 36 | + messageButtonComponent.setType(MessageComponentType.BUTTON); |
| 37 | + messageButtonComponent.setSub_type("url"); |
| 38 | + MessageParam textParam = new MessageParam(); |
| 39 | + textParam.setType(TemplateMediaType.TEXT); |
| 40 | + textParam.setText("23493282245"); |
| 41 | + |
| 42 | + messageButtonComponent.setParameters(Collections.singletonList(textParam)); |
| 43 | + conversationContentHsm.setComponents(Collections.singletonList(messageButtonComponent)); |
| 44 | + conversationContent.setHsm(conversationContentHsm); |
| 45 | + ConversationSendRequest request = new ConversationSendRequest( |
| 46 | + args[2], |
| 47 | + ConversationContentType.HSM, |
| 48 | + conversationContent, |
| 49 | + args[1], |
| 50 | + "", |
| 51 | + null, |
| 52 | + null, |
| 53 | + null); |
| 54 | + |
| 55 | + try { |
| 56 | + ConversationSendResponse sendResponse = messageBirdClient.sendMessage(request); |
| 57 | + System.out.println(sendResponse.toString()); |
| 58 | + |
| 59 | + } catch (GeneralException | UnauthorizedException exception) { |
| 60 | + exception.printStackTrace(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments