From 72cb9447ded08e87af13fa76e83e9415ce23bb80 Mon Sep 17 00:00:00 2001 From: David Oberholzer Date: Wed, 17 Aug 2022 16:14:38 +0200 Subject: [PATCH 1/3] WhatsApp Templates updated with WABA ID attribute and client methods updated. Version bumped to 4.0.0 due to non-backwards compatible changes. --- .../com/messagebird/MessageBirdClient.java | 171 +++++++++++--- .../messagebird/MessageBirdServiceImpl.java | 2 +- .../objects/integrations/Template.java | 29 ++- .../integrations/TemplateResponse.java | 20 ++ .../messagebird/MessageBirdClientTest.java | 220 +++++++++++++++++- .../test/java/com/messagebird/TestUtil.java | 5 + examples/pom.xml | 4 +- .../src/main/java/ExampleCreateTemplate.java | 7 +- ...xampleDeleteTemplateByNameAndLanguage.java | 40 ---- ...ExampleFetchTemplateByNameAndLanguage.java | 75 +++++- .../main/java/ExampleListTemplatesByName.java | 75 +++++- .../ExampleListTemplatesByWABAOrChannel.java | 66 ++++++ 12 files changed, 619 insertions(+), 95 deletions(-) delete mode 100644 examples/src/main/java/ExampleDeleteTemplateByNameAndLanguage.java create mode 100644 examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 4a2859a4..276ba3c8 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -1894,6 +1894,34 @@ public TemplateList listWhatsAppTemplates(final int offset, final int limit) return messageBirdService.requestList(url, offset, limit, TemplateList.class); } + /** + * Gets a WhatsAppTemplate listing with specified pagination options and a wabaID or channelID filter. + * + * @param offset Number of objects to skip. + * @param limit Number of objects to take. + * @param wabaID The WABA ID to filter templates by. + * @param channelID A channel ID filter to return only templates that can be sent via that channel. + * @return List of templates. + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws IllegalArgumentException if the provided arguments are not valid + */ + public TemplateList listWhatsAppTemplates(final int offset, final int limit, final String wabaID, final String channelID) + throws UnauthorizedException, GeneralException, IllegalArgumentException { + validateWABAIDAndChannelIDArguments(wabaID, channelID); + + Map map = new LinkedHashMap<>(); + if (wabaID != null) map.put("wabaId", wabaID); + if (channelID != null) map.put("channelId", channelID); + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V3, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + return messageBirdService.requestList(url, map, offset, limit, TemplateList.class); + } + /** * Gets a template listing with default pagination options. * @@ -1913,12 +1941,13 @@ public TemplateList listWhatsAppTemplates() throws UnauthorizedException, Genera * * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable * @return {@code List} template list - * @throws UnauthorizedException if client is unauthorized - * @throws GeneralException general exception - * @throws NotFoundException if template name is not found + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws NotFoundException if template name is not found + * @throws IllegalArgumentException if the provided arguments are not valid */ public List getWhatsAppTemplatesBy(final String templateName) - throws GeneralException, UnauthorizedException, NotFoundException { + throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException { if (templateName == null) { throw new IllegalArgumentException("Template name must be specified."); } @@ -1933,19 +1962,50 @@ public List getWhatsAppTemplatesBy(final String templateName) return messageBirdService.requestByIdAsList(url, templateName, TemplateResponse.class); } + /** + * Retrieves the template of an existing template name. + * + * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable + * @param wabaID An optional WABA ID to look for the template ID under. + * @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template. + * + * @return {@code List} template list + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws NotFoundException if template name is not found under the given WABA or cannot be sent under the supplied channel ID + * @throws IllegalArgumentException if the provided arguments are not valid + */ + public List getWhatsAppTemplatesBy(final String templateName, final String wabaID, final String channelID) + throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException { + if (templateName == null) { + throw new IllegalArgumentException("Template name must be specified."); + } + + String id = String.format("%s%s", templateName, getWabaIDOrChannelIDQuery(wabaID, channelID)); + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V2, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + + return messageBirdService.requestByIdAsList(url, id, TemplateResponse.class); + } + /** - * Retrieves the template of an existing template name and language. + * Retrieves the template of an existing template name and language under the first waba connected to the requesting user. * * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable * @param language A language code as returned by getWhatsAppTemplateBy in the language variable * - * @return {@code TemplateResponse} template list + * @return {@code TemplateResponse} template * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception - * @throws NotFoundException if template name and language are not found + * @throws NotFoundException if template name and language are not found under the first waba connected to the requesting user. + * @throws IllegalArgumentException if the provided arguments are not valid */ public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language) - throws GeneralException, UnauthorizedException, NotFoundException { + throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException { if (templateName == null || language == null) { throw new IllegalArgumentException("Template name and language must be specified."); } @@ -1962,51 +2022,98 @@ public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final } /** - * Delete templates of an existing template name. + * Retrieves the template of an existing template name and language under a WABA or for a channel. * - * @param templateName A template name which is created on the MessageBird platform - * @throws UnauthorizedException if client is unauthorized - * @throws GeneralException general exception - * @throws NotFoundException if template name is not found + * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable + * @param language A language code as returned by getWhatsAppTemplateBy in the language variable + * @param wabaID An optional WABA ID to look for the template ID under. + * @param channelID An optional channel ID to specify. If the template can be sent via the channel, it will return the template. + * + * @return {@code TemplateResponse} template + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws NotFoundException if template name and language are not found under the given WABA or cannot be sent under the supplied channel ID. + * @throws IllegalArgumentException if the provided arguments are not valid */ - public void deleteTemplatesBy(final String templateName) - throws UnauthorizedException, GeneralException, NotFoundException { - if (templateName == null) { - throw new IllegalArgumentException("Template name must be specified."); + public TemplateResponse fetchWhatsAppTemplateBy(final String templateName, final String language, final String wabaID, final String channelID) + throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException { + if (templateName == null || language == null) { + throw new IllegalArgumentException("Template name and language must be specified."); } String url = String.format( - "%s%s%s/%s", - INTEGRATIONS_BASE_URL_V2, - INTEGRATIONS_WHATSAPP_PATH, - TEMPLATES_PATH, - templateName + "%s%s%s/%s/%s%s", + INTEGRATIONS_BASE_URL_V2, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH, + templateName, + language, + getWabaIDOrChannelIDQuery(wabaID, channelID) ); - messageBirdService.delete(url, null); + return messageBirdService.request(url, TemplateResponse.class); } /** - * Delete template of an existing template name and language. + * Validates the WABA ID and Channel ID argument pair. + * + * @param wabaID A WABA ID. + * @param channelID A channel ID. + * @throws IllegalArgumentException if the argument pair is invalid. + */ + private void validateWABAIDAndChannelIDArguments(String wabaID, String channelID) + throws IllegalArgumentException { + if (wabaID == null && channelID == null) { + throw new IllegalArgumentException("wabaID or channelID must be specified"); + } + + if (wabaID != null && channelID != null) { + throw new IllegalArgumentException("only supply wabaID or channelID - not both"); + } + } + + /** + * Validates the WABA ID and Channel ID argument pair and returns a valid query parameter string. + * + * @param wabaID A WABA ID. + * @param channelID A channel ID. + * @throws IllegalArgumentException if the argument pair is invalid. + */ + private String getWabaIDOrChannelIDQuery(String wabaID, String channelID) + throws IllegalArgumentException { + validateWABAIDAndChannelIDArguments(wabaID, channelID); + + String query = ""; + + if (wabaID != null) { + query = String.format("?wabaId=%s", wabaID); + } + if (channelID != null) { + query = String.format("?channelId=%s", channelID); + } + + return query; + } + + /** + * Delete templates of an existing template name. * * @param templateName A template name which is created on the MessageBird platform - * @param language A language which is created on the MessageBird platform * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception - * @throws NotFoundException if template name or language are not found + * @throws NotFoundException if template name is not found */ - public void deleteTemplatesBy(final String templateName, final String language) + public void deleteTemplatesBy(final String templateName) throws UnauthorizedException, GeneralException, NotFoundException { - if (templateName == null || language == null) { - throw new IllegalArgumentException("Template name and language must be specified."); + if (templateName == null) { + throw new IllegalArgumentException("Template name must be specified."); } String url = String.format( - "%s%s%s/%s/%s", + "%s%s%s/%s", INTEGRATIONS_BASE_URL_V2, INTEGRATIONS_WHATSAPP_PATH, TEMPLATES_PATH, - templateName, - language + templateName ); messageBirdService.delete(url, null); } diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index a6cf289a..d8cc63ce 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -74,7 +74,7 @@ public class MessageBirdServiceImpl implements MessageBirdService { private final String accessKey; private final String serviceUrl; - private final String clientVersion = "3.2.6"; + private final String clientVersion = "4.0.0"; private final String userAgentString; private Proxy proxy = null; diff --git a/api/src/main/java/com/messagebird/objects/integrations/Template.java b/api/src/main/java/com/messagebird/objects/integrations/Template.java index 715aeb48..333c7ec3 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/Template.java +++ b/api/src/main/java/com/messagebird/objects/integrations/Template.java @@ -12,16 +12,18 @@ public class Template { private String name; private String language; + private String wabaID; private List components; private HSMCategory category; public Template() { } - public Template(String name, String language, - List components, HSMCategory category) { + public Template(String name, String language, String wabaID, + List components, HSMCategory category) { this.name = name; this.language = language; + this.wabaID = wabaID; this.components = components; this.category = category; } @@ -42,6 +44,14 @@ public void setLanguage(String language) { this.language = language; } + public String getWabaID() { + return wabaID; + } + + public void setWabaID(String wabaID) { + this.wabaID = wabaID; + } + public List getComponents() { return components; } @@ -63,6 +73,7 @@ public String toString() { return "WhatsAppTemplate{" + "name='" + name + '\'' + ", language='" + language + '\'' + + ", wabaID='" + wabaID + '\'' + ", components=" + components + ", category='" + category + '\'' + '}'; @@ -77,6 +88,7 @@ public void validate() throws IllegalArgumentException { this.validateComponents(); this.validateName(); this.validateLanguage(); + this.validateWABAID(); this.validateCategory(); } @@ -123,6 +135,19 @@ private void validateLanguage() { } } + /** + * Check if wabaID field is valid. + * + * @throws IllegalArgumentException If wabaID field is null or empty string. + */ + private void validateWABAID() { + if (this.wabaID == null) { + throw new IllegalArgumentException("A \"wabaID\" field is required."); + } else if (this.wabaID.length() == 0) { + throw new IllegalArgumentException("A \"wabaID\" field can not be an empty string."); + } + } + /** * Check if category field is valid. * diff --git a/api/src/main/java/com/messagebird/objects/integrations/TemplateResponse.java b/api/src/main/java/com/messagebird/objects/integrations/TemplateResponse.java index bce35f82..426ae64a 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/TemplateResponse.java +++ b/api/src/main/java/com/messagebird/objects/integrations/TemplateResponse.java @@ -18,6 +18,8 @@ public class TemplateResponse implements Serializable { private List components; private HSMStatus status; private String rejectedReason; + private String wabaID; + private String namespace; private Date createdAt; private Date updatedAt; @@ -73,6 +75,22 @@ public void setRejectedReason(String rejectedReason) { this.rejectedReason = rejectedReason; } + public String getWabaID() { + return wabaID; + } + + public void setWabaID(String wabaID) { + this.wabaID = wabaID; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + public Date getCreatedAt() { return createdAt; } @@ -98,6 +116,8 @@ public String toString() { ", components=" + components + ", status='" + status + '\'' + ", rejectedReason='" + rejectedReason + '\'' + + ", wabaID='" + wabaID + '\'' + + ", namespace='" + namespace + '\'' + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + '}'; diff --git a/api/src/test/java/com/messagebird/MessageBirdClientTest.java b/api/src/test/java/com/messagebird/MessageBirdClientTest.java index bfc254aa..50459497 100644 --- a/api/src/test/java/com/messagebird/MessageBirdClientTest.java +++ b/api/src/test/java/com/messagebird/MessageBirdClientTest.java @@ -1066,6 +1066,7 @@ public void testCreateWhatsAppTemplate() throws UnauthorizedException, GeneralEx assertEquals(response.getLanguage(), templateResponse.getLanguage()); assertEquals(response.getCategory(), templateResponse.getCategory()); assertEquals(response.getStatus(), templateResponse.getStatus()); + assertEquals(response.getWabaID(), templateResponse.getWabaID()); assertEquals(response.getCreatedAt(), templateResponse.getCreatedAt()); assertEquals(response.getUpdatedAt(), templateResponse.getUpdatedAt()); @@ -1101,6 +1102,86 @@ public void testListWhatsAppTemplates() throws UnauthorizedException, GeneralExc } } + @Test + public void testListWhatsAppTemplatesDefault() throws UnauthorizedException, GeneralException { + final TemplateList templateList = TestUtil.createWhatsAppTemplateList("sample_template_name"); + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V3, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + + when(messageBirdServiceMock.requestList(url, 0, 10, TemplateList.class)) + .thenReturn(templateList); + + final TemplateList response = messageBirdClientInjectMock.listWhatsAppTemplates(); + verify(messageBirdServiceMock, times(1)).requestList(url, 0, 10, TemplateList.class); + assertNotNull(response); + for(int i = 0; i < response.getItems().size() ; i++) { + assertReflectionEquals(response.getItems().get(i), templateList.getItems().get(i)); + } + } + + @Test + public void testListWhatsAppTemplatesByWABAID() throws UnauthorizedException, GeneralException { + final TemplateList templateList = TestUtil.createWhatsAppTemplateList("sample_template_name"); + final String wabaID = "testWABAID"; + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V3, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + + Map map = new LinkedHashMap<>(); + map.put("wabaId", wabaID); + + when(messageBirdServiceMock.requestList(url, map, 0, 0, TemplateList.class)) + .thenReturn(templateList); + + final TemplateList response = messageBirdClientInjectMock.listWhatsAppTemplates(0, 0, wabaID, null); + verify(messageBirdServiceMock, times(1)).requestList(url, map, 0, 0, TemplateList.class); + assertNotNull(response); + for(int i = 0; i < response.getItems().size() ; i++) { + assertReflectionEquals(response.getItems().get(i), templateList.getItems().get(i)); + } + } + + @Test + public void testListWhatsAppTemplatesByChannelID() throws UnauthorizedException, GeneralException { + final TemplateList templateList = TestUtil.createWhatsAppTemplateList("sample_template_name"); + final String channelID = "channel-id"; + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V3, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + + Map map = new LinkedHashMap<>(); + map.put("channelId", channelID); + + when(messageBirdServiceMock.requestList(url, map, 0, 0, TemplateList.class)) + .thenReturn(templateList); + + final TemplateList response = messageBirdClientInjectMock.listWhatsAppTemplates(0, 0, null, channelID); + verify(messageBirdServiceMock, times(1)).requestList(url, map, 0, 0, TemplateList.class); + assertNotNull(response); + for(int i = 0; i < response.getItems().size() ; i++) { + assertReflectionEquals(response.getItems().get(i), templateList.getItems().get(i)); + } + } + @Test public void testGetWhatsAppTemplatesBy() throws GeneralException, UnauthorizedException, NotFoundException { final String templateName = "sample_template_name"; @@ -1132,6 +1213,80 @@ public void testGetWhatsAppTemplatesBy() throws GeneralException, UnauthorizedEx } } + @Test + public void testGetWhatsAppTemplatesByNameAndWABAID() throws GeneralException, UnauthorizedException, NotFoundException { + final String templateName = "sample_template_name"; + final String wabaID = "testWABAID"; + final TemplateResponse templateResponse1 = TestUtil.createWhatsAppTemplateResponse(templateName, "en_US"); + final TemplateResponse templateResponse2 = TestUtil.createWhatsAppTemplateResponse("another_template", "en_US"); + final List templateList = new ArrayList<>(); + templateList.add(templateResponse1); + templateList.add(templateResponse2); + + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V2, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + String id = String.format( + "%s?wabaId=%s", + templateName, + wabaID + ); + + when(messageBirdServiceMock.requestByIdAsList(url, id, TemplateResponse.class)) + .thenReturn(templateList); + + final List response = messageBirdClientInjectMock.getWhatsAppTemplatesBy(templateName, wabaID, null); + verify(messageBirdServiceMock, times(1)).requestByIdAsList(url, id, TemplateResponse.class); + assertNotNull(response); + assertEquals(response.size(), templateList.size()); + for(int i = 0; i < response.size() ; i++) { + assertReflectionEquals(response.get(i), templateList.get(i)); + } + } + + @Test + public void testGetWhatsAppTemplatesByNameForChannelID() throws GeneralException, UnauthorizedException, NotFoundException { + final String templateName = "sample_template_name"; + final String channelID = "channel-id"; + final TemplateResponse templateResponse1 = TestUtil.createWhatsAppTemplateResponse(templateName, "en_US"); + final TemplateResponse templateResponse2 = TestUtil.createWhatsAppTemplateResponse("another_template", "en_US"); + final List templateList = new ArrayList<>(); + templateList.add(templateResponse1); + templateList.add(templateResponse2); + + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s", + INTEGRATIONS_BASE_URL_V2, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH + ); + String id = String.format( + "%s?channelId=%s", + templateName, + channelID + ); + + when(messageBirdServiceMock.requestByIdAsList(url, id, TemplateResponse.class)) + .thenReturn(templateList); + + final List response = messageBirdClientInjectMock.getWhatsAppTemplatesBy(templateName, null, channelID); + verify(messageBirdServiceMock, times(1)).requestByIdAsList(url, id, TemplateResponse.class); + assertNotNull(response); + assertEquals(response.size(), templateList.size()); + for(int i = 0; i < response.size() ; i++) { + assertReflectionEquals(response.get(i), templateList.get(i)); + } + } + @Test public void testFetchWhatsAppTemplateBy() throws UnauthorizedException, GeneralException, NotFoundException { final String templateName = "sample_template_name"; @@ -1163,45 +1318,86 @@ public void testFetchWhatsAppTemplateBy() throws UnauthorizedException, GeneralE } @Test - public void testDeleteTemplatesByName() throws UnauthorizedException, GeneralException, NotFoundException { + public void testFetchWhatsAppTemplateByNameAndLanguageAndWABAID() throws UnauthorizedException, GeneralException, NotFoundException { final String templateName = "sample_template_name"; + final String language = "ko"; + final String wabaID = "testWABAID"; + final TemplateResponse template = TestUtil.createWhatsAppTemplateResponse(templateName, language); MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); String url = String.format( - "%s%s%s/%s", + "%s%s%s/%s/%s?wabaId=%s", INTEGRATIONS_BASE_URL_V2, INTEGRATIONS_WHATSAPP_PATH, TEMPLATES_PATH, - templateName + templateName, + language, + wabaID ); - when(messageBirdServiceMock.delete(url, null)).thenReturn(null); - messageBirdClientInjectMock.deleteTemplatesBy(templateName); - verify(messageBirdServiceMock).delete(url, null); + when(messageBirdServiceMock.request(url, TemplateResponse.class)) + .thenReturn(template); + + final TemplateResponse response = messageBirdClientInjectMock.fetchWhatsAppTemplateBy(templateName, language, wabaID, null); + verify(messageBirdServiceMock, times(1)).request(url, TemplateResponse.class); + assertNotNull(response); + assertEquals(response.getName(), template.getName()); + assertEquals(response.getLanguage(), template.getLanguage()); + assertEquals(response.getStatus(), template.getStatus()); + assertReflectionEquals(response.getComponents(), template.getComponents()); } @Test - public void testDeleteTemplatesByNameAndLanguage() - throws UnauthorizedException, GeneralException, NotFoundException { + public void testFetchWhatsAppTemplateByNameAndLanguageForChannelID() throws UnauthorizedException, GeneralException, NotFoundException { final String templateName = "sample_template_name"; - final String language = "en_US"; + final String language = "ko"; + final String channelID = "channel-id"; + final TemplateResponse template = TestUtil.createWhatsAppTemplateResponse(templateName, language); MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); String url = String.format( - "%s%s%s/%s/%s", + "%s%s%s/%s/%s?channelId=%s", INTEGRATIONS_BASE_URL_V2, INTEGRATIONS_WHATSAPP_PATH, TEMPLATES_PATH, templateName, - language + language, + channelID + ); + + when(messageBirdServiceMock.request(url, TemplateResponse.class)) + .thenReturn(template); + + final TemplateResponse response = messageBirdClientInjectMock.fetchWhatsAppTemplateBy(templateName, language, null, channelID); + verify(messageBirdServiceMock, times(1)).request(url, TemplateResponse.class); + assertNotNull(response); + assertEquals(response.getName(), template.getName()); + assertEquals(response.getLanguage(), template.getLanguage()); + assertEquals(response.getStatus(), template.getStatus()); + assertReflectionEquals(response.getComponents(), template.getComponents()); + } + + @Test + public void testDeleteTemplatesByName() throws UnauthorizedException, GeneralException, NotFoundException { + final String templateName = "sample_template_name"; + + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + + String url = String.format( + "%s%s%s/%s", + INTEGRATIONS_BASE_URL_V2, + INTEGRATIONS_WHATSAPP_PATH, + TEMPLATES_PATH, + templateName ); when(messageBirdServiceMock.delete(url, null)).thenReturn(null); - messageBirdClientInjectMock.deleteTemplatesBy(templateName, language); + messageBirdClientInjectMock.deleteTemplatesBy(templateName); verify(messageBirdServiceMock).delete(url, null); } diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index 9bf28eb8..f8d8fb43 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -316,6 +316,9 @@ public static TemplateResponse createWhatsAppTemplateResponse(final String templ components.add(createHSMComponentButton()); templateResponse.setComponents(components); + templateResponse.setWabaID("testWABAID"); + templateResponse.setNamespace("testNamespace"); + return templateResponse; } @@ -332,6 +335,8 @@ public static Template createWhatsAppTemplate(final String templateName, final S components.add(createHSMComponentButton()); template.setComponents(components); + template.setWabaID("testWABAID"); + return template; } diff --git a/examples/pom.xml b/examples/pom.xml index 43716020..020e33d4 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -6,7 +6,7 @@ com.messagebird examples - 3.2.6 + 4.0.0 @@ -20,7 +20,7 @@ com.messagebird messagebird-api - 3.2.6 + 4.0.0 compile diff --git a/examples/src/main/java/ExampleCreateTemplate.java b/examples/src/main/java/ExampleCreateTemplate.java index 8e2d9887..fe18ea22 100644 --- a/examples/src/main/java/ExampleCreateTemplate.java +++ b/examples/src/main/java/ExampleCreateTemplate.java @@ -25,8 +25,8 @@ public class ExampleCreateTemplate { public static void main(String[] args) { - if (args.length < 2) { - System.out.println("Please specify your access key and a template name example : java -jar test_accesskey \"My template name\""); + 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; } @@ -84,13 +84,14 @@ public static void main(String[] args) { template.setName(args[1]); template.setLanguage("en_US"); + template.setWABAID(args[2]) template.setComponents(components); template.setCategory(HSMCategory.ACCOUNT_UPDATE); try { TemplateResponse response = messageBirdClient.createWhatsAppTemplate(template); System.out.println(response.toString()); - } catch (GeneralException | UnauthorizedException exception) { + } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { exception.printStackTrace(); } } diff --git a/examples/src/main/java/ExampleDeleteTemplateByNameAndLanguage.java b/examples/src/main/java/ExampleDeleteTemplateByNameAndLanguage.java deleted file mode 100644 index 1b215f10..00000000 --- a/examples/src/main/java/ExampleDeleteTemplateByNameAndLanguage.java +++ /dev/null @@ -1,40 +0,0 @@ -import com.messagebird.MessageBirdClient; -import com.messagebird.MessageBirdService; -import com.messagebird.MessageBirdServiceImpl; -import com.messagebird.exceptions.GeneralException; -import com.messagebird.exceptions.NotFoundException; -import com.messagebird.exceptions.UnauthorizedException; - -/** - * Delete template by name and language - * - * @see Delete template by name and language - * @author ssk910 - */ -public class ExampleDeleteTemplateByNameAndLanguage { - - public static void main(String[] args) { - if (args.length < 3) { - System.out.println("Please specify your access key and a template name example : java -jar test_accesskey \"My template name\" \"Template language\""); - 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); - - // template name and langugae from input - final String templateName = args[1]; - final String language = args[2]; - - try { - System.out.println("Deleting WhatsApp Template list by {name: " + templateName + ", language: " + language + "}"); - messageBirdClient.deleteTemplatesBy(templateName, language); - System.out.println("Deleted {name: " + templateName + ", language: " + language + "}"); - } catch (GeneralException | UnauthorizedException | NotFoundException exception) { - exception.printStackTrace(); - } - } -} diff --git a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java index 02377ed0..219ee36d 100644 --- a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java +++ b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java @@ -26,7 +26,7 @@ public static void main(String[] args) { // Add the service to the client final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); - // template name and langugae from input + // template name and language from input final String templateName = args[1]; final String language = args[2]; @@ -34,7 +34,78 @@ public static void main(String[] args) { System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + "}"); final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language); System.out.println(template.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException exception) { + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} + +/** + * Fetch template by name and language by WABA ID + * + * @see Fetch template by name and language by WABA ID + * @author ssk910 + */ +public class ExampleFetchTemplateByNameAndLanguage { + + public static void main(String[] args) { + if (args.length < 4) { + System.out.println("Please specify your access key, template name, language and WABA ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"WABA ID\""); + 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); + + // template name, language and waba ID from input + final String templateName = args[1]; + final String language = args[2]; + final String wabaID = args[3]; + + try { + System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", wabaID: " + wabaID + "}"); + final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, wabaID, null); + System.out.println(template.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} + +/** + * Fetch template by name and language for Channel ID + * + * @see Fetch template by name and language for Channel ID + * @author ssk910 + */ +public class ExampleFetchTemplateByNameAndLanguage { + + public static void main(String[] args) { + if (args.length < 4) { + System.out.println("Please specify your access key, template name, language and Channel ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"Channel ID\""); + 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); + + // template name, language and the channel ID from input + final String templateName = args[1]; + final String language = args[2]; + final String channelID = args[3]; + + // Will return a template only if the channel belongs to the same WABA that the template belongs to. + try { + System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", channelID: " + channelID + "}"); + final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, null, channelID); + System.out.println(template.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { exception.printStackTrace(); } } diff --git a/examples/src/main/java/ExampleListTemplatesByName.java b/examples/src/main/java/ExampleListTemplatesByName.java index 36159796..67fb2936 100644 --- a/examples/src/main/java/ExampleListTemplatesByName.java +++ b/examples/src/main/java/ExampleListTemplatesByName.java @@ -34,7 +34,80 @@ public static void main(String[] args) { System.out.println("Retrieving WhatsApp Template list by name : " + templateName); final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName); System.out.println(templateList.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException exception) { + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} + +/** + * List templates by name and WABA ID + * + * @see List templates by name and WABA ID + * @author ssk910 + */ +public class ExampleListTemplatesByNameAndWABAID { + + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Please specify your access key, template name and WABA ID example : java -jar test_accesskey \"My template name\" \"WABA ID\""); + 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); + + // template name from input + final String templateName = args[1]; + + // WABA ID from input + final String wabaID = args[2]; + + try { + System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and WABA ID '" + wabaID + "'"); + final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, wabaID, null); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} + +/** + * List templates by name and for channel ID + * + * @see List templates by name and for channel ID + * @author ssk910 + */ +public class ExampleListTemplatesByNameAndForChannelID { + + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Please specify your access key, template name and channel ID example : java -jar test_accesskey \"My template name\" \"Channel ID\""); + 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); + + // template name from input + final String templateName = args[1]; + + // Channel ID from input + final String channelID = args[2]; + + // Will return templates only if the channel belongs to the same WABA that the templates belongs to. + try { + System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and channel ID '" + channelID + "'"); + final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, null, channelID); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { exception.printStackTrace(); } } diff --git a/examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java b/examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java new file mode 100644 index 00000000..e4971182 --- /dev/null +++ b/examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java @@ -0,0 +1,66 @@ +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.TemplateList; + +/** + * List templates by WABA ID + * + * @see List templates by WABA ID + * @author ssk910 + */ +public class ExampleListTemplatesByWABAID { + + public static void main(String[] args) { + if (args.length == 0) { + System.out.println("Please specify your access key and WABA ID example : java -jar test_accesskey \"WABA ID\""); + 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); + + try { + System.out.println("Retrieving WhatsApp Template list by WABA"); + final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, "your-waba-id", null); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} + +/** + * List templates for Channel ID + * + * @see List templates for Channel ID + * @author ssk910 + */ +public class ExampleListTemplatesForChannelID { + + public static void main(String[] args) { + if (args.length == 0) { + System.out.println("Please specify your access key example : java -jar test_accesskey"); + 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); + + try { + System.out.println("Retrieving WhatsApp Template list for a channel"); + final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, null, "channel-id"); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} From f1a1c97ce5455f6702cbebbae27c0b67da9bf859 Mon Sep 17 00:00:00 2001 From: David Oberholzer Date: Fri, 19 Aug 2022 11:00:40 +0200 Subject: [PATCH 2/3] Remove version change in this PR --- api/src/main/java/com/messagebird/MessageBirdServiceImpl.java | 2 +- examples/pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java index d8cc63ce..a6cf289a 100644 --- a/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java +++ b/api/src/main/java/com/messagebird/MessageBirdServiceImpl.java @@ -74,7 +74,7 @@ public class MessageBirdServiceImpl implements MessageBirdService { private final String accessKey; private final String serviceUrl; - private final String clientVersion = "4.0.0"; + private final String clientVersion = "3.2.6"; private final String userAgentString; private Proxy proxy = null; diff --git a/examples/pom.xml b/examples/pom.xml index 020e33d4..43716020 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -6,7 +6,7 @@ com.messagebird examples - 4.0.0 + 3.2.6 @@ -20,7 +20,7 @@ com.messagebird messagebird-api - 4.0.0 + 3.2.6 compile From fd2ef082d70c39db7c72bbd8c5a1139b7fff3347 Mon Sep 17 00:00:00 2001 From: David Oberholzer Date: Fri, 19 Aug 2022 11:35:29 +0200 Subject: [PATCH 3/3] Examples fixed --- .../objects/integrations/Template.java | 4 +- .../test/java/com/messagebird/TestUtil.java | 2 +- .../src/main/java/ExampleCreateTemplate.java | 2 +- ...ExampleFetchTemplateByNameAndLanguage.java | 71 ------------------ ...tchTemplateByNameAndLanguageAndWABAID.java | 42 +++++++++++ ...TemplateByNameAndLanguageForChannelID.java | 43 +++++++++++ .../main/java/ExampleListTemplatesByName.java | 73 ------------------- ...pleListTemplatesByNameAndForChannelID.java | 45 ++++++++++++ .../ExampleListTemplatesByNameAndWABAID.java | 44 +++++++++++ ...java => ExampleListTemplatesByWABAID.java} | 36 +-------- .../ExampleListTemplatesForChannelID.java | 36 +++++++++ 11 files changed, 217 insertions(+), 181 deletions(-) create mode 100644 examples/src/main/java/ExampleFetchTemplateByNameAndLanguageAndWABAID.java create mode 100644 examples/src/main/java/ExampleFetchTemplateByNameAndLanguageForChannelID.java create mode 100644 examples/src/main/java/ExampleListTemplatesByNameAndForChannelID.java create mode 100644 examples/src/main/java/ExampleListTemplatesByNameAndWABAID.java rename examples/src/main/java/{ExampleListTemplatesByWABAOrChannel.java => ExampleListTemplatesByWABAID.java} (52%) create mode 100644 examples/src/main/java/ExampleListTemplatesForChannelID.java diff --git a/api/src/main/java/com/messagebird/objects/integrations/Template.java b/api/src/main/java/com/messagebird/objects/integrations/Template.java index 333c7ec3..a61741f5 100644 --- a/api/src/main/java/com/messagebird/objects/integrations/Template.java +++ b/api/src/main/java/com/messagebird/objects/integrations/Template.java @@ -44,11 +44,11 @@ public void setLanguage(String language) { this.language = language; } - public String getWabaID() { + public String getWABAID() { return wabaID; } - public void setWabaID(String wabaID) { + public void setWABAID(String wabaID) { this.wabaID = wabaID; } diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index f8d8fb43..34b6afc1 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -335,7 +335,7 @@ public static Template createWhatsAppTemplate(final String templateName, final S components.add(createHSMComponentButton()); template.setComponents(components); - template.setWabaID("testWABAID"); + template.setWABAID("testWABAID"); return template; } diff --git a/examples/src/main/java/ExampleCreateTemplate.java b/examples/src/main/java/ExampleCreateTemplate.java index fe18ea22..5ca63530 100644 --- a/examples/src/main/java/ExampleCreateTemplate.java +++ b/examples/src/main/java/ExampleCreateTemplate.java @@ -84,7 +84,7 @@ public static void main(String[] args) { template.setName(args[1]); template.setLanguage("en_US"); - template.setWABAID(args[2]) + template.setWABAID(args[2]); template.setComponents(components); template.setCategory(HSMCategory.ACCOUNT_UPDATE); diff --git a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java index 219ee36d..c28d124c 100644 --- a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java +++ b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguage.java @@ -39,74 +39,3 @@ public static void main(String[] args) { } } } - -/** - * Fetch template by name and language by WABA ID - * - * @see Fetch template by name and language by WABA ID - * @author ssk910 - */ -public class ExampleFetchTemplateByNameAndLanguage { - - public static void main(String[] args) { - if (args.length < 4) { - System.out.println("Please specify your access key, template name, language and WABA ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"WABA ID\""); - 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); - - // template name, language and waba ID from input - final String templateName = args[1]; - final String language = args[2]; - final String wabaID = args[3]; - - try { - System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", wabaID: " + wabaID + "}"); - final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, wabaID, null); - System.out.println(template.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { - exception.printStackTrace(); - } - } -} - -/** - * Fetch template by name and language for Channel ID - * - * @see Fetch template by name and language for Channel ID - * @author ssk910 - */ -public class ExampleFetchTemplateByNameAndLanguage { - - public static void main(String[] args) { - if (args.length < 4) { - System.out.println("Please specify your access key, template name, language and Channel ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"Channel ID\""); - 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); - - // template name, language and the channel ID from input - final String templateName = args[1]; - final String language = args[2]; - final String channelID = args[3]; - - // Will return a template only if the channel belongs to the same WABA that the template belongs to. - try { - System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", channelID: " + channelID + "}"); - final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, null, channelID); - System.out.println(template.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { - exception.printStackTrace(); - } - } -} diff --git a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageAndWABAID.java b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageAndWABAID.java new file mode 100644 index 00000000..deb12bf8 --- /dev/null +++ b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageAndWABAID.java @@ -0,0 +1,42 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.integrations.TemplateResponse; + +/** + * Fetch template by name and language by WABA ID + * + * @see Fetch template by name and language by WABA ID + * @author ssk910 + */ +public class ExampleFetchTemplateByNameAndLanguageAndWABAID { + + public static void main(String[] args) { + if (args.length < 4) { + System.out.println("Please specify your access key, template name, language and WABA ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"WABA ID\""); + 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); + + // template name, language and waba ID from input + final String templateName = args[1]; + final String language = args[2]; + final String wabaID = args[3]; + + try { + System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", wabaID: " + wabaID + "}"); + final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, wabaID, null); + System.out.println(template.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageForChannelID.java b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageForChannelID.java new file mode 100644 index 00000000..639370e2 --- /dev/null +++ b/examples/src/main/java/ExampleFetchTemplateByNameAndLanguageForChannelID.java @@ -0,0 +1,43 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.integrations.TemplateResponse; + +/** + * Fetch template by name and language for Channel ID + * + * @see Fetch template by name and language for Channel ID + * @author ssk910 + */ +public class ExampleFetchTemplateByNameAndLanguageForChannelID { + + public static void main(String[] args) { + if (args.length < 4) { + System.out.println("Please specify your access key, template name, language and Channel ID example : java -jar test_accesskey \"My template name\" \"Template language\" \"Channel ID\""); + 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); + + // template name, language and the channel ID from input + final String templateName = args[1]; + final String language = args[2]; + final String channelID = args[3]; + + // Will return a template only if the channel belongs to the same WABA that the template belongs to. + try { + System.out.println("Fetching WhatsApp Template list by {name: " + templateName + ", language: " + language + ", channelID: " + channelID + "}"); + final TemplateResponse template = messageBirdClient.fetchWhatsAppTemplateBy(templateName, language, null, channelID); + System.out.println(template.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/examples/src/main/java/ExampleListTemplatesByName.java b/examples/src/main/java/ExampleListTemplatesByName.java index 67fb2936..33241ca6 100644 --- a/examples/src/main/java/ExampleListTemplatesByName.java +++ b/examples/src/main/java/ExampleListTemplatesByName.java @@ -39,76 +39,3 @@ public static void main(String[] args) { } } } - -/** - * List templates by name and WABA ID - * - * @see List templates by name and WABA ID - * @author ssk910 - */ -public class ExampleListTemplatesByNameAndWABAID { - - public static void main(String[] args) { - if (args.length < 3) { - System.out.println("Please specify your access key, template name and WABA ID example : java -jar test_accesskey \"My template name\" \"WABA ID\""); - 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); - - // template name from input - final String templateName = args[1]; - - // WABA ID from input - final String wabaID = args[2]; - - try { - System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and WABA ID '" + wabaID + "'"); - final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, wabaID, null); - System.out.println(templateList.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { - exception.printStackTrace(); - } - } -} - -/** - * List templates by name and for channel ID - * - * @see List templates by name and for channel ID - * @author ssk910 - */ -public class ExampleListTemplatesByNameAndForChannelID { - - public static void main(String[] args) { - if (args.length < 3) { - System.out.println("Please specify your access key, template name and channel ID example : java -jar test_accesskey \"My template name\" \"Channel ID\""); - 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); - - // template name from input - final String templateName = args[1]; - - // Channel ID from input - final String channelID = args[2]; - - // Will return templates only if the channel belongs to the same WABA that the templates belongs to. - try { - System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and channel ID '" + channelID + "'"); - final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, null, channelID); - System.out.println(templateList.toString()); - } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { - exception.printStackTrace(); - } - } -} diff --git a/examples/src/main/java/ExampleListTemplatesByNameAndForChannelID.java b/examples/src/main/java/ExampleListTemplatesByNameAndForChannelID.java new file mode 100644 index 00000000..02f2377b --- /dev/null +++ b/examples/src/main/java/ExampleListTemplatesByNameAndForChannelID.java @@ -0,0 +1,45 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.integrations.TemplateResponse; +import java.util.List; + +/** + * List templates by name and for channel ID + * + * @see List templates by name and for channel ID + * @author ssk910 + */ +public class ExampleListTemplatesByNameAndForChannelID { + + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Please specify your access key, template name and channel ID example : java -jar test_accesskey \"My template name\" \"Channel ID\""); + 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); + + // template name from input + final String templateName = args[1]; + + // Channel ID from input + final String channelID = args[2]; + + // Will return templates only if the channel belongs to the same WABA that the templates belongs to. + try { + System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and channel ID '" + channelID + "'"); + final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, null, channelID); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/examples/src/main/java/ExampleListTemplatesByNameAndWABAID.java b/examples/src/main/java/ExampleListTemplatesByNameAndWABAID.java new file mode 100644 index 00000000..6bb8fd3f --- /dev/null +++ b/examples/src/main/java/ExampleListTemplatesByNameAndWABAID.java @@ -0,0 +1,44 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.integrations.TemplateResponse; +import java.util.List; + +/** + * List templates by name and WABA ID + * + * @see List templates by name and WABA ID + * @author ssk910 + */ +public class ExampleListTemplatesByNameAndWABAID { + + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Please specify your access key, template name and WABA ID example : java -jar test_accesskey \"My template name\" \"WABA ID\""); + 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); + + // template name from input + final String templateName = args[1]; + + // WABA ID from input + final String wabaID = args[2]; + + try { + System.out.println("Retrieving WhatsApp Template list by name '" + templateName + "' and WABA ID '" + wabaID + "'"); + final List templateList = messageBirdClient.getWhatsAppTemplatesBy(templateName, wabaID, null); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | NotFoundException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java b/examples/src/main/java/ExampleListTemplatesByWABAID.java similarity index 52% rename from examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java rename to examples/src/main/java/ExampleListTemplatesByWABAID.java index e4971182..a11ed37f 100644 --- a/examples/src/main/java/ExampleListTemplatesByWABAOrChannel.java +++ b/examples/src/main/java/ExampleListTemplatesByWABAID.java @@ -14,7 +14,7 @@ public class ExampleListTemplatesByWABAID { public static void main(String[] args) { - if (args.length == 0) { + if (args.length < 2) { System.out.println("Please specify your access key and WABA ID example : java -jar test_accesskey \"WABA ID\""); return; } @@ -27,40 +27,10 @@ public static void main(String[] args) { try { System.out.println("Retrieving WhatsApp Template list by WABA"); - final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, "your-waba-id", null); + final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, args[1], null); System.out.println(templateList.toString()); } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { exception.printStackTrace(); } } -} - -/** - * List templates for Channel ID - * - * @see List templates for Channel ID - * @author ssk910 - */ -public class ExampleListTemplatesForChannelID { - - public static void main(String[] args) { - if (args.length == 0) { - System.out.println("Please specify your access key example : java -jar test_accesskey"); - 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); - - try { - System.out.println("Retrieving WhatsApp Template list for a channel"); - final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, null, "channel-id"); - System.out.println(templateList.toString()); - } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { - exception.printStackTrace(); - } - } -} +} \ No newline at end of file diff --git a/examples/src/main/java/ExampleListTemplatesForChannelID.java b/examples/src/main/java/ExampleListTemplatesForChannelID.java new file mode 100644 index 00000000..4a2bdda9 --- /dev/null +++ b/examples/src/main/java/ExampleListTemplatesForChannelID.java @@ -0,0 +1,36 @@ +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.TemplateList; + +/** + * List templates for Channel ID + * + * @see List templates for Channel ID + * @author ssk910 + */ +public class ExampleListTemplatesForChannelID { + + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key and channel ID example : java -jar test_accesskey"); + 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); + + try { + System.out.println("Retrieving WhatsApp Template list for a channel"); + final TemplateList templateList = messageBirdClient.listWhatsAppTemplates(0, 25, null, args[1]); + System.out.println(templateList.toString()); + } catch (GeneralException | UnauthorizedException | IllegalArgumentException exception) { + exception.printStackTrace(); + } + } +}