Skip to content

Commit 4c3036c

Browse files
authored
Merge pull request #239 from messagebird/Issue_238_AddAuthTemplate
Issue238- Added support to create and read the OTP authentification template
2 parents 839b44e + 99bf129 commit 4c3036c

File tree

5 files changed

+248
-77
lines changed

5 files changed

+248
-77
lines changed

api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.messagebird.objects.integrations;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
35
import java.util.List;
46

57
/**
@@ -13,7 +15,11 @@ public class HSMComponent {
1315
private HSMComponentType type;
1416
private HSMComponentFormat format;
1517
private String text;
16-
private List<HSMComponentButton> buttons;
18+
@JsonProperty("add_security_recommendation")
19+
private Boolean addSecurityRecommendation;
20+
@JsonProperty("code_expiration_minutes")
21+
private Integer codeExpirationMinutes;
22+
private List<HSMComponentButton> buttons;
1723
private HSMExample example;
1824

1925
public HSMComponentType getType() {
@@ -56,6 +62,21 @@ public void setExample(HSMExample example) {
5662
this.example = example;
5763
}
5864

65+
public Boolean getAddSecurityRecommendation() {
66+
return addSecurityRecommendation;
67+
}
68+
69+
public void setAddSecurityRecommendation(Boolean addSecurityRecommendation) {
70+
this.addSecurityRecommendation = addSecurityRecommendation;
71+
}
72+
73+
public Integer getCodeExpirationMinutes() {
74+
return codeExpirationMinutes;
75+
}
76+
77+
public void setCodeExpirationMinutes(Integer codeExpirationMinutes) {
78+
this.codeExpirationMinutes = codeExpirationMinutes;
79+
}
5980
@Override
6081
public String toString() {
6182
return "HSMComponent{" +
Lines changed: 117 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.messagebird.objects.integrations;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
35
import java.util.List;
46

57
/**
@@ -10,79 +12,119 @@
1012
*/
1113
public class HSMComponentButton {
1214

13-
private HSMComponentButtonType type;
14-
private String text;
15-
private String url;
16-
private String phone_number;
17-
private List<String> example;
18-
19-
public HSMComponentButtonType getType() {
20-
return type;
21-
}
22-
23-
public void setType(HSMComponentButtonType type) {
24-
this.type = type;
25-
}
26-
27-
public String getText() {
28-
return text;
29-
}
30-
31-
public void setText(String text) {
32-
this.text = text;
33-
}
34-
35-
public String getUrl() {
36-
return url;
37-
}
38-
39-
public void setUrl(String url) {
40-
this.url = url;
41-
}
42-
43-
public String getPhone_number() {
44-
return phone_number;
45-
}
46-
47-
public void setPhone_number(String phone_number) {
48-
this.phone_number = phone_number;
49-
}
50-
51-
public List<String> getExample() {
52-
return example;
53-
}
54-
55-
public void setExample(List<String> example) {
56-
this.example = example;
57-
}
58-
59-
@Override
60-
public String toString() {
61-
return "HSMComponentButton{" +
62-
"type=" + type +
63-
", text='" + text + '\'' +
64-
", url='" + url + '\'' +
65-
", phone_number='" + phone_number + '\'' +
66-
", example=" + example +
67-
'}';
68-
}
69-
70-
/**
71-
* Check if example field is able to use.
72-
*
73-
* @throws IllegalArgumentException Occurs if button type is not {@code URL} or {@code QUICK_REPLY}.
74-
*/
75-
public void validateButtonExample() throws IllegalArgumentException {
76-
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
77-
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
78-
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));
79-
80-
if (isExampleEmpty) {
81-
return;
82-
}
83-
84-
if (isNotProperType) {
85-
throw new IllegalArgumentException("An example field in HSMComponentButton is available for only URL or QUICK_REPLY button types.");
86-
}
87-
}
15+
private HSMComponentButtonType type;
16+
private String text;
17+
private String url;
18+
private String phone_number;
19+
private List<String> example;
20+
21+
//fields used by the authentification template
22+
@JsonProperty("otp_type")
23+
private HSMOTPButtonType otpType;
24+
@JsonProperty("autofill_text")
25+
private String autofillText;
26+
@JsonProperty("package_name")
27+
private String packageName;
28+
@JsonProperty("signature_hash")
29+
private String signatureHash;
30+
31+
public HSMComponentButtonType getType() {
32+
return type;
33+
}
34+
35+
public void setType(HSMComponentButtonType type) {
36+
this.type = type;
37+
}
38+
39+
public String getText() {
40+
return text;
41+
}
42+
43+
public void setText(String text) {
44+
this.text = text;
45+
}
46+
47+
public String getUrl() {
48+
return url;
49+
}
50+
51+
public void setUrl(String url) {
52+
this.url = url;
53+
}
54+
55+
public String getPhone_number() {
56+
return phone_number;
57+
}
58+
59+
public void setPhone_number(String phone_number) {
60+
this.phone_number = phone_number;
61+
}
62+
63+
public List<String> getExample() {
64+
return example;
65+
}
66+
67+
public void setExample(List<String> example) {
68+
this.example = example;
69+
}
70+
public HSMOTPButtonType getOtpType() {
71+
return otpType;
72+
}
73+
74+
public void setOtpType(HSMOTPButtonType otpType) {
75+
this.otpType = otpType;
76+
}
77+
78+
public String getAutofillText() {
79+
return autofillText;
80+
}
81+
82+
public void setAutofillText(String autofillText) {
83+
this.autofillText = autofillText;
84+
}
85+
86+
public String getPackageName() {
87+
return packageName;
88+
}
89+
90+
public void setPackageName(String packageName) {
91+
this.packageName = packageName;
92+
}
93+
94+
public String getSignatureHash() {
95+
return signatureHash;
96+
}
97+
98+
public void setSignatureHash(String signatureHash) {
99+
this.signatureHash = signatureHash;
100+
}
101+
@Override
102+
public String toString() {
103+
return "HSMComponentButton{" +
104+
"type=" + type +
105+
", text='" + text + '\'' +
106+
", url='" + url + '\'' +
107+
", phone_number='" + phone_number + '\'' +
108+
", example=" + example +
109+
'}';
110+
}
111+
112+
/**
113+
* Check if example field is able to use.
114+
*
115+
* @throws IllegalArgumentException Occurs if button type is not {@code URL} or {@code QUICK_REPLY}.
116+
*/
117+
public void validateButtonExample() throws IllegalArgumentException {
118+
final boolean isExampleEmpty = this.example == null || this.example.isEmpty();
119+
final boolean isNotProperType = !(this.type.equals(HSMComponentButtonType.URL)
120+
|| this.type.equals(HSMComponentButtonType.QUICK_REPLY));
121+
122+
if (isExampleEmpty) {
123+
return;
124+
}
125+
126+
if (isNotProperType) {
127+
throw new IllegalArgumentException("An example field in HSMComponentButton is available for only URL or QUICK_REPLY button types.");
128+
}
129+
}
88130
}

api/src/main/java/com/messagebird/objects/integrations/HSMComponentButtonType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum HSMComponentButtonType {
1313

1414
PHONE_NUMBER("PHONE_NUMBER"),
1515
URL("URL"),
16-
QUICK_REPLY("QUICK_REPLY");
16+
QUICK_REPLY("QUICK_REPLY"),
17+
OTP("OTP");
1718

1819
private final String type;
1920

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.messagebird.objects.integrations;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum HSMOTPButtonType {
7+
ONE_TAP("ONE_TAP"),
8+
COPY_CODE("COPY_CODE");
9+
10+
private final String type;
11+
12+
HSMOTPButtonType(String type) {
13+
this.type = type;
14+
}
15+
@JsonCreator
16+
public static HSMOTPButtonType forValue(String value) {
17+
for (HSMOTPButtonType OTPButtonType : HSMOTPButtonType.values()) {
18+
if (OTPButtonType.getType().equals(value)) {
19+
return OTPButtonType;
20+
}
21+
}
22+
return null;
23+
}
24+
25+
@JsonValue
26+
public String toJson() {
27+
return getType();
28+
}
29+
30+
public String getType() {
31+
return type;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return getType();
37+
}
38+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.integrations.*;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class ExampleCreateAuthTemplate {
12+
public static void main(String[] args) {
13+
14+
if (args.length < 3) {
15+
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\"");
16+
return;
17+
}
18+
19+
// First create your service object
20+
MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
21+
22+
// Add the service to the client
23+
MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
24+
25+
/* body */
26+
HSMComponent bodyComponent = new HSMComponent();
27+
bodyComponent.setType(HSMComponentType.BODY);
28+
bodyComponent.setAddSecurityRecommendation(true);
29+
30+
/* footer */
31+
HSMComponent footerComponent = new HSMComponent();
32+
footerComponent.setType(HSMComponentType.FOOTER);
33+
footerComponent.setCodeExpirationMinutes(8);
34+
35+
/* button */
36+
HSMComponent buttonComponent = new HSMComponent();
37+
List<HSMComponentButton> buttons = new ArrayList<>();
38+
HSMComponentButton otpButton = new HSMComponentButton();
39+
otpButton.setOtpType(HSMOTPButtonType.ONE_TAP);
40+
otpButton.setText("Copy code");
41+
otpButton.setAutofillText("Autofill");
42+
otpButton.setPackageName("com.example.luckyshrub");
43+
otpButton.setSignatureHash("K8a%2FAINcGX7");
44+
45+
buttons.add(otpButton);
46+
buttonComponent.setType(HSMComponentType.BUTTONS);
47+
buttonComponent.setButtons(buttons);
48+
49+
/* set components */
50+
Template template = new Template();
51+
List<HSMComponent> components = new ArrayList<>();
52+
components.add(bodyComponent);
53+
components.add(footerComponent);
54+
components.add(buttonComponent);
55+
56+
template.setName(args[1]);
57+
template.setLanguage("en_US");
58+
template.setWABAID(args[2]);
59+
template.setComponents(components);
60+
template.setCategory(HSMCategory.AUTHENTICATION);
61+
62+
try {
63+
TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template);
64+
System.out.println(response.toString());
65+
} catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) {
66+
exception.printStackTrace();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)