From 6b506235e24c2ad95f6f180219ed030797119b25 Mon Sep 17 00:00:00 2001 From: Marius Potgieter Date: Fri, 9 Aug 2019 14:13:41 +0200 Subject: [PATCH 1/5] Added features to all conversations API to use the WhatsApp sandbox. --- README.md | 12 ++++++++++++ src/MessageBird/Client.php | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index abaeb981..eee3b131 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,18 @@ $Balance = $MessageBird->balance->read(); ``` +Conversations Whatsapp Sandbox +------------- + +To use the whatsapp sandbox you need to add `\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX` to the list of features you want enabled. Don't forget to replace `YOUR_ACCESS_KEY` with your actual access key. + +```php +$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]); +``` + +If you use a custom `HttpClient` you will have to manually direct Conversation API request to the WhatsApp sandbox endpoint. + + Documentation ---- Complete documentation, instructions, and examples are available at: diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 018a2ec2..4263aa5d 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -14,6 +14,9 @@ class Client const CONVERSATIONSAPI_ENDPOINT = 'https://conversations.messagebird.com/v1'; const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; + const ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX = 'ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX'; + const CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT = 'https://whatsapp-sandbox.messagebird.com'; + const CLIENT_VERSION = '1.15.0'; /** @@ -160,11 +163,13 @@ class Client * @param string $accessKey * @param Common\HttpClient $httpClient */ - public function __construct($accessKey = null, Common\HttpClient $httpClient = null) + public function __construct($accessKey = null, Common\HttpClient $httpClient = null, Array $config = []) { + print($config); + print(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX,$config)); if ($httpClient === null) { $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); - $this->ConversationsAPIHttpClient = new Common\HttpClient(self::CONVERSATIONSAPI_ENDPOINT); + $this->ConversationsAPIHttpClient = new Common\HttpClient(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX,$config) ? self::CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT : self::CONVERSATIONSAPI_ENDPOINT); $this->HttpClient = new Common\HttpClient(self::ENDPOINT); $this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array( 'X-MessageBird-Version' => '20170314', From 5e2e48bbb0f8801df0026e86f8466f69421f5230 Mon Sep 17 00:00:00 2001 From: Marius Potgieter Date: Tue, 13 Aug 2019 13:41:13 +0200 Subject: [PATCH 2/5] Added Feature Flag to enable Whatsapp Sandbox --- README.md | 2 +- src/MessageBird/Client.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eee3b131..1a8af61d 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Conversations Whatsapp Sandbox To use the whatsapp sandbox you need to add `\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX` to the list of features you want enabled. Don't forget to replace `YOUR_ACCESS_KEY` with your actual access key. ```php -$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]); +$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]); ``` If you use a custom `HttpClient` you will have to manually direct Conversation API request to the WhatsApp sandbox endpoint. diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 4263aa5d..2841d686 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -15,7 +15,7 @@ class Client const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; const ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX = 'ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX'; - const CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT = 'https://whatsapp-sandbox.messagebird.com'; + const CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT = 'https://whatsapp-sandbox.messagebird.com/v1'; const CLIENT_VERSION = '1.15.0'; @@ -165,8 +165,6 @@ class Client */ public function __construct($accessKey = null, Common\HttpClient $httpClient = null, Array $config = []) { - print($config); - print(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX,$config)); if ($httpClient === null) { $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); $this->ConversationsAPIHttpClient = new Common\HttpClient(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX,$config) ? self::CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT : self::CONVERSATIONSAPI_ENDPOINT); From 31e2542cc30f9db6ead6156055edb69da958a446 Mon Sep 17 00:00:00 2001 From: Marius Potgieter Date: Tue, 3 Sep 2019 09:12:29 +0200 Subject: [PATCH 3/5] Fixed style porblems --- src/MessageBird/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 2841d686..f65c7564 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -163,11 +163,11 @@ class Client * @param string $accessKey * @param Common\HttpClient $httpClient */ - public function __construct($accessKey = null, Common\HttpClient $httpClient = null, Array $config = []) + public function __construct($accessKey = null, Common\HttpClient $httpClient = null, array $config = []) { if ($httpClient === null) { $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); - $this->ConversationsAPIHttpClient = new Common\HttpClient(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX,$config) ? self::CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT : self::CONVERSATIONSAPI_ENDPOINT); + $this->ConversationsAPIHttpClient = new Common\HttpClient(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX, $config) ? self::CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT : self::CONVERSATIONSAPI_ENDPOINT); $this->HttpClient = new Common\HttpClient(self::ENDPOINT); $this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array( 'X-MessageBird-Version' => '20170314', From c924750a6fc6f1595aa27c658b9d4a6eb7725eca Mon Sep 17 00:00:00 2001 From: Marius Potgieter Date: Tue, 3 Sep 2019 09:19:13 +0200 Subject: [PATCH 4/5] Added WhatsApp sandbox example. --- .../conversations/enable-whatsapp-sandbox.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/conversations/enable-whatsapp-sandbox.php diff --git a/examples/conversations/enable-whatsapp-sandbox.php b/examples/conversations/enable-whatsapp-sandbox.php new file mode 100644 index 00000000..5a9b7d1b --- /dev/null +++ b/examples/conversations/enable-whatsapp-sandbox.php @@ -0,0 +1,30 @@ +text = 'Hello world'; + +$message = new \MessageBird\Objects\Conversation\Message(); +$message->channelId = 'CHANNEL_ID'; +$message->content = $content; +$message->to = 'RECIPIENT'; // Channel-specific, e.g. MSISDN for SMS. +$message->type = 'text'; + +try { + $conversation = $messageBird->conversations->start($message); + + var_dump($conversation); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} From 93a686bc776e8d1e71f9f733534ecc2e27672dfd Mon Sep 17 00:00:00 2001 From: Marius Potgieter Date: Tue, 3 Sep 2019 09:29:39 +0200 Subject: [PATCH 5/5] Changed example --- examples/conversations/enable-whatsapp-sandbox.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/conversations/enable-whatsapp-sandbox.php b/examples/conversations/enable-whatsapp-sandbox.php index 5a9b7d1b..6c8decf0 100644 --- a/examples/conversations/enable-whatsapp-sandbox.php +++ b/examples/conversations/enable-whatsapp-sandbox.php @@ -10,13 +10,13 @@ // Create a client with WhatsApp sandbox enabled. $messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]); -// Continue using whatsapp channel as normal. +// Use WhatsApp sandbox channel as normal. $content = new \MessageBird\Objects\Conversation\Content(); $content->text = 'Hello world'; $message = new \MessageBird\Objects\Conversation\Message(); -$message->channelId = 'CHANNEL_ID'; +$message->channelId = 'WHATSAPP_SANDBOX_CHANNEL_ID'; $message->content = $content; $message->to = 'RECIPIENT'; // Channel-specific, e.g. MSISDN for SMS. $message->type = 'text';