From 36700858c7a0b3c9bf2415c75fbf4b15d37ae2d6 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:31:48 +0200 Subject: [PATCH 01/13] Add Voice API endpoints --- examples/voice-call-flows-create.php | 22 ++ examples/voice-call-flows-delete.php | 12 + examples/voice-call-flows-list.php | 12 + examples/voice-call-flows-read.php | 12 + examples/voice-call-flows-update.php | 14 + examples/voice-calls-create.php | 26 ++ examples/voice-calls-list.php | 12 + examples/voice-calls-read.php | 12 + examples/voice-legs-list.php | 12 + examples/voice-legs-read.php | 12 + examples/voice-recordings-download.php | 12 + examples/voice-recordings-list.php | 12 + examples/voice-recordings-read.php | 12 + examples/voice-transcriptions-create.php | 13 + examples/voice-transcriptions-download.php | 12 + examples/voice-transcriptions-list.php | 12 + examples/voice-transcriptions-read.php | 12 + examples/voice-webhooks-create.php | 15 + examples/voice-webhooks-delete.php | 12 + examples/voice-webhooks-list.php | 12 + examples/voice-webhooks-read.php | 12 + examples/voice-webhooks-update.php | 15 + src/MessageBird/Client.php | 47 ++- src/MessageBird/Common/HttpClient.php | 25 +- src/MessageBird/Common/ResponseError.php | 6 + src/MessageBird/Objects/Voice/Call.php | 86 +++++ src/MessageBird/Objects/Voice/CallFlow.php | 67 ++++ src/MessageBird/Objects/Voice/Leg.php | 79 ++++ src/MessageBird/Objects/Voice/Recording.php | 50 +++ src/MessageBird/Objects/Voice/Step.php | 32 ++ .../Objects/Voice/Transcription.php | 43 +++ src/MessageBird/Objects/Voice/Webhook.php | 44 +++ src/MessageBird/Resources/Voice/Base.php | 65 ++++ src/MessageBird/Resources/Voice/CallFlows.php | 27 ++ src/MessageBird/Resources/Voice/Calls.php | 27 ++ src/MessageBird/Resources/Voice/Legs.php | 123 ++++++ .../Resources/Voice/Recordings.php | 142 +++++++ .../Resources/Voice/Transcriptions.php | 164 ++++++++ src/MessageBird/Resources/Voice/Webhooks.php | 27 ++ tests/integration/voice/VoiceTest.php | 364 ++++++++++++++++++ 40 files changed, 1699 insertions(+), 16 deletions(-) create mode 100644 examples/voice-call-flows-create.php create mode 100644 examples/voice-call-flows-delete.php create mode 100644 examples/voice-call-flows-list.php create mode 100644 examples/voice-call-flows-read.php create mode 100644 examples/voice-call-flows-update.php create mode 100644 examples/voice-calls-create.php create mode 100644 examples/voice-calls-list.php create mode 100644 examples/voice-calls-read.php create mode 100644 examples/voice-legs-list.php create mode 100644 examples/voice-legs-read.php create mode 100644 examples/voice-recordings-download.php create mode 100644 examples/voice-recordings-list.php create mode 100644 examples/voice-recordings-read.php create mode 100644 examples/voice-transcriptions-create.php create mode 100644 examples/voice-transcriptions-download.php create mode 100644 examples/voice-transcriptions-list.php create mode 100644 examples/voice-transcriptions-read.php create mode 100644 examples/voice-webhooks-create.php create mode 100644 examples/voice-webhooks-delete.php create mode 100644 examples/voice-webhooks-list.php create mode 100644 examples/voice-webhooks-read.php create mode 100644 examples/voice-webhooks-update.php create mode 100644 src/MessageBird/Objects/Voice/Call.php create mode 100644 src/MessageBird/Objects/Voice/CallFlow.php create mode 100644 src/MessageBird/Objects/Voice/Leg.php create mode 100644 src/MessageBird/Objects/Voice/Recording.php create mode 100644 src/MessageBird/Objects/Voice/Step.php create mode 100644 src/MessageBird/Objects/Voice/Transcription.php create mode 100644 src/MessageBird/Objects/Voice/Webhook.php create mode 100644 src/MessageBird/Resources/Voice/Base.php create mode 100644 src/MessageBird/Resources/Voice/CallFlows.php create mode 100644 src/MessageBird/Resources/Voice/Calls.php create mode 100644 src/MessageBird/Resources/Voice/Legs.php create mode 100644 src/MessageBird/Resources/Voice/Recordings.php create mode 100644 src/MessageBird/Resources/Voice/Transcriptions.php create mode 100644 src/MessageBird/Resources/Voice/Webhooks.php create mode 100644 tests/integration/voice/VoiceTest.php diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php new file mode 100644 index 00000000..19463664 --- /dev/null +++ b/examples/voice-call-flows-create.php @@ -0,0 +1,22 @@ +title = 'Foobar'; +$step = new \MessageBird\Objects\Voice\Step(); +$step->action = 'say'; +$step->options = [ + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +]; +$callFlow->steps = [$step]; + +try { + $result = $messageBird->voiceCallFlows->create($callFlow); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-delete.php b/examples/voice-call-flows-delete.php new file mode 100644 index 00000000..31800178 --- /dev/null +++ b/examples/voice-call-flows-delete.php @@ -0,0 +1,12 @@ +voiceCallFlows->delete('7d3c2125-4ab4-4dcb-acf9-1c2dbfa24087'); // Set a call flow id here + var_dump('Deleted: ' . $deleted); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-list.php b/examples/voice-call-flows-list.php new file mode 100644 index 00000000..886ab582 --- /dev/null +++ b/examples/voice-call-flows-list.php @@ -0,0 +1,12 @@ +voiceCallFlows->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-read.php b/examples/voice-call-flows-read.php new file mode 100644 index 00000000..55567ed5 --- /dev/null +++ b/examples/voice-call-flows-read.php @@ -0,0 +1,12 @@ +voiceCallFlows->read('f24dd28c-90da-4ed6-af92-d8e32a0e5f55'); // Set a call flow id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-update.php b/examples/voice-call-flows-update.php new file mode 100644 index 00000000..ece6c9d8 --- /dev/null +++ b/examples/voice-call-flows-update.php @@ -0,0 +1,14 @@ +title = 'Foobar updated'; + +try { + $result = $messageBird->voiceCallFlows->update($callFlow, '21e5fc51-3285-4f41-97fd-cd1785ab54f8'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-create.php b/examples/voice-calls-create.php new file mode 100644 index 00000000..16cef429 --- /dev/null +++ b/examples/voice-calls-create.php @@ -0,0 +1,26 @@ +source = '31971234567'; +$call->destination = '31612345678'; +$callFlow = new \MessageBird\Objects\Voice\CallFlow(); +$callFlow->title = 'Say message'; +$step = new \MessageBird\Objects\Voice\Step(); +$step->action = 'say'; +$step->options = [ + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +]; +$callFlow->steps = [$step]; +$call->callFlow = $callFlow; + +try { + $result = $messageBird->voiceCalls->create($call); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-list.php b/examples/voice-calls-list.php new file mode 100644 index 00000000..7deb377a --- /dev/null +++ b/examples/voice-calls-list.php @@ -0,0 +1,12 @@ +voiceCalls->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-read.php b/examples/voice-calls-read.php new file mode 100644 index 00000000..911e35e2 --- /dev/null +++ b/examples/voice-calls-read.php @@ -0,0 +1,12 @@ +voiceCalls->read('dbf1373c-6781-43c7-bfe4-6538583c444b'); // Set a call id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-legs-list.php b/examples/voice-legs-list.php new file mode 100644 index 00000000..b718a5a7 --- /dev/null +++ b/examples/voice-legs-list.php @@ -0,0 +1,12 @@ +voiceLegs->getList('dbf1373c-6781-43c7-bfe4-6538583c444b', array('offset' => 100, 'limit' => 30)); // Set a call id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-legs-read.php b/examples/voice-legs-read.php new file mode 100644 index 00000000..53e946cb --- /dev/null +++ b/examples/voice-legs-read.php @@ -0,0 +1,12 @@ +voiceLegs->read('dbf1373c-6781-43c7-bfe4-6538583c444b', '6f39d883-94ac-4068-9fed-a9e31b77acda'); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-download.php b/examples/voice-recordings-download.php new file mode 100644 index 00000000..3e0ebd8d --- /dev/null +++ b/examples/voice-recordings-download.php @@ -0,0 +1,12 @@ +voiceRecordings->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here + echo sprintf("Received %d bytes.", mb_strlen($data)); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-list.php b/examples/voice-recordings-list.php new file mode 100644 index 00000000..12f773f7 --- /dev/null +++ b/examples/voice-recordings-list.php @@ -0,0 +1,12 @@ +voiceRecordings->getList('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', array('offset' => 100, 'limit' => 30)); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-read.php b/examples/voice-recordings-read.php new file mode 100644 index 00000000..377cd6cf --- /dev/null +++ b/examples/voice-recordings-read.php @@ -0,0 +1,12 @@ +voiceRecordings->read('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-create.php b/examples/voice-transcriptions-create.php new file mode 100644 index 00000000..02965ccf --- /dev/null +++ b/examples/voice-transcriptions-create.php @@ -0,0 +1,13 @@ +voiceTranscriptions->create('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-download.php b/examples/voice-transcriptions-download.php new file mode 100644 index 00000000..f2918d82 --- /dev/null +++ b/examples/voice-transcriptions-download.php @@ -0,0 +1,12 @@ +voiceTranscriptions->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', '44e73d1f-201d-4a7d-963a-9d76bbca6c4f'); // Set call, leg, recording and transcription id here + echo sprintf("Received %d bytes.", mb_strlen($data)); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-list.php b/examples/voice-transcriptions-list.php new file mode 100644 index 00000000..71e8bde4 --- /dev/null +++ b/examples/voice-transcriptions-list.php @@ -0,0 +1,12 @@ +voiceTranscriptions->getList('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', array('offset' => 100, 'limit' => 30)); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-read.php b/examples/voice-transcriptions-read.php new file mode 100644 index 00000000..8f272180 --- /dev/null +++ b/examples/voice-transcriptions-read.php @@ -0,0 +1,12 @@ +voiceTranscriptions->read('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg, recording and transcription id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-create.php b/examples/voice-webhooks-create.php new file mode 100644 index 00000000..2ff7ad5e --- /dev/null +++ b/examples/voice-webhooks-create.php @@ -0,0 +1,15 @@ +url = 'https://example.com/status'; +$webhook->token = 'foobar'; + +try { + $result = $messageBird->voiceWebhooks->create($webhook); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-delete.php b/examples/voice-webhooks-delete.php new file mode 100644 index 00000000..2a7307d5 --- /dev/null +++ b/examples/voice-webhooks-delete.php @@ -0,0 +1,12 @@ +voiceWebhooks->delete('e5f56d49-4fa2-4802-895d-b0a306f73f76'); // Set a webhook id here + var_dump('Deleted: ' . $deleted); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-list.php b/examples/voice-webhooks-list.php new file mode 100644 index 00000000..452529aa --- /dev/null +++ b/examples/voice-webhooks-list.php @@ -0,0 +1,12 @@ +voiceWebhooks->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-read.php b/examples/voice-webhooks-read.php new file mode 100644 index 00000000..99718795 --- /dev/null +++ b/examples/voice-webhooks-read.php @@ -0,0 +1,12 @@ +voiceWebhooks->read('e5f56d49-4fa2-4802-895d-b0a306f73f76'); // Set a webhook id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-update.php b/examples/voice-webhooks-update.php new file mode 100644 index 00000000..942a80e9 --- /dev/null +++ b/examples/voice-webhooks-update.php @@ -0,0 +1,15 @@ +url = 'https://example.com/foobar'; +$webhook->token = 'baz'; + +try { + $result = $messageBird->voiceWebhooks->update($webhook, 'e5f56d49-4fa2-4802-895d-b0a306f73f76'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 5f97416d..c9815aa2 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -11,6 +11,7 @@ class Client { const ENDPOINT = 'https://rest.messagebird.com'; const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1'; + const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; const CLIENT_VERSION = '1.6.6'; @@ -74,6 +75,16 @@ class Client */ public $chatContacts; + /** + * @var Resources\Voice\Calls + */ + public $Voice; + + /** + * @var Resources\Voice\Legs + */ + public $voiceLegs; + /** * @var Common\HttpClient */ @@ -93,9 +104,13 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n if ($httpClient === null) { $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); $this->HttpClient = new Common\HttpClient(self::ENDPOINT); + $this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array( + 'X-MessageBird-Version' => '20170314', + )); } else { $this->ChatAPIHttpClient = $httpClient; $this->HttpClient = $httpClient; + $this->VoiceAPIHttpClient = $httpClient; } $this->HttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); @@ -104,21 +119,30 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n $this->ChatAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); $this->ChatAPIHttpClient->addUserAgentString($this->getPhpVersion()); + $this->VoiceAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); + $this->VoiceAPIHttpClient->addUserAgentString($this->getPhpVersion()); + if ($accessKey !== null) { $this->setAccessKey($accessKey); } - $this->messages = new Resources\Messages($this->HttpClient); - $this->hlr = new Resources\Hlr($this->HttpClient); - $this->verify = new Resources\Verify($this->HttpClient); - $this->balance = new Resources\Balance($this->HttpClient); - $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); - $this->lookup = new Resources\Lookup($this->HttpClient); - $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); - $this->chatMessages = new Resources\Chat\Message($this->ChatAPIHttpClient); - $this->chatChannels = new Resources\Chat\Channel($this->ChatAPIHttpClient); - $this->chatPlatforms = new Resources\Chat\Platform($this->ChatAPIHttpClient); - $this->chatContacts = new Resources\Chat\Contact($this->ChatAPIHttpClient); + $this->messages = new Resources\Messages($this->HttpClient); + $this->hlr = new Resources\Hlr($this->HttpClient); + $this->verify = new Resources\Verify($this->HttpClient); + $this->balance = new Resources\Balance($this->HttpClient); + $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); + $this->lookup = new Resources\Lookup($this->HttpClient); + $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); + $this->chatMessages = new Resources\Chat\Message($this->ChatAPIHttpClient); + $this->chatChannels = new Resources\Chat\Channel($this->ChatAPIHttpClient); + $this->chatPlatforms = new Resources\Chat\Platform($this->ChatAPIHttpClient); + $this->chatContacts = new Resources\Chat\Contact($this->ChatAPIHttpClient); + $this->voiceCallFlows = new Resources\Voice\CallFlows($this->VoiceAPIHttpClient); + $this->voiceCalls = new Resources\Voice\Calls($this->VoiceAPIHttpClient); + $this->voiceLegs = new Resources\Voice\Legs($this->VoiceAPIHttpClient); + $this->voiceRecordings = new Resources\Voice\Recordings($this->VoiceAPIHttpClient); + $this->voiceTranscriptions = new Resources\Voice\Transcriptions($this->VoiceAPIHttpClient); + $this->voiceWebhooks = new Resources\Voice\Webhooks($this->VoiceAPIHttpClient); } /** @@ -130,6 +154,7 @@ public function setAccessKey ($accessKey) $this->ChatAPIHttpClient->setAuthentication($Authentication); $this->HttpClient->setAuthentication($Authentication); + $this->VoiceAPIHttpClient->setAuthentication($Authentication); } /** diff --git a/src/MessageBird/Common/HttpClient.php b/src/MessageBird/Common/HttpClient.php index a06de3c5..ce5f70a3 100644 --- a/src/MessageBird/Common/HttpClient.php +++ b/src/MessageBird/Common/HttpClient.php @@ -44,14 +44,18 @@ class HttpClient */ private $connectionTimeout = 2; + /** + * @var array + */ + private $headers = array(); + /** * @param string $endpoint - * @param int $timeout > 0 - * @param int $connectionTimeout >= 0 - * - * @throws \InvalidArgumentException if timeout settings are invalid + * @param int $timeout > 0 + * @param int $connectionTimeout >= 0 + * @param array $headers */ - public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) + public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2, $headers = array()) { $this->endpoint = $endpoint; @@ -72,6 +76,7 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) } $this->connectionTimeout = $connectionTimeout; + $this->headers = $headers; } /** @@ -109,6 +114,14 @@ public function getRequestUrl($resourceName, $query) return $requestUrl; } + /** + * @param array $headers + */ + public function setHeaders(array $headers) + { + $this->headers = $headers; + } + /** * @param string $method * @param string $resourceName @@ -136,6 +149,8 @@ public function performHttpRequest($method, $resourceName, $query = null, $body sprintf('Authorization: AccessKey %s', $this->Authentication->accessKey) ); + $headers = array_merge($headers, $this->headers); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_URL, $this->getRequestUrl($resourceName, $query)); diff --git a/src/MessageBird/Common/ResponseError.php b/src/MessageBird/Common/ResponseError.php index 2b39c33d..687c78b0 100644 --- a/src/MessageBird/Common/ResponseError.php +++ b/src/MessageBird/Common/ResponseError.php @@ -49,6 +49,12 @@ public function __construct($body) throw new Exceptions\AuthenticateException; } + // Rewrite error for Voice API. + if (!empty($error->message)) { + $error->description = $error->message; + unset($error->message); + } + $this->errors[] = $error; } } diff --git a/src/MessageBird/Objects/Voice/Call.php b/src/MessageBird/Objects/Voice/Call.php new file mode 100644 index 00000000..b3ca3746 --- /dev/null +++ b/src/MessageBird/Objects/Voice/Call.php @@ -0,0 +1,86 @@ +callFlow)) { + $callFlow = new CallFlow(); + $this->callFlow = $callFlow->loadFromArray($this->callFlow); + } + + return $this; + } +} diff --git a/src/MessageBird/Objects/Voice/CallFlow.php b/src/MessageBird/Objects/Voice/CallFlow.php new file mode 100644 index 00000000..d68bb5b8 --- /dev/null +++ b/src/MessageBird/Objects/Voice/CallFlow.php @@ -0,0 +1,67 @@ +steps)) { + foreach ($this->steps as &$item) { + $step = new Step(); + $step->loadFromArray($item); + $step->options = (array) $step->options; + + $item = $step; + } + } + + return $this; + } +} diff --git a/src/MessageBird/Objects/Voice/Leg.php b/src/MessageBird/Objects/Voice/Leg.php new file mode 100644 index 00000000..caa35c73 --- /dev/null +++ b/src/MessageBird/Objects/Voice/Leg.php @@ -0,0 +1,79 @@ +HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + $this->resourceName, + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $data = $body->data; + unset($body->items); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($data as $item) { + $itemObject = new $objectName($this->HttpClient); + + $Message = $itemObject->loadFromArray($item); + $baseList->items[] = $Message; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @inheritdoc + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/CallFlows.php b/src/MessageBird/Resources/Voice/CallFlows.php new file mode 100644 index 00000000..5ba537b1 --- /dev/null +++ b/src/MessageBird/Resources/Voice/CallFlows.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\CallFlow()); + $this->setResourceName('call-flows'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Voice/Calls.php b/src/MessageBird/Resources/Voice/Calls.php new file mode 100644 index 00000000..e4330f7f --- /dev/null +++ b/src/MessageBird/Resources/Voice/Calls.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\Call()); + $this->setResourceName('calls'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Voice/Legs.php b/src/MessageBird/Resources/Voice/Legs.php new file mode 100644 index 00000000..e7eb91dc --- /dev/null +++ b/src/MessageBird/Resources/Voice/Legs.php @@ -0,0 +1,123 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Leg()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Leg + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param array $parameters + * + * @return Objects\Voice\Leg + */ + public function getList($callId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return $this->Object + */ + public function read($callId, $legId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId"); + + return $this->processRequest($body); + } + + /** + * @param string $body + * + * @return Objects\Voice\Leg + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Recordings.php b/src/MessageBird/Resources/Voice/Recordings.php new file mode 100644 index 00000000..57a6f122 --- /dev/null +++ b/src/MessageBird/Resources/Voice/Recordings.php @@ -0,0 +1,142 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Recording()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Recording + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param string $legId + * @param array $parameters + * + * @return Objects\Voice\Recording + */ + public function getList($callId, $legId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return $this->Object + */ + public function read($callId, $legId, $recordingId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId/recordings/$recordingId"); + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return self|string + */ + public function download($callId, $legId, $recordingId) + { + list($status, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId/recordings/$recordingId.wav"); + + if ($status !== 200) { + return $this->processRequest($body); + } + + return $body; + } + + /** + * @param string $body + * + * @return Objects\Voice\Recording + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Transcriptions.php b/src/MessageBird/Resources/Voice/Transcriptions.php new file mode 100644 index 00000000..d49a4bf3 --- /dev/null +++ b/src/MessageBird/Resources/Voice/Transcriptions.php @@ -0,0 +1,164 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Transcription()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Transcription + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param string $legId + * @param string $recordingId + * + * @return Objects\Voice\Transcription + */ + public function create($callId, $legId, $recordingId) + { + list(, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_POST, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions" + ); + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * @param string $recordingId + * @param array $parameters + * + * @return Objects\Voice\Transcription + */ + public function getList($callId, $legId, $recordingId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId string + * @param string $legId string + * @param string $recordingId + * @param string $transcriptionId + * + * @return $this ->Object + */ + public function read($callId, $legId, $recordingId, $transcriptionId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions/$transcriptionId"); + + return $this->processRequest($body); + } + + /** + * @param string $callId string + * @param string $legId string + * @param string $recordingId + * @param string $transcriptionId + * + * @return self|string + */ + public function download($callId, $legId, $recordingId, $transcriptionId) + { + list($status, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions/$transcriptionId.txt"); + + if ($status !== 200) { + return $this->processRequest($body); + } + + return $body; + } + + /** + * @param string $body + * + * @return Objects\Voice\Transcription + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Webhooks.php b/src/MessageBird/Resources/Voice/Webhooks.php new file mode 100644 index 00000000..57a3284e --- /dev/null +++ b/src/MessageBird/Resources/Voice/Webhooks.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\Webhook()); + $this->setResourceName('webhooks'); + + parent::__construct($HttpClient); + } +} diff --git a/tests/integration/voice/VoiceTest.php b/tests/integration/voice/VoiceTest.php new file mode 100644 index 00000000..b92868a0 --- /dev/null +++ b/tests/integration/voice/VoiceTest.php @@ -0,0 +1,364 @@ +client = new \MessageBird\Client('YOUR_ACCESS_KEY', $this->mockClient); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceCall() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceCalls->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceCall() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foobar', null, null); + $this->client->voiceCalls->read('foobar'); + } + + public function testCreateVoiceCall() + { + $voiceCall = new \MessageBird\Objects\Voice\Call(); + $voiceCall->source = '31612354678'; + $voiceCall->destination = '31611223344'; + $voiceCall->callFlow = array( + 'title' => 'Foobar', + 'steps' => array( + array( + 'action' => 'hangup', + ), + ), + ); + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "21025ed1-cc1d-4554-ac05-043fa6c84e00", + "status": "starting", + "source": "31644556677", + "destination": "31612345678", + "createdAt": "2017-08-30T07:35:37Z", + "updatedAt": "2017-08-30T07:35:37Z", + "endedAt": null + } + ], + "_links": { + "self": "/calls/21025ed1-cc1d-4554-ac05-043fa6c84e00" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'calls', null, '{"source":"31612354678","destination":"31611223344","callFlow":{"title":"Foobar","steps":[{"action":"hangup"}]}}'); + $this->client->voiceCalls->create($voiceCall); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceLegs() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foobar/legs', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceLegs->getList('foobar', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceLeg() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar', null, null); + $this->client->voiceLegs->read('foo', 'bar'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceRecordings() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceRecordings->getList('foo', 'bar', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceRecording() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz', null, null); + $this->client->voiceRecordings->read('foo', 'bar', 'baz'); + } + + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDownloadVoiceRecording() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz.wav', null, null); + $this->client->voiceRecordings->download('foo', 'bar', 'baz'); + } + + public function testCreateVoiceTranscription() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "87c377ce-1629-48b6-ad01-4b4fd069c53c", + "recordingID": "baz", + "error": null, + "createdAt": "2017-06-20T10:03:14Z", + "updatedAt": "2017-06-20T10:03:14Z", + "_links": { + "self": "/calls/foo/legs/bar/recordings/baz/transcriptions/87c377ce-1629-48b6-ad01-4b4fd069c53c", + "file": "/calls/foo/legs/bar/recordings/baz/transcriptions/87c377ce-1629-48b6-ad01-4b4fd069c53c.txt" + } + } + ] +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'calls/foo/legs/bar/recordings/baz/transcriptions', null, null); + $this->client->voiceTranscriptions->create('foo', 'bar', 'baz'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceTranscriptions() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceTranscriptions->getList('foo', 'bar', 'baz', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceTranscription() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions/bups', null, null); + $this->client->voiceTranscriptions->read('foo', 'bar', 'baz', 'bups'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDownloadVoiceTranscription() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions/bups.txt', null, null); + $this->client->voiceTranscriptions->download('foo', 'bar', 'baz', 'bups'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceWebhooks() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'webhooks', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceWebhooks->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceWebhook() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'webhooks/foobar', null, null); + $this->client->voiceWebhooks->read('foobar'); + } + + public function testCreateVoiceWebhook() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->url = 'https://example.com/'; + $webhook->token = 'foobar'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "534e1848-235f-482d-983d-e3e11a04f58a", + "url": "https://example.com/", + "token": "foobar", + "createdAt": "2017-03-15T13:28:32Z", + "updatedAt": "2017-03-15T13:28:32Z", + "_links": { + "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" + } + } + ], + "_links": { + "self": "/webhooks?page=1" + }, + "pagination": { + "totalCount": 1, + "pageCount": 1, + "currentPage": 1, + "perPage": 10 + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'webhooks', null, '{"url":"https:\/\/example.com\/","token":"foobar"}'); + $this->client->voiceWebhooks->create($webhook); + } + + public function testUpdateVoiceWebhook() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->url = 'https://example.com/foo'; + $webhook->token = 'foobar'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{ + "data": [ + { + "id": "foobar123", + "url": "https://example.com/baz", + "token": "foobar", + "createdAt": "2017-03-15T13:27:02Z", + "updatedAt": "2017-03-15T13:28:01Z" + } + ], + "_links": { + "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("PUT", 'webhooks/foobar123', null, '{"url":"https:\/\/example.com\/foo","token":"foobar"}'); + $this->client->voiceWebhooks->update($webhook, 'foobar123'); + } + + public function testDeleteVoiceWebhook() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(204, '', null)); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'webhooks/foobar123', null, ''); + $this->client->voiceWebhooks->delete('foobar123'); + } + + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceCallFlows() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'call-flows', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceCallFlows->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceCallFlow() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'call-flows/foobar', null, null); + $this->client->voiceCallFlows->read('foobar'); + } + + public function testCreateVoiceCallFlow() + { + $callFlow = new \MessageBird\Objects\Voice\CallFlow(); + $callFlow->title = 'Foobar'; + $callFlow->steps = [ + [ + 'action' => 'transfer', + 'options' => [ + 'destination' => '31612345678', + ], + ] , + ]; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", + "title": "Forward call to 31612345678", + "steps": [ + { + "id": "3538a6b8-5a2e-4537-8745-f72def6bd393", + "action": "transfer", + "options": { + "destination": "31612345678" + } + } + ], + "createdAt": "2017-03-06T13:34:14Z", + "updatedAt": "2017-03-06T13:34:14Z", + "_links": { + "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635" + } + } + ], + "_links": { + "self": "/call-flows?page=1" + }, + "pagination": { + "totalCount": 1, + "pageCount": 1, + "currentPage": 1, + "perPage": 10 + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'call-flows', null, '{"title":"Foobar","steps":[{"action":"transfer","options":{"destination":"31612345678"}}]}'); + $this->client->voiceCallFlows->create($callFlow); + } + + public function testUpdateVoiceCallFlow() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->title = 'Updated call flow'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{ + "data": [ + { + "id": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", + "title": "Updated call flow", + "steps": [ + { + "id": "3538a6b8-5a2e-4537-8745-f72def6bd393", + "action": "transfer", + "options": { + "destination": "31611223344" + } + } + ], + "createdAt": "2017-03-06T13:34:14Z", + "updatedAt": "2017-03-06T15:02:38Z" + } + ], + "_links": { + "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("PUT", 'call-flows/foobar123', null, '{"title":"Updated call flow"}'); + $this->client->voiceCallFlows->update($webhook, 'foobar123'); + } + + public function testDeleteVoiceCallFlow() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(204, '', null)); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'call-flows/foobar123', null, ''); + $this->client->voiceCallFlows->delete('foobar123'); + } +} From c4c18d00b05f3ae88b2e5eb8e628c7bda61e4826 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:37:02 +0200 Subject: [PATCH 02/13] Version bump --- src/MessageBird/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index c9815aa2..c7f5031b 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -13,7 +13,7 @@ class Client const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1'; const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; - const CLIENT_VERSION = '1.6.6'; + const CLIENT_VERSION = '1.7.0'; /** * @var string From fa7f349ab085071f45fb7647f1e262916fcd69e7 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:39:28 +0200 Subject: [PATCH 03/13] Fix placeholder API key --- examples/voice-call-flows-create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php index 19463664..8b7f30d2 100644 --- a/examples/voice-call-flows-create.php +++ b/examples/voice-call-flows-create.php @@ -2,7 +2,7 @@ require_once(__DIR__ . '/../autoload.php'); -$messageBird = new \MessageBird\Client('OURTRANSCRIPTION'); // Set your own API access key here. +$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here. $callFlow = new \MessageBird\Objects\Voice\CallFlow(); $callFlow->title = 'Foobar'; $step = new \MessageBird\Objects\Voice\Step(); From f85ec9e48c79cbd0b82788d520178d55834f5b55 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Wed, 13 Sep 2017 18:16:40 +0200 Subject: [PATCH 04/13] Update Voice API examples to download/output downloaded contents --- examples/voice-recordings-download.php | 7 ++++++- examples/voice-transcriptions-download.php | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/voice-recordings-download.php b/examples/voice-recordings-download.php index 3e0ebd8d..b135b99e 100644 --- a/examples/voice-recordings-download.php +++ b/examples/voice-recordings-download.php @@ -6,7 +6,12 @@ try { $data = $messageBird->voiceRecordings->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here - echo sprintf("Received %d bytes.", mb_strlen($data)); + echo sprintf("Received %d bytes.\n", mb_strlen($data)); + $tmpfname = tempnam('/tmp', 'voice-recording-'); + $handle = fopen($tmpfname, "w"); + fwrite($handle, $data); + fclose($handle); + echo sprintf("Wrote to file: %s\n", $tmpfname); } catch (\Exception $e) { echo sprintf("%s: %s", get_class($e), $e->getMessage()); } diff --git a/examples/voice-transcriptions-download.php b/examples/voice-transcriptions-download.php index f2918d82..1fa09883 100644 --- a/examples/voice-transcriptions-download.php +++ b/examples/voice-transcriptions-download.php @@ -6,7 +6,8 @@ try { $data = $messageBird->voiceTranscriptions->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', '44e73d1f-201d-4a7d-963a-9d76bbca6c4f'); // Set call, leg, recording and transcription id here - echo sprintf("Received %d bytes.", mb_strlen($data)); + echo sprintf("Received %d bytes.\n", mb_strlen($data)); + echo sprintf("Transcription contents: `%s`.\n", $data); } catch (\Exception $e) { echo sprintf("%s: %s", get_class($e), $e->getMessage()); } From d497b4996da7dcc290ed35b8f489a2354c698da0 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 19 Sep 2017 15:15:42 +0200 Subject: [PATCH 05/13] Add BaseList for Voice, set pagination properties --- src/MessageBird/Objects/Voice/BaseList.php | 21 +++++++++++++++++++++ src/MessageBird/Resources/Voice/Base.php | 9 +++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/MessageBird/Objects/Voice/BaseList.php diff --git a/src/MessageBird/Objects/Voice/BaseList.php b/src/MessageBird/Objects/Voice/BaseList.php new file mode 100644 index 00000000..79cacd95 --- /dev/null +++ b/src/MessageBird/Objects/Voice/BaseList.php @@ -0,0 +1,21 @@ +data; - unset($body->items); - $baseList = new Objects\BaseList(); - $baseList->loadFromArray($body); + $baseList = new BaseList(); + if (property_exists($body, 'pagination')) { + $baseList->loadFromArray($body->pagination); + } $objectName = $this->Object; From 09b3747fbe5150e013bbbf7b37b43bd1ac2004e5 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:31:48 +0200 Subject: [PATCH 06/13] Add Voice API endpoints --- examples/voice-call-flows-create.php | 22 ++ examples/voice-call-flows-delete.php | 12 + examples/voice-call-flows-list.php | 12 + examples/voice-call-flows-read.php | 12 + examples/voice-call-flows-update.php | 14 + examples/voice-calls-create.php | 26 ++ examples/voice-calls-list.php | 12 + examples/voice-calls-read.php | 12 + examples/voice-legs-list.php | 12 + examples/voice-legs-read.php | 12 + examples/voice-recordings-download.php | 12 + examples/voice-recordings-list.php | 12 + examples/voice-recordings-read.php | 12 + examples/voice-transcriptions-create.php | 13 + examples/voice-transcriptions-download.php | 12 + examples/voice-transcriptions-list.php | 12 + examples/voice-transcriptions-read.php | 12 + examples/voice-webhooks-create.php | 15 + examples/voice-webhooks-delete.php | 12 + examples/voice-webhooks-list.php | 12 + examples/voice-webhooks-read.php | 12 + examples/voice-webhooks-update.php | 15 + src/MessageBird/Client.php | 47 ++- src/MessageBird/Common/HttpClient.php | 25 +- src/MessageBird/Common/ResponseError.php | 6 + src/MessageBird/Objects/Voice/Call.php | 86 +++++ src/MessageBird/Objects/Voice/CallFlow.php | 67 ++++ src/MessageBird/Objects/Voice/Leg.php | 79 ++++ src/MessageBird/Objects/Voice/Recording.php | 50 +++ src/MessageBird/Objects/Voice/Step.php | 32 ++ .../Objects/Voice/Transcription.php | 43 +++ src/MessageBird/Objects/Voice/Webhook.php | 44 +++ src/MessageBird/Resources/Voice/Base.php | 65 ++++ src/MessageBird/Resources/Voice/CallFlows.php | 27 ++ src/MessageBird/Resources/Voice/Calls.php | 27 ++ src/MessageBird/Resources/Voice/Legs.php | 123 ++++++ .../Resources/Voice/Recordings.php | 142 +++++++ .../Resources/Voice/Transcriptions.php | 164 ++++++++ src/MessageBird/Resources/Voice/Webhooks.php | 27 ++ tests/integration/voice/VoiceTest.php | 364 ++++++++++++++++++ 40 files changed, 1699 insertions(+), 16 deletions(-) create mode 100644 examples/voice-call-flows-create.php create mode 100644 examples/voice-call-flows-delete.php create mode 100644 examples/voice-call-flows-list.php create mode 100644 examples/voice-call-flows-read.php create mode 100644 examples/voice-call-flows-update.php create mode 100644 examples/voice-calls-create.php create mode 100644 examples/voice-calls-list.php create mode 100644 examples/voice-calls-read.php create mode 100644 examples/voice-legs-list.php create mode 100644 examples/voice-legs-read.php create mode 100644 examples/voice-recordings-download.php create mode 100644 examples/voice-recordings-list.php create mode 100644 examples/voice-recordings-read.php create mode 100644 examples/voice-transcriptions-create.php create mode 100644 examples/voice-transcriptions-download.php create mode 100644 examples/voice-transcriptions-list.php create mode 100644 examples/voice-transcriptions-read.php create mode 100644 examples/voice-webhooks-create.php create mode 100644 examples/voice-webhooks-delete.php create mode 100644 examples/voice-webhooks-list.php create mode 100644 examples/voice-webhooks-read.php create mode 100644 examples/voice-webhooks-update.php create mode 100644 src/MessageBird/Objects/Voice/Call.php create mode 100644 src/MessageBird/Objects/Voice/CallFlow.php create mode 100644 src/MessageBird/Objects/Voice/Leg.php create mode 100644 src/MessageBird/Objects/Voice/Recording.php create mode 100644 src/MessageBird/Objects/Voice/Step.php create mode 100644 src/MessageBird/Objects/Voice/Transcription.php create mode 100644 src/MessageBird/Objects/Voice/Webhook.php create mode 100644 src/MessageBird/Resources/Voice/Base.php create mode 100644 src/MessageBird/Resources/Voice/CallFlows.php create mode 100644 src/MessageBird/Resources/Voice/Calls.php create mode 100644 src/MessageBird/Resources/Voice/Legs.php create mode 100644 src/MessageBird/Resources/Voice/Recordings.php create mode 100644 src/MessageBird/Resources/Voice/Transcriptions.php create mode 100644 src/MessageBird/Resources/Voice/Webhooks.php create mode 100644 tests/integration/voice/VoiceTest.php diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php new file mode 100644 index 00000000..19463664 --- /dev/null +++ b/examples/voice-call-flows-create.php @@ -0,0 +1,22 @@ +title = 'Foobar'; +$step = new \MessageBird\Objects\Voice\Step(); +$step->action = 'say'; +$step->options = [ + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +]; +$callFlow->steps = [$step]; + +try { + $result = $messageBird->voiceCallFlows->create($callFlow); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-delete.php b/examples/voice-call-flows-delete.php new file mode 100644 index 00000000..31800178 --- /dev/null +++ b/examples/voice-call-flows-delete.php @@ -0,0 +1,12 @@ +voiceCallFlows->delete('7d3c2125-4ab4-4dcb-acf9-1c2dbfa24087'); // Set a call flow id here + var_dump('Deleted: ' . $deleted); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-list.php b/examples/voice-call-flows-list.php new file mode 100644 index 00000000..886ab582 --- /dev/null +++ b/examples/voice-call-flows-list.php @@ -0,0 +1,12 @@ +voiceCallFlows->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-read.php b/examples/voice-call-flows-read.php new file mode 100644 index 00000000..55567ed5 --- /dev/null +++ b/examples/voice-call-flows-read.php @@ -0,0 +1,12 @@ +voiceCallFlows->read('f24dd28c-90da-4ed6-af92-d8e32a0e5f55'); // Set a call flow id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-call-flows-update.php b/examples/voice-call-flows-update.php new file mode 100644 index 00000000..ece6c9d8 --- /dev/null +++ b/examples/voice-call-flows-update.php @@ -0,0 +1,14 @@ +title = 'Foobar updated'; + +try { + $result = $messageBird->voiceCallFlows->update($callFlow, '21e5fc51-3285-4f41-97fd-cd1785ab54f8'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-create.php b/examples/voice-calls-create.php new file mode 100644 index 00000000..16cef429 --- /dev/null +++ b/examples/voice-calls-create.php @@ -0,0 +1,26 @@ +source = '31971234567'; +$call->destination = '31612345678'; +$callFlow = new \MessageBird\Objects\Voice\CallFlow(); +$callFlow->title = 'Say message'; +$step = new \MessageBird\Objects\Voice\Step(); +$step->action = 'say'; +$step->options = [ + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +]; +$callFlow->steps = [$step]; +$call->callFlow = $callFlow; + +try { + $result = $messageBird->voiceCalls->create($call); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-list.php b/examples/voice-calls-list.php new file mode 100644 index 00000000..7deb377a --- /dev/null +++ b/examples/voice-calls-list.php @@ -0,0 +1,12 @@ +voiceCalls->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-calls-read.php b/examples/voice-calls-read.php new file mode 100644 index 00000000..911e35e2 --- /dev/null +++ b/examples/voice-calls-read.php @@ -0,0 +1,12 @@ +voiceCalls->read('dbf1373c-6781-43c7-bfe4-6538583c444b'); // Set a call id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-legs-list.php b/examples/voice-legs-list.php new file mode 100644 index 00000000..b718a5a7 --- /dev/null +++ b/examples/voice-legs-list.php @@ -0,0 +1,12 @@ +voiceLegs->getList('dbf1373c-6781-43c7-bfe4-6538583c444b', array('offset' => 100, 'limit' => 30)); // Set a call id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-legs-read.php b/examples/voice-legs-read.php new file mode 100644 index 00000000..53e946cb --- /dev/null +++ b/examples/voice-legs-read.php @@ -0,0 +1,12 @@ +voiceLegs->read('dbf1373c-6781-43c7-bfe4-6538583c444b', '6f39d883-94ac-4068-9fed-a9e31b77acda'); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-download.php b/examples/voice-recordings-download.php new file mode 100644 index 00000000..3e0ebd8d --- /dev/null +++ b/examples/voice-recordings-download.php @@ -0,0 +1,12 @@ +voiceRecordings->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here + echo sprintf("Received %d bytes.", mb_strlen($data)); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-list.php b/examples/voice-recordings-list.php new file mode 100644 index 00000000..12f773f7 --- /dev/null +++ b/examples/voice-recordings-list.php @@ -0,0 +1,12 @@ +voiceRecordings->getList('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', array('offset' => 100, 'limit' => 30)); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-recordings-read.php b/examples/voice-recordings-read.php new file mode 100644 index 00000000..377cd6cf --- /dev/null +++ b/examples/voice-recordings-read.php @@ -0,0 +1,12 @@ +voiceRecordings->read('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-create.php b/examples/voice-transcriptions-create.php new file mode 100644 index 00000000..02965ccf --- /dev/null +++ b/examples/voice-transcriptions-create.php @@ -0,0 +1,13 @@ +voiceTranscriptions->create('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-download.php b/examples/voice-transcriptions-download.php new file mode 100644 index 00000000..f2918d82 --- /dev/null +++ b/examples/voice-transcriptions-download.php @@ -0,0 +1,12 @@ +voiceTranscriptions->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', '44e73d1f-201d-4a7d-963a-9d76bbca6c4f'); // Set call, leg, recording and transcription id here + echo sprintf("Received %d bytes.", mb_strlen($data)); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-list.php b/examples/voice-transcriptions-list.php new file mode 100644 index 00000000..71e8bde4 --- /dev/null +++ b/examples/voice-transcriptions-list.php @@ -0,0 +1,12 @@ +voiceTranscriptions->getList('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', array('offset' => 100, 'limit' => 30)); // Set a call and leg id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-transcriptions-read.php b/examples/voice-transcriptions-read.php new file mode 100644 index 00000000..8f272180 --- /dev/null +++ b/examples/voice-transcriptions-read.php @@ -0,0 +1,12 @@ +voiceTranscriptions->read('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg, recording and transcription id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-create.php b/examples/voice-webhooks-create.php new file mode 100644 index 00000000..2ff7ad5e --- /dev/null +++ b/examples/voice-webhooks-create.php @@ -0,0 +1,15 @@ +url = 'https://example.com/status'; +$webhook->token = 'foobar'; + +try { + $result = $messageBird->voiceWebhooks->create($webhook); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-delete.php b/examples/voice-webhooks-delete.php new file mode 100644 index 00000000..2a7307d5 --- /dev/null +++ b/examples/voice-webhooks-delete.php @@ -0,0 +1,12 @@ +voiceWebhooks->delete('e5f56d49-4fa2-4802-895d-b0a306f73f76'); // Set a webhook id here + var_dump('Deleted: ' . $deleted); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-list.php b/examples/voice-webhooks-list.php new file mode 100644 index 00000000..452529aa --- /dev/null +++ b/examples/voice-webhooks-list.php @@ -0,0 +1,12 @@ +voiceWebhooks->getList(array('offset' => 100, 'limit' => 30)); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-read.php b/examples/voice-webhooks-read.php new file mode 100644 index 00000000..99718795 --- /dev/null +++ b/examples/voice-webhooks-read.php @@ -0,0 +1,12 @@ +voiceWebhooks->read('e5f56d49-4fa2-4802-895d-b0a306f73f76'); // Set a webhook id here + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/examples/voice-webhooks-update.php b/examples/voice-webhooks-update.php new file mode 100644 index 00000000..942a80e9 --- /dev/null +++ b/examples/voice-webhooks-update.php @@ -0,0 +1,15 @@ +url = 'https://example.com/foobar'; +$webhook->token = 'baz'; + +try { + $result = $messageBird->voiceWebhooks->update($webhook, 'e5f56d49-4fa2-4802-895d-b0a306f73f76'); + var_dump($result); +} catch (\Exception $e) { + echo sprintf("%s: %s", get_class($e), $e->getMessage()); +} diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 5f97416d..c9815aa2 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -11,6 +11,7 @@ class Client { const ENDPOINT = 'https://rest.messagebird.com'; const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1'; + const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; const CLIENT_VERSION = '1.6.6'; @@ -74,6 +75,16 @@ class Client */ public $chatContacts; + /** + * @var Resources\Voice\Calls + */ + public $Voice; + + /** + * @var Resources\Voice\Legs + */ + public $voiceLegs; + /** * @var Common\HttpClient */ @@ -93,9 +104,13 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n if ($httpClient === null) { $this->ChatAPIHttpClient = new Common\HttpClient(self::CHATAPI_ENDPOINT); $this->HttpClient = new Common\HttpClient(self::ENDPOINT); + $this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array( + 'X-MessageBird-Version' => '20170314', + )); } else { $this->ChatAPIHttpClient = $httpClient; $this->HttpClient = $httpClient; + $this->VoiceAPIHttpClient = $httpClient; } $this->HttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); @@ -104,21 +119,30 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n $this->ChatAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); $this->ChatAPIHttpClient->addUserAgentString($this->getPhpVersion()); + $this->VoiceAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION); + $this->VoiceAPIHttpClient->addUserAgentString($this->getPhpVersion()); + if ($accessKey !== null) { $this->setAccessKey($accessKey); } - $this->messages = new Resources\Messages($this->HttpClient); - $this->hlr = new Resources\Hlr($this->HttpClient); - $this->verify = new Resources\Verify($this->HttpClient); - $this->balance = new Resources\Balance($this->HttpClient); - $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); - $this->lookup = new Resources\Lookup($this->HttpClient); - $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); - $this->chatMessages = new Resources\Chat\Message($this->ChatAPIHttpClient); - $this->chatChannels = new Resources\Chat\Channel($this->ChatAPIHttpClient); - $this->chatPlatforms = new Resources\Chat\Platform($this->ChatAPIHttpClient); - $this->chatContacts = new Resources\Chat\Contact($this->ChatAPIHttpClient); + $this->messages = new Resources\Messages($this->HttpClient); + $this->hlr = new Resources\Hlr($this->HttpClient); + $this->verify = new Resources\Verify($this->HttpClient); + $this->balance = new Resources\Balance($this->HttpClient); + $this->voicemessages = new Resources\VoiceMessage($this->HttpClient); + $this->lookup = new Resources\Lookup($this->HttpClient); + $this->lookupHlr = new Resources\LookupHlr($this->HttpClient); + $this->chatMessages = new Resources\Chat\Message($this->ChatAPIHttpClient); + $this->chatChannels = new Resources\Chat\Channel($this->ChatAPIHttpClient); + $this->chatPlatforms = new Resources\Chat\Platform($this->ChatAPIHttpClient); + $this->chatContacts = new Resources\Chat\Contact($this->ChatAPIHttpClient); + $this->voiceCallFlows = new Resources\Voice\CallFlows($this->VoiceAPIHttpClient); + $this->voiceCalls = new Resources\Voice\Calls($this->VoiceAPIHttpClient); + $this->voiceLegs = new Resources\Voice\Legs($this->VoiceAPIHttpClient); + $this->voiceRecordings = new Resources\Voice\Recordings($this->VoiceAPIHttpClient); + $this->voiceTranscriptions = new Resources\Voice\Transcriptions($this->VoiceAPIHttpClient); + $this->voiceWebhooks = new Resources\Voice\Webhooks($this->VoiceAPIHttpClient); } /** @@ -130,6 +154,7 @@ public function setAccessKey ($accessKey) $this->ChatAPIHttpClient->setAuthentication($Authentication); $this->HttpClient->setAuthentication($Authentication); + $this->VoiceAPIHttpClient->setAuthentication($Authentication); } /** diff --git a/src/MessageBird/Common/HttpClient.php b/src/MessageBird/Common/HttpClient.php index a06de3c5..ce5f70a3 100644 --- a/src/MessageBird/Common/HttpClient.php +++ b/src/MessageBird/Common/HttpClient.php @@ -44,14 +44,18 @@ class HttpClient */ private $connectionTimeout = 2; + /** + * @var array + */ + private $headers = array(); + /** * @param string $endpoint - * @param int $timeout > 0 - * @param int $connectionTimeout >= 0 - * - * @throws \InvalidArgumentException if timeout settings are invalid + * @param int $timeout > 0 + * @param int $connectionTimeout >= 0 + * @param array $headers */ - public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) + public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2, $headers = array()) { $this->endpoint = $endpoint; @@ -72,6 +76,7 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) } $this->connectionTimeout = $connectionTimeout; + $this->headers = $headers; } /** @@ -109,6 +114,14 @@ public function getRequestUrl($resourceName, $query) return $requestUrl; } + /** + * @param array $headers + */ + public function setHeaders(array $headers) + { + $this->headers = $headers; + } + /** * @param string $method * @param string $resourceName @@ -136,6 +149,8 @@ public function performHttpRequest($method, $resourceName, $query = null, $body sprintf('Authorization: AccessKey %s', $this->Authentication->accessKey) ); + $headers = array_merge($headers, $this->headers); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_URL, $this->getRequestUrl($resourceName, $query)); diff --git a/src/MessageBird/Common/ResponseError.php b/src/MessageBird/Common/ResponseError.php index 2b39c33d..687c78b0 100644 --- a/src/MessageBird/Common/ResponseError.php +++ b/src/MessageBird/Common/ResponseError.php @@ -49,6 +49,12 @@ public function __construct($body) throw new Exceptions\AuthenticateException; } + // Rewrite error for Voice API. + if (!empty($error->message)) { + $error->description = $error->message; + unset($error->message); + } + $this->errors[] = $error; } } diff --git a/src/MessageBird/Objects/Voice/Call.php b/src/MessageBird/Objects/Voice/Call.php new file mode 100644 index 00000000..b3ca3746 --- /dev/null +++ b/src/MessageBird/Objects/Voice/Call.php @@ -0,0 +1,86 @@ +callFlow)) { + $callFlow = new CallFlow(); + $this->callFlow = $callFlow->loadFromArray($this->callFlow); + } + + return $this; + } +} diff --git a/src/MessageBird/Objects/Voice/CallFlow.php b/src/MessageBird/Objects/Voice/CallFlow.php new file mode 100644 index 00000000..d68bb5b8 --- /dev/null +++ b/src/MessageBird/Objects/Voice/CallFlow.php @@ -0,0 +1,67 @@ +steps)) { + foreach ($this->steps as &$item) { + $step = new Step(); + $step->loadFromArray($item); + $step->options = (array) $step->options; + + $item = $step; + } + } + + return $this; + } +} diff --git a/src/MessageBird/Objects/Voice/Leg.php b/src/MessageBird/Objects/Voice/Leg.php new file mode 100644 index 00000000..caa35c73 --- /dev/null +++ b/src/MessageBird/Objects/Voice/Leg.php @@ -0,0 +1,79 @@ +HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + $this->resourceName, + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $data = $body->data; + unset($body->items); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($data as $item) { + $itemObject = new $objectName($this->HttpClient); + + $Message = $itemObject->loadFromArray($item); + $baseList->items[] = $Message; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @inheritdoc + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/CallFlows.php b/src/MessageBird/Resources/Voice/CallFlows.php new file mode 100644 index 00000000..5ba537b1 --- /dev/null +++ b/src/MessageBird/Resources/Voice/CallFlows.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\CallFlow()); + $this->setResourceName('call-flows'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Voice/Calls.php b/src/MessageBird/Resources/Voice/Calls.php new file mode 100644 index 00000000..e4330f7f --- /dev/null +++ b/src/MessageBird/Resources/Voice/Calls.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\Call()); + $this->setResourceName('calls'); + + parent::__construct($HttpClient); + } +} diff --git a/src/MessageBird/Resources/Voice/Legs.php b/src/MessageBird/Resources/Voice/Legs.php new file mode 100644 index 00000000..e7eb91dc --- /dev/null +++ b/src/MessageBird/Resources/Voice/Legs.php @@ -0,0 +1,123 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Leg()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Leg + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param array $parameters + * + * @return Objects\Voice\Leg + */ + public function getList($callId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return $this->Object + */ + public function read($callId, $legId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId"); + + return $this->processRequest($body); + } + + /** + * @param string $body + * + * @return Objects\Voice\Leg + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Recordings.php b/src/MessageBird/Resources/Voice/Recordings.php new file mode 100644 index 00000000..57a6f122 --- /dev/null +++ b/src/MessageBird/Resources/Voice/Recordings.php @@ -0,0 +1,142 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Recording()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Recording + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param string $legId + * @param array $parameters + * + * @return Objects\Voice\Recording + */ + public function getList($callId, $legId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return $this->Object + */ + public function read($callId, $legId, $recordingId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId/recordings/$recordingId"); + + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * + * @return self|string + */ + public function download($callId, $legId, $recordingId) + { + list($status, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, "calls/$callId/legs/$legId/recordings/$recordingId.wav"); + + if ($status !== 200) { + return $this->processRequest($body); + } + + return $body; + } + + /** + * @param string $body + * + * @return Objects\Voice\Recording + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Transcriptions.php b/src/MessageBird/Resources/Voice/Transcriptions.php new file mode 100644 index 00000000..d49a4bf3 --- /dev/null +++ b/src/MessageBird/Resources/Voice/Transcriptions.php @@ -0,0 +1,164 @@ +HttpClient = $HttpClient; + $this->setObject(new Objects\Voice\Transcription()); + } + + /** + * @param $Object + */ + public function setObject($Object) + { + $this->Object = $Object; + } + + /** + * @return Objects\Voice\Transcription + */ + public function getObject() + { + return $this->Object; + } + + /** + * @param string $callId + * @param string $legId + * @param string $recordingId + * + * @return Objects\Voice\Transcription + */ + public function create($callId, $legId, $recordingId) + { + list(, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_POST, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions" + ); + return $this->processRequest($body); + } + + /** + * @param string $callId + * @param string $legId + * @param string $recordingId + * @param array $parameters + * + * @return Objects\Voice\Transcription + */ + public function getList($callId, $legId, $recordingId, $parameters = array()) + { + list($status, , $body) = $this->HttpClient->performHttpRequest( + Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions", + $parameters + ); + + if ($status === 200) { + $body = json_decode($body); + + $items = $body->data; + unset($body->data); + + $baseList = new Objects\BaseList(); + $baseList->loadFromArray($body); + + $objectName = $this->Object; + + foreach ($items as $item) { + $object = new $objectName($this->HttpClient); + + $itemObject = $object->loadFromArray($item); + $baseList->items[] = $itemObject; + } + return $baseList; + } + + return $this->processRequest($body); + } + + /** + * @param string $callId string + * @param string $legId string + * @param string $recordingId + * @param string $transcriptionId + * + * @return $this ->Object + */ + public function read($callId, $legId, $recordingId, $transcriptionId) + { + list(, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions/$transcriptionId"); + + return $this->processRequest($body); + } + + /** + * @param string $callId string + * @param string $legId string + * @param string $recordingId + * @param string $transcriptionId + * + * @return self|string + */ + public function download($callId, $legId, $recordingId, $transcriptionId) + { + list($status, , $body) = $this->HttpClient->performHttpRequest(Common\HttpClient::REQUEST_GET, + "calls/$callId/legs/$legId/recordings/$recordingId/transcriptions/$transcriptionId.txt"); + + if ($status !== 200) { + return $this->processRequest($body); + } + + return $body; + } + + /** + * @param string $body + * + * @return Objects\Voice\Transcription + * @throws \MessageBird\Exceptions\RequestException + * @throws \MessageBird\Exceptions\ServerException + */ + public function processRequest($body) + { + $body = @json_decode($body); + + if ($body === null or $body === false) { + throw new Exceptions\ServerException('Got an invalid JSON response from the server.'); + } + + if (empty($body->errors)) { + return $this->Object->loadFromArray($body->data[0]); + } + + $ResponseError = new Common\ResponseError($body); + throw new Exceptions\RequestException($ResponseError->getErrorString()); + } +} diff --git a/src/MessageBird/Resources/Voice/Webhooks.php b/src/MessageBird/Resources/Voice/Webhooks.php new file mode 100644 index 00000000..57a3284e --- /dev/null +++ b/src/MessageBird/Resources/Voice/Webhooks.php @@ -0,0 +1,27 @@ +setObject(new Objects\Voice\Webhook()); + $this->setResourceName('webhooks'); + + parent::__construct($HttpClient); + } +} diff --git a/tests/integration/voice/VoiceTest.php b/tests/integration/voice/VoiceTest.php new file mode 100644 index 00000000..b92868a0 --- /dev/null +++ b/tests/integration/voice/VoiceTest.php @@ -0,0 +1,364 @@ +client = new \MessageBird\Client('YOUR_ACCESS_KEY', $this->mockClient); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceCall() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceCalls->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceCall() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foobar', null, null); + $this->client->voiceCalls->read('foobar'); + } + + public function testCreateVoiceCall() + { + $voiceCall = new \MessageBird\Objects\Voice\Call(); + $voiceCall->source = '31612354678'; + $voiceCall->destination = '31611223344'; + $voiceCall->callFlow = array( + 'title' => 'Foobar', + 'steps' => array( + array( + 'action' => 'hangup', + ), + ), + ); + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "21025ed1-cc1d-4554-ac05-043fa6c84e00", + "status": "starting", + "source": "31644556677", + "destination": "31612345678", + "createdAt": "2017-08-30T07:35:37Z", + "updatedAt": "2017-08-30T07:35:37Z", + "endedAt": null + } + ], + "_links": { + "self": "/calls/21025ed1-cc1d-4554-ac05-043fa6c84e00" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'calls', null, '{"source":"31612354678","destination":"31611223344","callFlow":{"title":"Foobar","steps":[{"action":"hangup"}]}}'); + $this->client->voiceCalls->create($voiceCall); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceLegs() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foobar/legs', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceLegs->getList('foobar', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceLeg() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar', null, null); + $this->client->voiceLegs->read('foo', 'bar'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceRecordings() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceRecordings->getList('foo', 'bar', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceRecording() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz', null, null); + $this->client->voiceRecordings->read('foo', 'bar', 'baz'); + } + + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDownloadVoiceRecording() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz.wav', null, null); + $this->client->voiceRecordings->download('foo', 'bar', 'baz'); + } + + public function testCreateVoiceTranscription() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "87c377ce-1629-48b6-ad01-4b4fd069c53c", + "recordingID": "baz", + "error": null, + "createdAt": "2017-06-20T10:03:14Z", + "updatedAt": "2017-06-20T10:03:14Z", + "_links": { + "self": "/calls/foo/legs/bar/recordings/baz/transcriptions/87c377ce-1629-48b6-ad01-4b4fd069c53c", + "file": "/calls/foo/legs/bar/recordings/baz/transcriptions/87c377ce-1629-48b6-ad01-4b4fd069c53c.txt" + } + } + ] +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'calls/foo/legs/bar/recordings/baz/transcriptions', null, null); + $this->client->voiceTranscriptions->create('foo', 'bar', 'baz'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceTranscriptions() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceTranscriptions->getList('foo', 'bar', 'baz', array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceTranscription() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions/bups', null, null); + $this->client->voiceTranscriptions->read('foo', 'bar', 'baz', 'bups'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testDownloadVoiceTranscription() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'calls/foo/legs/bar/recordings/baz/transcriptions/bups.txt', null, null); + $this->client->voiceTranscriptions->download('foo', 'bar', 'baz', 'bups'); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceWebhooks() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'webhooks', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceWebhooks->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceWebhook() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'webhooks/foobar', null, null); + $this->client->voiceWebhooks->read('foobar'); + } + + public function testCreateVoiceWebhook() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->url = 'https://example.com/'; + $webhook->token = 'foobar'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "534e1848-235f-482d-983d-e3e11a04f58a", + "url": "https://example.com/", + "token": "foobar", + "createdAt": "2017-03-15T13:28:32Z", + "updatedAt": "2017-03-15T13:28:32Z", + "_links": { + "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" + } + } + ], + "_links": { + "self": "/webhooks?page=1" + }, + "pagination": { + "totalCount": 1, + "pageCount": 1, + "currentPage": 1, + "perPage": 10 + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'webhooks', null, '{"url":"https:\/\/example.com\/","token":"foobar"}'); + $this->client->voiceWebhooks->create($webhook); + } + + public function testUpdateVoiceWebhook() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->url = 'https://example.com/foo'; + $webhook->token = 'foobar'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{ + "data": [ + { + "id": "foobar123", + "url": "https://example.com/baz", + "token": "foobar", + "createdAt": "2017-03-15T13:27:02Z", + "updatedAt": "2017-03-15T13:28:01Z" + } + ], + "_links": { + "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("PUT", 'webhooks/foobar123', null, '{"url":"https:\/\/example.com\/foo","token":"foobar"}'); + $this->client->voiceWebhooks->update($webhook, 'foobar123'); + } + + public function testDeleteVoiceWebhook() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(204, '', null)); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'webhooks/foobar123', null, ''); + $this->client->voiceWebhooks->delete('foobar123'); + } + + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testListVoiceCallFlows() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'call-flows', array( + 'offset' => 100, + 'limit' => 30, + ), null); + $this->client->voiceCallFlows->getList(array('offset' => 100, 'limit' => 30)); + } + + /** + * @expectedException MessageBird\Exceptions\ServerException + */ + public function testReadVoiceCallFlow() + { + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("GET", 'call-flows/foobar', null, null); + $this->client->voiceCallFlows->read('foobar'); + } + + public function testCreateVoiceCallFlow() + { + $callFlow = new \MessageBird\Objects\Voice\CallFlow(); + $callFlow->title = 'Foobar'; + $callFlow->steps = [ + [ + 'action' => 'transfer', + 'options' => [ + 'destination' => '31612345678', + ], + ] , + ]; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ + "data": [ + { + "id": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", + "title": "Forward call to 31612345678", + "steps": [ + { + "id": "3538a6b8-5a2e-4537-8745-f72def6bd393", + "action": "transfer", + "options": { + "destination": "31612345678" + } + } + ], + "createdAt": "2017-03-06T13:34:14Z", + "updatedAt": "2017-03-06T13:34:14Z", + "_links": { + "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635" + } + } + ], + "_links": { + "self": "/call-flows?page=1" + }, + "pagination": { + "totalCount": 1, + "pageCount": 1, + "currentPage": 1, + "perPage": 10 + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("POST", 'call-flows', null, '{"title":"Foobar","steps":[{"action":"transfer","options":{"destination":"31612345678"}}]}'); + $this->client->voiceCallFlows->create($callFlow); + } + + public function testUpdateVoiceCallFlow() + { + $webhook = new \MessageBird\Objects\Voice\Webhook(); + $webhook->title = 'Updated call flow'; + + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(200, '', '{ + "data": [ + { + "id": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", + "title": "Updated call flow", + "steps": [ + { + "id": "3538a6b8-5a2e-4537-8745-f72def6bd393", + "action": "transfer", + "options": { + "destination": "31611223344" + } + } + ], + "createdAt": "2017-03-06T13:34:14Z", + "updatedAt": "2017-03-06T15:02:38Z" + } + ], + "_links": { + "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635" + } +}')); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("PUT", 'call-flows/foobar123', null, '{"title":"Updated call flow"}'); + $this->client->voiceCallFlows->update($webhook, 'foobar123'); + } + + public function testDeleteVoiceCallFlow() + { + $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(204, '', null)); + $this->mockClient->expects($this->once())->method('performHttpRequest')->with("DELETE", 'call-flows/foobar123', null, ''); + $this->client->voiceCallFlows->delete('foobar123'); + } +} From c426b4a34bca14b466d8cecfec2cc9bbc386a95a Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:37:02 +0200 Subject: [PATCH 07/13] Version bump --- src/MessageBird/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index c9815aa2..c7f5031b 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -13,7 +13,7 @@ class Client const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1'; const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com'; - const CLIENT_VERSION = '1.6.6'; + const CLIENT_VERSION = '1.7.0'; /** * @var string From 642c1b758d4e99ef3b1681d41ecaa987012a5fcc Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 5 Sep 2017 14:39:28 +0200 Subject: [PATCH 08/13] Fix placeholder API key --- examples/voice-call-flows-create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php index 19463664..8b7f30d2 100644 --- a/examples/voice-call-flows-create.php +++ b/examples/voice-call-flows-create.php @@ -2,7 +2,7 @@ require_once(__DIR__ . '/../autoload.php'); -$messageBird = new \MessageBird\Client('OURTRANSCRIPTION'); // Set your own API access key here. +$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here. $callFlow = new \MessageBird\Objects\Voice\CallFlow(); $callFlow->title = 'Foobar'; $step = new \MessageBird\Objects\Voice\Step(); From c41c20d41d527fbb96f4d9581513a5dd638013a4 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Wed, 13 Sep 2017 18:16:40 +0200 Subject: [PATCH 09/13] Update Voice API examples to download/output downloaded contents --- examples/voice-recordings-download.php | 7 ++++++- examples/voice-transcriptions-download.php | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/voice-recordings-download.php b/examples/voice-recordings-download.php index 3e0ebd8d..b135b99e 100644 --- a/examples/voice-recordings-download.php +++ b/examples/voice-recordings-download.php @@ -6,7 +6,12 @@ try { $data = $messageBird->voiceRecordings->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577'); // Set call, leg and recording id here - echo sprintf("Received %d bytes.", mb_strlen($data)); + echo sprintf("Received %d bytes.\n", mb_strlen($data)); + $tmpfname = tempnam('/tmp', 'voice-recording-'); + $handle = fopen($tmpfname, "w"); + fwrite($handle, $data); + fclose($handle); + echo sprintf("Wrote to file: %s\n", $tmpfname); } catch (\Exception $e) { echo sprintf("%s: %s", get_class($e), $e->getMessage()); } diff --git a/examples/voice-transcriptions-download.php b/examples/voice-transcriptions-download.php index f2918d82..1fa09883 100644 --- a/examples/voice-transcriptions-download.php +++ b/examples/voice-transcriptions-download.php @@ -6,7 +6,8 @@ try { $data = $messageBird->voiceTranscriptions->download('c226420d-f107-4db1-b2f9-4646656a90bc', '4f5ab5f4-c4b6-4586-9255-980bb3fd7336', 'a94f7d51-19b5-4eb8-9e8e-90fce490a577', '44e73d1f-201d-4a7d-963a-9d76bbca6c4f'); // Set call, leg, recording and transcription id here - echo sprintf("Received %d bytes.", mb_strlen($data)); + echo sprintf("Received %d bytes.\n", mb_strlen($data)); + echo sprintf("Transcription contents: `%s`.\n", $data); } catch (\Exception $e) { echo sprintf("%s: %s", get_class($e), $e->getMessage()); } From e8dfdd1ec7e478121bec4c20b757b90147507395 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Tue, 19 Sep 2017 15:15:42 +0200 Subject: [PATCH 10/13] Add BaseList for Voice, set pagination properties --- src/MessageBird/Objects/Voice/BaseList.php | 21 +++++++++++++++++++++ src/MessageBird/Resources/Voice/Base.php | 9 +++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/MessageBird/Objects/Voice/BaseList.php diff --git a/src/MessageBird/Objects/Voice/BaseList.php b/src/MessageBird/Objects/Voice/BaseList.php new file mode 100644 index 00000000..79cacd95 --- /dev/null +++ b/src/MessageBird/Objects/Voice/BaseList.php @@ -0,0 +1,21 @@ +data; - unset($body->items); - $baseList = new Objects\BaseList(); - $baseList->loadFromArray($body); + $baseList = new BaseList(); + if (property_exists($body, 'pagination')) { + $baseList->loadFromArray($body->pagination); + } $objectName = $this->Object; From f11ff02bca60703117d9f42351c3a73dab7c608e Mon Sep 17 00:00:00 2001 From: Dirk Hoekstra Date: Wed, 20 Sep 2017 12:20:38 +0200 Subject: [PATCH 11/13] Fixed array notation in VoiceTest --- tests/integration/voice/VoiceTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/integration/voice/VoiceTest.php b/tests/integration/voice/VoiceTest.php index b92868a0..4bbbfb58 100644 --- a/tests/integration/voice/VoiceTest.php +++ b/tests/integration/voice/VoiceTest.php @@ -280,14 +280,14 @@ public function testCreateVoiceCallFlow() { $callFlow = new \MessageBird\Objects\Voice\CallFlow(); $callFlow->title = 'Foobar'; - $callFlow->steps = [ - [ + $callFlow->steps = array( + array( 'action' => 'transfer', - 'options' => [ + 'options' => array( 'destination' => '31612345678', - ], - ] , - ]; + ), + ) , + ); $this->mockClient->expects($this->atLeastOnce())->method('performHttpRequest')->willReturn(array(201, '', '{ "data": [ From 77464ba356a96d3dc9c258086b624ba7c406c9eb Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Mon, 25 Sep 2017 16:01:37 +0200 Subject: [PATCH 12/13] For consistency, use traditional array syntax in Voice Calling examples --- examples/voice-call-flows-create.php | 6 +++--- examples/voice-calls-create.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php index 8b7f30d2..b4e94349 100644 --- a/examples/voice-call-flows-create.php +++ b/examples/voice-call-flows-create.php @@ -7,12 +7,12 @@ $callFlow->title = 'Foobar'; $step = new \MessageBird\Objects\Voice\Step(); $step->action = 'say'; -$step->options = [ +$step->options = array( 'payload' => 'This is a journey into sound.', 'language' => 'en-GB', 'voice' => 'male', -]; -$callFlow->steps = [$step]; +); +$callFlow->steps = array($step); try { $result = $messageBird->voiceCallFlows->create($callFlow); diff --git a/examples/voice-calls-create.php b/examples/voice-calls-create.php index 16cef429..6b9d9c5a 100644 --- a/examples/voice-calls-create.php +++ b/examples/voice-calls-create.php @@ -10,12 +10,12 @@ $callFlow->title = 'Say message'; $step = new \MessageBird\Objects\Voice\Step(); $step->action = 'say'; -$step->options = [ +$step->options = array( 'payload' => 'This is a journey into sound.', 'language' => 'en-GB', 'voice' => 'male', -]; -$callFlow->steps = [$step]; +); +$callFlow->steps = array($step); $call->callFlow = $callFlow; try { From b7d0b2714a6527f20f3f9536dc7d55d545317371 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Thu, 5 Oct 2017 09:26:28 +0200 Subject: [PATCH 13/13] Code cleanup --- src/MessageBird/Client.php | 10 +++++----- src/MessageBird/Objects/Contact.php | 2 -- src/MessageBird/Resources/Contacts.php | 1 - src/MessageBird/Resources/Voice/Recordings.php | 1 - 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/MessageBird/Client.php b/src/MessageBird/Client.php index 440d157d..c54c02f8 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -90,11 +90,6 @@ class Client */ public $chatContacts; - /** - * @var Resources\Voice\Calls - */ - public $Voice; - /** * @var Resources\Voice\Calls */ @@ -135,6 +130,11 @@ class Client */ protected $ChatAPIHttpClient; + /** + * @var Common\HttpClient + */ + protected $VoiceAPIHttpClient; + /** * @param string $accessKey * @param Common\HttpClient $httpClient diff --git a/src/MessageBird/Objects/Contact.php b/src/MessageBird/Objects/Contact.php index 360833ac..281fd8b2 100644 --- a/src/MessageBird/Objects/Contact.php +++ b/src/MessageBird/Objects/Contact.php @@ -3,8 +3,6 @@ namespace MessageBird\Objects; -use MessageBird\Resources\Groups; - /** * Class Contact * diff --git a/src/MessageBird/Resources/Contacts.php b/src/MessageBird/Resources/Contacts.php index 3efa8392..54e5d8ea 100644 --- a/src/MessageBird/Resources/Contacts.php +++ b/src/MessageBird/Resources/Contacts.php @@ -3,7 +3,6 @@ namespace MessageBird\Resources; use MessageBird\Objects; -use MessageBird\Exceptions; use MessageBird\Common; use InvalidArgumentException; diff --git a/src/MessageBird/Resources/Voice/Recordings.php b/src/MessageBird/Resources/Voice/Recordings.php index 57a6f122..3138fc99 100644 --- a/src/MessageBird/Resources/Voice/Recordings.php +++ b/src/MessageBird/Resources/Voice/Recordings.php @@ -2,7 +2,6 @@ namespace MessageBird\Resources\Voice; -use MessageBird\Client; use MessageBird\Common; use MessageBird\Exceptions; use MessageBird\Objects;