From 99bf12976404544e9b2b65056bcb956074a33919 Mon Sep 17 00:00:00 2001 From: "Daniel A. Morales" Date: Tue, 13 Jun 2023 21:00:12 +0100 Subject: [PATCH] Added support to create and read the OTP authentification template --- .../objects/integrations/HSMComponent.java | 23 ++- .../integrations/HSMComponentButton.java | 192 +++++++++++------- .../integrations/HSMComponentButtonType.java | 3 +- .../integrations/HSMOTPButtonType.java | 38 ++++ .../main/java/ExampleCreateAuthTemplate.java | 69 +++++++ 5 files changed, 248 insertions(+), 77 deletions(-) create mode 100644 api/src/main/java/com/messagebird/objects/integrations/HSMOTPButtonType.java create mode 100644 examples/src/main/java/ExampleCreateAuthTemplate.java diff --git a/api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java b/api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java index 51b7efba..ccda2075 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java +++ b/api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java @@ -1,5 +1,7 @@ package com.messagebird.objects.integrations; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.List; /** @@ -13,7 +15,11 @@ public class HSMComponent { private HSMComponentType type; private HSMComponentFormat format; private String text; - private List buttons; + @JsonProperty("add_security_recommendation") + private Boolean addSecurityRecommendation; + @JsonProperty("code_expiration_minutes") + private Integer codeExpirationMinutes; + private List buttons; private HSMExample example; public HSMComponentType getType() { @@ -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{" + diff --git a/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButton.java b/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButton.java index 6438f79e..9bb4ffb1 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButton.java +++ b/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButton.java @@ -1,5 +1,7 @@ package com.messagebird.objects.integrations; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.List; /** @@ -10,79 +12,119 @@ */ public class HSMComponentButton { - private HSMComponentButtonType type; - private String text; - private String url; - private String phone_number; - private List 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 getExample() { - return example; - } - - public void setExample(List 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 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 getExample() { + return example; + } + + public void setExample(List 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."); + } + } } diff --git a/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java b/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java index 937f6baf..5029fc73 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java +++ b/api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java @@ -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; diff --git a/api/src/main/java/com/messagebird/objects/integrations/HSMOTPButtonType.java b/api/src/main/java/com/messagebird/objects/integrations/HSMOTPButtonType.java new file mode 100644 index 00000000..499ef1f3 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/integrations/HSMOTPButtonType.java @@ -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(); + } +} diff --git a/examples/src/main/java/ExampleCreateAuthTemplate.java b/examples/src/main/java/ExampleCreateAuthTemplate.java new file mode 100644 index 00000000..31b5f415 --- /dev/null +++ b/examples/src/main/java/ExampleCreateAuthTemplate.java @@ -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 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 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 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(); + } + } +} \ No newline at end of file