Skip to content

Commit

Permalink
Merge pull request #101 from mariuspot/master
Browse files Browse the repository at this point in the history
Add WhatsApp Sandbox support to Conversations API
  • Loading branch information
Alexander Timmermann authored Sep 5, 2019
2 parents 748f21b + 93a686b commit b85d0c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions examples/conversations/enable-whatsapp-sandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

// Initiates a conversation by sending a first message. This example uses a
// plain text message, but other types are also available. See the
// conversations-messages-create examples.

require(__DIR__ . '/../../autoload.php');

// Set your own API access key here.
// Create a client with WhatsApp sandbox enabled.
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY', null, [\MessageBird\Client::ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX]);

// Use WhatsApp sandbox channel as normal.

$content = new \MessageBird\Objects\Conversation\Content();
$content->text = 'Hello world';

$message = new \MessageBird\Objects\Conversation\Message();
$message->channelId = 'WHATSAPP_SANDBOX_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());
}
7 changes: 5 additions & 2 deletions src/MessageBird/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/v1';

const CLIENT_VERSION = '1.15.0';

/**
Expand Down Expand Up @@ -160,11 +163,11 @@ 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 = [])
{
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',
Expand Down

0 comments on commit b85d0c0

Please sign in to comment.