Skip to content

Commit d3ec197

Browse files
authored
Merge pull request #206 from messagebird/version-4.0.0-whatsapp-template-updates
WhatsApp Templates updated with Multiple WABA Changes
2 parents 885143c + fd2ef08 commit d3ec197

15 files changed

+652
-92
lines changed

api/src/main/java/com/messagebird/MessageBirdClient.java

Lines changed: 139 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,34 @@ public TemplateList listWhatsAppTemplates(final int offset, final int limit)
18941894
return messageBirdService.requestList(url, offset, limit, TemplateList.class);
18951895
}
18961896

1897+
/**
1898+
* Gets a WhatsAppTemplate listing with specified pagination options and a wabaID or channelID filter.
1899+
*
1900+
* @param offset Number of objects to skip.
1901+
* @param limit Number of objects to take.
1902+
* @param wabaID The WABA ID to filter templates by.
1903+
* @param channelID A channel ID filter to return only templates that can be sent via that channel.
1904+
* @return List of templates.
1905+
* @throws UnauthorizedException if client is unauthorized
1906+
* @throws GeneralException general exception
1907+
* @throws IllegalArgumentException if the provided arguments are not valid
1908+
*/
1909+
public TemplateList listWhatsAppTemplates(final int offset, final int limit, final String wabaID, final String channelID)
1910+
throws UnauthorizedException, GeneralException, IllegalArgumentException {
1911+
validateWABAIDAndChannelIDArguments(wabaID, channelID);
1912+
1913+
Map<String, Object> map = new LinkedHashMap<>();
1914+
if (wabaID != null) map.put("wabaId", wabaID);
1915+
if (channelID != null) map.put("channelId", channelID);
1916+
String url = String.format(
1917+
"%s%s%s",
1918+
INTEGRATIONS_BASE_URL_V3,
1919+
INTEGRATIONS_WHATSAPP_PATH,
1920+
TEMPLATES_PATH
1921+
);
1922+
return messageBirdService.requestList(url, map, offset, limit, TemplateList.class);
1923+
}
1924+
18971925
/**
18981926
* Gets a template listing with default pagination options.
18991927
*
@@ -1913,12 +1941,13 @@ public TemplateList listWhatsAppTemplates() throws UnauthorizedException, Genera
19131941
*
19141942
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
19151943
* @return {@code List<TemplateResponse>} template list
1916-
* @throws UnauthorizedException if client is unauthorized
1917-
* @throws GeneralException general exception
1918-
* @throws NotFoundException if template name is not found
1944+
* @throws UnauthorizedException if client is unauthorized
1945+
* @throws GeneralException general exception
1946+
* @throws NotFoundException if template name is not found
1947+
* @throws IllegalArgumentException if the provided arguments are not valid
19191948
*/
19201949
public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName)
1921-
throws GeneralException, UnauthorizedException, NotFoundException {
1950+
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
19221951
if (templateName == null) {
19231952
throw new IllegalArgumentException("Template name must be specified.");
19241953
}
@@ -1933,19 +1962,50 @@ public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName)
19331962
return messageBirdService.requestByIdAsList(url, templateName, TemplateResponse.class);
19341963
}
19351964

1965+
/**
1966+
* Retrieves the template of an existing template name.
1967+
*
1968+
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1969+
* @param wabaID An optional WABA ID to look for the template ID under.
1970+
* @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template.
1971+
*
1972+
* @return {@code List<TemplateResponse>} template list
1973+
* @throws UnauthorizedException if client is unauthorized
1974+
* @throws GeneralException general exception
1975+
* @throws NotFoundException if template name is not found under the given WABA or cannot be sent under the supplied channel ID
1976+
* @throws IllegalArgumentException if the provided arguments are not valid
1977+
*/
1978+
public List<TemplateResponse> getWhatsAppTemplatesBy(final String templateName, final String wabaID, final String channelID)
1979+
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
1980+
if (templateName == null) {
1981+
throw new IllegalArgumentException("Template name must be specified.");
1982+
}
1983+
1984+
String id = String.format("%s%s", templateName, getWabaIDOrChannelIDQuery(wabaID, channelID));
1985+
String url = String.format(
1986+
"%s%s%s",
1987+
INTEGRATIONS_BASE_URL_V2,
1988+
INTEGRATIONS_WHATSAPP_PATH,
1989+
TEMPLATES_PATH
1990+
);
1991+
1992+
return messageBirdService.requestByIdAsList(url, id, TemplateResponse.class);
1993+
}
1994+
19361995
/**
1937-
* Retrieves the template of an existing template name and language.
1996+
* Retrieves the template of an existing template name and language under the first waba connected to the requesting user.
19381997
*
19391998
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
19401999
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
19412000
*
1942-
* @return {@code TemplateResponse} template list
2001+
* @return {@code TemplateResponse} template
19432002
* @throws UnauthorizedException if client is unauthorized
19442003
* @throws GeneralException general exception
1945-
* @throws NotFoundException if template name and language are not found
2004+
* @throws NotFoundException if template name and language are not found under the first waba connected to the requesting user.
2005+
* @throws IllegalArgumentException if the provided arguments are not valid
19462006
*/
19472007
public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language)
1948-
throws GeneralException, UnauthorizedException, NotFoundException {
2008+
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
19492009
if (templateName == null || language == null) {
19502010
throw new IllegalArgumentException("Template name and language must be specified.");
19512011
}
@@ -1962,51 +2022,98 @@ public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final
19622022
}
19632023

19642024
/**
1965-
* Delete templates of an existing template name.
2025+
* Retrieves the template of an existing template name and language under a WABA or for a channel.
19662026
*
1967-
* @param templateName A template name which is created on the MessageBird platform
1968-
* @throws UnauthorizedException if client is unauthorized
1969-
* @throws GeneralException general exception
1970-
* @throws NotFoundException if template name is not found
2027+
* @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
2028+
* @param language A language code as returned by getWhatsAppTemplateBy in the language variable
2029+
* @param wabaID An optional WABA ID to look for the template ID under.
2030+
* @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template.
2031+
*
2032+
* @return {@code TemplateResponse} template
2033+
* @throws UnauthorizedException if client is unauthorized
2034+
* @throws GeneralException general exception
2035+
* @throws NotFoundException if template name and language are not found under the given WABA or cannot be sent under the supplied channel ID.
2036+
* @throws IllegalArgumentException if the provided arguments are not valid
19712037
*/
1972-
public void deleteTemplatesBy(final String templateName)
1973-
throws UnauthorizedException, GeneralException, NotFoundException {
1974-
if (templateName == null) {
1975-
throw new IllegalArgumentException("Template name must be specified.");
2038+
public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language, final String wabaID, final String channelID)
2039+
throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
2040+
if (templateName == null || language == null) {
2041+
throw new IllegalArgumentException("Template name and language must be specified.");
19762042
}
19772043

19782044
String url = String.format(
1979-
"%s%s%s/%s",
1980-
INTEGRATIONS_BASE_URL_V2,
1981-
INTEGRATIONS_WHATSAPP_PATH,
1982-
TEMPLATES_PATH,
1983-
templateName
2045+
"%s%s%s/%s/%s%s",
2046+
INTEGRATIONS_BASE_URL_V2,
2047+
INTEGRATIONS_WHATSAPP_PATH,
2048+
TEMPLATES_PATH,
2049+
templateName,
2050+
language,
2051+
getWabaIDOrChannelIDQuery(wabaID, channelID)
19842052
);
1985-
messageBirdService.delete(url, null);
2053+
return messageBirdService.request(url, TemplateResponse.class);
19862054
}
19872055

19882056
/**
1989-
* Delete template of an existing template name and language.
2057+
* Validates the WABA ID and Channel ID argument pair.
2058+
*
2059+
* @param wabaID A WABA ID.
2060+
* @param channelID A channel ID.
2061+
* @throws IllegalArgumentException if the argument pair is invalid.
2062+
*/
2063+
private void validateWABAIDAndChannelIDArguments(String wabaID, String channelID)
2064+
throws IllegalArgumentException {
2065+
if (wabaID == null && channelID == null) {
2066+
throw new IllegalArgumentException("wabaID or channelID must be specified");
2067+
}
2068+
2069+
if (wabaID != null && channelID != null) {
2070+
throw new IllegalArgumentException("only supply wabaID or channelID - not both");
2071+
}
2072+
}
2073+
2074+
/**
2075+
* Validates the WABA ID and Channel ID argument pair and returns a valid query parameter string.
2076+
*
2077+
* @param wabaID A WABA ID.
2078+
* @param channelID A channel ID.
2079+
* @throws IllegalArgumentException if the argument pair is invalid.
2080+
*/
2081+
private String getWabaIDOrChannelIDQuery(String wabaID, String channelID)
2082+
throws IllegalArgumentException {
2083+
validateWABAIDAndChannelIDArguments(wabaID, channelID);
2084+
2085+
String query = "";
2086+
2087+
if (wabaID != null) {
2088+
query = String.format("?wabaId=%s", wabaID);
2089+
}
2090+
if (channelID != null) {
2091+
query = String.format("?channelId=%s", channelID);
2092+
}
2093+
2094+
return query;
2095+
}
2096+
2097+
/**
2098+
* Delete templates of an existing template name.
19902099
*
19912100
* @param templateName A template name which is created on the MessageBird platform
1992-
* @param language A language which is created on the MessageBird platform
19932101
* @throws UnauthorizedException if client is unauthorized
19942102
* @throws GeneralException general exception
1995-
* @throws NotFoundException if template name or language are not found
2103+
* @throws NotFoundException if template name is not found
19962104
*/
1997-
public void deleteTemplatesBy(final String templateName, final String language)
2105+
public void deleteTemplatesBy(final String templateName)
19982106
throws UnauthorizedException, GeneralException, NotFoundException {
1999-
if (templateName == null || language == null) {
2000-
throw new IllegalArgumentException("Template name and language must be specified.");
2107+
if (templateName == null) {
2108+
throw new IllegalArgumentException("Template name must be specified.");
20012109
}
20022110

20032111
String url = String.format(
2004-
"%s%s%s/%s/%s",
2112+
"%s%s%s/%s",
20052113
INTEGRATIONS_BASE_URL_V2,
20062114
INTEGRATIONS_WHATSAPP_PATH,
20072115
TEMPLATES_PATH,
2008-
templateName,
2009-
language
2116+
templateName
20102117
);
20112118
messageBirdService.delete(url, null);
20122119
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ public class Template {
1212

1313
private String name;
1414
private String language;
15+
private String wabaID;
1516
private List<HSMComponent> components;
1617
private HSMCategory category;
1718

1819
public Template() {
1920
}
2021

21-
public Template(String name, String language,
22-
List<HSMComponent> components, HSMCategory category) {
22+
public Template(String name, String language, String wabaID,
23+
List<HSMComponent> components, HSMCategory category) {
2324
this.name = name;
2425
this.language = language;
26+
this.wabaID = wabaID;
2527
this.components = components;
2628
this.category = category;
2729
}
@@ -42,6 +44,14 @@ public void setLanguage(String language) {
4244
this.language = language;
4345
}
4446

47+
public String getWABAID() {
48+
return wabaID;
49+
}
50+
51+
public void setWABAID(String wabaID) {
52+
this.wabaID = wabaID;
53+
}
54+
4555
public List<HSMComponent> getComponents() {
4656
return components;
4757
}
@@ -63,6 +73,7 @@ public String toString() {
6373
return "WhatsAppTemplate{" +
6474
"name='" + name + '\'' +
6575
", language='" + language + '\'' +
76+
", wabaID='" + wabaID + '\'' +
6677
", components=" + components +
6778
", category='" + category + '\'' +
6879
'}';
@@ -77,6 +88,7 @@ public void validate() throws IllegalArgumentException {
7788
this.validateComponents();
7889
this.validateName();
7990
this.validateLanguage();
91+
this.validateWABAID();
8092
this.validateCategory();
8193
}
8294

@@ -123,6 +135,19 @@ private void validateLanguage() {
123135
}
124136
}
125137

138+
/**
139+
* Check if wabaID field is valid.
140+
*
141+
* @throws IllegalArgumentException If wabaID field is null or empty string.
142+
*/
143+
private void validateWABAID() {
144+
if (this.wabaID == null) {
145+
throw new IllegalArgumentException("A \"wabaID\" field is required.");
146+
} else if (this.wabaID.length() == 0) {
147+
throw new IllegalArgumentException("A \"wabaID\" field can not be an empty string.");
148+
}
149+
}
150+
126151
/**
127152
* Check if category field is valid.
128153
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class TemplateResponse implements Serializable {
1818
private List<HSMComponent> components;
1919
private HSMStatus status;
2020
private String rejectedReason;
21+
private String wabaID;
22+
private String namespace;
2123
private Date createdAt;
2224
private Date updatedAt;
2325

@@ -73,6 +75,22 @@ public void setRejectedReason(String rejectedReason) {
7375
this.rejectedReason = rejectedReason;
7476
}
7577

78+
public String getWabaID() {
79+
return wabaID;
80+
}
81+
82+
public void setWabaID(String wabaID) {
83+
this.wabaID = wabaID;
84+
}
85+
86+
public String getNamespace() {
87+
return namespace;
88+
}
89+
90+
public void setNamespace(String namespace) {
91+
this.namespace = namespace;
92+
}
93+
7694
public Date getCreatedAt() {
7795
return createdAt;
7896
}
@@ -98,6 +116,8 @@ public String toString() {
98116
", components=" + components +
99117
", status='" + status + '\'' +
100118
", rejectedReason='" + rejectedReason + '\'' +
119+
", wabaID='" + wabaID + '\'' +
120+
", namespace='" + namespace + '\'' +
101121
", createdAt=" + createdAt +
102122
", updatedAt=" + updatedAt +
103123
'}';

0 commit comments

Comments
 (0)