diff --git a/examples/voice-call-flows-create.php b/examples/voice-call-flows-create.php new file mode 100644 index 00000000..b4e94349 --- /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 = array( + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +); +$callFlow->steps = array($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..6b9d9c5a --- /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 = array( + 'payload' => 'This is a journey into sound.', + 'language' => 'en-GB', + 'voice' => 'male', +); +$callFlow->steps = array($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..b135b99e --- /dev/null +++ b/examples/voice-recordings-download.php @@ -0,0 +1,17 @@ +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.\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-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..1fa09883 --- /dev/null +++ b/examples/voice-transcriptions-download.php @@ -0,0 +1,13 @@ +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.\n", mb_strlen($data)); + echo sprintf("Transcription contents: `%s`.\n", $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 88a0e022..c54c02f8 100644 --- a/src/MessageBird/Client.php +++ b/src/MessageBird/Client.php @@ -11,8 +11,9 @@ 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.8.0'; + const CLIENT_VERSION = '1.9.0'; /** * @var string @@ -89,6 +90,36 @@ class Client */ public $chatContacts; + /** + * @var Resources\Voice\Calls + */ + public $voiceCalls; + + /** + * @var Resources\Voice\CallFlows + */ + public $voiceCallFlows; + + /** + * @var Resources\Voice\Legs + */ + public $voiceLegs; + + /** + * @var Resources\Voice\Recordings + */ + public $voiceRecordings; + + /** + * @var Resources\Voice\Transcriptions + */ + public $voiceTranscriptions; + + /** + * @var Resources\Voice\Webhooks + */ + public $voiceWebhooks; + /** * @var Common\HttpClient */ @@ -99,6 +130,11 @@ class Client */ protected $ChatAPIHttpClient; + /** + * @var Common\HttpClient + */ + protected $VoiceAPIHttpClient; + /** * @param string $accessKey * @param Common\HttpClient $httpClient @@ -108,9 +144,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); @@ -119,24 +159,33 @@ 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->mmsMessages = new Resources\MmsMessages($this->HttpClient); - $this->contacts = new Resources\Contacts($this->HttpClient); - $this->groups = new Resources\Groups($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); + $this->mmsMessages = new Resources\MmsMessages($this->HttpClient); + $this->contacts = new Resources\Contacts($this->HttpClient); + $this->groups = new Resources\Groups($this->HttpClient); } /** @@ -148,6 +197,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 687f6a32..d37742bd 100644 --- a/src/MessageBird/Common/HttpClient.php +++ b/src/MessageBird/Common/HttpClient.php @@ -45,14 +45,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; @@ -73,6 +77,7 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) } $this->connectionTimeout = $connectionTimeout; + $this->headers = $headers; } /** @@ -110,6 +115,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 @@ -137,6 +150,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/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/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 @@ +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; + + $baseList = new BaseList(); + if (property_exists($body, 'pagination')) { + $baseList->loadFromArray($body->pagination); + } + + $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..3138fc99 --- /dev/null +++ b/src/MessageBird/Resources/Voice/Recordings.php @@ -0,0 +1,141 @@ +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..4bbbfb58 --- /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 = array( + array( + 'action' => 'transfer', + 'options' => array( + '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'); + } +}