diff --git a/README.md b/README.md index fbacf4fe..39d62596 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 57c42f29..3b60b2a3 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -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"}; @@ -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 features) { + this(messageBirdService); + if(features.indexOf(Feature.ENABLE_CONVERSATION_API_WHATSAPP_SANDBOX) >= 0) { + this.conversationsBaseUrl = BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX; + } + } /****************************************************************************************************/ /** Balance and HRL methods **/ /****************************************************************************************************/ @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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 @@ -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); } @@ -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 @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/examples/src/main/java/ExampleStartConversationsWithWhatsAppSandbox.java b/examples/src/main/java/ExampleStartConversationsWithWhatsAppSandbox.java new file mode 100644 index 00000000..08e82f33 --- /dev/null +++ b/examples/src/main/java/ExampleStartConversationsWithWhatsAppSandbox.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.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 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(); + } + } +}