Skip to content

Commit 163eb3a

Browse files
authored
Merge pull request #65 from mariuspot/master
Add WhatsApp Sandbox support to Conversations API
2 parents bfa4071 + c1dcb98 commit 163eb3a

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ If you server doesn't have a direct connection to the internet you can setup a p
115115
messageBirdService.setProxy(proxy);
116116
```
117117

118+
##### Conversations WhatsApp Sandbox
119+
To use the whatsapp sandbox you need to add `MessageBirdClient.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX` to the list of features you want enabled. Don't forget to replace `YOUR_ACCESS_KEY` with your actual access key.
120+
121+
```java
122+
// Create a MessageBirdService
123+
final MessageBirdService messageBirdService = new MessageBirdServiceImpl("YOUR_ACCESS_KEY");
124+
// Add the service to the client
125+
final MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService, List.of(MessageBirdClient.Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX));
126+
```
127+
118128
Documentation
119129
-------------
120130
Complete documentation, instructions, and examples are available at:

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public class MessageBirdClient {
4141
* can, however, override this behaviour by providing absolute URLs
4242
* ourselves.
4343
*/
44-
private static final String CONVERSATIONS_BASE_URL = "https://conversations.messagebird.com/v1";
44+
private static final String BASE_URL_CONVERSATIONS = "https://conversations.messagebird.com/v1";
45+
private static final String BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX = "https://whatsapp-sandbox.messagebird.com/v1";
46+
4547
static final String VOICE_CALLS_BASE_URL = "https://voice.messagebird.com";
4648
private static String[] supportedLanguages = {"de-DE", "en-AU", "en-UK", "en-US", "es-ES", "es-LA", "fr-FR", "it-IT", "nl-NL", "pt-BR"};
4749

@@ -66,11 +68,23 @@ public class MessageBirdClient {
6668
private static final String VOICELEGS_SUFFIX_PATH = "/legs";
6769

6870
private MessageBirdService messageBirdService;
71+
private String conversationsBaseUrl;
72+
73+
public enum Feature {
74+
ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX
75+
}
6976

7077
public MessageBirdClient(final MessageBirdService messageBirdService) {
7178
this.messageBirdService = messageBirdService;
79+
this.conversationsBaseUrl = BASE_URL_CONVERSATIONS;
7280
}
7381

82+
public MessageBirdClient(final MessageBirdService messageBirdService, List<Feature> features) {
83+
this(messageBirdService);
84+
if(features.indexOf(Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX) >= 0) {
85+
this.conversationsBaseUrl = BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX;
86+
}
87+
}
7488
/****************************************************************************************************/
7589
/** Balance and HRL methods **/
7690
/****************************************************************************************************/
@@ -800,7 +814,7 @@ public Conversation viewConversation(final String id) throws NotFoundException,
800814
if (id == null) {
801815
throw new IllegalArgumentException("Id must be specified");
802816
}
803-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_PATH;
817+
String url = this.conversationsBaseUrl + CONVERSATION_PATH;
804818
return messageBirdService.requestByID(url, id, Conversation.class);
805819
}
806820

@@ -816,7 +830,7 @@ public Conversation updateConversation(final String id, final ConversationStatus
816830
if (id == null) {
817831
throw new IllegalArgumentException("Id must be specified.");
818832
}
819-
String url = String.format("%s%s/%s", CONVERSATIONS_BASE_URL, CONVERSATION_PATH, id);
833+
String url = String.format("%s%s/%s", this.conversationsBaseUrl, CONVERSATION_PATH, id);
820834
return messageBirdService.sendPayLoad("PATCH", url, status, Conversation.class);
821835
}
822836

@@ -829,7 +843,7 @@ public Conversation updateConversation(final String id, final ConversationStatus
829843
*/
830844
public ConversationList listConversations(final int offset, final int limit)
831845
throws UnauthorizedException, GeneralException {
832-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_PATH;
846+
String url = this.conversationsBaseUrl + CONVERSATION_PATH;
833847
return messageBirdService.requestList(url, offset, limit, ConversationList.class);
834848
}
835849

@@ -853,7 +867,7 @@ public ConversationList listConversations() throws UnauthorizedException, Genera
853867
*/
854868
public Conversation startConversation(ConversationStartRequest request)
855869
throws UnauthorizedException, GeneralException {
856-
String url = String.format("%s%s/start", CONVERSATIONS_BASE_URL, CONVERSATION_PATH);
870+
String url = String.format("%s%s/start", this.conversationsBaseUrl, CONVERSATION_PATH);
857871
return messageBirdService.sendPayLoad(url, request, Conversation.class);
858872
}
859873

@@ -872,7 +886,7 @@ public ConversationMessageList listConversationMessages(
872886
) throws UnauthorizedException, GeneralException {
873887
String url = String.format(
874888
"%s%s/%s%s",
875-
CONVERSATIONS_BASE_URL,
889+
this.conversationsBaseUrl,
876890
CONVERSATION_PATH,
877891
conversationId,
878892
CONVERSATION_MESSAGE_PATH
@@ -903,7 +917,7 @@ public ConversationMessageList listConversationMessages(
903917
*/
904918
public ConversationMessage viewConversationMessage(final String messageId)
905919
throws NotFoundException, GeneralException, UnauthorizedException {
906-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_MESSAGE_PATH;
920+
String url = this.conversationsBaseUrl + CONVERSATION_MESSAGE_PATH;
907921
return messageBirdService.requestByID(url, messageId, ConversationMessage.class);
908922
}
909923

@@ -920,7 +934,7 @@ public ConversationMessage sendConversationMessage(
920934
) throws UnauthorizedException, GeneralException {
921935
String url = String.format(
922936
"%s%s/%s%s",
923-
CONVERSATIONS_BASE_URL,
937+
this.conversationsBaseUrl,
924938
CONVERSATION_PATH,
925939
conversationId,
926940
CONVERSATION_MESSAGE_PATH
@@ -935,7 +949,7 @@ public ConversationMessage sendConversationMessage(
935949
*/
936950
public void deleteConversationWebhook(final String webhookId)
937951
throws NotFoundException, GeneralException, UnauthorizedException {
938-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
952+
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
939953
messageBirdService.deleteByID(url, webhookId);
940954
}
941955

@@ -947,7 +961,7 @@ public void deleteConversationWebhook(final String webhookId)
947961
*/
948962
public ConversationWebhook sendConversationWebhook(final ConversationWebhookCreateRequest request)
949963
throws UnauthorizedException, GeneralException {
950-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
964+
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
951965
return messageBirdService.sendPayLoad(url, request, ConversationWebhook.class);
952966
}
953967

@@ -962,7 +976,7 @@ public ConversationWebhook updateConversationWebhook(final String id, final Conv
962976
throw new IllegalArgumentException("Conversation webhook ID must be specified.");
963977
}
964978

965-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH + "/" + id;
979+
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH + "/" + id;
966980
return messageBirdService.sendPayLoad("PATCH", url, request, ConversationWebhook.class);
967981
}
968982

@@ -973,7 +987,7 @@ public ConversationWebhook updateConversationWebhook(final String id, final Conv
973987
* @return The retrieved webhook.
974988
*/
975989
public ConversationWebhook viewConversationWebhook(final String webhookId) throws NotFoundException, GeneralException, UnauthorizedException {
976-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
990+
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
977991
return messageBirdService.requestByID(url, webhookId, ConversationWebhook.class);
978992
}
979993

@@ -986,7 +1000,7 @@ public ConversationWebhook viewConversationWebhook(final String webhookId) throw
9861000
*/
9871001
ConversationWebhookList listConversationWebhooks(final int offset, final int limit)
9881002
throws UnauthorizedException, GeneralException {
989-
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
1003+
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
9901004
return messageBirdService.requestList(url, offset, limit, ConversationWebhookList.class);
9911005
}
9921006

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.conversations.*;
7+
import java.util.List;
8+
9+
public class ExampleStartConversationsWithWhatsAppSandbox {
10+
11+
public static void main(String[] args) {
12+
if (args.length != 3) {
13+
System.out.println("Please at least specify your access key, the channel id and destination address.\n" +
14+
"Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) to(Required)");
15+
return;
16+
}
17+
18+
//First create your service object
19+
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
20+
21+
//Add the service to the client
22+
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr, List.of(MessageBirdClient.Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX)); //Create client with WhatsApp Sandbox enabled
23+
24+
ConversationContent conversationContent = new ConversationContent();
25+
conversationContent.setText("Hello world from java sdk");
26+
27+
ConversationStartRequest request = new ConversationStartRequest(
28+
args[2],
29+
ConversationContentType.TEXT,
30+
conversationContent,
31+
args[1]
32+
);
33+
try {
34+
Conversation conversation = messageBirdClient.startConversation(request);
35+
// assertEquals("convid", conversation.getId());
36+
System.out.println(conversation.getId());
37+
38+
} catch (GeneralException | UnauthorizedException exception) {
39+
exception.printStackTrace();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)