Skip to content

Add example for HSM media template #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -4,17 +4,17 @@

public class MessageComponent {

private String type;
private MessageComponentType type;
private String sub_type;
private int index;
private List<MessageParam> parameters;

public String getType() {
return type;
public void setType(MessageComponentType type) {
this.type = type;
}

public void setType(String type) {
this.type = type;
public MessageComponentType getType() {
return type;
}

public String getSub_type() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.messagebird.objects.conversations;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum MessageComponentType {

HEADER("header"),
BODY("body"),
FOOTER("footer"),
BUTTONS("buttons");


private final String type;

MessageComponentType(final String type) {
this.type = type;
}

@JsonCreator
public static MessageComponentType forValue(String value) {
for (MessageComponentType componentType: MessageComponentType.values()) {
if (componentType.getType().equals(value)) {
return componentType;
}
}

return null;
}

@JsonValue
public String toJson() {
return getType();
}

public String getType() {
return type;
}

@Override
public String toString() {
return getType();
}
}
103 changes: 103 additions & 0 deletions examples/src/main/java/ExampleConversationSendHSMMediaTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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.conversations.ConversationContent;
import com.messagebird.objects.conversations.ConversationContentHsm;
import com.messagebird.objects.conversations.ConversationContentType;
import com.messagebird.objects.conversations.ConversationHsmLanguage;
import com.messagebird.objects.conversations.ConversationSendRequest;
import com.messagebird.objects.conversations.ConversationSendResponse;
import com.messagebird.objects.conversations.Media;
import com.messagebird.objects.conversations.MessageComponent;
import com.messagebird.objects.conversations.MessageComponentType;
import com.messagebird.objects.conversations.MessageParam;
import com.messagebird.objects.conversations.TemplateMediaType;
import java.util.ArrayList;
import java.util.List;

public class ExampleConversationSendHSMMediaTemplate {

// Reference Example: https://developers.messagebird.com/quickstarts/whatsapp/send-media-template-message/
public static void main(String[] args) {

if (args.length < 4) {
System.out.println("Please at least specify your access key, the channel id and destination address.\n" +
"Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) from(Required) to(Required)");
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);


ConversationContent conversationContent = new ConversationContent();
ConversationContentHsm conversationContentHsm = new ConversationContentHsm();
conversationContentHsm.setNamespace("20332cd4_f095_b080_d255_35677159aaff");
conversationContentHsm.setTemplateName("33172012024_ship_img_but_1");
ConversationHsmLanguage language = new ConversationHsmLanguage();
language.setCode("en");
conversationContentHsm.setLanguage(language);
List<MessageComponent> messageComponents = new ArrayList<>();
//Define header component with image
MessageComponent messageHeaderComponent = new MessageComponent();
messageHeaderComponent.setType(MessageComponentType.HEADER);
MessageParam imageParam = new MessageParam();
Media media = new Media();
media.setUrl("https://i.ytimg.com/vi/3fDoOw4lIeU/maxresdefault.jpg");
imageParam.setImage(media);
imageParam.setType(TemplateMediaType.IMAGE);
List<MessageParam> messageHeaderParams = new ArrayList<>();
messageHeaderParams.add(imageParam);
messageHeaderComponent.setParameters(messageHeaderParams);
//Define body component with texts
MessageComponent messageBodyComponent = new MessageComponent();
messageBodyComponent.setType(MessageComponentType.BODY);
List<MessageParam> messageBodyParams = new ArrayList<>();
messageBodyComponent.setParameters(messageBodyParams);
MessageParam firstText = new MessageParam();
firstText.setType(TemplateMediaType.TEXT);
firstText.setText("John");
messageBodyParams.add(firstText);

MessageParam secondText = new MessageParam();
secondText.setType(TemplateMediaType.TEXT);
secondText.setText("MB93824");
messageBodyParams.add(secondText);

MessageParam thirdText = new MessageParam();
thirdText.setType(TemplateMediaType.TEXT);
thirdText.setText("2 days");
messageBodyParams.add(thirdText);

MessageParam fourthText = new MessageParam();
fourthText.setType(TemplateMediaType.TEXT);
fourthText.setText("MessageBird");
messageBodyParams.add(fourthText);

messageComponents.add(messageHeaderComponent);
messageComponents.add(messageBodyComponent);
conversationContentHsm.setComponents(messageComponents);
conversationContent.setHsm(conversationContentHsm);
ConversationSendRequest request = new ConversationSendRequest(
args[2],
ConversationContentType.HSM,
conversationContent,
args[1],
"",
null,
null,
null);

try {
ConversationSendResponse sendResponse = messageBirdClient.sendMessage(request);
System.out.println(sendResponse.toString());

} catch (GeneralException | UnauthorizedException exception) {
exception.printStackTrace();
}
}
}