24
24
import com .messagebird .objects .PhoneNumbersResponse ;
25
25
import com .messagebird .objects .PurchasedNumber ;
26
26
import com .messagebird .objects .PurchasedNumberCreatedResponse ;
27
- import com .messagebird .objects .PurchasedNumbersResponse ;
28
27
import com .messagebird .objects .PurchasedNumbersFilter ;
28
+ import com .messagebird .objects .PurchasedNumbersResponse ;
29
29
import com .messagebird .objects .Verify ;
30
30
import com .messagebird .objects .VerifyMessage ;
31
31
import com .messagebird .objects .VerifyRequest ;
46
46
import com .messagebird .objects .conversations .ConversationWebhookCreateRequest ;
47
47
import com .messagebird .objects .conversations .ConversationWebhookList ;
48
48
import com .messagebird .objects .conversations .ConversationWebhookUpdateRequest ;
49
+ import com .messagebird .objects .integrations .WhatsAppTemplate ;
50
+ import com .messagebird .objects .integrations .WhatsAppTemplateList ;
51
+ import com .messagebird .objects .integrations .WhatsAppTemplateResponse ;
49
52
import com .messagebird .objects .voicecalls .RecordingResponse ;
50
53
import com .messagebird .objects .voicecalls .TranscriptionResponse ;
51
54
import com .messagebird .objects .voicecalls .VoiceCall ;
59
62
import com .messagebird .objects .voicecalls .Webhook ;
60
63
import com .messagebird .objects .voicecalls .WebhookList ;
61
64
import com .messagebird .objects .voicecalls .WebhookResponseData ;
62
-
63
65
import java .io .UnsupportedEncodingException ;
64
66
import java .math .BigInteger ;
65
- import java .nio .charset .StandardCharsets ;
66
67
import java .net .URLEncoder ;
68
+ import java .nio .charset .StandardCharsets ;
67
69
import java .util .Arrays ;
68
70
import java .util .HashMap ;
71
+ import java .util .HashSet ;
69
72
import java .util .LinkedHashMap ;
70
73
import java .util .LinkedList ;
71
74
import java .util .List ;
72
75
import java .util .Locale ;
73
76
import java .util .Map ;
74
77
import java .util .Set ;
75
- import java .util .HashSet ;
76
78
77
79
/**
78
80
* Message bird general client
@@ -102,6 +104,8 @@ public class MessageBirdClient {
102
104
static final String VOICE_CALLS_BASE_URL = "https://voice.messagebird.com" ;
103
105
static final String NUMBERS_CALLS_BASE_URL = "https://numbers.messagebird.com/v1" ;
104
106
static final String MESSAGING_BASE_URL = "https://messaging.messagebird.com/v1" ;
107
+ static final String INTEGRATIONS_BASE_URL_V2 = "https://integrations.messagebird.com/v2" ;
108
+ static final String INTEGRATIONS_BASE_URL_V3 = "https://integrations.messagebird.com/v3" ;
105
109
private static String [] supportedLanguages = {"de-DE" , "en-AU" , "en-UK" , "en-US" , "es-ES" , "es-LA" , "fr-FR" , "it-IT" , "nl-NL" , "pt-BR" };
106
110
107
111
private static final String BALANCEPATH = "/balance" ;
@@ -118,6 +122,7 @@ public class MessageBirdClient {
118
122
private static final String CONVERSATION_SEND_PATH = "/send" ;
119
123
private static final String CONVERSATION_MESSAGE_PATH = "/messages" ;
120
124
private static final String CONVERSATION_WEBHOOK_PATH = "/webhooks" ;
125
+ private static final String INTEGRATIONS_WHATSAPP_PATH = "/platforms/whatsapp" ;
121
126
static final String VOICECALLSPATH = "/calls" ;
122
127
static final String LEGSPATH = "/legs" ;
123
128
static final String RECORDINGPATH = "/recordings" ;
@@ -126,6 +131,7 @@ public class MessageBirdClient {
126
131
static final String VOICECALLFLOWPATH = "/call-flows" ;
127
132
private static final String VOICELEGS_SUFFIX_PATH = "/legs" ;
128
133
static final String FILES_PATH = "/files" ;
134
+ static final String TEMPLATES_PATH = "/templates" ;
129
135
130
136
static final String RECORDING_DOWNLOAD_FORMAT = ".wav" ;
131
137
@@ -1848,4 +1854,168 @@ public String downloadFile(String id, String filename, String basePath) throws G
1848
1854
final String url = String .format ("%s%s/%s" , MESSAGING_BASE_URL , FILES_PATH , id );
1849
1855
return messageBirdService .getBinaryData (url , basePath , filename );
1850
1856
}
1851
- }
1857
+
1858
+ /****************************************************************************************************/
1859
+ /** WhatsApp Templates **/
1860
+ /****************************************************************************************************/
1861
+
1862
+ /**
1863
+ * Create a WhatsApp message template through messagebird.
1864
+ *
1865
+ * @param template {@link WhatsAppTemplate} object to be created
1866
+ * @return {@link WhatsAppTemplateResponse} response object
1867
+ * @throws UnauthorizedException if client is unauthorized
1868
+ * @throws GeneralException general exception or invalid template format
1869
+ */
1870
+ public WhatsAppTemplateResponse createWhatsAppTemplate (final WhatsAppTemplate template )
1871
+ throws UnauthorizedException , GeneralException {
1872
+ template .validate ();
1873
+
1874
+ String url = String .format (
1875
+ "%s%s%s" ,
1876
+ INTEGRATIONS_BASE_URL_V2 ,
1877
+ INTEGRATIONS_WHATSAPP_PATH ,
1878
+ TEMPLATES_PATH
1879
+ );
1880
+ return messageBirdService .sendPayLoad (url , template , WhatsAppTemplateResponse .class );
1881
+ }
1882
+
1883
+ /**
1884
+ * Gets a WhatsAppTemplate listing with specified pagination options.
1885
+ *
1886
+ * @param offset Number of objects to skip.
1887
+ * @param limit Number of objects to take.
1888
+ * @return List of templates.
1889
+ * @throws UnauthorizedException if client is unauthorized
1890
+ * @throws GeneralException general exception
1891
+ */
1892
+ public WhatsAppTemplateList listWhatsAppTemplates (final int offset , final int limit )
1893
+ throws UnauthorizedException , GeneralException {
1894
+ String url = String .format (
1895
+ "%s%s%s" ,
1896
+ INTEGRATIONS_BASE_URL_V3 ,
1897
+ INTEGRATIONS_WHATSAPP_PATH ,
1898
+ TEMPLATES_PATH
1899
+ );
1900
+ return messageBirdService .requestList (url , offset , limit , WhatsAppTemplateList .class );
1901
+ }
1902
+
1903
+ /**
1904
+ * Gets a template listing with default pagination options.
1905
+ *
1906
+ * @return List of whatsapp templates.
1907
+ * @throws UnauthorizedException if client is unauthorized
1908
+ * @throws GeneralException general exception
1909
+ */
1910
+ public WhatsAppTemplateList listWhatsAppTemplates () throws UnauthorizedException , GeneralException {
1911
+ final int offset = 0 ;
1912
+ final int limit = 10 ;
1913
+
1914
+ return listWhatsAppTemplates (offset , limit );
1915
+ }
1916
+
1917
+ /**
1918
+ * Retrieves the template of an existing template name.
1919
+ *
1920
+ * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1921
+ * @return {@code List<WhatsAppTemplateResponse>} template list
1922
+ * @throws UnauthorizedException if client is unauthorized
1923
+ * @throws GeneralException general exception
1924
+ * @throws NotFoundException if template name is not found
1925
+ */
1926
+ public List <WhatsAppTemplateResponse > getWhatsAppTemplatesBy (final String templateName )
1927
+ throws GeneralException , UnauthorizedException , NotFoundException {
1928
+ if (templateName == null ) {
1929
+ throw new IllegalArgumentException ("Template name must be specified." );
1930
+ }
1931
+
1932
+ String url = String .format (
1933
+ "%s%s%s" ,
1934
+ INTEGRATIONS_BASE_URL_V2 ,
1935
+ INTEGRATIONS_WHATSAPP_PATH ,
1936
+ TEMPLATES_PATH
1937
+ );
1938
+
1939
+ final WhatsAppTemplateResponse [] templateResponses = messageBirdService .requestByID (url , templateName , WhatsAppTemplateResponse [].class );
1940
+ return Arrays .asList (templateResponses );
1941
+ }
1942
+
1943
+ /**
1944
+ * Retrieves the template of an existing template name and language.
1945
+ *
1946
+ * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable
1947
+ * @param language A language code as returned by getWhatsAppTemplateBy in the language variable
1948
+ *
1949
+ * @return {@code WhatsAppTemplateResponse} template list
1950
+ * @throws UnauthorizedException if client is unauthorized
1951
+ * @throws GeneralException general exception
1952
+ * @throws NotFoundException if template name and language are not found
1953
+ */
1954
+ public WhatsAppTemplateResponse fetchWhatsAppTemplateBy (final String templateName , final String language )
1955
+ throws GeneralException , UnauthorizedException , NotFoundException {
1956
+ if (templateName == null || language == null ) {
1957
+ throw new IllegalArgumentException ("Template name and language must be specified." );
1958
+ }
1959
+
1960
+ String url = String .format (
1961
+ "%s%s%s/%s/%s" ,
1962
+ INTEGRATIONS_BASE_URL_V2 ,
1963
+ INTEGRATIONS_WHATSAPP_PATH ,
1964
+ TEMPLATES_PATH ,
1965
+ templateName ,
1966
+ language
1967
+ );
1968
+ return messageBirdService .request (url , WhatsAppTemplateResponse .class );
1969
+ }
1970
+
1971
+
1972
+ /**
1973
+ * Delete templates of an existing template name.
1974
+ *
1975
+ * @param templateName A template name which is created on the MessageBird platform
1976
+ * @throws UnauthorizedException if client is unauthorized
1977
+ * @throws GeneralException general exception
1978
+ * @throws NotFoundException if template name is not found
1979
+ */
1980
+ public void deleteTemplatesBy (final String templateName )
1981
+ throws UnauthorizedException , GeneralException , NotFoundException {
1982
+ if (templateName == null ) {
1983
+ throw new IllegalArgumentException ("Template name must be specified." );
1984
+ }
1985
+
1986
+ String url = String .format (
1987
+ "%s%s%s/%s" ,
1988
+ INTEGRATIONS_BASE_URL_V2 ,
1989
+ INTEGRATIONS_WHATSAPP_PATH ,
1990
+ TEMPLATES_PATH ,
1991
+ templateName
1992
+ );
1993
+ messageBirdService .delete (url , null );
1994
+ }
1995
+
1996
+ /**
1997
+ * Delete template of an existing template name and language.
1998
+ *
1999
+ * @param templateName A template name which is created on the MessageBird platform
2000
+ * @param language A language which is created on the MessageBird platform
2001
+ * @throws UnauthorizedException if client is unauthorized
2002
+ * @throws GeneralException general exception
2003
+ * @throws NotFoundException if template name or language are not found
2004
+ */
2005
+ public void deleteTemplatesBy (final String templateName , final String language )
2006
+ throws UnauthorizedException , GeneralException , NotFoundException {
2007
+ if (templateName == null || language == null ) {
2008
+ throw new IllegalArgumentException ("Template name and language must be specified." );
2009
+ }
2010
+
2011
+ String url = String .format (
2012
+ "%s%s%s/%s/%s" ,
2013
+ INTEGRATIONS_BASE_URL_V2 ,
2014
+ INTEGRATIONS_WHATSAPP_PATH ,
2015
+ TEMPLATES_PATH ,
2016
+ templateName ,
2017
+ language
2018
+ );
2019
+ messageBirdService .delete (url , null );
2020
+ }
2021
+ }
0 commit comments