Skip to content

Commit b85d0c0

Browse files
author
Alexander Timmermann
authored
Merge pull request #101 from mariuspot/master
Add WhatsApp Sandbox support to Conversations API
2 parents 748f21b + 93a686b commit b85d0c0

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ $Balance = $MessageBird->balance->read();
4646
```
4747

4848

49+
Conversations Whatsapp Sandbox
50+
-------------
51+
52+
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.
53+
54+
```php
55+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]);
56+
```
57+
58+
If you use a custom `HttpClient` you will have to manually direct Conversation API request to the WhatsApp sandbox endpoint.
59+
60+
4961
Documentation
5062
----
5163
Complete documentation, instructions, and examples are available at:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
// Initiates a conversation by sending a first message. This example uses a
4+
// plain text message, but other types are also available. See the
5+
// conversations-messages-create examples.
6+
7+
require(__DIR__ . '/../../autoload.php');
8+
9+
// Set your own API access key here.
10+
// Create a client with WhatsApp sandbox enabled.
11+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]);
12+
13+
// Use WhatsApp sandbox channel as normal.
14+
15+
$content = new \MessageBird\Objects\Conversation\Content();
16+
$content->text = 'Hello world';
17+
18+
$message = new \MessageBird\Objects\Conversation\Message();
19+
$message->channelId = 'WHATSAPP_SANDBOX_CHANNEL_ID';
20+
$message->content = $content;
21+
$message->to = 'RECIPIENT'; // Channel-specific, e.g. MSISDN for SMS.
22+
$message->type = 'text';
23+
24+
try {
25+
$conversation = $messageBird->conversations->start($message);
26+
27+
var_dump($conversation);
28+
} catch (\Exception $e) {
29+
echo sprintf("%s: %s", get_class($e), $e->getMessage());
30+
}

src/MessageBird/Client.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class Client
1414
const CONVERSATIONSAPI_ENDPOINT = 'https://conversations.messagebird.com/v1';
1515
const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com';
1616

17+
const ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX = 'ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX';
18+
const CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT = 'https://whatsapp-sandbox.messagebird.com/v1';
19+
1720
const CLIENT_VERSION = '1.15.0';
1821

1922
/**
@@ -160,11 +163,11 @@ class Client
160163
* @param string $accessKey
161164
* @param Common\HttpClient $httpClient
162165
*/
163-
public function __construct($accessKey = null, Common\HttpClient $httpClient = null)
166+
public function __construct($accessKey = null, Common\HttpClient $httpClient = null, array $config = [])
164167
{
165168
if ($httpClient === null) {
166169
$this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT);
167-
$this->ConversationsAPIHttpClient = new Common\HttpClient(self::CONVERSATIONSAPI_ENDPOINT);
170+
$this->ConversationsAPIHttpClient = new Common\HttpClient(in_array(self::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX, $config) ? self::CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT : self::CONVERSATIONSAPI_ENDPOINT);
168171
$this->HttpClient = new Common\HttpClient(self::ENDPOINT);
169172
$this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array(
170173
'X-MessageBird-Version' => '20170314',

0 commit comments

Comments
 (0)