Skip to content

Add WhatsApp Sandbox support to Conversations API #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ If you server doesn't have a direct connection to the internet you can setup a p
messageBirdService.setProxy(proxy);
```

##### Conversations WhatsApp Sandbox
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.

```java
// Create a MessageBirdService
final MessageBirdService messageBirdService = new MessageBirdServiceImpl("YOUR_ACCESS_KEY");
// Add the service to the client
final MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService, List.of(MessageBirdClient.Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX));
```

Documentation
-------------
Complete documentation, instructions, and examples are available at:
Expand Down
39 changes: 26 additions & 13 deletions api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public class MessageBirdClient {
* can, however, override this behaviour by providing absolute URLs
* ourselves.
*/
private static final String CONVERSATIONS_BASE_URL = "https://conversations.messagebird.com/v1";
private static final String BASE_URL_CONVERSATIONS = "https://conversations.messagebird.com/v1";
private static final String BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX = "https://whatsapp-sandbox.messagebird.com/v1";

static final String VOICE_CALLS_BASE_URL = "https://voice.messagebird.com";
private static String[] supportedLanguages = {"de-DE", "en-AU", "en-UK", "en-US", "es-ES", "es-LA", "fr-FR", "it-IT", "nl-NL", "pt-BR"};

Expand All @@ -67,11 +69,22 @@ public class MessageBirdClient {
private static final String VOICELEGS_SUFFIX_PATH = "/legs";

private MessageBirdService messageBirdService;
private String conversationsBaseUrl = BASE_URL_CONVERSATIONS;

public enum Feature {
ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX
}

public MessageBirdClient(final MessageBirdService messageBirdService) {
this.messageBirdService = messageBirdService;
}

public MessageBirdClient(final MessageBirdService messageBirdService, List<Feature> features) {
this.messageBirdService = messageBirdService;
if(features.indexOf(Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX) >= 0) {
this.conversationsBaseUrl = BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX;
}
}
/****************************************************************************************************/
/** Balance and HRL methods **/
/****************************************************************************************************/
Expand Down Expand Up @@ -710,7 +723,7 @@ public Conversation viewConversation(final String id) throws NotFoundException,
if (id == null) {
throw new IllegalArgumentException("Id must be specified");
}
String url = CONVERSATIONS_BASE_URL + CONVERSATION_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_PATH;
return messageBirdService.requestByID(url, id, Conversation.class);
}

Expand All @@ -726,7 +739,7 @@ public Conversation updateConversation(final String id, final ConversationStatus
if (id == null) {
throw new IllegalArgumentException("Id must be specified.");
}
String url = String.format("%s%s/%s", CONVERSATIONS_BASE_URL, CONVERSATION_PATH, id);
String url = String.format("%s%s/%s", this.conversationsBaseUrl, CONVERSATION_PATH, id);
return messageBirdService.sendPayLoad("PATCH", url, status, Conversation.class);
}

Expand All @@ -739,7 +752,7 @@ public Conversation updateConversation(final String id, final ConversationStatus
*/
public ConversationList listConversations(final int offset, final int limit)
throws UnauthorizedException, GeneralException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_PATH;
return messageBirdService.requestList(url, offset, limit, ConversationList.class);
}

Expand All @@ -763,7 +776,7 @@ public ConversationList listConversations() throws UnauthorizedException, Genera
*/
public Conversation startConversation(ConversationStartRequest request)
throws UnauthorizedException, GeneralException {
String url = String.format("%s%s/start", CONVERSATIONS_BASE_URL, CONVERSATION_PATH);
String url = String.format("%s%s/start", this.conversationsBaseUrl, CONVERSATION_PATH);
return messageBirdService.sendPayLoad(url, request, Conversation.class);
}

Expand All @@ -782,7 +795,7 @@ public ConversationMessageList listConversationMessages(
) throws UnauthorizedException, GeneralException {
String url = String.format(
"%s%s/%s%s",
CONVERSATIONS_BASE_URL,
this.conversationsBaseUrl,
CONVERSATION_PATH,
conversationId,
CONVERSATION_MESSAGE_PATH
Expand Down Expand Up @@ -813,7 +826,7 @@ public ConversationMessageList listConversationMessages(
*/
public ConversationMessage viewConversationMessage(final String messageId)
throws NotFoundException, GeneralException, UnauthorizedException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_MESSAGE_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_MESSAGE_PATH;
return messageBirdService.requestByID(url, messageId, ConversationMessage.class);
}

Expand All @@ -830,7 +843,7 @@ public ConversationMessage sendConversationMessage(
) throws UnauthorizedException, GeneralException {
String url = String.format(
"%s%s/%s%s",
CONVERSATIONS_BASE_URL,
this.conversationsBaseUrl,
CONVERSATION_PATH,
conversationId,
CONVERSATION_MESSAGE_PATH
Expand All @@ -845,7 +858,7 @@ public ConversationMessage sendConversationMessage(
*/
public void deleteConversationWebhook(final String webhookId)
throws NotFoundException, GeneralException, UnauthorizedException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
messageBirdService.deleteByID(url, webhookId);
}

Expand All @@ -857,7 +870,7 @@ public void deleteConversationWebhook(final String webhookId)
*/
public ConversationWebhook sendConversationWebhook(final ConversationWebhookCreateRequest request)
throws UnauthorizedException, GeneralException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
return messageBirdService.sendPayLoad(url, request, ConversationWebhook.class);
}

Expand All @@ -872,7 +885,7 @@ public ConversationWebhook updateConversationWebhook(final String id, final Conv
throw new IllegalArgumentException("Conversation webhook ID must be specified.");
}

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

Expand All @@ -883,7 +896,7 @@ public ConversationWebhook updateConversationWebhook(final String id, final Conv
* @return The retrieved webhook.
*/
public ConversationWebhook viewConversationWebhook(final String webhookId) throws NotFoundException, GeneralException, UnauthorizedException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
return messageBirdService.requestByID(url, webhookId, ConversationWebhook.class);
}

Expand All @@ -896,7 +909,7 @@ public ConversationWebhook viewConversationWebhook(final String webhookId) throw
*/
ConversationWebhookList listConversationWebhooks(final int offset, final int limit)
throws UnauthorizedException, GeneralException {
String url = CONVERSATIONS_BASE_URL + CONVERSATION_WEBHOOK_PATH;
String url = this.conversationsBaseUrl + CONVERSATION_WEBHOOK_PATH;
return messageBirdService.requestList(url, offset, limit, ConversationWebhookList.class);
}

Expand Down