Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 27 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,23 @@ public class MessageBirdClient {
private static final String VOICELEGS_SUFFIX_PATH = "/legs";

private MessageBirdService messageBirdService;
private String conversationsBaseUrl;

public enum Feature {
ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX
}

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

public MessageBirdClient(final MessageBirdService messageBirdService, List<Feature> features) {
this(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 +724,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 +740,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 +753,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 +777,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 +796,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 +827,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 +844,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 +859,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 +871,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 +886,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 +897,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 +910,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
Original file line number Diff line number Diff line change
@@ -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.UnauthorizedException;
import com.messagebird.objects.conversations.*;
import java.util.List;

public class ExampleStartConversationsWithWhatsAppSandbox {

public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Please at least specify your access key, the channel id and destination address.\n" +
"Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) to(Required)");
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, List.of(MessageBirdClient.Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX)); //Create client with WhatsApp Sandbox enabled

ConversationContent conversationContent = new ConversationContent();
conversationContent.setText("Hello world from java sdk");

ConversationStartRequest request = new ConversationStartRequest(
args[2],
ConversationContentType.TEXT,
conversationContent,
args[1]
);
try {
Conversation conversation = messageBirdClient.startConversation(request);
// assertEquals("convid", conversation.getId());
System.out.println(conversation.getId());

} catch (GeneralException | UnauthorizedException exception) {
exception.printStackTrace();
}
}
}