Skip to content

Issue238- Added support to create and read the OTP authentification template #239

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 1 commit into from
Jun 15, 2023
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
@@ -1,5 +1,7 @@
package com.messagebird.objects.integrations;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

/**
Expand All @@ -13,7 +15,11 @@ public class HSMComponent {
private HSMComponentType type;
private HSMComponentFormat format;
private String text;
private List<HSMComponentButton> buttons;
@JsonProperty("add_security_recommendation")
private Boolean addSecurityRecommendation;
@JsonProperty("code_expiration_minutes")
private Integer codeExpirationMinutes;
private List<HSMComponentButton> buttons;
private HSMExample example;

public HSMComponentType getType() {
Expand Down Expand Up @@ -56,6 +62,21 @@ public void setExample(HSMExample example) {
this.example = example;
}

public Boolean getAddSecurityRecommendation() {
return addSecurityRecommendation;
}

public void setAddSecurityRecommendation(Boolean addSecurityRecommendation) {
this.addSecurityRecommendation = addSecurityRecommendation;
}

public Integer getCodeExpirationMinutes() {
return codeExpirationMinutes;
}

public void setCodeExpirationMinutes(Integer codeExpirationMinutes) {
this.codeExpirationMinutes = codeExpirationMinutes;
}
@Override
public String toString() {
return "HSMComponent{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.messagebird.objects.integrations;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

/**
Expand All @@ -10,79 +12,119 @@
*/
public class HSMComponentButton {

private HSMComponentButtonType type;
private String text;
private String url;
private String phone_number;
private List<String> example;

public HSMComponentButtonType getType() {
return type;
}

public void setType(HSMComponentButtonType type) {
this.type = type;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getPhone_number() {
return phone_number;
}

public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}

public List<String> getExample() {
return example;
}

public void setExample(List<String> example) {
this.example = example;
}

@Override
public String toString() {
return "HSMComponentButton{" +
"type=" + type +
", text='" + text + '\'' +
", url='" + url + '\'' +
", phone_number='" + phone_number + '\'' +
", example=" + example +
'}';
}

/**
* Check if example field is able to use.
*
* @throws IllegalArgumentException Occurs if button type is not {@code URL} or {@code QUICK_REPLY}.
*/
public void validateButtonExample() throws IllegalArgumentException {
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));

if (isExampleEmpty) {
return;
}

if (isNotProperType) {
throw new IllegalArgumentException("An example field in HSMComponentButton is available for only URL or QUICK_REPLY button types.");
}
}
private HSMComponentButtonType type;
private String text;
private String url;
private String phone_number;
private List<String> example;

//fields used by the authentification template
@JsonProperty("otp_type")
private HSMOTPButtonType otpType;
@JsonProperty("autofill_text")
private String autofillText;
@JsonProperty("package_name")
private String packageName;
@JsonProperty("signature_hash")
private String signatureHash;

public HSMComponentButtonType getType() {
return type;
}

public void setType(HSMComponentButtonType type) {
this.type = type;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getPhone_number() {
return phone_number;
}

public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}

public List<String> getExample() {
return example;
}

public void setExample(List<String> example) {
this.example = example;
}
public HSMOTPButtonType getOtpType() {
return otpType;
}

public void setOtpType(HSMOTPButtonType otpType) {
this.otpType = otpType;
}

public String getAutofillText() {
return autofillText;
}

public void setAutofillText(String autofillText) {
this.autofillText = autofillText;
}

public String getPackageName() {
return packageName;
}

public void setPackageName(String packageName) {
this.packageName = packageName;
}

public String getSignatureHash() {
return signatureHash;
}

public void setSignatureHash(String signatureHash) {
this.signatureHash = signatureHash;
}
@Override
public String toString() {
return "HSMComponentButton{" +
"type=" + type +
", text='" + text + '\'' +
", url='" + url + '\'' +
", phone_number='" + phone_number + '\'' +
", example=" + example +
'}';
}

/**
* Check if example field is able to use.
*
* @throws IllegalArgumentException Occurs if button type is not {@code URL} or {@code QUICK_REPLY}.
*/
public void validateButtonExample() throws IllegalArgumentException {
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));

if (isExampleEmpty) {
return;
}

if (isNotProperType) {
throw new IllegalArgumentException("An example field in HSMComponentButton is available for only URL or QUICK_REPLY button types.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum HSMComponentButtonType {

PHONE_NUMBER("PHONE_NUMBER"),
URL("URL"),
QUICK_REPLY("QUICK_REPLY");
QUICK_REPLY("QUICK_REPLY"),
OTP("OTP");

private final String type;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.messagebird.objects.integrations;

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

public enum HSMOTPButtonType {
ONE_TAP("ONE_TAP"),
COPY_CODE("COPY_CODE");

private final String type;

HSMOTPButtonType(String type) {
this.type = type;
}
@JsonCreator
public static HSMOTPButtonType forValue(String value) {
for (HSMOTPButtonType OTPButtonType : HSMOTPButtonType.values()) {
if (OTPButtonType.getType().equals(value)) {
return OTPButtonType;
}
}
return null;
}

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

public String getType() {
return type;
}

@Override
public String toString() {
return getType();
}
}
69 changes: 69 additions & 0 deletions examples/src/main/java/ExampleCreateAuthTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.integrations.*;

import java.util.ArrayList;
import java.util.List;

public class ExampleCreateAuthTemplate {
public static void main(String[] args) {

if (args.length < 3) {
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\"");
return;
}

// First create your service object
MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);

// Add the service to the client
MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);

/* body */
HSMComponent bodyComponent = new HSMComponent();
bodyComponent.setType(HSMComponentType.BODY);
bodyComponent.setAddSecurityRecommendation(true);

/* footer */
HSMComponent footerComponent = new HSMComponent();
footerComponent.setType(HSMComponentType.FOOTER);
footerComponent.setCodeExpirationMinutes(8);

/* button */
HSMComponent buttonComponent = new HSMComponent();
List<HSMComponentButton> buttons = new ArrayList<>();
HSMComponentButton otpButton = new HSMComponentButton();
otpButton.setOtpType(HSMOTPButtonType.ONE_TAP);
otpButton.setText("Copy code");
otpButton.setAutofillText("Autofill");
otpButton.setPackageName("com.example.luckyshrub");
otpButton.setSignatureHash("K8a%2FAINcGX7");

buttons.add(otpButton);
buttonComponent.setType(HSMComponentType.BUTTONS);
buttonComponent.setButtons(buttons);

/* set components */
Template template = new Template();
List<HSMComponent> components = new ArrayList<>();
components.add(bodyComponent);
components.add(footerComponent);
components.add(buttonComponent);

template.setName(args[1]);
template.setLanguage("en_US");
template.setWABAID(args[2]);
template.setComponents(components);
template.setCategory(HSMCategory.AUTHENTICATION);

try {
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
System.out.println(response.toString());
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
exception.printStackTrace();
}
}
}