From 6180447046788c1a35a041693cdd39d1062a2159 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Tue, 31 Aug 2010 22:11:14 +0800 Subject: [PATCH 001/107] (Fix) Corrected bugs in createSession() method. --- TropoClasses.php | 97 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 29 deletions(-) diff --git a/TropoClasses.php b/TropoClasses.php index d40bf4d..5af0df6 100644 --- a/TropoClasses.php +++ b/TropoClasses.php @@ -42,6 +42,12 @@ class Tropo extends BaseClass { */ private $_language; + // URL for the Tropo session API. + const SessionAPI = 'http://api.tropo.com/1.0/sessions?action=create&token='; + + // Success response from Tropo Session API. + const SessionResponse = 'true'; + /** * Class constructor for the Tropo class. * @access private @@ -142,7 +148,7 @@ public function ask($ask, Array $params=NULL) { */ public function call($call, Array $params=NULL) { if(!is_object($call)) { - $p = array('call', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording'); + $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -189,13 +195,13 @@ public function conference($conference, Array $params=NULL) { * Your app fetches a phone number from a database does something * like this... * - * createSession($token, array('dial' => $number)); - * ?> + * ?> * * Your Tropo application looks like this... * @@ -216,21 +222,29 @@ public function conference($conference, Array $params=NULL) { * @return bool True if the session was launched successfully */ public function createSession($token, Array $params = null) { - if (!function_exists('curl_open')) { - throw new Exception('curl not installed.'); - } - foreach ($params as $key=>$value) { - $querystring .= '&'. $key . '=' . $value; - } - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'http://api.tropo.com/1.0/sessions?action=create&token=' . $token . $params); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $result = curl_exec($ch); - curl_close($ch); - if (strpos($result, 'true') === false) { - throw new Exception('Session launch failed.'); - } - return true; + if (!function_exists('curl_init')) { + throw new Exception('PHP curl not installed.'); + } + if(isset($params)) { + foreach ($params as $key=>$value) { + $querystring .= '&'. $key . '=' . $value; + } + } + + $ch = curl_init(self::SessionAPI.$token.$querystring); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + $error = curl_error($ch); + curl_close($ch); + + if($result === false) { + throw new Exception('An error occurred: '.$error); + } else { + if (strpos($result, self::SessionResponse) === false) { + throw new Exception('An error occurred: Tropo session launch failed.'); + } + return true; + } } @@ -992,13 +1006,13 @@ class Result { private $_error; private $_actions; private $_name; - private $_attempts; - private $_disposition; - private $_confidence; - private $_interpretation; - private $_concept; - private $_utterance; - private $_value; + private $_attempts; + private $_disposition; + private $_confidence; + private $_interpretation; + private $_concept; + private $_utterance; + private $_value; /** * Class constructor @@ -1011,7 +1025,7 @@ public function __construct($json=NULL) { // if $json is still empty, there was nothing in // the POST so throw an exception if(empty($json)) { - throw new Exception('No json available.'); + throw new Exception('No JSON available.'); } } $result = json_decode($json); @@ -1169,7 +1183,7 @@ public function __construct($json=NULL) { // if $json is still empty, there was nothing in // the POST so throw exception if(empty($json)) { - throw new Exception('No json available.', 1); + throw new Exception('No JSON available.', 1); } } $session = json_decode($json); @@ -1183,7 +1197,7 @@ public function __construct($json=NULL) { $this->_initialText = $session->session->initialText; $this->_to = array("id" => $session->session->to->id, "channel" => $session->session->to->channel, "name" => $session->session->to->name, "network" => $session->session->to->network); $this->_from = array("id" => $session->session->from->id, "channel" => $session->session->from->channel, "name" => $session->session->from->name, "network" => $session->session->from->network); - $this->_headers = $session->session->headers; + $this->_headers = self::setHeaders($session->session->headers); $this->_parameters = property_exists($session->session, 'parameters') ? (Array) $session->session->parameters : null; } @@ -1251,6 +1265,14 @@ public function getParameters($name = null) { return $this->_parameters; } } + + public function setHeaders($headers) { + $formattedHeaders = new Headers(); + foreach($headers as $name => $value) { + $formattedHeaders->$name = $value; + } + return $formattedHeaders; + } } /** @@ -1361,7 +1383,7 @@ class Transfer extends BaseClass { /** * Class constructor * - * @param Endpoint $to + * @param string $to * @param boolean $answerOnMedia * @param Choices $choices * @param Endpoint $from @@ -1562,4 +1584,21 @@ class Voice { public static $Mexican_Spanish_female = "soledad"; } +/** + * SIP Headers Helper class. + * @package TropoPHP_Support + */ +class Headers { + + public function __set($name, $value) { + if(!strstr($name, "-")) { + $this->$name = $value; + } else { + $name = str_replace("-", "_", $name); + $this->$name = $value; + } + } + +} + ?> \ No newline at end of file From 216b173f2a23fbc6c929ad94fcf023864f00efcf Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Fri, 10 Sep 2010 03:10:45 +0800 Subject: [PATCH 002/107] Changing name of Tropo class file. --- readme.markdown | 4 ++-- samples/get_zip_code.php | 2 +- samples/hello_world.php | 2 +- samples/outbound_call.php | 2 +- samples/recordingdemo.php | 2 +- tests/AskTest.php | 2 +- tests/CallTest.php | 2 +- tests/ConferenceTest.php | 2 +- tests/HangupTest.php | 2 +- tests/MessageTest.php | 2 +- tests/RecordTest.php | 2 +- tests/StartRecordingTest.php | 2 +- TropoClasses.php => tropo.class.php | 4 ++-- 13 files changed, 15 insertions(+), 15 deletions(-) rename TropoClasses.php => tropo.class.php (99%) diff --git a/readme.markdown b/readme.markdown index 4698466..adbf1d8 100644 --- a/readme.markdown +++ b/readme.markdown @@ -9,7 +9,7 @@ Usage Answer the phone, say something, and hang up. ask('What is your favorite programming language?', array( diff --git a/samples/get_zip_code.php b/samples/get_zip_code.php index 2ebbe6b..2ed131e 100644 --- a/samples/get_zip_code.php +++ b/samples/get_zip_code.php @@ -1,7 +1,7 @@ Say("Hello World!"); diff --git a/samples/outbound_call.php b/samples/outbound_call.php index 51fe255..d7d395a 100644 --- a/samples/outbound_call.php +++ b/samples/outbound_call.php @@ -1,5 +1,5 @@ getParams("action") == "create") { * $tropo->call($session->getParams("dial")); From 9908ecb3f35cb46d8cdf7f96fbf8f5d1b46bccb8 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Fri, 10 Sep 2010 03:37:29 +0800 Subject: [PATCH 003/107] Added TropoClasses.php file for backward compatibility --- TropoClasses.php | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 TropoClasses.php diff --git a/TropoClasses.php b/TropoClasses.php new file mode 100644 index 0000000..0c63928 --- /dev/null +++ b/TropoClasses.php @@ -0,0 +1,5 @@ + + From a2084975758b1de16679f3268e27fa654fb61e29 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Tue, 28 Sep 2010 03:31:49 +0800 Subject: [PATCH 004/107] Added Tropo REST API classes, and provisioning samples. --- samples/Provisioning-AddAddress.php | 26 ++ samples/Provisioning-AddNumber.php | 26 ++ samples/Provisioning-AddToken.php | 26 ++ samples/Provisioning-CreateApp.php | 33 +++ samples/Provisioning-DeleteAddress.php | 26 ++ samples/Provisioning-DeleteApp.php | 26 ++ tropo-rest.class.php | 379 +++++++++++++++++++++++++ tropo.class.php | 276 +++++++++++++----- 8 files changed, 742 insertions(+), 76 deletions(-) create mode 100644 samples/Provisioning-AddAddress.php create mode 100644 samples/Provisioning-AddNumber.php create mode 100644 samples/Provisioning-AddToken.php create mode 100644 samples/Provisioning-CreateApp.php create mode 100644 samples/Provisioning-DeleteAddress.php create mode 100644 samples/Provisioning-DeleteApp.php create mode 100644 tropo-rest.class.php diff --git a/samples/Provisioning-AddAddress.php b/samples/Provisioning-AddAddress.php new file mode 100644 index 0000000..1cf582b --- /dev/null +++ b/samples/Provisioning-AddAddress.php @@ -0,0 +1,26 @@ +updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$aim, "username" => "AIMUser01", "password" => "secret")); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/samples/Provisioning-AddNumber.php b/samples/Provisioning-AddNumber.php new file mode 100644 index 0000000..03dc731 --- /dev/null +++ b/samples/Provisioning-AddNumber.php @@ -0,0 +1,26 @@ +updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$number, "prefix" => "1407")); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/samples/Provisioning-AddToken.php b/samples/Provisioning-AddToken.php new file mode 100644 index 0000000..dd7e2ac --- /dev/null +++ b/samples/Provisioning-AddToken.php @@ -0,0 +1,26 @@ +updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$token, "channel" => "voice")); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/samples/Provisioning-CreateApp.php b/samples/Provisioning-CreateApp.php new file mode 100644 index 0000000..c97221c --- /dev/null +++ b/samples/Provisioning-CreateApp.php @@ -0,0 +1,33 @@ + "My Awesome App", + "voiceUrl" => "http://www.fake.com/index.php", + "messagingUrl" => "http://www.fake.com/index2.php", + "platform" => "webapi", + "partition" => "staging"); + + echo $tropo->createApplication($userid, $password, $appSettings); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/samples/Provisioning-DeleteAddress.php b/samples/Provisioning-DeleteAddress.php new file mode 100644 index 0000000..5fe8036 --- /dev/null +++ b/samples/Provisioning-DeleteAddress.php @@ -0,0 +1,26 @@ +deleteApplicationAddress($userid, $password, $applicationID, AddressType::$number, $number); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/samples/Provisioning-DeleteApp.php b/samples/Provisioning-DeleteApp.php new file mode 100644 index 0000000..2497ca2 --- /dev/null +++ b/samples/Provisioning-DeleteApp.php @@ -0,0 +1,26 @@ +deleteApplication($userid, $password, $applicationID); +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} + +?> \ No newline at end of file diff --git a/tropo-rest.class.php b/tropo-rest.class.php new file mode 100644 index 0000000..7f9bec3 --- /dev/null +++ b/tropo-rest.class.php @@ -0,0 +1,379 @@ +true'; + + public function __construct() { + parent::__construct(); + } + + /** + * Launch a new Tropo session. + * + * @param string $token + * @param array $params + * @return boolean + */ + public function createSession($token, Array $params = null) { + + if(isset($params)) { + foreach ($params as $key=>$value) { + @ $querystring .= '&'. $key . '=' . $value; + } + } + + curl_setopt($this->ch, CURLOPT_URL, self::SessionURL.$token.$querystring); + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($this->ch); + $error = curl_error($this->ch); + parent::__destruct(); + + if($result === false) { + throw new Exception('An error occurred: '.$error); + } else { + if (strpos($result, self::SessionResponse) === false) { + throw new Exception('An error occurred: Tropo session launch failed.'); + } + return true; + } + } +} + +class ProvisioningAPI extends RestBase { + + // URL for the Tropo provisioning API. + const ProvisioningURLBase = 'http://api.tropo.com/provisioning/'; + + public function __construct($userid, $password) { + parent::__construct($userid, $password); + } + + /** + * Create a new Tropo application. + * + * @param string $href + * @param string $name + * @param string $voiceUrl + * @param string $messagingUrl + * @param string $platform + * @param string $partition + * @return string JSON + */ + public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition) { + + $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); + $url = self::ProvisioningURLBase.'applications'; + return self::makeAPICall('POST', $url, $payload); + + } + + /** + * Update an existing Tropo application to add an address. + * + * @param string $applicationID + * @param string $type + * @param string $prefix + * @param string $number + * @param string $city + * @param string $state + * @param string $channel + * @param string $username + * @param string $password + * @param string $token + * @return string JSON + */ + public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) { + + $payload = json_encode(new Address($type, $prefix, $number, $city, $state, $channel, $username, $password, $token)); + $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + return self::makeAPICall('POST', $url, $payload); + + } + + /** + * Update an application property. + * + * @param string $applicationID + * @param string $href + * @param string $name + * @param string $voiceUrl + * @param string $messagingUrl + * @param string $platform + * @param string $partition + * @return string JSON + */ + public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) { + + $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); + $url = self::ProvisioningURLBase.'applications/'.$applicationID; + return self::makeAPICall('PUT', $url, $payload); + + } + + /** + * Delete an existing Tropo application. + * + * @param string $applicationID + * @return string JSON + */ + public function deleteApplication($applicationID) { + + $url = self::ProvisioningURLBase.'applications/'.$applicationID; + return self::makeAPICall('DELETE', $url); + + } + + /** + * Delete an application address. + * + * @param string $applicationID + * @param string $type + * @param string $address + * @return string JSON + */ + public function deleteApplicationAddress($applicationID, $type, $address) { + + $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses/'.$type.'/'.$address; + return self::makeAPICall('DELETE', $url); + + } + + /** + * View all applications for an account. + * + * @return string JSON + */ + public function viewApplications() { + + $url = self::ProvisioningURLBase.'applications'; + return self::makeAPICall('GET', $url); + + } + + /** + * View the details of a specific application. + * + * @param string $applicationID + * @return string JSON + */ + public function viewSpecificApplication($applicationID) { + + $url = self::ProvisioningURLBase.'applications/'.$applicationID; + return self::makeAPICall('GET', $url); + + } + + /** + * View all of the addreses for an application. + * + * @param string $applicationID + * @return string JSON + */ + public function viewAddresses($applicationID) { + + $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + return self::makeAPICall('GET', $url); + + } + + /** + * View a list of availalbe exchanges + * + * @return string JSON + */ + public function viewExchanges() { + + $url = self::ProvisioningURLBase.'exchanges'; + return self::makeAPICall('GET', $url); + + } + + /** + * Method to make REST API call. + * + * @param string $method + * @param string $url + * @param string $payload + * @return string JSON + */ + private function makeAPICall($method, $url, $payload=NULL) { + + if(($method == 'POST' || $method == 'PUT') && !isset($payload)) { + throw new Exception("Method $method requires payload for request body."); + } + + curl_setopt($this->ch, CURLOPT_URL, $url); + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); + + switch($method) { + + case 'POST': + curl_setopt($this->ch, CURLOPT_POST, true); + curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($payload))); + curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload); + break; + + case 'PUT': + curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($payload))); + curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload); + break; + + case 'DELETE': + curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + + default: + curl_setopt($this->ch, CURLOPT_HTTPGET, true); + + } + + $this->result = curl_exec($this->ch); + $this->error = curl_error($this->ch); + $this->curl_info = curl_getinfo($this->ch); + $this->curl_http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE); + + if($this->result === false) { + throw new Exception('An error occurred: '.$this->error); + } else { + if ($this->curl_http_code != '200') { + throw new Exception('An error occurred: Invalid HTTP response returned: '.$this->curl_http_code); + } + return $this->result; + } + } + + public function getResult() { + return $this->result; + } + + public function getInfo() { + return $this->curl_info; + } + + public function getHTTPCode() { + return $this->curl_http_code; + } + + public function __destruct() { + parent::__destruct(); + } + +} + +/** + * Base class for all REST classes. + * + */ +class RestBase { + + protected $ch; + protected $result; + protected $error; + protected $curl_info; + protected $curl_http_code; + + public function __construct($userid=NULL, $password=NULL) { + if (!function_exists('curl_init')) { + throw new Exception('PHP curl not installed.'); + } + $this->ch = curl_init(); + if(isset($userid) && isset($password)) { + curl_setopt($this->ch, CURLOPT_USERPWD, "$userid:$password"); + } + } + + public function __destruct() { + @ curl_close($this->ch); + } +} + +/** + * Application class. Represents a Tropo application. + * + */ +class Application { + + public function __construct($href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) { + if(isset($href)) { $this->href = $href; } + if(isset($name)) { $this->name = $name; } + if(isset($voiceUrl)) { $this->voiceUrl = $voiceUrl; } + if(isset($messagingUrl)) { $this->messagingUrl = $messagingUrl; } + if(isset($platform)) { $this->platform = $platform; } + if(isset($partition)) { $this->partition = $partition; } + } + + public function __set($attribute, $value) { + $this->$attribute= $value; + } +} + +/** + * Address class. Represents an address assigned to a Tropo application. + * + */ +class Address { + + public function __construct($type=NULL, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) { + if(isset($type)) { $this->type = $type; } + if(isset($prefix)) { $this->prefix = $prefix; } + if(isset($number)) { $this->number = $number; } + if(isset($city)) { $this->type = $type; } + if(isset($state)) { $this->state = $state; } + if(isset($channel)) { $this->channel = $channel; } + if(isset($username)) { $this->username = $username; } + if(isset($password)) { $this->password = $password; } + if(isset($token)) { $this->token = $token; } + } + + public function __set($attribute, $value) { + $this->$attribute= $value; + } +} + +/** + * Exchange class. Represents an exchange. + * + */ +class Exchange { + + public function __construct($prefix=NULL, $city=NULL, $state=NULL, $country=NULL) { + if(isset($prefix)) { $this->prefix = $prefix; } + if(isset($city)) { $this->city = $city; } + if(isset($state)) { $this->state = $state; } + if(isset($country)) { $this->country = $country; } + } + + public function __set($attribute, $value) { + $this->$attribute= $value; + } +} +/** + * Helper class listing the type of addresses available to use with Tropo applications. + * + */ + +class AddressType { + public static $number = "number"; + public static $token = "token"; + public static $aim = "aim"; + public static $gtalk = "gtalk"; + public static $jabber = "jabber"; + public static $msn = "msn"; + public static $yahoo = "yahoo"; + public static $skype = "skype"; +} + +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index bc8ada6..5707df1 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -16,6 +16,8 @@ * @see https://www.tropo.com/docs/webapi/tropo.htm * */ +include 'tropo-rest.class.php'; + class Tropo extends BaseClass { /** @@ -42,12 +44,6 @@ class Tropo extends BaseClass { */ private $_language; - // URL for the Tropo session API. - const SessionAPI = 'http://api.tropo.com/1.0/sessions?action=create&token='; - - // Success response from Tropo Session API. - const SessionResponse = 'true'; - /** * Class constructor for the Tropo class. * @access private @@ -182,72 +178,6 @@ public function conference($conference, Array $params=NULL) { $this->conference = sprintf($conference); } - /** - * Launches a new session with the Tropo Session API - * - * Launching a session will cause Tropo to cal your URL as if - * someone has called your phone number. If you want to make an - * outbound call, have your code check the "action" parameter. - * If it's set to "create" then this is a token launched session - * and you can use $tropo->call() to make an outbound call. - * - * Example: - * Your app fetches a phone number from a database does something - * like this... - * - * createSession($token, array('dial' => $number)); - * ?> - * - * Your Tropo application looks like this... - * - * getParams("action") == "create") { - * $tropo->call($session->getParams("dial")); - * $tropo->say('This is an outbound call.'); - * } else { - * $tropo->say('Thank you for calling us.'); - * } - * $tropo->renderJSON(); - * ?> - * - * @param string $token Your outbound session token from Tropo - * @param array $params An array of key value pairs that will be added as query string parameters - * @return bool True if the session was launched successfully - */ - public function createSession($token, Array $params = null) { - if (!function_exists('curl_init')) { - throw new Exception('PHP curl not installed.'); - } - if(isset($params)) { - foreach ($params as $key=>$value) { - $querystring .= '&'. $key . '=' . $value; - } - } - - $ch = curl_init(self::SessionAPI.$token.$querystring); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $result = curl_exec($ch); - $error = curl_error($ch); - curl_close($ch); - - if($result === false) { - throw new Exception('An error occurred: '.$error); - } else { - if (strpos($result, self::SessionResponse) === false) { - throw new Exception('An error occurred: Tropo session launch failed.'); - } - return true; - } - } - - /** * This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. * @see https://www.tropo.com/docs/webapi/hangup.htm @@ -447,6 +377,195 @@ public function transfer($transfer, Array $params=NULL) { } $this->transfer = sprintf($transfer); } + + /** + * Launches a new session with the Tropo Session API. + * (Pass through to SessionAPI class.) + * + * @param string $token Your outbound session token from Tropo + * @param array $params An array of key value pairs that will be added as query string parameters + * @return bool True if the session was launched successfully + */ + public function createSession($token, Array $params=NULL) { + try { + $session = new SessionAPI(); + $result = $session->createSession($token, $params); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Creates a new Tropo Application + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param array $params + * @return string JSON + */ + public function createApplication($userid, $password, Array $params) { + $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $password); + $result = $provision->createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Add/Update an address (phone number, IM address or token) for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param array $params + * @return string JSON + */ + public function updateApplicationAddress($userid, $passwd, $applicationID, Array $params) { + $p = array('type', 'prefix', 'number', 'city', 'state', 'channel', 'username', 'password', 'token'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $passwd); + $result = $provision->updateApplicationAddress($applicationID, $type, $prefix, $number, $city, $state, $channel, $username, $password, $token); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Update a property (name, URL, platform, etc.) for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param array $params + * @return string JSON + */ + public function updateApplicationProperty($userid, $password, $applicationID, Array $params) { + $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $password); + $result = $provision->updateApplicationProperty($applicationID, $href, $name, $voiceUrl, $messagingUrl, $platform, $partition); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Delete an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function deleteApplication($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->deleteApplication($applicationID); + } + + /** + * Delete an address for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param string $number + * @return string JSON + */ + public function deleteApplicationAddress($userid, $password, $applicationID, $addresstype, $address) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->deleteApplicationAddress($applicationID, $addresstype, $address); + } + + /** + * View a list of Tropo applications. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @return string JSON + */ + public function viewApplications($userid, $password) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewApplications(); + } + + /** + * View the details of a specific Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function viewSpecificApplication($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewSpecificApplication($applicationID); + } + + /** + * View the addresses for a specific Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function viewAddresses($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewAddresses($applicationID); + } + + /** + * View a list of available exchanges for assigning a number to a Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param unknown_type $userid + * @param unknown_type $password + * @return unknown + */ + public function viewExchanges($userid, $password) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewExchanges(); + } /** * Renders the Tropo object as JSON. @@ -1025,12 +1144,12 @@ public function __construct($json=NULL) { // if $json is still empty, there was nothing in // the POST so throw an exception if(empty($json)) { - throw new Exception('No JSON available.'); + throw new TropoException('No JSON available.'); } } $result = json_decode($json); if (!is_object($result) || !property_exists($result, "result")) { - throw new Exception('Not a result object.'); + throw new TropoException('Not a result object.'); } $this->_sessionId = $result->result->sessionId; $this->_state = $result->result->state; @@ -1183,12 +1302,12 @@ public function __construct($json=NULL) { // if $json is still empty, there was nothing in // the POST so throw exception if(empty($json)) { - throw new Exception('No JSON available.', 1); + throw new TropoException('No JSON available.', 1); } } $session = json_decode($json); if (!is_object($session) || !property_exists($session, "session")) { - throw new Exception('Not a session object.', 2); + throw new TropoException('Not a session object.', 2); } $this->_id = $session->session->id; $this->_accountId = $session->session->accountId; @@ -1457,6 +1576,11 @@ public function __toString() { } } +/** + * A helper class for wrapping exceptions. Can be modified for custom excpetion handling. + * + */ +class TropoException extends Exception { } /** * Date Helper class. From 9db0b8a5ffd5c91866bad1fe2c4b71acfaa8ec14 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Mon, 25 Oct 2010 16:18:48 -0700 Subject: [PATCH 005/107] Add a sample showing how to access info about the incoming call or text (caller ID, called number, initial text) --- samples/call-info.php | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 samples/call-info.php diff --git a/samples/call-info.php b/samples/call-info.php new file mode 100644 index 0000000..7ac65dc --- /dev/null +++ b/samples/call-info.php @@ -0,0 +1,44 @@ +getFrom(); + +$tropo = new Tropo(); +// $caller now has a hash containing the keys: id, name, channel, and network +$tropo->say("Your phone number is " . $caller['id']); + +$called = $session->getTo(); + +// $called now has a hash containing the keys: id, name, channel, and network +$tropo->say("You called " . $called['id'] . " but you probably already knew that."); + +if ($called['channel'] == "TEXT") { + // This is a text message + $tropo->say("You contacted me via text."); + + // The first text of the session is going to be queued and applied to the first + // ask statement you include... + $tropo->ask("This will catch the first text", array('choices' => '[ANY]')); + + // ... or, you can grab that first text like this straight from the session. + $messsage = $tropo->getInitialText(); + + $tropo->say("You said " . $message); +} else { + // This is a phone call + $tropo->say("Awww. How nice. You cared enough to call."); +} + +print $tropo; +?> \ No newline at end of file From 2528d3c6c5a16dc28c8c7a883c3bf5d95fe503a7 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 9 Nov 2010 22:37:04 -0800 Subject: [PATCH 006/107] FIX: on some versions of PHP, the say on an ask results in json recursion errors or strange output on the ask --- tropo.class.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index ca64495..de6d1bc 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -120,17 +120,17 @@ public function ask($ask, Array $params=NULL) { $$option = $params[$option]; } } - $say = new Say($ask, $as, null, $voice); - $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; - $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["dtmf"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice); + $say[] = new Say($ask, $as, null, $voice); if (is_array($event)) { // If an event was passed in, add the events to the Ask foreach ($event as $e => $val){ - $ask->addEvent(new Say($val, $as, $e, $voice)); + $say[] = new Say($val, $as, $e, $voice); } } + $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; + $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; + $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["dtmf"]) : null; + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice); } $this->ask = sprintf($ask); } @@ -241,7 +241,7 @@ public function record($record) { if(!is_object($record) && is_array($record)) { $params = $record; $choices = isset($params["choices"]) ? new Choices($params["choices"]) : null; - $say = $params["say"]; + $say = new Say($params["say"], $params["as"], null, $params["voice"]); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); foreach ($p as $option) { @@ -695,7 +695,7 @@ class Ask extends BaseClass { * @param int $timeout * @param string $voice */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, Say $say=NULL, $timeout=NULL, $voice=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_choices = isset($choices) ? sprintf($choices) : null ; @@ -703,9 +703,8 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL $this->_name = $name; $this->_required = $required; $this->_voice = $voice; - $this->_say = isset($say) ? sprintf($say) : null; - $this->_say = array($this->_say); // Convert the say to an array - $this->_timeout = $timeout; + $this->_say = isset($say) ? $say : null; + $this->_timeout = $timeout; } /** @@ -720,6 +719,11 @@ public function __toString() { if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_say)) { $this->say = $this->_say; } + if (is_array($this->_say)) { + foreach ($this->_say as $k => $v) { + $this->_say[$k] = sprintf($v); + } + } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_voice)) { $this->voice = $this->_voice; } return $this->unescapeJSON(json_encode($this)); @@ -733,7 +737,7 @@ public function __toString() { * @param Say $say A say object */ public function addEvent(Say $say) { - $this->_say[] = sprintf($say); + $this->_say[] = $say; } } From 2b6068fb020b0e0d88b455f97d838abcf1b646cb Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 12 Nov 2010 15:26:34 -0800 Subject: [PATCH 007/107] Fix: outbound call sample has errors about missing getParams method Fix: Session object would complain about invalid foreach if the session isn't started by an inbound call or text Change: URL encode parameters passed to the session API in the createSession method. --- samples/outbound_call.php | 13 +++++++++---- tropo-rest.class.php | 2 +- tropo.class.php | 8 ++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/samples/outbound_call.php b/samples/outbound_call.php index d7d395a..3c615cd 100644 --- a/samples/outbound_call.php +++ b/samples/outbound_call.php @@ -1,25 +1,30 @@ getParams("action") == "create") { - $tropo->call($session->getParams("dial")); + if ($session->getParameters("action") == "create") { + $tropo->call($session->getParameters("dial")); $tropo->say('This is an outbound call.'); } else { $tropo->say('Thank you for calling us.'); } $tropo->renderJSON(); -} catch (Exception $e) { +} catch (TropoException $e) { if ($e->getCode() == '1') { // The session object threw an exception, so this file wasn't // loaded as part of a Tropo session. Launch a Tropo session. if ($tropo->createSession($token, array('dial' => $number))) { print 'Call launched to ' . $number; + } else { + print "call failed! Try it again with the Tropo debugger running to see what the error is."; } } } diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 7f9bec3..9fefdc7 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -31,7 +31,7 @@ public function createSession($token, Array $params = null) { if(isset($params)) { foreach ($params as $key=>$value) { - @ $querystring .= '&'. $key . '=' . $value; + @ $querystring .= '&'. urlencode($key) . '=' . urlencode($value); } } diff --git a/tropo.class.php b/tropo.class.php index de6d1bc..d273987 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1391,8 +1391,12 @@ public function getParameters($name = null) { public function setHeaders($headers) { $formattedHeaders = new Headers(); - foreach($headers as $name => $value) { - $formattedHeaders->$name = $value; + // headers don't exist on outboud calls + // so only do this if there are headers + if (is_array($headers)) { + foreach($headers as $name => $value) { + $formattedHeaders->$name = $value; + } } return $formattedHeaders; } From 933ad9b11f929363cd577ad1c2f22cf1c05f3621 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Sat, 13 Nov 2010 22:13:06 -0800 Subject: [PATCH 008/107] Added a lot more commenting to the outbound call example. --- samples/outbound_call.php | 41 ++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/samples/outbound_call.php b/samples/outbound_call.php index 3c615cd..941a6dd 100644 --- a/samples/outbound_call.php +++ b/samples/outbound_call.php @@ -1,26 +1,53 @@ getParameters("action") == "create") { - $tropo->call($session->getParameters("dial")); - $tropo->say('This is an outbound call.'); + + if ($session->getParameters("action") == "create") { + // A token-launched session (an outgoing call) will + // have a parameter called "action" that is set to + // "create". If this is true, we're trying to make an + // outgoing call. The next two lines make that call + // and say something. + $tropo->call($session->getParameters("dial")); + $tropo->say('This is an outbound call.'); } else { - $tropo->say('Thank you for calling us.'); + + // The session JSON exists, but there's no action + // parameter or it wasn't set to "create" so this must + // be an incoming call. + $tropo->say('Thank you for calling us.'); } $tropo->renderJSON(); } catch (TropoException $e) { if ($e->getCode() == '1') { + // The session object threw an exception, so this file wasn't - // loaded as part of a Tropo session. Launch a Tropo session. + // loaded as part of a Tropo session. Use the session API to + // launch a new session. if ($tropo->createSession($token, array('dial' => $number))) { print 'Call launched to ' . $number; } else { From f9523edfa16a653068d631da980a4ecaa43e5d47 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Wed, 17 Nov 2010 07:14:57 -0800 Subject: [PATCH 009/107] (Fix) Corrected issue with Ask method, not setting terminator on Choice properly. --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index d273987..39ff3c8 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -129,7 +129,7 @@ public function ask($ask, Array $params=NULL) { } $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["dtmf"]) : null; + $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice); } $this->ask = sprintf($ask); From f30eb9b7315e424258014736380001db27bf3564 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Mon, 6 Dec 2010 10:17:23 -0500 Subject: [PATCH 010/107] (Fix) Moified Voice property of Tropo object to render properly on Say and Ask --- tropo.class.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 39ff3c8..17dae28 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -112,7 +112,6 @@ public function setLanguage($language) { */ public function ask($ask, Array $params=NULL) { if(!is_object($ask)) { - $voice = isset($this->_voice) ? $this->_voice : null; $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout'); foreach ($p as $option) { $$option = null; @@ -129,7 +128,8 @@ public function ask($ask, Array $params=NULL) { } $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; + $voice = isset($this->_voice) ? $this->_voice : null; + $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice); } $this->ask = sprintf($ask); @@ -306,9 +306,6 @@ public function reject() { */ public function say($say, Array $params=NULL) { if(!is_object($say)) { - if (isset($this->_voice)) { - $voice = $this->_voice; - } $p = array('as', 'format', 'event','voice'); $value = $say; foreach ($p as $option) { @@ -317,6 +314,7 @@ public function say($say, Array $params=NULL) { $$option = $params[$option]; } } + $voice = isset($voice) ? $voice : $this->_voice; $say = new Say($value, $as, $event, $voice); } $this->say = array(sprintf($say)); From 502df5b2d06689d1d01e400b4512b5f472a4c182 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Fri, 10 Dec 2010 11:29:10 -0500 Subject: [PATCH 011/107] (Fix) Corrected issue with from param on transfer. --- samples/Provisioning-CreateApp.php | 2 +- tropo.class.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/Provisioning-CreateApp.php b/samples/Provisioning-CreateApp.php index c97221c..b670ca8 100644 --- a/samples/Provisioning-CreateApp.php +++ b/samples/Provisioning-CreateApp.php @@ -30,4 +30,4 @@ echo $ex->getMessage(); } -?> \ No newline at end of file +?> diff --git a/tropo.class.php b/tropo.class.php index 17dae28..b964cd1 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1516,11 +1516,11 @@ class Transfer extends BaseClass { * @param int $ringRepeat * @param int $timeout */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, Endpoint $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf($choices) : null; - $this->_from = isset($from) ? sprintf($from) : null; + $this->_from = $from; $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; $this->_on = isset($on) ? sprintf($on) : null; From 362ffe546bbf28478bdeed0de2ecfcfa8eaf048d Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Sun, 19 Dec 2010 13:28:21 -0500 Subject: [PATCH 012/107] Updated Tropo REST class for Provisioning API changes. --- samples/Provisioning-AddAddress.php | 5 ++++- samples/Provisioning-AddNewURL.php | 35 +++++++++++++++++++++++++++++ samples/Provisioning-AddNumber.php | 5 ++++- samples/Provisioning-AddToken.php | 2 +- samples/Provisioning-CreateApp.php | 16 ++++++++----- tropo-rest.class.php | 26 +++++++++++---------- 6 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 samples/Provisioning-AddNewURL.php diff --git a/samples/Provisioning-AddAddress.php b/samples/Provisioning-AddAddress.php index 1cf582b..e55d84d 100644 --- a/samples/Provisioning-AddAddress.php +++ b/samples/Provisioning-AddAddress.php @@ -16,7 +16,10 @@ $tropo = new Tropo(); try { - echo $tropo->updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$aim, "username" => "AIMUser01", "password" => "secret")); + + $params = array("type" => AddressType::$aim, "username" => "AIMUser01", "password" => "secret"); + echo $tropo->updateApplicationAddress($userid, $password, $applicationID, $params); + } catch (TropoException $ex) { diff --git a/samples/Provisioning-AddNewURL.php b/samples/Provisioning-AddNewURL.php new file mode 100644 index 0000000..3ca36e9 --- /dev/null +++ b/samples/Provisioning-AddNewURL.php @@ -0,0 +1,35 @@ + "My Awesome App", + "voiceUrl" => "http://www.anotherfake.com/index.php", + "messagingUrl" => "http://www.anotherfake.com/index2.php", + "platform" => "webapi", + "partition" => "staging" + ); + + echo $tropo->updateApplicationProperty($userid, $password, $applicationID, $appSettings); + +} + +catch (TropoException $ex) { + echo $ex->getMessage(); +} \ No newline at end of file diff --git a/samples/Provisioning-AddNumber.php b/samples/Provisioning-AddNumber.php index 03dc731..62e91a1 100644 --- a/samples/Provisioning-AddNumber.php +++ b/samples/Provisioning-AddNumber.php @@ -16,7 +16,10 @@ $tropo = new Tropo(); try { - echo $tropo->updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$number, "prefix" => "1407")); + + $params = array("type" => AddressType::$number, "prefix" => "1407"); + echo $tropo->updateApplicationAddress($userid, $password, $applicationID, $params); + } catch (TropoException $ex) { diff --git a/samples/Provisioning-AddToken.php b/samples/Provisioning-AddToken.php index dd7e2ac..96a9cb0 100644 --- a/samples/Provisioning-AddToken.php +++ b/samples/Provisioning-AddToken.php @@ -16,7 +16,7 @@ $tropo = new Tropo(); try { - echo $tropo->updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$token, "channel" => "voice")); + echo $tropo->updateApplicationAddress($userid, $password, $applicationID, array("type" => AddressType::$token, "channel" => "messaging")); } catch (TropoException $ex) { diff --git a/samples/Provisioning-CreateApp.php b/samples/Provisioning-CreateApp.php index b670ca8..2fce695 100644 --- a/samples/Provisioning-CreateApp.php +++ b/samples/Provisioning-CreateApp.php @@ -17,17 +17,21 @@ $tropo = new Tropo(); try { - $appSettings = array("name" => "My Awesome App", - "voiceUrl" => "http://www.fake.com/index.php", - "messagingUrl" => "http://www.fake.com/index2.php", - "platform" => "webapi", - "partition" => "staging"); + + $appSettings = array( + "name" => "My Awesome App", + "voiceUrl" => "http://www.fake.com/index.php", + "messagingUrl" => "http://www.fake.com/index2.php", + "platform" => "webapi", + "partition" => "staging" + ); echo $tropo->createApplication($userid, $password, $appSettings); + } catch (TropoException $ex) { echo $ex->getMessage(); } -?> +?> \ No newline at end of file diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 9fefdc7..984e28c 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -54,8 +54,9 @@ public function createSession($token, Array $params = null) { class ProvisioningAPI extends RestBase { - // URL for the Tropo provisioning API. - const ProvisioningURLBase = 'http://api.tropo.com/provisioning/'; + // URLs for the Tropo provisioning API. + const ApplicationProvisioningURLBase = 'http://api.tropo.com/v1/'; + const ExchangeProvisioningURLBase = 'http://api.tropo.com/v1/exchanges'; public function __construct($userid, $password) { parent::__construct($userid, $password); @@ -75,7 +76,7 @@ public function __construct($userid, $password) { public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition) { $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); - $url = self::ProvisioningURLBase.'applications'; + $url = self::ApplicationProvisioningURLBase.'applications'; return self::makeAPICall('POST', $url, $payload); } @@ -98,7 +99,7 @@ public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platf public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) { $payload = json_encode(new Address($type, $prefix, $number, $city, $state, $channel, $username, $password, $token)); - $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses'; return self::makeAPICall('POST', $url, $payload); } @@ -118,7 +119,7 @@ public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $n public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) { $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); - $url = self::ProvisioningURLBase.'applications/'.$applicationID; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; return self::makeAPICall('PUT', $url, $payload); } @@ -131,7 +132,7 @@ public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL */ public function deleteApplication($applicationID) { - $url = self::ProvisioningURLBase.'applications/'.$applicationID; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; return self::makeAPICall('DELETE', $url); } @@ -146,7 +147,7 @@ public function deleteApplication($applicationID) { */ public function deleteApplicationAddress($applicationID, $type, $address) { - $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses/'.$type.'/'.$address; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses/'.$type.'/'.$address; return self::makeAPICall('DELETE', $url); } @@ -158,7 +159,7 @@ public function deleteApplicationAddress($applicationID, $type, $address) { */ public function viewApplications() { - $url = self::ProvisioningURLBase.'applications'; + $url = self::ApplicationProvisioningURLBase.'applications'; return self::makeAPICall('GET', $url); } @@ -171,7 +172,7 @@ public function viewApplications() { */ public function viewSpecificApplication($applicationID) { - $url = self::ProvisioningURLBase.'applications/'.$applicationID; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; return self::makeAPICall('GET', $url); } @@ -184,7 +185,7 @@ public function viewSpecificApplication($applicationID) { */ public function viewAddresses($applicationID) { - $url = self::ProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses'; return self::makeAPICall('GET', $url); } @@ -196,7 +197,7 @@ public function viewAddresses($applicationID) { */ public function viewExchanges() { - $url = self::ProvisioningURLBase.'exchanges'; + $url = self::ExchangeProvisioningURLBase; return self::makeAPICall('GET', $url); } @@ -353,7 +354,8 @@ public function __construct($prefix=NULL, $city=NULL, $state=NULL, $country=NULL if(isset($prefix)) { $this->prefix = $prefix; } if(isset($city)) { $this->city = $city; } if(isset($state)) { $this->state = $state; } - if(isset($country)) { $this->country = $country; } + if(isset($country)) { $this->country = $country; } + if(isset($description)) { $this->description = $description; } } public function __set($attribute, $value) { From 2720051e45ac4b40e58ab5dc7d6fb609206a1061 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 7 Jan 2011 14:34:03 -0800 Subject: [PATCH 013/107] (Fix) incorrect case in hello_world sample --- samples/hello_world.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/hello_world.php b/samples/hello_world.php index c709d54..5e8c931 100644 --- a/samples/hello_world.php +++ b/samples/hello_world.php @@ -8,7 +8,7 @@ require('tropo.class.php'); $tropo = new Tropo(); -$tropo->Say("Hello World!"); -$tropo->RenderJson(); +$tropo->say("Hello World!"); +$tropo->renderJson(); ?> \ No newline at end of file From a4761ea09541c3db75b1091b5eb7adf945be496d Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Thu, 20 Jan 2011 14:47:03 -0500 Subject: [PATCH 014/107] Added support for sending events to running WebAPI scripts. --- samples/event-test.php | 40 +++++++++++++++ tropo-rest.class.php | 42 ++++++++++++++- tropo.class.php | 113 ++++++++++++++++++++++++----------------- 3 files changed, 147 insertions(+), 48 deletions(-) create mode 100644 samples/event-test.php diff --git a/samples/event-test.php b/samples/event-test.php new file mode 100644 index 0000000..b59ef05 --- /dev/null +++ b/samples/event-test.php @@ -0,0 +1,40 @@ +getId(); + +// Insert the Session object into a CouchDB database called sessions. +try { + $sag = new Sag(); + $sag->setDatabase("sessions"); + $sag->put($session_id, $json); +} +catch (SagCouchException $ex) { + die("*** ".$ex->getMessage()." ***"); +} + +// Create a new Tropo object. +$tropo = new Tropo(); + +// Set options for an Ask. +$options = array("attempts" => 20, "bargein" => true, "choices" => "[5 DIGITS]", "name" => "zip", "timeout" => 5, "allowSignals" => array("tooLong", "farTooLong")); +$tropo->ask("Please enter your 5 digit zip code.", $options); + +// Set event handlers +$tropo->on(array("event" => "continue", "next" => "get_zip_code.php?uri=end", "say" => "Please hold.")); +$tropo->on(array("event" => "tooLong", "next" => "get_zip_code.php?uri=end&tooLong=true", "say" => "Please hold on.")); +$tropo->on(array("event" => "farTooLong", "next" => "get_zip_code.php?uri=end&farTooLong=true", "say" => "Please hold on for dear life.")); + +// Render JSON for Tropo to consume. +$tropo->renderJSON(); + + +?> \ No newline at end of file diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 984e28c..8fceb3c 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -1,7 +1,7 @@ QUEUED'; + + public function __construct() { + parent::__construct(); + } + + /** + * Send an event into a running Tropo session. + * + * @param string $token + * @param array $params + * @return boolean + */ + public function sendEvent($session_id, $event) { + + $url = str_replace(array('%session_id%', '%value%'), array($session_id, $event), self::EventURL); + + curl_setopt($this->ch, CURLOPT_URL, $url); + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($this->ch); + $error = curl_error($this->ch); + parent::__destruct(); + + if($result === false) { + throw new Exception('An error occurred: '.$error); + } else { + if (strpos($result, self::EventResponse) === false) { + throw new Exception('An error occurred: Tropo event injection failed.'); + } + return true; + } + } +} + class ProvisioningAPI extends RestBase { // URLs for the Tropo provisioning API. diff --git a/tropo.class.php b/tropo.class.php index b964cd1..9de515e 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -89,30 +89,13 @@ public function setLanguage($language) { * pass in a fully-formed Ask object or a string to use as the * prompt and an array of parameters. * - * The available parameters are - * - choices: (string) The grammar to use in recognizing and validating input - * - as: (string) an SSML type hinting how to say the TTS - * - event: (string) only say this on certain events (i.e. no input) - * - voice: (string) Override the default TTS voice - * - attempts: (int) How many times the caller can attempt input before - * an error is thrown - * - bargein: (bool) Should the user be allowed to barge in before TTS is complete? - * - minConfidence: (int) How confident should Tropo be in a speech reco match? - * - name: (string) identifies the return value of an ask, so you know the context for - * the returned information. As an example, if you asked the user for their favorite - * color, the name value would be "color" while the returned value might be "blue". - * - required: (bool) Is input required here? - * - timeout: (float) How long (in seconds) should Tropo wait for input? - * - mode: (string) Only applies to the voice channel and can be either 'speech', 'dtmf', or 'both'. - * - terminator: (string) This is the touch-tone key (also known as "DTMF digit") that indicates the end of input. - * * @param string|Ask $ask * @param array $params * @see https://www.tropo.com/docs/webapi/ask.htm */ public function ask($ask, Array $params=NULL) { if(!is_object($ask)) { - $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout'); + $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -130,7 +113,7 @@ public function ask($ask, Array $params=NULL) { $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; $voice = isset($this->_voice) ? $this->_voice : null; $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice); + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals); } $this->ask = sprintf($ask); } @@ -144,14 +127,14 @@ public function ask($ask, Array $params=NULL) { */ public function call($call, Array $params=NULL) { if(!is_object($call)) { - $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording'); + $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording); + $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); } $this->call = sprintf($call); } @@ -166,14 +149,14 @@ public function call($call, Array $params=NULL) { */ public function conference($conference, Array $params=NULL) { if(!is_object($conference)) { - $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator'); + $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator); + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); } $this->conference = sprintf($conference); } @@ -254,14 +237,14 @@ public function record($record) { } else { $transcription = $params["transcription"]; } - $p = array('attempts', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url'); + $p = array('attempts', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'allowSignals'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $record = new Record($attempts, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url); + $record = new Record($attempts, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $allowSignals); } $this->record = sprintf($record); } @@ -306,7 +289,7 @@ public function reject() { */ public function say($say, Array $params=NULL) { if(!is_object($say)) { - $p = array('as', 'format', 'event','voice'); + $p = array('as', 'format', 'event','voice', 'allowSignals'); $value = $say; foreach ($p as $option) { $$option = null; @@ -315,7 +298,7 @@ public function say($say, Array $params=NULL) { } } $voice = isset($voice) ? $voice : $this->_voice; - $say = new Say($value, $as, $event, $voice); + $say = new Say($value, $as, $event, $voice, $allowSignals); } $this->say = array(sprintf($say)); } @@ -345,7 +328,7 @@ public function startRecording($startRecording) { /** * Stops a previously started recording. * - * + * @see https://www.tropo.com/docs/webapi/stoprecording.htm */ public function stopRecording() { $stopRecording = new stopRecording(); @@ -364,14 +347,14 @@ public function transfer($transfer, Array $params=NULL) { if(!is_object($transfer)) { $choices = isset($params["choices"]) ? new Choices($params["choices"]) : null; $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on'); + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on', 'allowSignals'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on); + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals); } $this->transfer = sprintf($transfer); } @@ -396,6 +379,17 @@ public function createSession($token, Array $params=NULL) { } } + public function sendEvent($session_id, $value) { + try { + $event = new EventAPI(); + $result = $event->sendEvent($session_id, $value); + return $result; + } + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + /** * Creates a new Tropo Application * (Pass through to ProvisioningAPI class). @@ -556,9 +550,9 @@ public function viewAddresses($userid, $password, $applicationID) { * View a list of available exchanges for assigning a number to a Tropo application. * (Pass through to ProvisioningAPI class). * - * @param unknown_type $userid - * @param unknown_type $password - * @return unknown + * @param string $userid + * @param string $password + * @return string JSON */ public function viewExchanges($userid, $password) { $provision = new ProvisioningAPI($userid, $password); @@ -679,6 +673,7 @@ class Ask extends BaseClass { private $_say; private $_timeout; private $_voice; + private $_allowSignals; /** * Class constructor @@ -692,17 +687,19 @@ class Ask extends BaseClass { * @param Say $say * @param int $timeout * @param string $voice + * @param string|array $allowSignals */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_choices = isset($choices) ? sprintf($choices) : null ; $this->_minConfidence = $minConfidence; $this->_name = $name; $this->_required = $required; - $this->_voice = $voice; $this->_say = isset($say) ? $say : null; $this->_timeout = $timeout; + $this->_voice = $voice; + $this->_allowSignals = $allowSignals; } /** @@ -724,6 +721,7 @@ public function __toString() { } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } @@ -755,6 +753,7 @@ class Call extends BaseClass { private $_timeout; private $_headers; private $_recording; + private $_allowSignals; /** * Class constructor @@ -767,8 +766,9 @@ class Call extends BaseClass { * @param int $timeout * @param array $headers * @param StartRecording $recording + * @param string|array $allowSignals */ - public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL) { + public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL) { $this->_to = $to; $this->_from = $from; $this->_network = $network; @@ -777,6 +777,7 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ $this->_timeout = $timeout; $this->_headers = $headers; $this->_recording = isset($recording) ? sprintf($recording) : null ; + $this->_allowSignals = $allowSignals; } /** @@ -791,7 +792,8 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } if(count($this->_headers)) { $this->headers = $this->_headers; } - if(isset($this->_recording)) { $this->recording = $this->_recording; } + if(isset($this->_recording)) { $this->recording = $this->_recording; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } } @@ -849,6 +851,8 @@ class Conference extends BaseClass { private $_playTones; private $_required; private $_terminator; + private $_allowSignals; + /** * Class constructor @@ -860,8 +864,9 @@ class Conference extends BaseClass { * @param boolean $playTones * @param boolean $required * @param string $terminator + * @param string|array $allowSignals */ - public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL) { + public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { $this->_name = $name; $this->_id = $id; $this->_mute = $mute; @@ -869,6 +874,7 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones $this->_playTones = $playTones; $this->_required = $required; $this->_terminator = $terminator; + $this->_allowSignals = $allowSignals; } /** @@ -882,7 +888,8 @@ public function __toString() { if(isset($this->_on)) { $this->on = $this->_on; } if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } if(isset($this->_required)) { $this->required = $this->_required; } - if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } + if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } } @@ -1012,6 +1019,7 @@ class Record extends BaseClass { private $_transcription; private $_username; private $_url; + private $_allowSignals; /** * Class constructor @@ -1029,8 +1037,9 @@ class Record extends BaseClass { * @param int $timeout * @param string $username * @param string $url + * @param string|array $allowSignals */ - public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $allowSignals=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_beep = $beep; @@ -1047,7 +1056,8 @@ public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $ $this->_timeout = $timeout; $this->_transcription = isset($transcription) ? sprintf($transcription) : null; $this->_username = $username; - $this->_url = $url; + $this->_url = $url; + $this->_allowSignals = $allowSignals; } /** @@ -1068,7 +1078,8 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } if(isset($this->_username)) { $this->username = $this->_username; } - if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } } @@ -1245,6 +1256,7 @@ class Say extends BaseClass { private $_event; private $_format; private $_voice; + private $_allowSignals; /** * Class constructor @@ -1253,12 +1265,14 @@ class Say extends BaseClass { * @param SayAs $as * @param string $event * @param string $voice + * @param string|array $allowSignals */ - public function __construct($value, $as=NULL, $event=NULL, $voice=NULL) { + public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL) { $this->_value = $value; $this->_as = $as; $this->_event = $event; - $this->_voice = $voice; + $this->_voice = $voice; + $this->_allowSignals = $allowSignals; } /** @@ -1269,7 +1283,8 @@ public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } $this->value = $this->_value; if(isset($this->_as)) { $this->as = $this->_as; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } } @@ -1504,6 +1519,7 @@ class Transfer extends BaseClass { private $_ringRepeat; private $_timeout; private $_to; + private $_allowSignals; /** * Class constructor @@ -1515,8 +1531,9 @@ class Transfer extends BaseClass { * @param On $on * @param int $ringRepeat * @param int $timeout + * @param string|array $allowSignals */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf($choices) : null; @@ -1524,6 +1541,7 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; $this->_on = isset($on) ? sprintf($on) : null; + $this->_allowSignals = $allowSignals; } /** @@ -1537,7 +1555,8 @@ public function __toString() { if(isset($this->_from)) { $this->from = $this->_from; } if(isset($this->_ringRepeat)) { $this->ringRepeat = $this->_ringRepeat; } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_on)) { $this->on = $this->_on; } + if(isset($this->_on)) { $this->on = $this->_on; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } return $this->unescapeJSON(json_encode($this)); } } From 5c0714681c8f733d15534c1b0d619531cdb2b1a3 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Mon, 24 Jan 2011 14:38:12 -0500 Subject: [PATCH 015/107] Added version/settings compatibility check. --- compatibilty.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 compatibilty.php diff --git a/compatibilty.php b/compatibilty.php new file mode 100644 index 0000000..51c7940 --- /dev/null +++ b/compatibilty.php @@ -0,0 +1,31 @@ += 0) { + echo "OK PHP Version: $version\n"; +} +else +{ + echo "WARNING - PHP Version: This library may not perform as expected wih the version of PHP you are currently running: { $version }.\n"; + +} + +// Check to see if errors/warnings are displayed. +if(ini_get('display_errors') == 0) { + echo "OK Display errors: disabled.\n"; +} +else { + echo "WARNING Display errors: Errors are displayed. This may cause issues with how JSON is rendered for Tropo.\n"; +} + +echo "\n======================================================\n\n"; + +?> \ No newline at end of file From 84f21798a34e48c408624c377877a6148f3ab428 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Fri, 11 Feb 2011 09:59:12 -0500 Subject: [PATCH 016/107] (Fix) Corrected use of voice property on record. --- tropo.class.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 9de515e..01950d3 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -237,14 +237,14 @@ public function record($record) { } else { $transcription = $params["transcription"]; } - $p = array('attempts', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'allowSignals'); + $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $record = new Record($attempts, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $allowSignals); + $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); } $this->record = sprintf($record); } @@ -1005,6 +1005,7 @@ public function __toString() { class Record extends BaseClass { private $_attempts; + private $_allowSignals; private $_bargein; private $_beep; private $_choices; @@ -1018,13 +1019,15 @@ class Record extends BaseClass { private $_timeout; private $_transcription; private $_username; - private $_url; - private $_allowSignals; + private $_url; + private $_voice; + /** * Class constructor * * @param int $attempts + * @param string|array $allowSignals * @param boolean $bargein * @param boolean $beep * @param Choices $choices @@ -1037,10 +1040,11 @@ class Record extends BaseClass { * @param int $timeout * @param string $username * @param string $url - * @param string|array $allowSignals + * @param string $voice */ - public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $allowSignals=NULL) { + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL) { $this->_attempts = $attempts; + $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; $this->_beep = $beep; $this->_choices = isset($choices) ? sprintf($choices) : null; @@ -1057,7 +1061,7 @@ public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $ $this->_transcription = isset($transcription) ? sprintf($transcription) : null; $this->_username = $username; $this->_url = $url; - $this->_allowSignals = $allowSignals; + $this->_voice = $voice; } /** @@ -1066,6 +1070,7 @@ public function __construct($attempts=NULL, $bargein=NULL, $beep=NULL, Choices $ */ public function __toString() { if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } if(isset($this->_beep)) { $this->beep = $this->_beep; } if(isset($this->_choices)) { $this->choices = $this->_choices; } @@ -1079,7 +1084,7 @@ public function __toString() { if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } return $this->unescapeJSON(json_encode($this)); } } From 611c82aa0eabe3f98e9c7e20803901b5085094b4 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Wed, 16 Mar 2011 11:37:48 -0400 Subject: [PATCH 017/107] Added callId property o Result object. --- tropo.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tropo.class.php b/tropo.class.php index 01950d3..cec277a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1136,6 +1136,7 @@ class Reject extends EmptyBaseClass { } class Result { private $_sessionId; + private $_callId; private $_state; private $_sessionDuration; private $_sequence; @@ -1170,6 +1171,7 @@ public function __construct($json=NULL) { throw new TropoException('Not a result object.'); } $this->_sessionId = $result->result->sessionId; + $this->_callId = $result->result->callId; $this->_state = $result->result->state; $this->_sessionDuration = $result->result->sessionDuration; $this->_sequence = $result->result->sequence; @@ -1191,6 +1193,10 @@ function getSessionId() { return $this->_sessionId; } + function getCallId() { + return $this->_callId; + } + function getState() { return $this->_state; } From 77876d38ce438874cd53b89b8e3f46430e435c61 Mon Sep 17 00:00:00 2001 From: Adam Varga Date: Thu, 7 Apr 2011 23:53:27 -0400 Subject: [PATCH 018/107] Bug fix. Event specific code wasn't getting spoken because the eventless say came before the event says. And order of operations matters. --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index cec277a..91c6be5 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -102,13 +102,13 @@ public function ask($ask, Array $params=NULL) { $$option = $params[$option]; } } - $say[] = new Say($ask, $as, null, $voice); if (is_array($event)) { // If an event was passed in, add the events to the Ask foreach ($event as $e => $val){ $say[] = new Say($val, $as, $e, $voice); } } + $say[] = new Say($ask, $as, null, $voice); $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; $voice = isset($this->_voice) ? $this->_voice : null; From cf9dd5463e5b66986757b9bf2dfb00b67360cad2 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Mon, 2 May 2011 16:12:17 -0700 Subject: [PATCH 019/107] Add requirements to readme --- readme.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.markdown b/readme.markdown index adbf1d8..15ee95e 100644 --- a/readme.markdown +++ b/readme.markdown @@ -3,6 +3,12 @@ Overview TropoPHP is a set of PHP classes for working with [Tropo's cloud communication service](http://tropo.com/). Tropo allows a developer to create applications that run over the phone, IM, SMS, and Twitter using web technologies. This library communicates with Tropo over JSON. +Requirements +============ + + * PHP 5.3.0 or greater + * PHP Notices disabled (All error reporting disabled is recommended for production use) + Usage ===== From ca3b6324ddc425233134168e5ab924a617db6294 Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Mon, 2 May 2011 23:17:29 +0800 Subject: [PATCH 020/107] Typo --- compatibilty.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compatibilty.php b/compatibilty.php index 51c7940..7600028 100644 --- a/compatibilty.php +++ b/compatibilty.php @@ -5,7 +5,7 @@ echo "\n\n======================================================\n"; -echo "Begin Tropo WebAPI compatibilty check.\n\n"; +echo "Begin Tropo WebAPI compatibility check.\n\n"; // Check to essure proper PHP version. $version = phpversion(); From 3dd0688db0652e4cdd8953b2c36f0ac2a69fa6a0 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Thu, 5 May 2011 20:14:23 -0700 Subject: [PATCH 021/107] (Fix) result "concept" is never filled (Fix) Miscellaneous PHP warnings about undefined indexes and missing properties --- tropo.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index cec277a..7b19b0d 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -111,6 +111,7 @@ public function ask($ask, Array $params=NULL) { } $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; + $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; $voice = isset($this->_voice) ? $this->_voice : null; $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals); @@ -1185,8 +1186,7 @@ public function __construct($json=NULL) { $this->_interpretation = $result->result->actions->interpretation; $this->_utterance = $result->result->actions->utterance; $this->_value = $result->result->actions->value; - $this->_concept = $result->result->concept->value; - + $this->_concept = $result->result->actions->concept; } function getSessionId() { From d34e30ceeaeb2e87a6130fde93dbb774f4f5592e Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Thu, 5 May 2011 21:40:48 -0700 Subject: [PATCH 022/107] (Fix) Voice on Tropo::ask() does not work --- tropo.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 7b19b0d..71be73a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -112,8 +112,10 @@ public function ask($ask, Array $params=NULL) { $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; - $voice = isset($this->_voice) ? $this->_voice : null; - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; + if (!isset($voice) && isset($this->_voice)) { + $voice = $this->_voice; + } + $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals); } $this->ask = sprintf($ask); From 9fe6d7facb8f176dbf9530660789bba1d6bf51a4 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Fri, 13 May 2011 17:04:43 -0400 Subject: [PATCH 023/107] Bug fixes and minor enhancements. --- tropo.class.php | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 71be73a..bfa400d 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -95,7 +95,7 @@ public function setLanguage($language) { */ public function ask($ask, Array $params=NULL) { if(!is_object($ask)) { - $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals'); + $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -116,7 +116,7 @@ public function ask($ask, Array $params=NULL) { $voice = $this->_voice; } $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals); + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer); } $this->ask = sprintf($ask); } @@ -348,7 +348,7 @@ public function stopRecording() { */ public function transfer($transfer, Array $params=NULL) { if(!is_object($transfer)) { - $choices = isset($params["choices"]) ? new Choices($params["choices"]) : null; + $choices = isset($params["choices"]) ? $params["choices"] : null; $to = isset($params["to"]) ? $params["to"] : $transfer; $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on', 'allowSignals'); foreach ($p as $option) { @@ -677,6 +677,7 @@ class Ask extends BaseClass { private $_timeout; private $_voice; private $_allowSignals; + private $_recognizer; /** * Class constructor @@ -692,7 +693,7 @@ class Ask extends BaseClass { * @param string $voice * @param string|array $allowSignals */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_choices = isset($choices) ? sprintf($choices) : null ; @@ -703,6 +704,7 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL $this->_timeout = $timeout; $this->_voice = $voice; $this->_allowSignals = $allowSignals; + $this->_recognizer = $recognizer; } /** @@ -725,6 +727,7 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } return $this->unescapeJSON(json_encode($this)); } @@ -809,19 +812,19 @@ class Choices extends BaseClass { private $_value; private $_mode; - private $_termChar; + private $_terminator; /** * Class constructor * * @param string $value * @param string $mode - * @param string $termChar + * @param string $terminator */ - public function __construct($value, $mode=NULL, $termChar=NULL) { + public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { $this->_value = $value; $this->_mode = $mode; - $this->_termChar = $termChar; + $this->_terminator = $terminator; } /** @@ -831,7 +834,7 @@ public function __construct($value, $mode=NULL, $termChar=NULL) { public function __toString() { $this->value = $this->_value; if(isset($this->_mode)) { $this->mode = $this->_mode; } - if(isset($this->_termChar)) { $this->terminator = $this->_termChar; } + if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } return $this->unescapeJSON(json_encode($this)); } } @@ -1419,7 +1422,7 @@ public function setHeaders($headers) { $formattedHeaders = new Headers(); // headers don't exist on outboud calls // so only do this if there are headers - if (is_array($headers)) { + if (is_object($headers)) { foreach($headers as $name => $value) { $formattedHeaders->$name = $value; } @@ -1748,6 +1751,24 @@ class Voice { public static $Mexican_Spanish_female = "soledad"; } +/** + * Recognizer Helper class + * @package TropoPHP_Support + * + */ +class Recognizer { + public static $German = 'de-de'; + public static $British_English = 'en-gb'; + public static $US_English = 'en-us'; + public static $Castilian_Spanish = 'es-es'; + public static $Mexican_Spanish = 'es-mx'; + public static $French_Canadian = 'fr-ca'; + public static $French = 'fr-fr'; + public static $Italian = 'it-it'; + public static $Polish = 'pl-pl'; + public static $Dutch = 'nl-nl'; +} + /** * SIP Headers Helper class. * @package TropoPHP_Support From e015d60d2ec4b7c718f3f6bab3a8eeac37e4650b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 19 Jun 2011 09:02:56 -0400 Subject: [PATCH 024/107] modified: tropo.class.php No longer throws notices about undefined properties, so error_reporting(E_ALL) is now valid. Added transcription properties and get method. Added getFromNetwork and getFromChannel methods --- tropo.class.php | 527 ++++++++++++++++++++++++++---------------------- 1 file changed, 286 insertions(+), 241 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index bfa400d..3b65064 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -2,7 +2,7 @@ /** * This file contains PHP classes that can be used to interact with the Tropo WebAPI/ * @see https://www.tropo.com/docs/webapi/ - * + * * @copyright 2010 Mark J. Headd (http://www.voiceingov.org) * @package TropoPHP * @author Mark Headd @@ -19,7 +19,7 @@ include 'tropo-rest.class.php'; class Tropo extends BaseClass { - + /** * The container for JSON actions. * @@ -27,7 +27,7 @@ class Tropo extends BaseClass { * @access private */ public $tropo; - + /** * The TTS voice to use when rendering content. * @@ -35,7 +35,7 @@ class Tropo extends BaseClass { * @access private */ private $_voice; - + /** * The language to use when rendering content. * @@ -43,7 +43,7 @@ class Tropo extends BaseClass { * @access private */ private $_language; - + /** * Class constructor for the Tropo class. * @access private @@ -51,7 +51,7 @@ class Tropo extends BaseClass { public function __construct() { $this->tropo = array(); } - + /** * Set a default voice for use with all Text To Speech. * @@ -66,7 +66,7 @@ public function __construct() { public function setVoice($voice) { $this->_voice = $voice; } - + /** * Set a default language to use in speech recognition. * @@ -80,13 +80,13 @@ public function setVoice($voice) { public function setLanguage($language) { $this->_language = $language; } - + /** * Sends a prompt to the user and optionally waits for a response. * - * The ask method allows for collecting input using either speech - * recognition or DTMF (also known as Touch Tone). You can either - * pass in a fully-formed Ask object or a string to use as the + * The ask method allows for collecting input using either speech + * recognition or DTMF (also known as Touch Tone). You can either + * pass in a fully-formed Ask object or a string to use as the * prompt and an array of parameters. * * @param string|Ask $ask @@ -106,7 +106,7 @@ public function ask($ask, Array $params=NULL) { if (is_array($event)) { // If an event was passed in, add the events to the Ask foreach ($event as $e => $val){ - $say[] = new Say($val, $as, $e, $voice); + $say[] = new Say($val, $as, $e, $voice); } } $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; @@ -122,7 +122,7 @@ public function ask($ask, Array $params=NULL) { } /** - * Places a call or sends an an IM, Twitter, or SMS message. To start a call, use the Session API to tell Tropo to launch your code. + * Places a call or sends an an IM, Twitter, or SMS message. To start a call, use the Session API to tell Tropo to launch your code. * * @param string|Call $call * @param array $params @@ -141,10 +141,10 @@ public function call($call, Array $params=NULL) { } $this->call = sprintf($call); } - + /** - * This object allows multiple lines in separate sessions to be conferenced together so that the parties on each line can talk to each other simultaneously. - * This is a voice channel only feature. + * This object allows multiple lines in separate sessions to be conferenced together so that the parties on each line can talk to each other simultaneously. + * This is a voice channel only feature. * * @param string|Conference $conference * @param array $params @@ -163,7 +163,7 @@ public function conference($conference, Array $params=NULL) { } $this->conference = sprintf($conference); } - + /** * This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. * @see https://www.tropo.com/docs/webapi/hangup.htm @@ -172,9 +172,9 @@ public function hangup() { $hangup = new Hangup(); $this->hangup = sprintf($hangup); } - + /** - * A shortcut method to create a session, say something, and hang up, all in one step. This is particularly useful for sending out a quick SMS or IM. + * A shortcut method to create a session, say something, and hang up, all in one step. This is particularly useful for sending out a quick SMS or IM. * * @param string|Message $message * @param array $params @@ -195,29 +195,29 @@ public function message($message, Array $params=null) { } $this->message = sprintf($message); } - + /** - * Adds an event callback so that your application may be notified when a particular event occurs. - * Possible events are: "continue", "error", "incomplete" and "hangup". + * Adds an event callback so that your application may be notified when a particular event occurs. + * Possible events are: "continue", "error", "incomplete" and "hangup". * * @param array $params * @see https://www.tropo.com/docs/webapi/on.htm */ - public function on($on) { + public function on($on) { if (!is_object($on) && is_array($on)) { $params = $on; $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; $next = (array_key_exists('next', $params)) ? $params["next"] : null; - $on = new On($params["event"], $next, $say); + $on = new On($params["event"], $next, $say); } $this->on = sprintf($on); } - + /** - * Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. - * If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below. - * The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set. - * At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form. + * Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. + * If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below. + * The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set. + * At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form. * If specified, the audio file may also be transcribed and the text returned to you via an email address or HTTP POST/Multipart Form. * * @param array|Record $record @@ -235,8 +235,8 @@ public function record($record) { if (!is_array($params["transcription"]) || !array_key_exists($option, $params["transcription"])) { $params["transcription"][$option] = null; } - } - $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"]); + } + $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"]); } else { $transcription = $params["transcription"]; } @@ -249,13 +249,13 @@ public function record($record) { } $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); } - $this->record = sprintf($record); + $this->record = sprintf($record); } - + /** - * The redirect function forwards an incoming call to another destination / phone number before answering it. - * The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state. - * Use transfer when working with active answered calls. + * The redirect function forwards an incoming call to another destination / phone number before answering it. + * The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state. + * Use transfer when working with active answered calls. * * @param string|Redirect $redirect * @param array $params @@ -269,11 +269,11 @@ public function redirect($redirect, Array $params=NULL) { } $this->redirect = sprintf($redirect); } - + /** - * Allows Tropo applications to reject incoming sessions before they are answered. - * For example, an application could inspect the callerID variable to determine if the user is known, and then use the reject call accordingly. - * + * Allows Tropo applications to reject incoming sessions before they are answered. + * For example, an application could inspect the callerID variable to determine if the user is known, and then use the reject call accordingly. + * * @see https://www.tropo.com/docs/webapi/reject.htm * */ @@ -281,10 +281,10 @@ public function reject() { $reject = new Reject(); $this->reject = sprintf($reject); } - + /** - * When the current session is a voice channel this key will either play a message or an audio file from a URL. - * In the case of an text channel it will send the text back to the user via i nstant messaging or SMS. + * When the current session is a voice channel this key will either play a message or an audio file from a URL. + * In the case of an text channel it will send the text back to the user via i nstant messaging or SMS. * * @param string|Say $say * @param array $params @@ -303,12 +303,12 @@ public function say($say, Array $params=NULL) { $voice = isset($voice) ? $voice : $this->_voice; $say = new Say($value, $as, $event, $voice, $allowSignals); } - $this->say = array(sprintf($say)); + $this->say = array(sprintf($say)); } - + /** - * Allows Tropo applications to begin recording the current session. - * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. + * Allows Tropo applications to begin recording the current session. + * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. * * @param array|StartRecording $startRecording * @see https://www.tropo.com/docs/webapi/startrecording.htm @@ -324,22 +324,22 @@ public function startRecording($startRecording) { } } $startRecording = new StartRecording($format, $method, $password, $url, $username); - } + } $this->startRecording = sprintf($startRecording); } - + /** * Stops a previously started recording. - * + * * @see https://www.tropo.com/docs/webapi/stoprecording.htm */ public function stopRecording() { $stopRecording = new stopRecording(); $this->stopRecording = sprintf($stopRecording); } - + /** - * Transfers an already answered call to another destination / phone number. + * Transfers an already answered call to another destination / phone number. * Call may be transferred to another phone number or SIP address, which is set through the "to" parameter and is in URL format. * * @param string|Transfer $transfer @@ -365,10 +365,10 @@ public function transfer($transfer, Array $params=NULL) { /** * Launches a new session with the Tropo Session API. * (Pass through to SessionAPI class.) - * + * * @param string $token Your outbound session token from Tropo * @param array $params An array of key value pairs that will be added as query string parameters - * @return bool True if the session was launched successfully + * @return bool True if the session was launched successfully */ public function createSession($token, Array $params=NULL) { try { @@ -379,9 +379,9 @@ public function createSession($token, Array $params=NULL) { // If an exception occurs, wrap it in a TropoException and rethrow. catch (Exception $ex) { throw new TropoException($ex->getMessage(), $ex->getCode()); - } + } } - + public function sendEvent($session_id, $value) { try { $event = new EventAPI(); @@ -392,7 +392,7 @@ public function sendEvent($session_id, $value) { throw new TropoException($ex->getMessage(), $ex->getCode()); } } - + /** * Creates a new Tropo Application * (Pass through to ProvisioningAPI class). @@ -420,7 +420,7 @@ public function createApplication($userid, $password, Array $params) { throw new TropoException($ex->getMessage(), $ex->getCode()); } } - + /** * Add/Update an address (phone number, IM address or token) for an existing Tropo application. * (Pass through to ProvisioningAPI class). @@ -449,7 +449,7 @@ public function updateApplicationAddress($userid, $passwd, $applicationID, Array throw new TropoException($ex->getMessage(), $ex->getCode()); } } - + /** * Update a property (name, URL, platform, etc.) for an existing Tropo application. * (Pass through to ProvisioningAPI class). @@ -478,7 +478,7 @@ public function updateApplicationProperty($userid, $password, $applicationID, Ar throw new TropoException($ex->getMessage(), $ex->getCode()); } } - + /** * Delete an existing Tropo application. * (Pass through to ProvisioningAPI class). @@ -492,7 +492,7 @@ public function deleteApplication($userid, $password, $applicationID) { $provision = new ProvisioningAPI($userid, $password); return $provision->deleteApplication($applicationID); } - + /** * Delete an address for an existing Tropo application. * (Pass through to ProvisioningAPI class). @@ -507,7 +507,7 @@ public function deleteApplicationAddress($userid, $password, $applicationID, $ad $provision = new ProvisioningAPI($userid, $password); return $provision->deleteApplicationAddress($applicationID, $addresstype, $address); } - + /** * View a list of Tropo applications. * (Pass through to ProvisioningAPI class). @@ -520,7 +520,7 @@ public function viewApplications($userid, $password) { $provision = new ProvisioningAPI($userid, $password); return $provision->viewApplications(); } - + /** * View the details of a specific Tropo application. * (Pass through to ProvisioningAPI class). @@ -534,7 +534,7 @@ public function viewSpecificApplication($userid, $password, $applicationID) { $provision = new ProvisioningAPI($userid, $password); return $provision->viewSpecificApplication($applicationID); } - + /** * View the addresses for a specific Tropo application. * (Pass through to ProvisioningAPI class). @@ -548,7 +548,7 @@ public function viewAddresses($userid, $password, $applicationID) { $provision = new ProvisioningAPI($userid, $password); return $provision->viewAddresses($applicationID); } - + /** * View a list of available exchanges for assigning a number to a Tropo application. * (Pass through to ProvisioningAPI class). @@ -561,7 +561,7 @@ public function viewExchanges($userid, $password) { $provision = new ProvisioningAPI($userid, $password); return $provision->viewExchanges(); } - + /** * Renders the Tropo object as JSON. * @@ -570,7 +570,7 @@ public function renderJSON() { header('Content-type: application/json'); echo $this; } - + /** * Allows undefined methods to be called. * This method is invloked by Tropo class methods to add action items to the Tropo array. @@ -581,8 +581,8 @@ public function renderJSON() { */ public function __set($name, $value) { array_push($this->tropo, array($name => $value)); - } - + } + /** * Controls how JSON structure for the Tropo object is rendered. * @@ -593,10 +593,10 @@ public function __toString() { // Remove voice and language so they do not appear in the rednered JSON. unset($this->_voice); unset($this->_language); - + // Call the unescapeJSON() method in the parent class. - return parent::unescapeJSON(json_encode($this)); - } + return parent::unescapeJSON(json_encode($this)); + } } /** @@ -607,19 +607,19 @@ public function __toString() { */ abstract class BaseClass { - + /** * Class constructor * @abstract __construct() */ abstract public function __construct(); - + /** * toString Function * @abstract __toString() */ abstract public function __toString(); - + /** * Allows derived classes to set Undeclared properties. * @@ -629,7 +629,7 @@ abstract public function __toString(); public function __set($attribute, $value) { $this->$attribute= $value; } - + /** * Removes escape characters from a JSON string. * @@ -642,15 +642,15 @@ public function unescapeJSON($json) { } /** - * Base class for empty actions. + * Base class for empty actions. * @package TropoPHP_Support * */ -class EmptyBaseClass { - - final public function __toString() { +class EmptyBaseClass { + + final public function __toString() { return json_encode(null); - } + } } @@ -666,7 +666,7 @@ final public function __toString() { * */ class Ask extends BaseClass { - + private $_attempts; private $_bargein; private $_choices; @@ -678,7 +678,7 @@ class Ask extends BaseClass { private $_voice; private $_allowSignals; private $_recognizer; - + /** * Class constructor * @@ -701,12 +701,12 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL $this->_name = $name; $this->_required = $required; $this->_say = isset($say) ? $say : null; - $this->_timeout = $timeout; + $this->_timeout = $timeout; $this->_voice = $voice; $this->_allowSignals = $allowSignals; $this->_recognizer = $recognizer; } - + /** * Renders object in JSON format. * @@ -724,18 +724,18 @@ public function __toString() { $this->_say[$k] = sprintf($v); } } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } return $this->unescapeJSON(json_encode($this)); } - + /** * Adds an additional Say to the Ask * * Used to add events such as a prompt to say on timeout or nomatch - * + * * @param Say $say A say object */ public function addEvent(Say $say) { @@ -750,17 +750,17 @@ public function addEvent(Say $say) { * */ class Call extends BaseClass { - + private $_to; private $_from; private $_network; private $_channel; private $_answerOnMedia; private $_timeout; - private $_headers; + private $_headers; private $_recording; private $_allowSignals; - + /** * Class constructor * @@ -776,16 +776,16 @@ class Call extends BaseClass { */ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL) { $this->_to = $to; - $this->_from = $from; - $this->_network = $network; - $this->_channel = $channel; - $this->_answerOnMedia = $answerOnMedia; + $this->_from = $from; + $this->_network = $network; + $this->_channel = $channel; + $this->_answerOnMedia = $answerOnMedia; $this->_timeout = $timeout; $this->_headers = $headers; $this->_recording = isset($recording) ? sprintf($recording) : null ; $this->_allowSignals = $allowSignals; } - + /** * Renders object in JSON format. * @@ -795,13 +795,13 @@ public function __toString() { if(isset($this->_from)) { $this->from = $this->_from; } if(isset($this->_network)) { $this->network = $this->_network; } if(isset($this->_channel)) { $this->channel = $this->_channel; } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(count($this->_headers)) { $this->headers = $this->_headers; } + if(count($this->_headers)) { $this->headers = $this->_headers; } if(isset($this->_recording)) { $this->recording = $this->_recording; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + return $this->unescapeJSON(json_encode($this)); + } } /** @@ -809,11 +809,11 @@ public function __toString() { * @package TropoPHP_Support */ class Choices extends BaseClass { - + private $_value; private $_mode; private $_terminator; - + /** * Class constructor * @@ -826,7 +826,7 @@ public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { $this->_mode = $mode; $this->_terminator = $terminator; } - + /** * Renders object in JSON format. * @@ -834,22 +834,22 @@ public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { public function __toString() { $this->value = $this->_value; if(isset($this->_mode)) { $this->mode = $this->_mode; } - if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } + return $this->unescapeJSON(json_encode($this)); } } /** - * This object allows multiple lines in separate sessions to be conferenced together so that - * the parties on each line can talk to each other simultaneously. - * This is a voice channel only feature. - * + * This object allows multiple lines in separate sessions to be conferenced together so that + * the parties on each line can talk to each other simultaneously. + * This is a voice channel only feature. + * * TODO: Conference object should support multiple event handlers (e.g. join and leave). * @package TropoPHP_Support * */ class Conference extends BaseClass { - + private $_id; private $_mute; private $_name; @@ -858,8 +858,8 @@ class Conference extends BaseClass { private $_required; private $_terminator; private $_allowSignals; - - + + /** * Class constructor * @@ -875,14 +875,14 @@ class Conference extends BaseClass { public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { $this->_name = $name; $this->_id = $id; - $this->_mute = $mute; + $this->_mute = $mute; $this->_on = isset($on) ? sprintf($on) : null; $this->_playTones = $playTones; $this->_required = $required; $this->_terminator = $terminator; $this->_allowSignals = $allowSignals; } - + /** * Renders object in JSON format. * @@ -895,9 +895,9 @@ public function __toString() { if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); + } } /** @@ -908,12 +908,12 @@ public function __toString() { class Hangup extends EmptyBaseClass { } /** - * This function instructs Tropo to send a message. + * This function instructs Tropo to send a message. * @package TropoPHP_Support * */ class Message extends BaseClass { - + private $_say; private $_to; private $_channel; @@ -923,7 +923,7 @@ class Message extends BaseClass { private $_timeout; private $_answerOnMedia; private $_headers; - + /** * Class constructor * @@ -948,7 +948,7 @@ public function __construct(Say $say, $to, $channel=null, $network=null, $from=n $this->_answerOnMedia = $answerOnMedia; $this->_headers = $headers; } - + /** * Renders object in JSON format. * @@ -961,9 +961,9 @@ public function __toString() { if(isset($this->_from)) { $this->from = $this->_from; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(count($this->_headers)) { $this->headers = $this->_headers; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } + if(count($this->_headers)) { $this->headers = $this->_headers; } + return $this->unescapeJSON(json_encode($this)); } } @@ -973,11 +973,11 @@ public function __toString() { * */ class On extends BaseClass { - + private $_event; private $_next; private $_say; - + /** * Class constructor * @@ -990,7 +990,7 @@ public function __construct($event=NULL, $next=NULL, Say $say=NULL) { $this->_next = $next; $this->_say = isset($say) ? sprintf($say) : null ; } - + /** * Renders object in JSON format. * @@ -998,8 +998,8 @@ public function __construct($event=NULL, $next=NULL, Say $say=NULL) { public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } if(isset($this->_next)) { $this->next = $this->_next; } - if(isset($this->_say)) { $this->say = $this->_say; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_say)) { $this->say = $this->_say; } + return $this->unescapeJSON(json_encode($this)); } } @@ -1009,7 +1009,7 @@ public function __toString() { * */ class Record extends BaseClass { - + private $_attempts; private $_allowSignals; private $_bargein; @@ -1024,11 +1024,11 @@ class Record extends BaseClass { private $_say; private $_timeout; private $_transcription; - private $_username; + private $_username; private $_url; private $_voice; - - + + /** * Class constructor * @@ -1046,7 +1046,7 @@ class Record extends BaseClass { * @param int $timeout * @param string $username * @param string $url - * @param string $voice + * @param string $voice */ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL) { $this->_attempts = $attempts; @@ -1065,18 +1065,18 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_say = isset($say) ? sprintf($say) : null; $this->_timeout = $timeout; $this->_transcription = isset($transcription) ? sprintf($transcription) : null; - $this->_username = $username; - $this->_url = $url; + $this->_username = $username; + $this->_url = $url; $this->_voice = $voice; } - + /** * Renders object in JSON format. * */ public function __toString() { if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } if(isset($this->_beep)) { $this->beep = $this->_beep; } if(isset($this->_choices)) { $this->choices = $this->_choices; } @@ -1087,11 +1087,11 @@ public function __toString() { if(isset($this->_password)) { $this->password = $this->_password; } if(isset($this->_say)) { $this->say = $this->_say; } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } - if(isset($this->_username)) { $this->username = $this->_username; } + if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } + if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_voice)) { $this->voice = $this->_voice; } + return $this->unescapeJSON(json_encode($this)); } } @@ -1101,10 +1101,10 @@ public function __toString() { * */ class Redirect extends BaseClass { - + private $_to; private $_from; - + /** * Class constructor * @@ -1115,15 +1115,15 @@ public function __construct($to=NULL, $from=NULL) { $this->_to = sprintf($to); $this->_from = isset($from) ? sprintf($from) : null; } - + /** * Renders object in JSON format. * */ public function __toString() { $this->to = $this->_to; - if(isset($this->_from)) { $this->from = $this->_from; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_from)) { $this->from = $this->_from; } + return $this->unescapeJSON(json_encode($this)); } } @@ -1140,7 +1140,7 @@ class Reject extends EmptyBaseClass { } * */ class Result { - + private $_sessionId; private $_callId; private $_state; @@ -1157,7 +1157,8 @@ class Result { private $_concept; private $_utterance; private $_value; - + private $_transcription; + /** * Class constructor * @@ -1166,7 +1167,7 @@ class Result { public function __construct($json=NULL) { if(empty($json)) { $json = file_get_contents("php://input"); - // if $json is still empty, there was nothing in + // if $json is still empty, there was nothing in // the POST so throw an exception if(empty($json)) { throw new TropoException('No JSON available.'); @@ -1183,7 +1184,7 @@ public function __construct($json=NULL) { $this->_sequence = $result->result->sequence; $this->_complete = $result->result->complete; $this->_error = $result->result->error; - $this->_actions = $result->result->actions; + $this->_actions = $result->result->actions; $this->_name = $result->result->actions->name; $this->_attempts = $result->result->actions->attempts; $this->_disposition = $result->result->actions->disposition; @@ -1191,89 +1192,98 @@ public function __construct($json=NULL) { $this->_interpretation = $result->result->actions->interpretation; $this->_utterance = $result->result->actions->utterance; $this->_value = $result->result->actions->value; - $this->_concept = $result->result->actions->concept; + $this->_concept = isset($result->result->actions->concept) + ? $result->result->actions->concept + : null; + $this->_transcription = isset($result->result->transcription) + ? $result->result->transcription + : null; } - + function getSessionId() { return $this->_sessionId; } - + function getCallId() { return $this->_callId; } - + function getState() { return $this->_state; } - + function getSessionDuration() { return $this->_sessionDuration; } - + function getSequence() { return $this->_sequence; } - + function isComplete() { return (bool) $this->_complete; } - + function getError() { return $this->_error; } - + function getActions() { return $this->_actions; } - + function getName() { return $this->_name; } - + function getAttempts() { return $this->_attempts; } - + function getDisposition() { return $this->_disposition; } - + function getConfidence() { return $this->_confidence; } - + function getInterpretation() { return $this->_interpretation; } - + function getConcept() { return $this->_concept; } - + function getUtterance() { return $this->_utterance; } - + function getValue() { return $this->_value; } + + function getTranscription() { + return $this->_transcription; + } } /** - * When the current session is a voice channel this key will either play a message or an audio file from a URL. + * When the current session is a voice channel this key will either play a message or an audio file from a URL. * In the case of an text channel it will send the text back to the user via instant messaging or SMS. * @package TropoPHP_Support * */ class Say extends BaseClass { - + private $_value; private $_as; private $_event; private $_format; private $_voice; private $_allowSignals; - + /** * Class constructor * @@ -1290,18 +1300,18 @@ public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSi $this->_voice = $voice; $this->_allowSignals = $allowSignals; } - + /** * Renders object in JSON format. * */ - public function __toString() { + public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } $this->value = $this->_value; if(isset($this->_as)) { $this->as = $this->_as; } if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); } } @@ -1313,7 +1323,7 @@ public function __toString() { * @package TropoPHP */ class Session { - + private $_id; private $_accountID; private $_timestamp; @@ -1323,7 +1333,7 @@ class Session { private $_from; private $_headers; private $_parameters; - + /** * Class constructor * @@ -1332,7 +1342,7 @@ class Session { public function __construct($json=NULL) { if(empty($json)) { $json = file_get_contents("php://input"); - // if $json is still empty, there was nothing in + // if $json is still empty, there was nothing in // the POST so throw exception if(empty($json)) { throw new TropoException('No JSON available.', 1); @@ -1347,54 +1357,89 @@ public function __construct($json=NULL) { $this->_timestamp = $session->session->timestamp; $this->_userType = $session->session->userType; $this->_initialText = $session->session->initialText; - $this->_to = array("id" => $session->session->to->id, "channel" => $session->session->to->channel, "name" => $session->session->to->name, "network" => $session->session->to->network); - $this->_from = array("id" => $session->session->from->id, "channel" => $session->session->from->channel, "name" => $session->session->from->name, "network" => $session->session->from->network); - $this->_headers = self::setHeaders($session->session->headers); - $this->_parameters = property_exists($session->session, 'parameters') ? (Array) $session->session->parameters : null; + $this->_to = isset($session->session->to) + ? array( + "id" => $session->session->to->id, + "channel" => $session->session->to->channel, + "name" => $session->session->to->name, + "network" => $session->session->to->network + ) + : array( + "id" => null, + "channel" => null, + "name" => null, + "network" => null + ); + $this->_from = isset($session->session->from->id) + ? array( + "id" => $session->session->from->id, + "channel" => $session->session->from->channel, + "name" => $session->session->from->name, + "network" => $session->session->from->network + ) + : array( + "id" => null, + "channel" => null, + "name" => null, + "network" => null + ); + + $this->_headers = isset($session->session->headers) + ? self::setHeaders($session->session->headers) + : array(); + $this->_parameters = property_exists($session->session, 'parameters') ? (Array) $session->session->parameters : null; } - + public function getId() { return $this->_id; } - + public function getAccountID() { return $this->_accountId; } - + public function getTimeStamp() { return $this->_timestamp; } public function getUserType() { - return $this->_userType; + return $this->_userType; } - + public function getInitialText() { return $this->_initialText; } - + public function getTo() { return $this->_to; } - + public function getFrom() { return $this->_from; } - + + function getFromChannel() { + return $this->_from['channel']; + } + + function getFromNetwork() { + return $this->_from['network']; + } + public function getHeaders() { return $this->_headers; } - + /** * Returns the query string parameters for the session api * * If an argument is provided, a string containing the value of a - * query string variable matching that string is returned or null - * if there is no match. If no argument is argument is provided, + * query string variable matching that string is returned or null + * if there is no match. If no argument is argument is provided, * an array is returned with all query string variables or an empty * array if there are no query string variables. - * + * * @param string $name A specific parameter to return - * @return string|array $param + * @return string|array $param */ public function getParameters($name = null) { if (isset($name)) { @@ -1414,10 +1459,10 @@ public function getParameters($name = null) { if (!is_array($this->_parameters)) { return array(); } - return $this->_parameters; + return $this->_parameters; } } - + public function setHeaders($headers) { $formattedHeaders = new Headers(); // headers don't exist on outboud calls @@ -1425,27 +1470,27 @@ public function setHeaders($headers) { if (is_object($headers)) { foreach($headers as $name => $value) { $formattedHeaders->$name = $value; - } + } } return $formattedHeaders; } } /** - * Allows Tropo applications to begin recording the current session. + * Allows Tropo applications to begin recording the current session. * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. * @package TropoPHP_Support - * + * */ class StartRecording extends BaseClass { - + private $_name; private $_format; private $_method; private $_password; private $_url; private $_username; - + /** * Class constructor * @@ -1456,26 +1501,26 @@ class StartRecording extends BaseClass { * @param string $url * @param string $username */ - public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL) { + public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL) { $this->_format = $format; $this->_method = $method; $this->_password = $password; $this->_url = $url; $this->_username = $username; } - + /** * Renders object in JSON format. * */ - public function __toString() { + public function __toString() { if(isset($this->_format)) { $this->format = $this->_format; } if(isset($this->_method)) { $this->method = $this->_method; } if(isset($this->_password)) { $this->password = $this->_password; } if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_username)) { $this->username = $this->_username; } - return $this->unescapeJSON(json_encode($this)); - } + if(isset($this->_username)) { $this->username = $this->_username; } + return $this->unescapeJSON(json_encode($this)); + } } /** @@ -1491,11 +1536,11 @@ class StopRecording extends EmptyBaseClass { } * */ class Transcription extends BaseClass { - + private $_url; private $_id; private $_emailFormat; - + /** * Class constructor * @@ -1508,7 +1553,7 @@ public function __construct($url, $id=NULL, $emailFormat=NULL) { $this->_id = $id; $this->_emailFormat = $emailFormat; } - + /** * Renders object in JSON format. * @@ -1517,17 +1562,17 @@ public function __toString() { if(isset($this->_id)) { $this->id = $this->_id; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_emailFormat)) { $this->emailFormat = $this->_emailFormat; } - return $this->unescapeJSON(json_encode($this)); + return $this->unescapeJSON(json_encode($this)); } } /** - * Transfers an already answered call to another destination / phone number. + * Transfers an already answered call to another destination / phone number. * @package TropoPHP_Support * */ class Transfer extends BaseClass { - + private $_answerOnMedia; private $_choices; private $_from; @@ -1536,7 +1581,7 @@ class Transfer extends BaseClass { private $_timeout; private $_to; private $_allowSignals; - + /** * Class constructor * @@ -1552,19 +1597,19 @@ class Transfer extends BaseClass { public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; - $this->_choices = isset($choices) ? sprintf($choices) : null; + $this->_choices = isset($choices) ? sprintf($choices) : null; $this->_from = $from; $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; $this->_on = isset($on) ? sprintf($on) : null; $this->_allowSignals = $allowSignals; } - + /** * Renders object in JSON format. * */ - public function __toString() { + public function __toString() { $this->to = $this->_to; if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } if(isset($this->_choices)) { $this->choices = $this->_choices; } @@ -1573,8 +1618,8 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_on)) { $this->on = $this->_on; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + return $this->unescapeJSON(json_encode($this)); + } } /** @@ -1583,12 +1628,12 @@ public function __toString() { * */ class Endpoint extends BaseClass { - + private $_id; private $_channel; private $_name = 'unknown'; private $_network; - + /** * Class constructor * @@ -1598,24 +1643,24 @@ class Endpoint extends BaseClass { * @param string $network */ public function __construct($id, $channel=NULL, $name=NULL, $network=NULL) { - + $this->_id = $id; $this->_channel = $channel; $this->_name = $name; $this->_network = $network; } - + /** * Renders object in JSON format. * */ public function __toString() { - + if(isset($this->_id)) { $this->id = $this->_id; } if(isset($this->_channel)) { $this->channel = $this->_channel; } if(isset($this->_name)) { $this->name = $this->_name; } - if(isset($this->_network)) { $this->network = $this->_network; } - return $this->unescapeJSON(json_encode($this)); + if(isset($this->_network)) { $this->network = $this->_network; } + return $this->unescapeJSON(json_encode($this)); } } @@ -1638,7 +1683,7 @@ class Date { public static $monthDay = "md"; public static $year = "y"; public static $month = "m"; - public static $day = "d"; + public static $day = "d"; } /** @@ -1647,7 +1692,7 @@ class Date { */ class Duration { public static $hoursMinutesSeconds = "hms"; - public static $hoursMinutes = "hm"; + public static $hoursMinutes = "hm"; public static $hours = "h"; public static $minutes = "m"; public static $seconds = "s"; @@ -1658,7 +1703,7 @@ class Duration { * @package TropoPHP_Support */ class Event { - + public static $continue = 'continue'; public static $incomplete = 'incomplete'; public static $error = 'error'; @@ -1677,7 +1722,7 @@ class Format { public $duration; public static $ordinal = "ordinal"; public static $digits = "digits"; - + public function __construct($date=NULL, $duration=NULL) { $this->date = $date; $this->duration = $duration; @@ -1706,7 +1751,7 @@ class Network { public static $jabber = "JABBER"; public static $msn = "MSN"; public static $sms = "SMS"; - public static $yahoo = "YAHOO"; + public static $yahoo = "YAHOO"; public static $twitter = "TWITTER"; } @@ -1761,12 +1806,12 @@ class Recognizer { public static $British_English = 'en-gb'; public static $US_English = 'en-us'; public static $Castilian_Spanish = 'es-es'; - public static $Mexican_Spanish = 'es-mx'; + public static $Mexican_Spanish = 'es-mx'; public static $French_Canadian = 'fr-ca'; public static $French = 'fr-fr'; public static $Italian = 'it-it'; public static $Polish = 'pl-pl'; - public static $Dutch = 'nl-nl'; + public static $Dutch = 'nl-nl'; } /** @@ -1774,16 +1819,16 @@ class Recognizer { * @package TropoPHP_Support */ class Headers { - + public function __set($name, $value) { if(!strstr($name, "-")) { - $this->$name = $value; + $this->$name = $value; } else { $name = str_replace("-", "_", $name); - $this->$name = $value; - } + $this->$name = $value; + } } - + } ?> \ No newline at end of file From b67091e9a7fc5222ae60a869e2159d322074a8e8 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Sat, 2 Jul 2011 13:08:15 -0700 Subject: [PATCH 025/107] (Fix) No way to set headers on transfer. Fixes #17 --- tropo.class.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 3b65064..84d7b55 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -350,14 +350,14 @@ public function transfer($transfer, Array $params=NULL) { if(!is_object($transfer)) { $choices = isset($params["choices"]) ? $params["choices"] : null; $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on', 'allowSignals'); + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on', 'allowSignals', 'headers'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals); + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); } $this->transfer = sprintf($transfer); } @@ -1581,6 +1581,7 @@ class Transfer extends BaseClass { private $_timeout; private $_to; private $_allowSignals; + private $_headers; /** * Class constructor @@ -1593,8 +1594,9 @@ class Transfer extends BaseClass { * @param int $ringRepeat * @param int $timeout * @param string|array $allowSignals + * @param array $headers */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf($choices) : null; @@ -1603,6 +1605,7 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr $this->_timeout = $timeout; $this->_on = isset($on) ? sprintf($on) : null; $this->_allowSignals = $allowSignals; + $this->_headers = $headers; } /** @@ -1618,6 +1621,7 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_on)) { $this->on = $this->_on; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(count($this->_headers)) { $this->headers = $this->_headers; } return $this->unescapeJSON(json_encode($this)); } } From 849670718453d73e4c7afb0a347b3c739097678b Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Sat, 2 Jul 2011 15:29:10 -0700 Subject: [PATCH 026/107] (New) Convenience parameter in Tropo::transfer() allowing an array to be passed for on events or a playvalue parameter to have the lib construct your ring event for you. Fixes #18 --- tropo.class.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index 84d7b55..7161b28 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -350,11 +350,24 @@ public function transfer($transfer, Array $params=NULL) { if(!is_object($transfer)) { $choices = isset($params["choices"]) ? $params["choices"] : null; $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'on', 'allowSignals', 'headers'); + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; + } + } + $on = null; + if (array_key_exists('playvalue', $params) && isset($params['playvalue'])) { + $on = new On('ring', null, new Say($params['playvalue'])); + } elseif (array_key_exists('on', $params) && isset($params['on'])) { + if (is_object($params['on'])) { + $on = $params['on']; + } else { + if (strtolower($params['on']['event']) != 'ring') { + throw new TropoException("The only event allowed on transfer is 'ring'"); + } + $on = new On('ring', null, new Say($params['on']['say'])); } } $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); From 6dd10a58b53353db86c20b6fb93abeb3039d73f4 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Sat, 2 Jul 2011 15:47:29 -0700 Subject: [PATCH 027/107] (Fix) Choices on record sets value instead of terminator. Fixes #8 --- tropo.class.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 7161b28..8a2824c 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -226,7 +226,13 @@ public function on($on) { public function record($record) { if(!is_object($record) && is_array($record)) { $params = $record; - $choices = isset($params["choices"]) ? new Choices($params["choices"]) : null; + $p = array('as', 'voice', 'emailFormat', 'transcription', 'terminator'); + foreach ($p as $option) { + $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; + } + $choices = isset($params["choices"]) + ? new Choices(null, null, $params["choices"]) + : null; $say = new Say($params["say"], $params["as"], null, $params["voice"]); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); @@ -845,7 +851,7 @@ public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { * */ public function __toString() { - $this->value = $this->_value; + if(isset($this->value)){ $this->value = $this->_value; } if(isset($this->_mode)) { $this->mode = $this->_mode; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } return $this->unescapeJSON(json_encode($this)); From 4975e0ebc1d7cdb35082a211790463f088e5c517 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Sat, 2 Jul 2011 16:03:18 -0700 Subject: [PATCH 028/107] (Fix) Say values that include a % throw an error. sprintf() is trying to use the % as a replace token. --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index 8a2824c..0d8b83a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1326,7 +1326,7 @@ public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSi */ public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } - $this->value = $this->_value; + $this->value = str_replace('%', '%%', $this->_value); if(isset($this->_as)) { $this->as = $this->_as; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } From 25625827c39a969d0b536a6b82ca3a1973f4473e Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 5 Jul 2011 11:21:12 -0700 Subject: [PATCH 029/107] (New) Allow "terminator" param on tropo::record() to match Tropo scripting syntax --- tropo.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tropo.class.php b/tropo.class.php index b27ed41..f299adb 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -233,6 +233,9 @@ public function record($record) { $choices = isset($params["choices"]) ? new Choices(null, null, $params["choices"]) : null; + $choices = isset($params["terminator"]) + ? new Choices(null, null, $params["terminator"]) + : null; $say = new Say($params["say"], $params["as"], null, $params["voice"]); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); From 78afaa3ff61e84ea49674354897913d0fd5229c2 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 5 Jul 2011 12:18:06 -0700 Subject: [PATCH 030/107] (New) Convenience param "terminator" on tropo::transfer() to allow syntax similar to Scripting (Fix) choices param on tropo::record() ignored. --- tropo.class.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index f299adb..3ca1cbf 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -235,7 +235,7 @@ public function record($record) { : null; $choices = isset($params["terminator"]) ? new Choices(null, null, $params["terminator"]) - : null; + : $choices; $say = new Say($params["say"], $params["as"], null, $params["voice"]); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); @@ -358,6 +358,9 @@ public function stopRecording() { public function transfer($transfer, Array $params=NULL) { if(!is_object($transfer)) { $choices = isset($params["choices"]) ? $params["choices"] : null; + $choices = isset($params["terminator"]) + ? new Choices(null, null, $params["terminator"]) + : $choices; $to = isset($params["to"]) ? $params["to"] : $transfer; $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers'); foreach ($p as $option) { From f04360972a8a64d2a2f79a9bac46d1c32916ba19 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Mon, 11 Jul 2011 11:11:38 -0700 Subject: [PATCH 031/107] (Fix) Choices value is never rendered in outgoing json. --- tropo.class.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 3ca1cbf..a3d28eb 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -129,16 +129,16 @@ public function ask($ask, Array $params=NULL) { * @see https://www.tropo.com/docs/webapi/call.htm */ public function call($call, Array $params=NULL) { - if(!is_object($call)) { - $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); - } + if(!is_object($call)) { + $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); + } $this->call = sprintf($call); } @@ -857,7 +857,7 @@ public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { * */ public function __toString() { - if(isset($this->value)){ $this->value = $this->_value; } + if(isset($this->_value)){ $this->value = $this->_value; } if(isset($this->_mode)) { $this->mode = $this->_mode; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } return $this->unescapeJSON(json_encode($this)); From 35b1a42eb37ac451bc5616828fc260d6d2e9cbb2 Mon Sep 17 00:00:00 2001 From: "Keith C. Ivey" Date: Tue, 16 Aug 2011 16:12:29 -0400 Subject: [PATCH 032/107] fix handling of "voice" in record() --- tropo.class.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index a3d28eb..e9a620f 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -236,7 +236,10 @@ public function record($record) { $choices = isset($params["terminator"]) ? new Choices(null, null, $params["terminator"]) : $choices; - $say = new Say($params["say"], $params["as"], null, $params["voice"]); + if (!isset($params['voice'])) { + $params['voice'] = $this->_voice; + } + $say = new Say($params["say"], $params["as"], null, null); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); foreach ($p as $option) { @@ -1860,4 +1863,4 @@ public function __set($name, $value) { } -?> \ No newline at end of file +?> From ceb7fb29a77c8b1751349b66fb5f1331f8e0908d Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 30 Sep 2011 12:45:55 -0700 Subject: [PATCH 033/107] Add deployment example for Engine Yard Orchestra --- .gitmodules | 3 +++ samples/orchestra | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 samples/orchestra diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ace484d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "samples/orchestra"] + path = samples/orchestra + url = https://github.com/mheadd/tropo-orchestra.git diff --git a/samples/orchestra b/samples/orchestra new file mode 160000 index 0000000..56e8dff --- /dev/null +++ b/samples/orchestra @@ -0,0 +1 @@ +Subproject commit 56e8dffda54d2f029a5eda2953b2ef9f9788acea From e48403690c4e1da7dcd01637a106efbbce3ab620 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 21 Oct 2011 13:59:25 -0700 Subject: [PATCH 034/107] (Fix) sprintf without formatting string fails in some PHP versions --- tropo.class.php | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index a3d28eb..42638e7 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -118,7 +118,7 @@ public function ask($ask, Array $params=NULL) { $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer); } - $this->ask = sprintf($ask); + $this->ask = sprintf('%s', $ask); } /** @@ -139,7 +139,7 @@ public function call($call, Array $params=NULL) { } $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); } - $this->call = sprintf($call); + $this->call = sprintf('%s', $call); } /** @@ -161,7 +161,7 @@ public function conference($conference, Array $params=NULL) { } $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); } - $this->conference = sprintf($conference); + $this->conference = sprintf('%s', $conference); } /** @@ -170,7 +170,7 @@ public function conference($conference, Array $params=NULL) { */ public function hangup() { $hangup = new Hangup(); - $this->hangup = sprintf($hangup); + $this->hangup = sprintf('%s', $hangup); } /** @@ -193,7 +193,7 @@ public function message($message, Array $params=null) { } $message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers); } - $this->message = sprintf($message); + $this->message = sprintf('%s', $message); } /** @@ -210,7 +210,7 @@ public function on($on) { $next = (array_key_exists('next', $params)) ? $params["next"] : null; $on = new On($params["event"], $next, $say); } - $this->on = sprintf($on); + $this->on = sprintf('%s', $on); } /** @@ -258,7 +258,7 @@ public function record($record) { } $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); } - $this->record = sprintf($record); + $this->record = sprintf('%s', $record); } /** @@ -276,7 +276,7 @@ public function redirect($redirect, Array $params=NULL) { $from = isset($params["from"]) ? $params["from"] : null; $redirect = new Redirect($to, $from); } - $this->redirect = sprintf($redirect); + $this->redirect = sprintf('%s', $redirect); } /** @@ -288,7 +288,7 @@ public function redirect($redirect, Array $params=NULL) { */ public function reject() { $reject = new Reject(); - $this->reject = sprintf($reject); + $this->reject = sprintf('%s', $reject); } /** @@ -312,7 +312,7 @@ public function say($say, Array $params=NULL) { $voice = isset($voice) ? $voice : $this->_voice; $say = new Say($value, $as, $event, $voice, $allowSignals); } - $this->say = array(sprintf($say)); + $this->say = array(sprintf('%s', $say)); } /** @@ -334,7 +334,7 @@ public function startRecording($startRecording) { } $startRecording = new StartRecording($format, $method, $password, $url, $username); } - $this->startRecording = sprintf($startRecording); + $this->startRecording = sprintf('%s', $startRecording); } /** @@ -344,7 +344,7 @@ public function startRecording($startRecording) { */ public function stopRecording() { $stopRecording = new stopRecording(); - $this->stopRecording = sprintf($stopRecording); + $this->stopRecording = sprintf('%s', $stopRecording); } /** @@ -384,7 +384,7 @@ public function transfer($transfer, Array $params=NULL) { } $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); } - $this->transfer = sprintf($transfer); + $this->transfer = sprintf('%s', $transfer); } /** @@ -721,7 +721,7 @@ class Ask extends BaseClass { public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; - $this->_choices = isset($choices) ? sprintf($choices) : null ; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; $this->_minConfidence = $minConfidence; $this->_name = $name; $this->_required = $required; @@ -746,7 +746,7 @@ public function __toString() { if(isset($this->_say)) { $this->say = $this->_say; } if (is_array($this->_say)) { foreach ($this->_say as $k => $v) { - $this->_say[$k] = sprintf($v); + $this->_say[$k] = sprintf('%s', $v); } } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } @@ -807,7 +807,7 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ $this->_answerOnMedia = $answerOnMedia; $this->_timeout = $timeout; $this->_headers = $headers; - $this->_recording = isset($recording) ? sprintf($recording) : null ; + $this->_recording = isset($recording) ? sprintf('%s', $recording) : null ; $this->_allowSignals = $allowSignals; } @@ -901,7 +901,7 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones $this->_name = $name; $this->_id = $id; $this->_mute = $mute; - $this->_on = isset($on) ? sprintf($on) : null; + $this->_on = isset($on) ? sprintf('%s', $on) : null; $this->_playTones = $playTones; $this->_required = $required; $this->_terminator = $terminator; @@ -963,7 +963,7 @@ class Message extends BaseClass { * @param array $headers */ public function __construct(Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null) { - $this->_say = isset($say) ? sprintf($say) : null ; + $this->_say = isset($say) ? sprintf('%s', $say) : null ; $this->_to = $to; $this->_channel = $channel; $this->_network = $network; @@ -1013,7 +1013,7 @@ class On extends BaseClass { public function __construct($event=NULL, $next=NULL, Say $say=NULL) { $this->_event = $event; $this->_next = $next; - $this->_say = isset($say) ? sprintf($say) : null ; + $this->_say = isset($say) ? sprintf('%s', $say) : null ; } /** @@ -1078,7 +1078,7 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; $this->_beep = $beep; - $this->_choices = isset($choices) ? sprintf($choices) : null; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; $this->_format = $format; $this->_maxSilence = $maxSilence; $this->_maxTime = $maxTime; @@ -1087,9 +1087,9 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ if (!is_object($say)) { $say = new Say($say); } - $this->_say = isset($say) ? sprintf($say) : null; + $this->_say = isset($say) ? sprintf('%s', $say) : null; $this->_timeout = $timeout; - $this->_transcription = isset($transcription) ? sprintf($transcription) : null; + $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; $this->_username = $username; $this->_url = $url; $this->_voice = $voice; @@ -1137,8 +1137,8 @@ class Redirect extends BaseClass { * @param Endpoint $from */ public function __construct($to=NULL, $from=NULL) { - $this->_to = sprintf($to); - $this->_from = isset($from) ? sprintf($from) : null; + $this->_to = sprintf('%s', $to); + $this->_from = isset($from) ? sprintf('%s', $from) : null; } /** @@ -1624,11 +1624,11 @@ class Transfer extends BaseClass { public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; - $this->_choices = isset($choices) ? sprintf($choices) : null; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; $this->_from = $from; $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; - $this->_on = isset($on) ? sprintf($on) : null; + $this->_on = isset($on) ? sprintf('%s', $on) : null; $this->_allowSignals = $allowSignals; $this->_headers = $headers; } From d86cf3557cbff72b961414d82a232bcc1c0efa7a Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Thu, 27 Oct 2011 11:59:49 -0700 Subject: [PATCH 035/107] (Add) Movie trilogy sample from the docs --- samples/favorite-movie-webapi.php | 141 ++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 samples/favorite-movie-webapi.php diff --git a/samples/favorite-movie-webapi.php b/samples/favorite-movie-webapi.php new file mode 100644 index 0000000..4004197 --- /dev/null +++ b/samples/favorite-movie-webapi.php @@ -0,0 +1,141 @@ +getFrom(); +$network = $from_info['channel']; + +// Welcome prompt. +$tropo->ask("Welcome to the Tropo PHP example for $network"); + + // Provide a prompt based on the the initial_text value + if ($initial_text == "1") + {$tropo->say("You picked Lord of the Rings. Did you know Gandalf is also Magneto? Weird."); + } + if ($initial_text == "2") + {$tropo->say("You picked the original Star Wars. I hear Leonard Nimoy was awesome in those."); + } + if ($initial_text == "3") + {$tropo->say("You picked the Star Wars prequels. Stop calling this number, Mr. Lucas, we know it's you."); + } + if ($initial_text == "4") + {$tropo->say("You picked the Matrix. Dude, whoa."); + } + + // Tell Tropo what to do next. This redirects to the instructions under dispatch_post('/hangup', 'app_hangup'). + $tropo->on(array("event" => "continue", "next" => "testapp.php?uri=hangup")); + + // Tell Tropo what to do if there's an error. This redirects to the instructions under dispatch_post('/incomplete', 'app_incomplete'). + $tropo->on(array("event" => "incomplete", "next" => "testapp.php?uri=incomplete")); + +} + +dispatch_post('/start', 'app_start'); +function app_start() { + + // Create a new instance of the Session object, and get the channel information. + $session = new Session(); + $from_info = $session->getFrom(); + $network = $from_info['channel']; + + // Create a new instance of the Tropo object. + $tropo = new Tropo(); + + // See if any text was sent with session start. + $initial_text = $session->getInitialText(); + + // If the initial text is a zip code, skip the input collection and go right to results. + if(strlen($initial_text) == 1 && is_numeric($initial_text)) { + valid_text($tropo, $initial_text); + } + + else { + + // Welcome prompt. + $tropo->say("Welcome to the Tropo PHP example for $network"); + + // Set up options for input. + $options = array("attempts" => 3, "bargein" => true, "choices" => "1,2,3,4", "mode" => "dtmf", "name" => "movie", "timeout" => 30); + + // Ask the caller for input, pass in options. + $tropo->ask("Which of these trilogies do you like the best? Press 1 to vote for Lord of the Rings, press 2 for the original Star Wars, 3 for the Star Wars prequels, or press 4 for the Matrix", $options); + + // Tell Tropo what to do when the user has entered input, or if there's a problem. This redirects to the instructions under dispatch_post('/choice', 'app_choice') or dispatch_post('/incomplete', 'app_incomplete'). + $tropo->on(array("event" => "continue", "next" => "testapp.php?uri=choice", "say" => "Please hold.")); + $tropo->on(array("event" => "incomplete", "next" => "testapp.php?uri=incomplete")); + + } + + // Render the JSON for the Tropo WebAPI to consume. + return $tropo->RenderJson(); + +} + +dispatch_post('/choice', 'app_choice'); +function app_choice() { + + // Accessing the result object + $result = new Result(); + $choice = $result->getValue(); + + // Create a new instance of the Tropo object. + $tropo = new Tropo(); + + // Provide a prompt based on the value + if ($choice == "1") + {$tropo->say("You picked Lord of the Rings. Did you know Gandalf is also Mag knee toe? Weird."); + } + if ($choice == "2") + {$tropo->say("You picked the original Star Wars. I hear Leonard Nimoy was awe some in those."); + } + if ($choice == "3") + {$tropo->say("You picked the Star Wars prequels. Stop calling this number, Mr. Lucas, we know it's you."); + } + if ($choice == "4") + {$tropo->say("You picked the Matrix. Dude, woe."); + } + + // Tell Tropo what to do next. This redirects to the instructions under dispatch_post('/hangup', 'app_hangup'). + $tropo->on(array("event" => "continue", "next" => "testapp.php?uri=hangup")); + + // Tell Tropo what to do if there's an problem, like a timeout. This redirects to the instructions under dispatch_post('/incomplete', 'app_incomplete'). + $tropo->on(array("event" => "incomplete", "next" => "testapp.php?uri=incomplete")); + + // Render the JSON for the Tropo WebAPI to consume. + return $tropo->RenderJson(); +} + +dispatch_post('/hangup', 'app_hangup'); +function app_hangup() { + + $tropo = new Tropo(); + + $tropo->say("Thanks for voting!"); + $tropo->hangup(); + return $tropo->RenderJson(); +} + +dispatch_post('/incomplete', 'app_incomplete'); +function app_error() { + + $tropo = new Tropo(); + + $tropo->say("Something has gone wrong, please call back."); + $tropo->hangup(); + return $tropo->RenderJson(); +} + +// Run this sucker! +run(); + +?> \ No newline at end of file From 650c064ca782c43a3b3fa4d7c2a613e46cf509f3 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Mon, 7 Nov 2011 12:07:03 -0500 Subject: [PATCH 036/107] (Fix) added missing getCallId() method on Session object. --- tropo.class.php | 56 +++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 42638e7..0364b63 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1217,80 +1217,76 @@ public function __construct($json=NULL) { $this->_interpretation = $result->result->actions->interpretation; $this->_utterance = $result->result->actions->utterance; $this->_value = $result->result->actions->value; - $this->_concept = isset($result->result->actions->concept) - ? $result->result->actions->concept - : null; - $this->_transcription = isset($result->result->transcription) - ? $result->result->transcription - : null; + $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; + $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; } - function getSessionId() { + public function getSessionId() { return $this->_sessionId; } - function getCallId() { + public function getCallId() { return $this->_callId; } - function getState() { + public function getState() { return $this->_state; } - function getSessionDuration() { + public function getSessionDuration() { return $this->_sessionDuration; } - function getSequence() { + public function getSequence() { return $this->_sequence; } - function isComplete() { + public function isComplete() { return (bool) $this->_complete; } - function getError() { + public function getError() { return $this->_error; } - function getActions() { + public function getActions() { return $this->_actions; } - function getName() { + public function getName() { return $this->_name; } - function getAttempts() { + public function getAttempts() { return $this->_attempts; } - function getDisposition() { + public function getDisposition() { return $this->_disposition; } - function getConfidence() { + public function getConfidence() { return $this->_confidence; } - function getInterpretation() { + public function getInterpretation() { return $this->_interpretation; } - function getConcept() { + public function getConcept() { return $this->_concept; } - function getUtterance() { + public function getUtterance() { return $this->_utterance; } - function getValue() { + public function getValue() { return $this->_value; } - function getTranscription() { - return $this->_transcription; + public function getTranscription() { + return $this->_transcription; } } @@ -1350,7 +1346,8 @@ public function __toString() { class Session { private $_id; - private $_accountID; + private $_accountId; + private $_callId; private $_timestamp; private $_userType; private $_initialText; @@ -1379,12 +1376,13 @@ public function __construct($json=NULL) { } $this->_id = $session->session->id; $this->_accountId = $session->session->accountId; + $this->_callId = $session->session->callId; $this->_timestamp = $session->session->timestamp; $this->_userType = $session->session->userType; $this->_initialText = $session->session->initialText; - $this->_to = isset($session->session->to) + $this->_to = isset($session->session->to) ? array( - "id" => $session->session->to->id, + "id" => $session->session->to->id, "channel" => $session->session->to->channel, "name" => $session->session->to->name, "network" => $session->session->to->network @@ -1422,6 +1420,10 @@ public function getId() { public function getAccountID() { return $this->_accountId; } + + public function getcallid() { + return $this->_callId; + } public function getTimeStamp() { return $this->_timestamp; From e3ef7f6c79d50a481acf6c990d34586503e3f92c Mon Sep 17 00:00:00 2001 From: Tim Lytle Date: Tue, 22 Nov 2011 15:27:48 -0500 Subject: [PATCH 037/107] Now returning the session id, so signals can be sent to the created call. --- tropo-rest.class.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 8fceb3c..9ec1db8 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -40,15 +40,16 @@ public function createSession($token, Array $params = null) { $result = curl_exec($this->ch); $error = curl_error($this->ch); parent::__destruct(); - - if($result === false) { + + //check result and parse + if($result === false OR !($xml = new SimpleXMLElement($result))) { throw new Exception('An error occurred: '.$error); - } else { - if (strpos($result, self::SessionResponse) === false) { - throw new Exception('An error occurred: Tropo session launch failed.'); - } - return true; - } + } else { + if(!($xml->success == 'true')){ + throw new Exception('An error occurred: Tropo session launch failed.'); + } + return trim((string) $xml->id); + } } } From 493dc96c1730212ee96ba910a0edf302f606c796 Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Wed, 23 Nov 2011 10:27:47 -0500 Subject: [PATCH 038/107] Changed base URL on REST API calls to use HTTPS --- tropo-rest.class.php | 4 ++-- tropo.class.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 8fceb3c..7db2ba9 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -95,8 +95,8 @@ public function sendEvent($session_id, $event) { class ProvisioningAPI extends RestBase { // URLs for the Tropo provisioning API. - const ApplicationProvisioningURLBase = 'http://api.tropo.com/v1/'; - const ExchangeProvisioningURLBase = 'http://api.tropo.com/v1/exchanges'; + const ApplicationProvisioningURLBase = 'https://api.tropo.com/v1/'; + const ExchangeProvisioningURLBase = 'https://api.tropo.com/v1/exchanges'; public function __construct($userid, $password) { parent::__construct($userid, $password); diff --git a/tropo.class.php b/tropo.class.php index 0364b63..3981fb4 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1421,7 +1421,7 @@ public function getAccountID() { return $this->_accountId; } - public function getcallid() { + public function getCallId() { return $this->_callId; } From 2a1304f553b585df0390b1517d8d65b53a93039a Mon Sep 17 00:00:00 2001 From: Mark Headd Date: Wed, 23 Nov 2011 10:32:09 -0500 Subject: [PATCH 039/107] Fixed typo in file name. --- compatibilty.php => compatibility.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename compatibilty.php => compatibility.php (100%) diff --git a/compatibilty.php b/compatibility.php similarity index 100% rename from compatibilty.php rename to compatibility.php From fe08f01119a2f4a5faf2e06314b8bccc460680bd Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 16 Dec 2011 10:39:18 -0800 Subject: [PATCH 040/107] Include http body in REST error exception, as this often has useful data --- tropo-rest.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 051dc4e..bc87bbd 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -291,7 +291,8 @@ private function makeAPICall($method, $url, $payload=NULL) { throw new Exception('An error occurred: '.$this->error); } else { if ($this->curl_http_code != '200') { - throw new Exception('An error occurred: Invalid HTTP response returned: '.$this->curl_http_code); + $e = 'An error occurred: Invalid HTTP response returned: '.$this->curl_http_code . ' - Details: ' . $this->result; + throw new Exception($e); } return $this->result; } From fc232a4f5282c15c34011f7d746304e21e9885dc Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 16 Dec 2011 11:20:06 -0800 Subject: [PATCH 041/107] Refactor to allow setting of the base URL for rest calls --- tropo-rest.class.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index bc87bbd..c8de8eb 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -96,13 +96,21 @@ public function sendEvent($session_id, $event) { class ProvisioningAPI extends RestBase { // URLs for the Tropo provisioning API. - const ApplicationProvisioningURLBase = 'https://api.tropo.com/v1/'; - const ExchangeProvisioningURLBase = 'https://api.tropo.com/v1/exchanges'; + //const ApplicationProvisioningURLBase = 'https://api.tropo.com/v1/'; + var $base = 'https://api.tropo.com/v1/'; public function __construct($userid, $password) { parent::__construct($userid, $password); } + public function setBaseURL($url) { + $this->base = $url; + } + + protected function getBaseURL() { + return $this->base; + } + /** * Create a new Tropo application. * @@ -117,7 +125,7 @@ public function __construct($userid, $password) { public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition) { $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); - $url = self::ApplicationProvisioningURLBase.'applications'; + $url = $this->base . 'applications'; return self::makeAPICall('POST', $url, $payload); } @@ -140,7 +148,7 @@ public function createApplication($href, $name, $voiceUrl, $messagingUrl, $platf public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $number=NULL, $city=NULL, $state=NULL, $channel=NULL, $username=NULL, $password=NULL, $token=NULL) { $payload = json_encode(new Address($type, $prefix, $number, $city, $state, $channel, $username, $password, $token)); - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + $url = $this->base . 'applications/'.$applicationID.'/addresses'; return self::makeAPICall('POST', $url, $payload); } @@ -160,7 +168,7 @@ public function updateApplicationAddress($applicationID, $type, $prefix=NULL, $n public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL, $voiceUrl=NULL, $messagingUrl=NULL, $platform=NULL, $partition=NULL) { $payload = json_encode(new Application($href, $name, $voiceUrl, $messagingUrl, $platform, $partition)); - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; + $url = $this->base . 'applications/'.$applicationID; return self::makeAPICall('PUT', $url, $payload); } @@ -173,7 +181,7 @@ public function updateApplicationProperty($applicationID, $href=NULL, $name=NULL */ public function deleteApplication($applicationID) { - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; + $url = $this->base . 'applications/'.$applicationID; return self::makeAPICall('DELETE', $url); } @@ -188,7 +196,7 @@ public function deleteApplication($applicationID) { */ public function deleteApplicationAddress($applicationID, $type, $address) { - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses/'.$type.'/'.$address; + $url = $this->base . 'applications/'.$applicationID.'/addresses/'.$type.'/'.$address; return self::makeAPICall('DELETE', $url); } @@ -200,7 +208,7 @@ public function deleteApplicationAddress($applicationID, $type, $address) { */ public function viewApplications() { - $url = self::ApplicationProvisioningURLBase.'applications'; + $url = $this->base . 'applications'; return self::makeAPICall('GET', $url); } @@ -213,7 +221,7 @@ public function viewApplications() { */ public function viewSpecificApplication($applicationID) { - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID; + $url = $this->base . 'applications/'.$applicationID; return self::makeAPICall('GET', $url); } @@ -226,7 +234,7 @@ public function viewSpecificApplication($applicationID) { */ public function viewAddresses($applicationID) { - $url = self::ApplicationProvisioningURLBase.'applications/'.$applicationID.'/addresses'; + $url = $this->base . 'applications/'.$applicationID.'/addresses'; return self::makeAPICall('GET', $url); } @@ -238,7 +246,7 @@ public function viewAddresses($applicationID) { */ public function viewExchanges() { - $url = self::ExchangeProvisioningURLBase; + $url = $this->base . '/exchanges'; return self::makeAPICall('GET', $url); } @@ -251,7 +259,7 @@ public function viewExchanges() { * @param string $payload * @return string JSON */ - private function makeAPICall($method, $url, $payload=NULL) { + protected function makeAPICall($method, $url, $payload=NULL) { if(($method == 'POST' || $method == 'PUT') && !isset($payload)) { throw new Exception("Method $method requires payload for request body."); From c22d4e09968643b6788b70f2483ac0e658f7722f Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 16 Dec 2011 14:29:58 -0800 Subject: [PATCH 042/107] (Fix) 2xx response codes other than 200 are considered errors --- tropo-rest.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index c8de8eb..4546656 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -298,7 +298,7 @@ protected function makeAPICall($method, $url, $payload=NULL) { if($this->result === false) { throw new Exception('An error occurred: '.$this->error); } else { - if ($this->curl_http_code != '200') { + if (substr($this->curl_http_code, 0, 1) != '2') { $e = 'An error occurred: Invalid HTTP response returned: '.$this->curl_http_code . ' - Details: ' . $this->result; throw new Exception($e); } From c9f3f0ff2b6dfb428aa9c05bf6eef00f8f30051a Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Wed, 11 Jan 2012 11:54:47 -0800 Subject: [PATCH 043/107] (Fix) viewExchanges throws a server error due to an extra slash in the URL --- tropo-rest.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 4546656..e39fb4f 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -246,7 +246,7 @@ public function viewAddresses($applicationID) { */ public function viewExchanges() { - $url = $this->base . '/exchanges'; + $url = $this->base . 'exchanges'; return self::makeAPICall('GET', $url); } From 0b018721f1507f061a6003c1ca8296be02cef23a Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 17 Jan 2012 21:09:42 -0800 Subject: [PATCH 044/107] (Change) include http response code in exceptions --- tropo-rest.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index e39fb4f..d087ba8 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -299,8 +299,8 @@ protected function makeAPICall($method, $url, $payload=NULL) { throw new Exception('An error occurred: '.$this->error); } else { if (substr($this->curl_http_code, 0, 1) != '2') { - $e = 'An error occurred: Invalid HTTP response returned: '.$this->curl_http_code . ' - Details: ' . $this->result; - throw new Exception($e); + $body = json_decode($this->result); + throw new Exception($body->error, $this->curl_http_code); } return $this->result; } From 8a8912162fc9228722d1cdee74ceb9cbc682dfbb Mon Sep 17 00:00:00 2001 From: Roderik van der Veer Date: Sun, 4 Mar 2012 09:55:44 +0100 Subject: [PATCH 045/107] The method signature should be the same. The Choices class extends BaseClass but has another method signature for __construct(). Since the existence of a constructor is implied in a class this abstract mention is not required nor useful. ps. PHP 5.4 craps out over this change in signature --- tropo.class.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 3981fb4..f00e1bb 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -633,12 +633,6 @@ public function __toString() { abstract class BaseClass { - /** - * Class constructor - * @abstract __construct() - */ - abstract public function __construct(); - /** * toString Function * @abstract __toString() From 2f9d0614934f722814c79b84bdc0fec6941b3778 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 22 May 2012 09:57:16 -0700 Subject: [PATCH 046/107] (New) Allow setting of the base URL in the session and event API calls. --- tropo-rest.class.php | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index d087ba8..086b939 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -11,7 +11,7 @@ class SessionAPI extends RestBase { // URL for the Tropo session API. - const SessionURL = 'http://api.tropo.com/1.0/sessions?action=create&token='; + var $base = 'https://api.tropo.com/1.0/'; // Success response from Tropo Session API. const SessionResponse = 'true'; @@ -20,6 +20,15 @@ public function __construct() { parent::__construct(); } + + public function setBaseURL($url) { + $this->base = $url; + } + + protected function getBaseURL() { + return $this->base; + } + /** * Launch a new Tropo session. * @@ -35,10 +44,10 @@ public function createSession($token, Array $params = null) { } } - curl_setopt($this->ch, CURLOPT_URL, self::SessionURL.$token.$querystring); + curl_setopt($this->ch, CURLOPT_URL, $this->base . 'sessions?action=create&token=' . $token . $querystring); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); - $result = curl_exec($this->ch); - $error = curl_error($this->ch); + $result = curl_exec($this->ch); + $error = curl_error($this->ch); parent::__destruct(); //check result and parse @@ -56,8 +65,8 @@ public function createSession($token, Array $params = null) { class EventAPI extends RestBase { // URL for the Tropo session API. - const EventURL = 'https://api.tropo.com/1.0/sessions/%session_id%/signals?action=signal&value=%value%'; - + var $base = 'https://api.tropo.com/1.0/'; + // Success response from Tropo Session API. const EventResponse = 'QUEUED'; @@ -74,7 +83,8 @@ public function __construct() { */ public function sendEvent($session_id, $event) { - $url = str_replace(array('%session_id%', '%value%'), array($session_id, $event), self::EventURL); + $url = $this->base . '%session_id%/signals?action=signal&value=%value%'; + $url = str_replace(array('%session_id%', '%value%'), array($session_id, $event), $url); curl_setopt($this->ch, CURLOPT_URL, $url); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); @@ -349,6 +359,8 @@ public function __construct($userid=NULL, $password=NULL) { public function __destruct() { @ curl_close($this->ch); } + + } /** From dc8895012d7ee046ed37d0504e3365c8dd1559a9 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Tue, 5 Jun 2012 17:57:11 -0700 Subject: [PATCH 047/107] (Fix) warnings about unset querystring variable when launching a session with no parameters. --- tropo-rest.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tropo-rest.class.php b/tropo-rest.class.php index 086b939..d86089e 100644 --- a/tropo-rest.class.php +++ b/tropo-rest.class.php @@ -38,6 +38,7 @@ protected function getBaseURL() { */ public function createSession($token, Array $params = null) { + $querystring = ''; if(isset($params)) { foreach ($params as $key=>$value) { @ $querystring .= '&'. urlencode($key) . '=' . urlencode($value); From d80fd5434bf699420f3a2e6f53a538a85b7854a1 Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 22 Jun 2012 15:54:47 -0700 Subject: [PATCH 048/107] (Change) ease of use improvements to Tropo::conference (New) Conference calling application sample. --- samples/conference.php | 72 ++++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 4 ++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 samples/conference.php diff --git a/samples/conference.php b/samples/conference.php new file mode 100644 index 0000000..6fd9549 --- /dev/null +++ b/samples/conference.php @@ -0,0 +1,72 @@ +map('/', 'start') + ->via('GET') + ->via('POST'); +$app->post('/restart', 'start'); + +function start() { + $tropo = new Tropo(); + $tropo->setVoice($voice); + + $tropo->ask("Enter your conference ID, followed by the pound key.", array( + "choices" => "[1-10 DIGITS]", + "name" => "confid", + "attempts" => 5, + "timeout" => 60, + "mode" => "dtmf", + "terminator" => "#", + "event" => array( + "incomplete" => 'Sorry, I didn\'t hear anything.', + "nomatch" => 'Sorry, that is not a valid conference ID.' + ) + )); + + $tropo->on(array("event" => "continue", "next" => "conference")); + $tropo->on(array("event" => "incomplete", "next" => "restart")); + + $tropo->RenderJson(); +} + +$app->post('/conference', 'conference'); +function conference() { + $tropo = new Tropo(); + $tropo->setVoice($voice); + + $result = new Result(); + $conference = $result->getValue(); + + $tropo->say('Conference ID ' . $conference . ' accepted.'); + $tropo->say('You will now be placed into the conference. Please announce yourself. To exit the conference without disconnecting, press pound.'); + $tropo->conference($conference, array('id' => $conference, 'terminator' => '#')); + $tropo->say('You have left the conference.'); + + $tropo->on(array("event" => "continue", "next" => "restart")); + $tropo->RenderJson(); +} + +$app->run(); +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 3981fb4..7671729 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -159,6 +159,8 @@ public function conference($conference, Array $params=NULL) { $$option = $params[$option]; } } + $id = (empty($id) && !empty($conference)) ? $conference : $id; + $name = (empty($name)) ? (string)$id : $name; $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); } $this->conference = sprintf('%s', $conference); @@ -899,7 +901,7 @@ class Conference extends BaseClass { */ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { $this->_name = $name; - $this->_id = $id; + $this->_id = (string) $id; $this->_mute = $mute; $this->_on = isset($on) ? sprintf('%s', $on) : null; $this->_playTones = $playTones; From ab210fa87c65f4d5fb9a0391cac595f6f543637a Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Fri, 22 Jun 2012 16:21:44 -0700 Subject: [PATCH 049/107] (Fix) Make $voice a global in conference sample --- samples/conference.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/conference.php b/samples/conference.php index 6fd9549..f2d440b 100644 --- a/samples/conference.php +++ b/samples/conference.php @@ -29,6 +29,7 @@ $app->post('/restart', 'start'); function start() { + global $voice; $tropo = new Tropo(); $tropo->setVoice($voice); @@ -53,6 +54,7 @@ function start() { $app->post('/conference', 'conference'); function conference() { + global $voice; $tropo = new Tropo(); $tropo->setVoice($voice); From aa4c88f786aeae78c41441f6feae78816e004b8a Mon Sep 17 00:00:00 2001 From: JustinDupree Date: Tue, 26 Jun 2012 10:15:54 -0300 Subject: [PATCH 050/107] Updated to omit portions of code that caused confusion and looping applications --- samples/outbound_call.php | 80 ++++++++++++--------------------------- 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/samples/outbound_call.php b/samples/outbound_call.php index 941a6dd..63f23c4 100644 --- a/samples/outbound_call.php +++ b/samples/outbound_call.php @@ -1,58 +1,28 @@ -getParameters("numbertodial"); +$name = $session->getParameters("customername"); +$msg = $session->getParameters("msg"); -// When the the session object is created, it tries -// to load the json that Tropo posts when reciving or -// making a call. If the json doesn't exist, the -// Session object throws a TropoException. -// This try/catch block checks to see if this code is -// being run as part of a session or being run directly. -try { - - // this next line throws an exception if the code isn't - // being run by Tropo. If that happens, the catch block - // below will run. - $session = new Session(); - - if ($session->getParameters("action") == "create") { - // A token-launched session (an outgoing call) will - // have a parameter called "action" that is set to - // "create". If this is true, we're trying to make an - // outgoing call. The next two lines make that call - // and say something. - $tropo->call($session->getParameters("dial")); - $tropo->say('This is an outbound call.'); - } else { - - // The session JSON exists, but there's no action - // parameter or it wasn't set to "create" so this must - // be an incoming call. - $tropo->say('Thank you for calling us.'); - } - $tropo->renderJSON(); -} catch (TropoException $e) { - if ($e->getCode() == '1') { - - // The session object threw an exception, so this file wasn't - // loaded as part of a Tropo session. Use the session API to - // launch a new session. - if ($tropo->createSession($token, array('dial' => $number))) { - print 'Call launched to ' . $number; - } else { - print "call failed! Try it again with the Tropo debugger running to see what the error is."; - } - } -} -?> +//extracts the contects of the passed parameters and assigns them as variables for later use + +$tropo = new Tropo(); + +$tropo->call($to, array('network'=>'SMS')); +$tropo->say("OMG ".$name.", ".$msg."!"); + +//actually creates the call, passed the "to" value and then adds in the other variables for the message + +return $tropo->RenderJson(); + +//defines the response to Tropo in JSON + +?> \ No newline at end of file From ab8a831c2de2853d9e97fe6834ab8815d2fcfaca Mon Sep 17 00:00:00 2001 From: JustinDupree Date: Tue, 26 Jun 2012 10:55:38 -0300 Subject: [PATCH 051/107] Typo fix --- samples/outbound_call.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/outbound_call.php b/samples/outbound_call.php index 63f23c4..c1f527b 100644 --- a/samples/outbound_call.php +++ b/samples/outbound_call.php @@ -12,7 +12,7 @@ $name = $session->getParameters("customername"); $msg = $session->getParameters("msg"); -//extracts the contects of the passed parameters and assigns them as variables for later use +//extracts the contents of the passed parameters and assigns them as variables for later use $tropo = new Tropo(); From e3a207bc3b7980883f75ee362246cbba3259e0c0 Mon Sep 17 00:00:00 2001 From: JustinDupree Date: Fri, 20 Jul 2012 17:22:33 -0300 Subject: [PATCH 052/107] Fixed incorrect variable --- samples/call-info.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/call-info.php b/samples/call-info.php index 7ac65dc..4a3b0e0 100644 --- a/samples/call-info.php +++ b/samples/call-info.php @@ -32,7 +32,7 @@ $tropo->ask("This will catch the first text", array('choices' => '[ANY]')); // ... or, you can grab that first text like this straight from the session. - $messsage = $tropo->getInitialText(); + $messsage = $session->getInitialText(); $tropo->say("You said " . $message); } else { From d6745d1d5149270e0cf6ca8777ccebca91d8be5b Mon Sep 17 00:00:00 2001 From: JustinDupree Date: Wed, 17 Oct 2012 15:41:41 -0300 Subject: [PATCH 053/107] Added 5.4 restriction --- readme.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.markdown b/readme.markdown index 15ee95e..0873a23 100644 --- a/readme.markdown +++ b/readme.markdown @@ -6,7 +6,7 @@ TropoPHP is a set of PHP classes for working with [Tropo's cloud communication s Requirements ============ - * PHP 5.3.0 or greater + * PHP 5.3.0 or greater (5.4 not yet supported) * PHP Notices disabled (All error reporting disabled is recommended for production use) Usage From 8c1adf8698cbe0b4ddba8801635980bd504c8440 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:32:42 -0500 Subject: [PATCH 054/107] fixed spacing --- tropo.class.php | 3409 +++++++++++++++++++++++------------------------ 1 file changed, 1704 insertions(+), 1705 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 0de9783..a0efbc1 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1,1864 +1,1863 @@ tropo = array(); - } - - /** - * Set a default voice for use with all Text To Speech. - * - * Tropo's text to speech engine can pronounce your text with - * a variety of voices in different languages. All elements where - * you can create text to speech (TTS) accept a voice parameter. - * Tropo's default is "Allison" but you can set a default for this - * script here. - * - * @param string $voice - */ - public function setVoice($voice) { - $this->_voice = $voice; - } - - /** - * Set a default language to use in speech recognition. - * - * When recognizing spoken input, Tropo allows you to set a language - * to let the platform know which language is being spoken and which - * recognizer to use. The default is en-us (US English), but you can - * set a different default to be used in your application here. - * - * @param string $language - */ - public function setLanguage($language) { - $this->_language = $language; - } - - /** - * Sends a prompt to the user and optionally waits for a response. - * - * The ask method allows for collecting input using either speech - * recognition or DTMF (also known as Touch Tone). You can either - * pass in a fully-formed Ask object or a string to use as the - * prompt and an array of parameters. - * - * @param string|Ask $ask - * @param array $params - * @see https://www.tropo.com/docs/webapi/ask.htm - */ - public function ask($ask, Array $params=NULL) { - if(!is_object($ask)) { - $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - if (is_array($event)) { - // If an event was passed in, add the events to the Ask - foreach ($event as $e => $val){ - $say[] = new Say($val, $as, $e, $voice); - } - } - $say[] = new Say($ask, $as, null, $voice); - $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; - $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; - $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; - if (!isset($voice) && isset($this->_voice)) { - $voice = $this->_voice; - } - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer); - } - $this->ask = sprintf('%s', $ask); - } - - /** - * Places a call or sends an an IM, Twitter, or SMS message. To start a call, use the Session API to tell Tropo to launch your code. - * - * @param string|Call $call - * @param array $params - * @see https://www.tropo.com/docs/webapi/call.htm - */ - public function call($call, Array $params=NULL) { - if(!is_object($call)) { - $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); - } - $this->call = sprintf('%s', $call); - } - - /** - * This object allows multiple lines in separate sessions to be conferenced together so that the parties on each line can talk to each other simultaneously. - * This is a voice channel only feature. - * - * @param string|Conference $conference - * @param array $params - * @see https://www.tropo.com/docs/webapi/conference.htm - */ - public function conference($conference, Array $params=NULL) { - if(!is_object($conference)) { - $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $id = (empty($id) && !empty($conference)) ? $conference : $id; - $name = (empty($name)) ? (string)$id : $name; - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); - } - $this->conference = sprintf('%s', $conference); - } - - /** - * This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. - * @see https://www.tropo.com/docs/webapi/hangup.htm - */ - public function hangup() { - $hangup = new Hangup(); - $this->hangup = sprintf('%s', $hangup); - } - - /** - * A shortcut method to create a session, say something, and hang up, all in one step. This is particularly useful for sending out a quick SMS or IM. - * - * @param string|Message $message - * @param array $params - * @see https://www.tropo.com/docs/webapi/message.htm - */ - public function message($message, Array $params=null) { - if(!is_object($message)) { - $say = new Say($message); - $to = $params["to"]; - $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers); - } - $this->message = sprintf('%s', $message); - } - - /** - * Adds an event callback so that your application may be notified when a particular event occurs. - * Possible events are: "continue", "error", "incomplete" and "hangup". - * - * @param array $params - * @see https://www.tropo.com/docs/webapi/on.htm - */ - public function on($on) { - if (!is_object($on) && is_array($on)) { - $params = $on; - $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; - $next = (array_key_exists('next', $params)) ? $params["next"] : null; - $on = new On($params["event"], $next, $say); - } - $this->on = sprintf('%s', $on); - } - - /** - * Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. - * If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below. - * The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set. - * At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form. - * If specified, the audio file may also be transcribed and the text returned to you via an email address or HTTP POST/Multipart Form. - * - * @param array|Record $record - * @see https://www.tropo.com/docs/webapi/record.htm - */ - public function record($record) { - if(!is_object($record) && is_array($record)) { - $params = $record; - $p = array('as', 'voice', 'emailFormat', 'transcription', 'terminator'); - foreach ($p as $option) { - $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; - } - $choices = isset($params["choices"]) - ? new Choices(null, null, $params["choices"]) - : null; - $choices = isset($params["terminator"]) - ? new Choices(null, null, $params["terminator"]) - : $choices; - if (!isset($params['voice'])) { - $params['voice'] = $this->_voice; - } - $say = new Say($params["say"], $params["as"], null, null); - if (is_array($params['transcription'])) { - $p = array('url', 'id', 'emailFormat'); - foreach ($p as $option) { - $$option = null; - if (!is_array($params["transcription"]) || !array_key_exists($option, $params["transcription"])) { - $params["transcription"][$option] = null; - } - } - $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"]); - } else { - $transcription = $params["transcription"]; - } - $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); - } - $this->record = sprintf('%s', $record); - } - - /** - * The redirect function forwards an incoming call to another destination / phone number before answering it. - * The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state. - * Use transfer when working with active answered calls. - * - * @param string|Redirect $redirect - * @param array $params - * @see https://www.tropo.com/docs/webapi/redirect.htm - */ - public function redirect($redirect, Array $params=NULL) { - if(!is_object($redirect)) { - $to = isset($params["to"]) ? $params["to"]: null; - $from = isset($params["from"]) ? $params["from"] : null; - $redirect = new Redirect($to, $from); - } - $this->redirect = sprintf('%s', $redirect); - } - - /** - * Allows Tropo applications to reject incoming sessions before they are answered. - * For example, an application could inspect the callerID variable to determine if the user is known, and then use the reject call accordingly. - * - * @see https://www.tropo.com/docs/webapi/reject.htm - * - */ - public function reject() { - $reject = new Reject(); - $this->reject = sprintf('%s', $reject); - } - - /** - * When the current session is a voice channel this key will either play a message or an audio file from a URL. - * In the case of an text channel it will send the text back to the user via i nstant messaging or SMS. - * - * @param string|Say $say - * @param array $params - * @see https://www.tropo.com/docs/webapi/say.htm - */ - public function say($say, Array $params=NULL) { - if(!is_object($say)) { - $p = array('as', 'format', 'event','voice', 'allowSignals'); - $value = $say; - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $voice = isset($voice) ? $voice : $this->_voice; - $say = new Say($value, $as, $event, $voice, $allowSignals); - } - $this->say = array(sprintf('%s', $say)); - } - - /** - * Allows Tropo applications to begin recording the current session. - * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. - * - * @param array|StartRecording $startRecording - * @see https://www.tropo.com/docs/webapi/startrecording.htm - */ - public function startRecording($startRecording) { - if(!is_object($startRecording) && is_array($startRecording)) { - $params = $startRecording; - $p = array('format', 'method', 'password', 'url', 'username'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $startRecording = new StartRecording($format, $method, $password, $url, $username); - } - $this->startRecording = sprintf('%s', $startRecording); - } - - /** - * Stops a previously started recording. - * - * @see https://www.tropo.com/docs/webapi/stoprecording.htm - */ - public function stopRecording() { - $stopRecording = new stopRecording(); - $this->stopRecording = sprintf('%s', $stopRecording); - } - - /** - * Transfers an already answered call to another destination / phone number. - * Call may be transferred to another phone number or SIP address, which is set through the "to" parameter and is in URL format. - * - * @param string|Transfer $transfer - * @param array $params - * @see https://www.tropo.com/docs/webapi/transfer.htm - */ - public function transfer($transfer, Array $params=NULL) { - if(!is_object($transfer)) { - $choices = isset($params["choices"]) ? $params["choices"] : null; - $choices = isset($params["terminator"]) - ? new Choices(null, null, $params["terminator"]) - : $choices; - $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $on = null; - if (array_key_exists('playvalue', $params) && isset($params['playvalue'])) { - $on = new On('ring', null, new Say($params['playvalue'])); - } elseif (array_key_exists('on', $params) && isset($params['on'])) { - if (is_object($params['on'])) { - $on = $params['on']; - } else { - if (strtolower($params['on']['event']) != 'ring') { - throw new TropoException("The only event allowed on transfer is 'ring'"); - } - $on = new On('ring', null, new Say($params['on']['say'])); - } - } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); - } - $this->transfer = sprintf('%s', $transfer); - } - - /** - * Launches a new session with the Tropo Session API. - * (Pass through to SessionAPI class.) - * - * @param string $token Your outbound session token from Tropo - * @param array $params An array of key value pairs that will be added as query string parameters - * @return bool True if the session was launched successfully - */ - public function createSession($token, Array $params=NULL) { - try { - $session = new SessionAPI(); - $result = $session->createSession($token, $params); - return $result; - } - // If an exception occurs, wrap it in a TropoException and rethrow. - catch (Exception $ex) { - throw new TropoException($ex->getMessage(), $ex->getCode()); - } - } - - public function sendEvent($session_id, $value) { - try { - $event = new EventAPI(); - $result = $event->sendEvent($session_id, $value); - return $result; - } - catch (Exception $ex) { - throw new TropoException($ex->getMessage(), $ex->getCode()); - } - } - - /** - * Creates a new Tropo Application - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param array $params - * @return string JSON - */ - public function createApplication($userid, $password, Array $params) { - $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); - foreach ($p as $property) { - $$property = null; - if (is_array($params) && array_key_exists($property, $params)) { - $$property = $params[$property]; - } - } - try { - $provision = new ProvisioningAPI($userid, $password); - $result = $provision->createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition); - return $result; - } - // If an exception occurs, wrap it in a TropoException and rethrow. - catch (Exception $ex) { - throw new TropoException($ex->getMessage(), $ex->getCode()); - } - } - - /** - * Add/Update an address (phone number, IM address or token) for an existing Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @param array $params - * @return string JSON - */ - public function updateApplicationAddress($userid, $passwd, $applicationID, Array $params) { - $p = array('type', 'prefix', 'number', 'city', 'state', 'channel', 'username', 'password', 'token'); - foreach ($p as $property) { - $$property = null; - if (is_array($params) && array_key_exists($property, $params)) { - $$property = $params[$property]; - } - } - try { - $provision = new ProvisioningAPI($userid, $passwd); - $result = $provision->updateApplicationAddress($applicationID, $type, $prefix, $number, $city, $state, $channel, $username, $password, $token); - return $result; - } - // If an exception occurs, wrap it in a TropoException and rethrow. - catch (Exception $ex) { - throw new TropoException($ex->getMessage(), $ex->getCode()); - } - } - - /** - * Update a property (name, URL, platform, etc.) for an existing Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @param array $params - * @return string JSON - */ - public function updateApplicationProperty($userid, $password, $applicationID, Array $params) { - $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); - foreach ($p as $property) { - $$property = null; - if (is_array($params) && array_key_exists($property, $params)) { - $$property = $params[$property]; - } - } - try { - $provision = new ProvisioningAPI($userid, $password); - $result = $provision->updateApplicationProperty($applicationID, $href, $name, $voiceUrl, $messagingUrl, $platform, $partition); - return $result; - } - // If an exception occurs, wrap it in a TropoException and rethrow. - catch (Exception $ex) { - throw new TropoException($ex->getMessage(), $ex->getCode()); - } - } - - /** - * Delete an existing Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @return string JSON - */ - public function deleteApplication($userid, $password, $applicationID) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->deleteApplication($applicationID); - } - - /** - * Delete an address for an existing Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @param string $number - * @return string JSON - */ - public function deleteApplicationAddress($userid, $password, $applicationID, $addresstype, $address) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->deleteApplicationAddress($applicationID, $addresstype, $address); - } - - /** - * View a list of Tropo applications. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @return string JSON - */ - public function viewApplications($userid, $password) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->viewApplications(); - } - - /** - * View the details of a specific Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @return string JSON - */ - public function viewSpecificApplication($userid, $password, $applicationID) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->viewSpecificApplication($applicationID); - } - - /** - * View the addresses for a specific Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @param string $applicationID - * @return string JSON - */ - public function viewAddresses($userid, $password, $applicationID) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->viewAddresses($applicationID); - } - - /** - * View a list of available exchanges for assigning a number to a Tropo application. - * (Pass through to ProvisioningAPI class). - * - * @param string $userid - * @param string $password - * @return string JSON - */ - public function viewExchanges($userid, $password) { - $provision = new ProvisioningAPI($userid, $password); - return $provision->viewExchanges(); - } - - /** - * Renders the Tropo object as JSON. - * - */ - public function renderJSON() { - header('Content-type: application/json'); - echo $this; - } - - /** - * Allows undefined methods to be called. - * This method is invloked by Tropo class methods to add action items to the Tropo array. - * - * @param string $name - * @param mixed $value - * @access private - */ - public function __set($name, $value) { - array_push($this->tropo, array($name => $value)); - } - - /** - * Controls how JSON structure for the Tropo object is rendered. - * - * @return string - * @access private - */ - public function __toString() { - // Remove voice and language so they do not appear in the rednered JSON. - unset($this->_voice); - unset($this->_language); - - // Call the unescapeJSON() method in the parent class. - return parent::unescapeJSON(json_encode($this)); - } + /** + * The container for JSON actions. + * + * @var array + * @access private + */ + public $tropo; + + /** + * The TTS voice to use when rendering content. + * + * @var string + * @access private + */ + private $_voice; + + /** + * The language to use when rendering content. + * + * @var string + * @access private + */ + private $_language; + + /** + * Class constructor for the Tropo class. + * @access private + */ + public function __construct() { + $this->tropo = array(); + } + + /** + * Set a default voice for use with all Text To Speech. + * + * Tropo's text to speech engine can pronounce your text with + * a variety of voices in different languages. All elements where + * you can create text to speech (TTS) accept a voice parameter. + * Tropo's default is "Allison" but you can set a default for this + * script here. + * + * @param string $voice + */ + public function setVoice($voice) { + $this->_voice = $voice; + } + + /** + * Set a default language to use in speech recognition. + * + * When recognizing spoken input, Tropo allows you to set a language + * to let the platform know which language is being spoken and which + * recognizer to use. The default is en-us (US English), but you can + * set a different default to be used in your application here. + * + * @param string $language + */ + public function setLanguage($language) { + $this->_language = $language; + } + + /** + * Sends a prompt to the user and optionally waits for a response. + * + * The ask method allows for collecting input using either speech + * recognition or DTMF (also known as Touch Tone). You can either + * pass in a fully-formed Ask object or a string to use as the + * prompt and an array of parameters. + * + * @param string|Ask $ask + * @param array $params + * @see https://www.tropo.com/docs/webapi/ask.htm + */ + public function ask($ask, Array $params=NULL) { + if(!is_object($ask)) { + $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + if (is_array($event)) { + // If an event was passed in, add the events to the Ask + foreach ($event as $e => $val){ + $say[] = new Say($val, $as, $e, $voice); + } + } + $say[] = new Say($ask, $as, null, $voice); + $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; + $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; + $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; + if (!isset($voice) && isset($this->_voice)) { + $voice = $this->_voice; + } + $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer); + } + $this->ask = sprintf('%s', $ask); + } + + /** + * Places a call or sends an an IM, Twitter, or SMS message. To start a call, use the Session API to tell Tropo to launch your code. + * + * @param string|Call $call + * @param array $params + * @see https://www.tropo.com/docs/webapi/call.htm + */ + public function call($call, Array $params=NULL) { + if(!is_object($call)) { + $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); + } + $this->call = sprintf('%s', $call); + } + + /** + * This object allows multiple lines in separate sessions to be conferenced together so that the parties on each line can talk to each other simultaneously. + * This is a voice channel only feature. + * + * @param string|Conference $conference + * @param array $params + * @see https://www.tropo.com/docs/webapi/conference.htm + */ + public function conference($conference, Array $params=NULL) { + if(!is_object($conference)) { + $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $id = (empty($id) && !empty($conference)) ? $conference : $id; + $name = (empty($name)) ? (string)$id : $name; + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); + } + $this->conference = sprintf('%s', $conference); + } + + /** + * This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. + * @see https://www.tropo.com/docs/webapi/hangup.htm + */ + public function hangup() { + $hangup = new Hangup(); + $this->hangup = sprintf('%s', $hangup); + } + + /** + * A shortcut method to create a session, say something, and hang up, all in one step. This is particularly useful for sending out a quick SMS or IM. + * + * @param string|Message $message + * @param array $params + * @see https://www.tropo.com/docs/webapi/message.htm + */ + public function message($message, Array $params=null) { + if(!is_object($message)) { + $say = new Say($message); + $to = $params["to"]; + $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers); + } + $this->message = sprintf('%s', $message); + } + + /** + * Adds an event callback so that your application may be notified when a particular event occurs. + * Possible events are: "continue", "error", "incomplete" and "hangup". + * + * @param array $params + * @see https://www.tropo.com/docs/webapi/on.htm + */ + public function on($on) { + if (!is_object($on) && is_array($on)) { + $params = $on; + $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; + $next = (array_key_exists('next', $params)) ? $params["next"] : null; + $on = new On($params["event"], $next, $say); + } + $this->on = sprintf('%s', $on); + } + + /** + * Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. + * If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below. + * The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set. + * At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form. + * If specified, the audio file may also be transcribed and the text returned to you via an email address or HTTP POST/Multipart Form. + * + * @param array|Record $record + * @see https://www.tropo.com/docs/webapi/record.htm + */ + public function record($record) { + if(!is_object($record) && is_array($record)) { + $params = $record; + $p = array('as', 'voice', 'emailFormat', 'transcription', 'terminator'); + foreach ($p as $option) { + $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; + } + $choices = isset($params["choices"]) + ? new Choices(null, null, $params["choices"]) + : null; + $choices = isset($params["terminator"]) + ? new Choices(null, null, $params["terminator"]) + : $choices; + if (!isset($params['voice'])) { + $params['voice'] = $this->_voice; + } + $say = new Say($params["say"], $params["as"], null, null); + if (is_array($params['transcription'])) { + $p = array('url', 'id', 'emailFormat'); + foreach ($p as $option) { + $$option = null; + if (!is_array($params["transcription"]) || !array_key_exists($option, $params["transcription"])) { + $params["transcription"][$option] = null; + } + } + $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"]); + } else { + $transcription = $params["transcription"]; + } + $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); + } + $this->record = sprintf('%s', $record); + } + + /** + * The redirect function forwards an incoming call to another destination / phone number before answering it. + * The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state. + * Use transfer when working with active answered calls. + * + * @param string|Redirect $redirect + * @param array $params + * @see https://www.tropo.com/docs/webapi/redirect.htm + */ + public function redirect($redirect, Array $params=NULL) { + if(!is_object($redirect)) { + $to = isset($params["to"]) ? $params["to"]: null; + $from = isset($params["from"]) ? $params["from"] : null; + $redirect = new Redirect($to, $from); + } + $this->redirect = sprintf('%s', $redirect); + } + + /** + * Allows Tropo applications to reject incoming sessions before they are answered. + * For example, an application could inspect the callerID variable to determine if the user is known, and then use the reject call accordingly. + * + * @see https://www.tropo.com/docs/webapi/reject.htm + * + */ + public function reject() { + $reject = new Reject(); + $this->reject = sprintf('%s', $reject); + } + + /** + * When the current session is a voice channel this key will either play a message or an audio file from a URL. + * In the case of an text channel it will send the text back to the user via i nstant messaging or SMS. + * + * @param string|Say $say + * @param array $params + * @see https://www.tropo.com/docs/webapi/say.htm + */ + public function say($say, Array $params=NULL) { + if(!is_object($say)) { + $p = array('as', 'format', 'event','voice', 'allowSignals'); + $value = $say; + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $voice = isset($voice) ? $voice : $this->_voice; + $say = new Say($value, $as, $event, $voice, $allowSignals); + } + $this->say = array(sprintf('%s', $say)); + } + + /** + * Allows Tropo applications to begin recording the current session. + * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. + * + * @param array|StartRecording $startRecording + * @see https://www.tropo.com/docs/webapi/startrecording.htm + */ + public function startRecording($startRecording) { + if(!is_object($startRecording) && is_array($startRecording)) { + $params = $startRecording; + $p = array('format', 'method', 'password', 'url', 'username'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $startRecording = new StartRecording($format, $method, $password, $url, $username); + } + $this->startRecording = sprintf('%s', $startRecording); + } + + /** + * Stops a previously started recording. + * + * @see https://www.tropo.com/docs/webapi/stoprecording.htm + */ + public function stopRecording() { + $stopRecording = new stopRecording(); + $this->stopRecording = sprintf('%s', $stopRecording); + } + + /** + * Transfers an already answered call to another destination / phone number. + * Call may be transferred to another phone number or SIP address, which is set through the "to" parameter and is in URL format. + * + * @param string|Transfer $transfer + * @param array $params + * @see https://www.tropo.com/docs/webapi/transfer.htm + */ + public function transfer($transfer, Array $params=NULL) { + if(!is_object($transfer)) { + $choices = isset($params["choices"]) ? $params["choices"] : null; + $choices = isset($params["terminator"]) + ? new Choices(null, null, $params["terminator"]) + : $choices; + $to = isset($params["to"]) ? $params["to"] : $transfer; + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $on = null; + if (array_key_exists('playvalue', $params) && isset($params['playvalue'])) { + $on = new On('ring', null, new Say($params['playvalue'])); + } elseif (array_key_exists('on', $params) && isset($params['on'])) { + if (is_object($params['on'])) { + $on = $params['on']; + } else { + if (strtolower($params['on']['event']) != 'ring') { + throw new TropoException("The only event allowed on transfer is 'ring'"); + } + $on = new On('ring', null, new Say($params['on']['say'])); + } + } + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); + } + $this->transfer = sprintf('%s', $transfer); + } + + /** + * Launches a new session with the Tropo Session API. + * (Pass through to SessionAPI class.) + * + * @param string $token Your outbound session token from Tropo + * @param array $params An array of key value pairs that will be added as query string parameters + * @return bool True if the session was launched successfully + */ + public function createSession($token, Array $params=NULL) { + try { + $session = new SessionAPI(); + $result = $session->createSession($token, $params); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + public function sendEvent($session_id, $value) { + try { + $event = new EventAPI(); + $result = $event->sendEvent($session_id, $value); + return $result; + } + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Creates a new Tropo Application + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param array $params + * @return string JSON + */ + public function createApplication($userid, $password, Array $params) { + $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $password); + $result = $provision->createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Add/Update an address (phone number, IM address or token) for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param array $params + * @return string JSON + */ + public function updateApplicationAddress($userid, $passwd, $applicationID, Array $params) { + $p = array('type', 'prefix', 'number', 'city', 'state', 'channel', 'username', 'password', 'token'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $passwd); + $result = $provision->updateApplicationAddress($applicationID, $type, $prefix, $number, $city, $state, $channel, $username, $password, $token); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Update a property (name, URL, platform, etc.) for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param array $params + * @return string JSON + */ + public function updateApplicationProperty($userid, $password, $applicationID, Array $params) { + $p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition'); + foreach ($p as $property) { + $$property = null; + if (is_array($params) && array_key_exists($property, $params)) { + $$property = $params[$property]; + } + } + try { + $provision = new ProvisioningAPI($userid, $password); + $result = $provision->updateApplicationProperty($applicationID, $href, $name, $voiceUrl, $messagingUrl, $platform, $partition); + return $result; + } + // If an exception occurs, wrap it in a TropoException and rethrow. + catch (Exception $ex) { + throw new TropoException($ex->getMessage(), $ex->getCode()); + } + } + + /** + * Delete an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function deleteApplication($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->deleteApplication($applicationID); + } + + /** + * Delete an address for an existing Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @param string $number + * @return string JSON + */ + public function deleteApplicationAddress($userid, $password, $applicationID, $addresstype, $address) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->deleteApplicationAddress($applicationID, $addresstype, $address); + } + + /** + * View a list of Tropo applications. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @return string JSON + */ + public function viewApplications($userid, $password) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewApplications(); + } + + /** + * View the details of a specific Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function viewSpecificApplication($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewSpecificApplication($applicationID); + } + + /** + * View the addresses for a specific Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @param string $applicationID + * @return string JSON + */ + public function viewAddresses($userid, $password, $applicationID) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewAddresses($applicationID); + } + + /** + * View a list of available exchanges for assigning a number to a Tropo application. + * (Pass through to ProvisioningAPI class). + * + * @param string $userid + * @param string $password + * @return string JSON + */ + public function viewExchanges($userid, $password) { + $provision = new ProvisioningAPI($userid, $password); + return $provision->viewExchanges(); + } + + /** + * Renders the Tropo object as JSON. + * + */ + public function renderJSON() { + header('Content-type: application/json'); + echo $this; + } + + /** + * Allows undefined methods to be called. + * This method is invloked by Tropo class methods to add action items to the Tropo array. + * + * @param string $name + * @param mixed $value + * @access private + */ + public function __set($name, $value) { + array_push($this->tropo, array($name => $value)); + } + + /** + * Controls how JSON structure for the Tropo object is rendered. + * + * @return string + * @access private + */ + public function __toString() { + // Remove voice and language so they do not appear in the rednered JSON. + unset($this->_voice); + unset($this->_language); + + // Call the unescapeJSON() method in the parent class. + return parent::unescapeJSON(json_encode($this)); + } } /** - * Base class for Tropo class and indvidual Tropo action classes. - * Derived classes must implement both a constructor and __toString() function. - * @package TropoPHP_Support - * @abstract BaseClass - */ +* Base class for Tropo class and indvidual Tropo action classes. +* Derived classes must implement both a constructor and __toString() function. +* @package TropoPHP_Support +* @abstract BaseClass +*/ abstract class BaseClass { - /** - * toString Function - * @abstract __toString() - */ - abstract public function __toString(); - - /** - * Allows derived classes to set Undeclared properties. - * - * @param mixed $attribute - * @param mixed $value - */ - public function __set($attribute, $value) { - $this->$attribute= $value; - } - - /** - * Removes escape characters from a JSON string. - * - * @param string $json - * @return string - */ - public function unescapeJSON($json) { - return str_replace(array("\\", "\"{", "}\""), array("", "{", "}"), $json); - } + /** + * toString Function + * @abstract __toString() + */ + abstract public function __toString(); + + /** + * Allows derived classes to set Undeclared properties. + * + * @param mixed $attribute + * @param mixed $value + */ + public function __set($attribute, $value) { + $this->$attribute= $value; + } + + /** + * Removes escape characters from a JSON string. + * + * @param string $json + * @return string + */ + public function unescapeJSON($json) { + return str_replace(array("\\", "\"{", "}\""), array("", "{", "}"), $json); + } } /** - * Base class for empty actions. - * @package TropoPHP_Support - * - */ +* Base class for empty actions. +* @package TropoPHP_Support +* +*/ class EmptyBaseClass { - final public function __toString() { - return json_encode(null); - } + final public function __toString() { + return json_encode(null); + } } /** - * Action classes. Each specific object represents a specific Tropo action. - * - */ +* Action classes. Each specific object represents a specific Tropo action. +* +*/ /** - * Sends a prompt to the user and optionally waits for a response. - * @package TropoPHP_Support - * - */ +* Sends a prompt to the user and optionally waits for a response. +* @package TropoPHP_Support +* +*/ class Ask extends BaseClass { - private $_attempts; - private $_bargein; - private $_choices; - private $_minConfidence; - private $_name; - private $_required; - private $_say; - private $_timeout; - private $_voice; - private $_allowSignals; - private $_recognizer; - - /** - * Class constructor - * - * @param int $attempts - * @param boolean $bargein - * @param Choices $choices - * @param float $minConfidence - * @param string $name - * @param boolean $required - * @param Say $say - * @param int $timeout - * @param string $voice - * @param string|array $allowSignals - */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL) { - $this->_attempts = $attempts; - $this->_bargein = $bargein; - $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; - $this->_minConfidence = $minConfidence; - $this->_name = $name; - $this->_required = $required; - $this->_say = isset($say) ? $say : null; - $this->_timeout = $timeout; - $this->_voice = $voice; - $this->_allowSignals = $allowSignals; - $this->_recognizer = $recognizer; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } - if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } - if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; } - if(isset($this->_name)) { $this->name = $this->_name; } - if(isset($this->_required)) { $this->required = $this->_required; } - if(isset($this->_say)) { $this->say = $this->_say; } - if (is_array($this->_say)) { - foreach ($this->_say as $k => $v) { - $this->_say[$k] = sprintf('%s', $v); - } - } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } - return $this->unescapeJSON(json_encode($this)); - } - - /** - * Adds an additional Say to the Ask - * - * Used to add events such as a prompt to say on timeout or nomatch - * - * @param Say $say A say object - */ - public function addEvent(Say $say) { - $this->_say[] = $say; - } + private $_attempts; + private $_bargein; + private $_choices; + private $_minConfidence; + private $_name; + private $_required; + private $_say; + private $_timeout; + private $_voice; + private $_allowSignals; + private $_recognizer; + + /** + * Class constructor + * + * @param int $attempts + * @param boolean $bargein + * @param Choices $choices + * @param float $minConfidence + * @param string $name + * @param boolean $required + * @param Say $say + * @param int $timeout + * @param string $voice + * @param string|array $allowSignals + */ + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL) { + $this->_attempts = $attempts; + $this->_bargein = $bargein; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; + $this->_minConfidence = $minConfidence; + $this->_name = $name; + $this->_required = $required; + $this->_say = isset($say) ? $say : null; + $this->_timeout = $timeout; + $this->_voice = $voice; + $this->_allowSignals = $allowSignals; + $this->_recognizer = $recognizer; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } + if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } + if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; } + if(isset($this->_name)) { $this->name = $this->_name; } + if(isset($this->_required)) { $this->required = $this->_required; } + if(isset($this->_say)) { $this->say = $this->_say; } + if (is_array($this->_say)) { + foreach ($this->_say as $k => $v) { + $this->_say[$k] = sprintf('%s', $v); + } + } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } + return $this->unescapeJSON(json_encode($this)); + } + + /** + * Adds an additional Say to the Ask + * + * Used to add events such as a prompt to say on timeout or nomatch + * + * @param Say $say A say object + */ + public function addEvent(Say $say) { + $this->_say[] = $say; + } } /** - * This object allows Tropo to make an outbound call. The call can be over voice or one - * of the text channels. - * @package TropoPHP_Support - * - */ +* This object allows Tropo to make an outbound call. The call can be over voice or one +* of the text channels. +* @package TropoPHP_Support +* +*/ class Call extends BaseClass { - private $_to; - private $_from; - private $_network; - private $_channel; - private $_answerOnMedia; - private $_timeout; - private $_headers; - private $_recording; - private $_allowSignals; - - /** - * Class constructor - * - * @param string $to - * @param string $from - * @param string $network - * @param string $channel - * @param boolean $answerOnMedia - * @param int $timeout - * @param array $headers - * @param StartRecording $recording - * @param string|array $allowSignals - */ - public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL) { - $this->_to = $to; - $this->_from = $from; - $this->_network = $network; - $this->_channel = $channel; - $this->_answerOnMedia = $answerOnMedia; - $this->_timeout = $timeout; - $this->_headers = $headers; - $this->_recording = isset($recording) ? sprintf('%s', $recording) : null ; - $this->_allowSignals = $allowSignals; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - $this->to = $this->_to; - if(isset($this->_from)) { $this->from = $this->_from; } - if(isset($this->_network)) { $this->network = $this->_network; } - if(isset($this->_channel)) { $this->channel = $this->_channel; } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(count($this->_headers)) { $this->headers = $this->_headers; } - if(isset($this->_recording)) { $this->recording = $this->_recording; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + private $_to; + private $_from; + private $_network; + private $_channel; + private $_answerOnMedia; + private $_timeout; + private $_headers; + private $_recording; + private $_allowSignals; + + /** + * Class constructor + * + * @param string $to + * @param string $from + * @param string $network + * @param string $channel + * @param boolean $answerOnMedia + * @param int $timeout + * @param array $headers + * @param StartRecording $recording + * @param string|array $allowSignals + */ + public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL) { + $this->_to = $to; + $this->_from = $from; + $this->_network = $network; + $this->_channel = $channel; + $this->_answerOnMedia = $answerOnMedia; + $this->_timeout = $timeout; + $this->_headers = $headers; + $this->_recording = isset($recording) ? sprintf('%s', $recording) : null ; + $this->_allowSignals = $allowSignals; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->to = $this->_to; + if(isset($this->_from)) { $this->from = $this->_from; } + if(isset($this->_network)) { $this->network = $this->_network; } + if(isset($this->_channel)) { $this->channel = $this->_channel; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } + if(count($this->_headers)) { $this->headers = $this->_headers; } + if(isset($this->_recording)) { $this->recording = $this->_recording; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Defines the input to be collected from the user. - * @package TropoPHP_Support - */ +* Defines the input to be collected from the user. +* @package TropoPHP_Support +*/ class Choices extends BaseClass { - private $_value; - private $_mode; - private $_terminator; - - /** - * Class constructor - * - * @param string $value - * @param string $mode - * @param string $terminator - */ - public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { - $this->_value = $value; - $this->_mode = $mode; - $this->_terminator = $terminator; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_value)){ $this->value = $this->_value; } - if(isset($this->_mode)) { $this->mode = $this->_mode; } - if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } - return $this->unescapeJSON(json_encode($this)); - } + private $_value; + private $_mode; + private $_terminator; + + /** + * Class constructor + * + * @param string $value + * @param string $mode + * @param string $terminator + */ + public function __construct($value=NULL, $mode=NULL, $terminator=NULL) { + $this->_value = $value; + $this->_mode = $mode; + $this->_terminator = $terminator; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_value)){ $this->value = $this->_value; } + if(isset($this->_mode)) { $this->mode = $this->_mode; } + if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * This object allows multiple lines in separate sessions to be conferenced together so that - * the parties on each line can talk to each other simultaneously. - * This is a voice channel only feature. - * - * TODO: Conference object should support multiple event handlers (e.g. join and leave). - * @package TropoPHP_Support - * - */ +* This object allows multiple lines in separate sessions to be conferenced together so that +* the parties on each line can talk to each other simultaneously. +* This is a voice channel only feature. +* +* TODO: Conference object should support multiple event handlers (e.g. join and leave). +* @package TropoPHP_Support +* +*/ class Conference extends BaseClass { - private $_id; - private $_mute; - private $_name; - private $_on; - private $_playTones; - private $_required; - private $_terminator; - private $_allowSignals; - - - /** - * Class constructor - * - * @param int $id - * @param boolean $mute - * @param string $name - * @param On $on - * @param boolean $playTones - * @param boolean $required - * @param string $terminator - * @param string|array $allowSignals - */ - public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { - $this->_name = $name; - $this->_id = (string) $id; - $this->_mute = $mute; - $this->_on = isset($on) ? sprintf('%s', $on) : null; - $this->_playTones = $playTones; - $this->_required = $required; - $this->_terminator = $terminator; - $this->_allowSignals = $allowSignals; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - $this->name = $this->_name; - if(isset($this->_id)) { $this->id = $this->_id; } - if(isset($this->_mute)) { $this->mute = $this->_mute; } - if(isset($this->_on)) { $this->on = $this->_on; } - if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } - if(isset($this->_required)) { $this->required = $this->_required; } - if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + private $_id; + private $_mute; + private $_name; + private $_on; + private $_playTones; + private $_required; + private $_terminator; + private $_allowSignals; + + + /** + * Class constructor + * + * @param int $id + * @param boolean $mute + * @param string $name + * @param On $on + * @param boolean $playTones + * @param boolean $required + * @param string $terminator + * @param string|array $allowSignals + */ + public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { + $this->_name = $name; + $this->_id = (string) $id; + $this->_mute = $mute; + $this->_on = isset($on) ? sprintf('%s', $on) : null; + $this->_playTones = $playTones; + $this->_required = $required; + $this->_terminator = $terminator; + $this->_allowSignals = $allowSignals; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->name = $this->_name; + if(isset($this->_id)) { $this->id = $this->_id; } + if(isset($this->_mute)) { $this->mute = $this->_mute; } + if(isset($this->_on)) { $this->on = $this->_on; } + if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } + if(isset($this->_required)) { $this->required = $this->_required; } + if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. - * @package TropoPHP_Support - * - */ +* This function instructs Tropo to "hang-up" or disconnect the session associated with the current session. +* @package TropoPHP_Support +* +*/ class Hangup extends EmptyBaseClass { } /** - * This function instructs Tropo to send a message. - * @package TropoPHP_Support - * - */ +* This function instructs Tropo to send a message. +* @package TropoPHP_Support +* +*/ class Message extends BaseClass { - private $_say; - private $_to; - private $_channel; - private $_network; - private $_from; - private $_voice; - private $_timeout; - private $_answerOnMedia; - private $_headers; - - /** - * Class constructor - * - * @param Say $say - * @param string $to - * @param string $channel - * @param string $network - * @param string $from - * @param string $voice - * @param integer $timeout - * @param boolean $answerOnMedia - * @param array $headers - */ - public function __construct(Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null) { - $this->_say = isset($say) ? sprintf('%s', $say) : null ; - $this->_to = $to; - $this->_channel = $channel; - $this->_network = $network; - $this->_from = $from; - $this->_voice = $voice; - $this->_timeout = $timeout; - $this->_answerOnMedia = $answerOnMedia; - $this->_headers = $headers; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - $this->say = $this->_say; - $this->to = $this->_to; - if(isset($this->_channel)) { $this->channel = $this->_channel; } - if(isset($this->_network)) { $this->network = $this->_network; } - if(isset($this->_from)) { $this->from = $this->_from; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(count($this->_headers)) { $this->headers = $this->_headers; } - return $this->unescapeJSON(json_encode($this)); - } + private $_say; + private $_to; + private $_channel; + private $_network; + private $_from; + private $_voice; + private $_timeout; + private $_answerOnMedia; + private $_headers; + + /** + * Class constructor + * + * @param Say $say + * @param string $to + * @param string $channel + * @param string $network + * @param string $from + * @param string $voice + * @param integer $timeout + * @param boolean $answerOnMedia + * @param array $headers + */ + public function __construct(Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null) { + $this->_say = isset($say) ? sprintf('%s', $say) : null ; + $this->_to = $to; + $this->_channel = $channel; + $this->_network = $network; + $this->_from = $from; + $this->_voice = $voice; + $this->_timeout = $timeout; + $this->_answerOnMedia = $answerOnMedia; + $this->_headers = $headers; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->say = $this->_say; + $this->to = $this->_to; + if(isset($this->_channel)) { $this->channel = $this->_channel; } + if(isset($this->_network)) { $this->network = $this->_network; } + if(isset($this->_from)) { $this->from = $this->_from; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } + if(count($this->_headers)) { $this->headers = $this->_headers; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Adds an event callback so that your application may be notified when a particular event occurs. - * @package TropoPHP_Support - * - */ +* Adds an event callback so that your application may be notified when a particular event occurs. +* @package TropoPHP_Support +* +*/ class On extends BaseClass { - private $_event; - private $_next; - private $_say; - - /** - * Class constructor - * - * @param string $event - * @param string $next - * @param Say $say - */ - public function __construct($event=NULL, $next=NULL, Say $say=NULL) { - $this->_event = $event; - $this->_next = $next; - $this->_say = isset($say) ? sprintf('%s', $say) : null ; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_event)) { $this->event = $this->_event; } - if(isset($this->_next)) { $this->next = $this->_next; } - if(isset($this->_say)) { $this->say = $this->_say; } - return $this->unescapeJSON(json_encode($this)); - } + private $_event; + private $_next; + private $_say; + + /** + * Class constructor + * + * @param string $event + * @param string $next + * @param Say $say + */ + public function __construct($event=NULL, $next=NULL, Say $say=NULL) { + $this->_event = $event; + $this->_next = $next; + $this->_say = isset($say) ? sprintf('%s', $say) : null ; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_event)) { $this->event = $this->_event; } + if(isset($this->_next)) { $this->next = $this->_next; } + if(isset($this->_say)) { $this->say = $this->_say; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. - * @package TropoPHP_Support - * - */ +* Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. +* @package TropoPHP_Support +* +*/ class Record extends BaseClass { - private $_attempts; - private $_allowSignals; - private $_bargein; - private $_beep; - private $_choices; - private $_format; - private $_maxSilence; - private $_maxTime; - private $_method; - private $_password; - private $_required; - private $_say; - private $_timeout; - private $_transcription; - private $_username; - private $_url; - private $_voice; - - - /** - * Class constructor - * - * @param int $attempts - * @param string|array $allowSignals - * @param boolean $bargein - * @param boolean $beep - * @param Choices $choices - * @param string $format - * @param int $maxSilence - * @param string $method - * @param string $password - * @param boolean $required - * @param Say $say - * @param int $timeout - * @param string $username - * @param string $url - * @param string $voice - */ - public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL) { - $this->_attempts = $attempts; - $this->_allowSignals = $allowSignals; - $this->_bargein = $bargein; - $this->_beep = $beep; - $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; - $this->_format = $format; - $this->_maxSilence = $maxSilence; - $this->_maxTime = $maxTime; - $this->_method = $method; - $this->_password = $password; - if (!is_object($say)) { - $say = new Say($say); - } - $this->_say = isset($say) ? sprintf('%s', $say) : null; - $this->_timeout = $timeout; - $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; - $this->_username = $username; - $this->_url = $url; - $this->_voice = $voice; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } - if(isset($this->_beep)) { $this->beep = $this->_beep; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } - if(isset($this->_format)) { $this->format = $this->_format; } - if(isset($this->_maxSilence)) { $this->maxSilence = $this->_maxSilence; } - if(isset($this->_maxTime)) { $this->maxTime = $this->_maxTime; } - if(isset($this->_method)) { $this->method = $this->_method; } - if(isset($this->_password)) { $this->password = $this->_password; } - if(isset($this->_say)) { $this->say = $this->_say; } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } - if(isset($this->_username)) { $this->username = $this->_username; } - if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - return $this->unescapeJSON(json_encode($this)); - } + private $_attempts; + private $_allowSignals; + private $_bargein; + private $_beep; + private $_choices; + private $_format; + private $_maxSilence; + private $_maxTime; + private $_method; + private $_password; + private $_required; + private $_say; + private $_timeout; + private $_transcription; + private $_username; + private $_url; + private $_voice; + + + /** + * Class constructor + * + * @param int $attempts + * @param string|array $allowSignals + * @param boolean $bargein + * @param boolean $beep + * @param Choices $choices + * @param string $format + * @param int $maxSilence + * @param string $method + * @param string $password + * @param boolean $required + * @param Say $say + * @param int $timeout + * @param string $username + * @param string $url + * @param string $voice + */ + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL) { + $this->_attempts = $attempts; + $this->_allowSignals = $allowSignals; + $this->_bargein = $bargein; + $this->_beep = $beep; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; + $this->_format = $format; + $this->_maxSilence = $maxSilence; + $this->_maxTime = $maxTime; + $this->_method = $method; + $this->_password = $password; + if (!is_object($say)) { + $say = new Say($say); + } + $this->_say = isset($say) ? sprintf('%s', $say) : null; + $this->_timeout = $timeout; + $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; + $this->_username = $username; + $this->_url = $url; + $this->_voice = $voice; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } + if(isset($this->_beep)) { $this->beep = $this->_beep; } + if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_format)) { $this->format = $this->_format; } + if(isset($this->_maxSilence)) { $this->maxSilence = $this->_maxSilence; } + if(isset($this->_maxTime)) { $this->maxTime = $this->_maxTime; } + if(isset($this->_method)) { $this->method = $this->_method; } + if(isset($this->_password)) { $this->password = $this->_password; } + if(isset($this->_say)) { $this->say = $this->_say; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } + if(isset($this->_username)) { $this->username = $this->_username; } + if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * The redirect function forwards an incoming call to another destination / phone number before answering it. - * @package TropoPHP_Support - * - */ +* The redirect function forwards an incoming call to another destination / phone number before answering it. +* @package TropoPHP_Support +* +*/ class Redirect extends BaseClass { - private $_to; - private $_from; - - /** - * Class constructor - * - * @param Endpoint $to - * @param Endpoint $from - */ - public function __construct($to=NULL, $from=NULL) { - $this->_to = sprintf('%s', $to); - $this->_from = isset($from) ? sprintf('%s', $from) : null; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - $this->to = $this->_to; - if(isset($this->_from)) { $this->from = $this->_from; } - return $this->unescapeJSON(json_encode($this)); - } + private $_to; + private $_from; + + /** + * Class constructor + * + * @param Endpoint $to + * @param Endpoint $from + */ + public function __construct($to=NULL, $from=NULL) { + $this->_to = sprintf('%s', $to); + $this->_from = isset($from) ? sprintf('%s', $from) : null; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->to = $this->_to; + if(isset($this->_from)) { $this->from = $this->_from; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Allows Tropo applications to reject incoming sessions before they are answered. - * @package TropoPHP_Support - * - */ +* Allows Tropo applications to reject incoming sessions before they are answered. +* @package TropoPHP_Support +* +*/ class Reject extends EmptyBaseClass { } /** - * Returned anytime a request is made to the Tropo Web API. - * @package TropoPHP - * - */ +* Returned anytime a request is made to the Tropo Web API. +* @package TropoPHP +* +*/ class Result { - private $_sessionId; - private $_callId; - private $_state; - private $_sessionDuration; - private $_sequence; - private $_complete; - private $_error; - private $_actions; - private $_name; - private $_attempts; - private $_disposition; - private $_confidence; - private $_interpretation; - private $_concept; - private $_utterance; - private $_value; - private $_transcription; - - /** - * Class constructor - * - * @param string $json - */ - public function __construct($json=NULL) { - if(empty($json)) { - $json = file_get_contents("php://input"); - // if $json is still empty, there was nothing in - // the POST so throw an exception - if(empty($json)) { - throw new TropoException('No JSON available.'); - } - } - $result = json_decode($json); - if (!is_object($result) || !property_exists($result, "result")) { - throw new TropoException('Not a result object.'); - } - $this->_sessionId = $result->result->sessionId; - $this->_callId = $result->result->callId; - $this->_state = $result->result->state; - $this->_sessionDuration = $result->result->sessionDuration; - $this->_sequence = $result->result->sequence; - $this->_complete = $result->result->complete; - $this->_error = $result->result->error; - $this->_actions = $result->result->actions; - $this->_name = $result->result->actions->name; - $this->_attempts = $result->result->actions->attempts; - $this->_disposition = $result->result->actions->disposition; - $this->_confidence = $result->result->actions->confidence; - $this->_interpretation = $result->result->actions->interpretation; - $this->_utterance = $result->result->actions->utterance; - $this->_value = $result->result->actions->value; - $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; - $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; - } - - public function getSessionId() { - return $this->_sessionId; - } - - public function getCallId() { - return $this->_callId; - } - - public function getState() { - return $this->_state; - } - - public function getSessionDuration() { - return $this->_sessionDuration; - } - - public function getSequence() { - return $this->_sequence; - } - - public function isComplete() { - return (bool) $this->_complete; - } - - public function getError() { - return $this->_error; - } - - public function getActions() { - return $this->_actions; - } - - public function getName() { - return $this->_name; - } - - public function getAttempts() { - return $this->_attempts; - } - - public function getDisposition() { - return $this->_disposition; - } - - public function getConfidence() { - return $this->_confidence; - } - - public function getInterpretation() { - return $this->_interpretation; - } - - public function getConcept() { - return $this->_concept; - } - - public function getUtterance() { - return $this->_utterance; - } - - public function getValue() { - return $this->_value; - } - - public function getTranscription() { - return $this->_transcription; + private $_sessionId; + private $_callId; + private $_state; + private $_sessionDuration; + private $_sequence; + private $_complete; + private $_error; + private $_actions; + private $_name; + private $_attempts; + private $_disposition; + private $_confidence; + private $_interpretation; + private $_concept; + private $_utterance; + private $_value; + private $_transcription; + + /** + * Class constructor + * + * @param string $json + */ + public function __construct($json=NULL) { + if(empty($json)) { + $json = file_get_contents("php://input"); + // if $json is still empty, there was nothing in + // the POST so throw an exception + if(empty($json)) { + throw new TropoException('No JSON available.'); + } + } + $result = json_decode($json); + if (!is_object($result) || !property_exists($result, "result")) { + throw new TropoException('Not a result object.'); + } + $this->_sessionId = $result->result->sessionId; + $this->_callId = $result->result->callId; + $this->_state = $result->result->state; + $this->_sessionDuration = $result->result->sessionDuration; + $this->_sequence = $result->result->sequence; + $this->_complete = $result->result->complete; + $this->_error = $result->result->error; + $this->_actions = $result->result->actions; + $this->_name = $result->result->actions->name; + $this->_attempts = $result->result->actions->attempts; + $this->_disposition = $result->result->actions->disposition; + $this->_confidence = $result->result->actions->confidence; + $this->_interpretation = $result->result->actions->interpretation; + $this->_utterance = $result->result->actions->utterance; + $this->_value = $result->result->actions->value; + $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; + $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; + } + + public function getSessionId() { + return $this->_sessionId; + } + + public function getCallId() { + return $this->_callId; + } + + public function getState() { + return $this->_state; + } + + public function getSessionDuration() { + return $this->_sessionDuration; + } + + public function getSequence() { + return $this->_sequence; + } + + public function isComplete() { + return (bool) $this->_complete; + } + + public function getError() { + return $this->_error; + } + + public function getActions() { + return $this->_actions; + } + + public function getName() { + return $this->_name; + } + + public function getAttempts() { + return $this->_attempts; + } + + public function getDisposition() { + return $this->_disposition; + } + + public function getConfidence() { + return $this->_confidence; + } + + public function getInterpretation() { + return $this->_interpretation; + } + + public function getConcept() { + return $this->_concept; + } + + public function getUtterance() { + return $this->_utterance; + } + + public function getValue() { + return $this->_value; + } + + public function getTranscription() { + return $this->_transcription; } } /** - * When the current session is a voice channel this key will either play a message or an audio file from a URL. - * In the case of an text channel it will send the text back to the user via instant messaging or SMS. - * @package TropoPHP_Support - * - */ +* When the current session is a voice channel this key will either play a message or an audio file from a URL. +* In the case of an text channel it will send the text back to the user via instant messaging or SMS. +* @package TropoPHP_Support +* +*/ class Say extends BaseClass { - private $_value; - private $_as; - private $_event; - private $_format; - private $_voice; - private $_allowSignals; - - /** - * Class constructor - * - * @param string $value - * @param SayAs $as - * @param string $event - * @param string $voice - * @param string|array $allowSignals - */ - public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL) { - $this->_value = $value; - $this->_as = $as; - $this->_event = $event; - $this->_voice = $voice; - $this->_allowSignals = $allowSignals; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_event)) { $this->event = $this->_event; } - $this->value = str_replace('%', '%%', $this->_value); - if(isset($this->_as)) { $this->as = $this->_as; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); - } + private $_value; + private $_as; + private $_event; + private $_format; + private $_voice; + private $_allowSignals; + + /** + * Class constructor + * + * @param string $value + * @param SayAs $as + * @param string $event + * @param string $voice + * @param string|array $allowSignals + */ + public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL) { + $this->_value = $value; + $this->_as = $as; + $this->_event = $event; + $this->_voice = $voice; + $this->_allowSignals = $allowSignals; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_event)) { $this->event = $this->_event; } + $this->value = str_replace('%', '%%', $this->_value); + if(isset($this->_as)) { $this->as = $this->_as; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * The payload sent as an HTTP POST to the web application when a new session arrives. - * - * TODO: Consider using associative array for To and From. - * TODO: Need to break out headers into a more accessible data structure. - * @package TropoPHP - */ +* The payload sent as an HTTP POST to the web application when a new session arrives. +* +* TODO: Consider using associative array for To and From. +* TODO: Need to break out headers into a more accessible data structure. +* @package TropoPHP +*/ class Session { - private $_id; - private $_accountId; - private $_callId; - private $_timestamp; - private $_userType; - private $_initialText; - private $_to; - private $_from; - private $_headers; - private $_parameters; - - /** - * Class constructor - * - * @param string $json - */ - public function __construct($json=NULL) { - if(empty($json)) { - $json = file_get_contents("php://input"); - // if $json is still empty, there was nothing in - // the POST so throw exception - if(empty($json)) { - throw new TropoException('No JSON available.', 1); - } - } - $session = json_decode($json); - if (!is_object($session) || !property_exists($session, "session")) { - throw new TropoException('Not a session object.', 2); - } - $this->_id = $session->session->id; - $this->_accountId = $session->session->accountId; - $this->_callId = $session->session->callId; - $this->_timestamp = $session->session->timestamp; - $this->_userType = $session->session->userType; - $this->_initialText = $session->session->initialText; - $this->_to = isset($session->session->to) - ? array( - "id" => $session->session->to->id, - "channel" => $session->session->to->channel, - "name" => $session->session->to->name, - "network" => $session->session->to->network - ) - : array( - "id" => null, - "channel" => null, - "name" => null, - "network" => null - ); - $this->_from = isset($session->session->from->id) - ? array( - "id" => $session->session->from->id, - "channel" => $session->session->from->channel, - "name" => $session->session->from->name, - "network" => $session->session->from->network - ) - : array( - "id" => null, - "channel" => null, - "name" => null, - "network" => null - ); - - $this->_headers = isset($session->session->headers) - ? self::setHeaders($session->session->headers) - : array(); - $this->_parameters = property_exists($session->session, 'parameters') ? (Array) $session->session->parameters : null; - } - - public function getId() { - return $this->_id; - } - - public function getAccountID() { - return $this->_accountId; - } - - public function getCallId() { - return $this->_callId; - } - - public function getTimeStamp() { - return $this->_timestamp; - } - public function getUserType() { - return $this->_userType; - } - - public function getInitialText() { - return $this->_initialText; - } - - public function getTo() { - return $this->_to; - } - - public function getFrom() { - return $this->_from; - } - - function getFromChannel() { - return $this->_from['channel']; - } - - function getFromNetwork() { - return $this->_from['network']; - } - - public function getHeaders() { - return $this->_headers; - } - - /** - * Returns the query string parameters for the session api - * - * If an argument is provided, a string containing the value of a - * query string variable matching that string is returned or null - * if there is no match. If no argument is argument is provided, - * an array is returned with all query string variables or an empty - * array if there are no query string variables. - * - * @param string $name A specific parameter to return - * @return string|array $param - */ - public function getParameters($name = null) { - if (isset($name)) { - if (!is_array($this->_parameters)) { - // We've asked for a specific param, not there's no params set - // return a null. - return null; - } - if (isset($this->_parameters[$name])) { - return $this->_parameters[$name]; - } else { - return null; - } - } else { - // If the parameters field doesn't exist or isn't an array - // then return an empty array() - if (!is_array($this->_parameters)) { - return array(); - } - return $this->_parameters; - } - } - - public function setHeaders($headers) { - $formattedHeaders = new Headers(); - // headers don't exist on outboud calls - // so only do this if there are headers - if (is_object($headers)) { - foreach($headers as $name => $value) { - $formattedHeaders->$name = $value; - } - } - return $formattedHeaders; - } -} + private $_id; + private $_accountId; + private $_callId; + private $_timestamp; + private $_userType; + private $_initialText; + private $_to; + private $_from; + private $_headers; + private $_parameters; + + /** + * Class constructor + * + * @param string $json + */ + public function __construct($json=NULL) { + if(empty($json)) { + $json = file_get_contents("php://input"); + // if $json is still empty, there was nothing in + // the POST so throw exception + if(empty($json)) { + throw new TropoException('No JSON available.', 1); + } + } + $session = json_decode($json); + if (!is_object($session) || !property_exists($session, "session")) { + throw new TropoException('Not a session object.', 2); + } + $this->_id = $session->session->id; + $this->_accountId = $session->session->accountId; + $this->_callId = $session->session->callId; + $this->_timestamp = $session->session->timestamp; + $this->_userType = $session->session->userType; + $this->_initialText = $session->session->initialText; + $this->_to = isset($session->session->to) + ? array( + "id" => $session->session->to->id, + "channel" => $session->session->to->channel, + "name" => $session->session->to->name, + "network" => $session->session->to->network + ) + : array( + "id" => null, + "channel" => null, + "name" => null, + "network" => null + ); + $this->_from = isset($session->session->from->id) + ? array( + "id" => $session->session->from->id, + "channel" => $session->session->from->channel, + "name" => $session->session->from->name, + "network" => $session->session->from->network + ) + : array( + "id" => null, + "channel" => null, + "name" => null, + "network" => null + ); + + $this->_headers = isset($session->session->headers) + ? self::setHeaders($session->session->headers) + : array(); + $this->_parameters = property_exists($session->session, 'parameters') ? (Array) $session->session->parameters : null; + } + + public function getId() { + return $this->_id; + } + + public function getAccountID() { + return $this->_accountId; + } + + public function getCallId() { + return $this->_callId; + } + + public function getTimeStamp() { + return $this->_timestamp; + } + public function getUserType() { + return $this->_userType; + } + + public function getInitialText() { + return $this->_initialText; + } + + public function getTo() { + return $this->_to; + } + + public function getFrom() { + return $this->_from; + } + + function getFromChannel() { + return $this->_from['channel']; + } + + function getFromNetwork() { + return $this->_from['network']; + } + + public function getHeaders() { + return $this->_headers; + } + + /** + * Returns the query string parameters for the session api + * + * If an argument is provided, a string containing the value of a + * query string variable matching that string is returned or null + * if there is no match. If no argument is argument is provided, + * an array is returned with all query string variables or an empty + * array if there are no query string variables. + * + * @param string $name A specific parameter to return + * @return string|array $param + */ + public function getParameters($name = null) { + if (isset($name)) { + if (!is_array($this->_parameters)) { + // We've asked for a specific param, not there's no params set + // return a null. + return null; + } + if (isset($this->_parameters[$name])) { + return $this->_parameters[$name]; + } else { + return null; + } + } else { + // If the parameters field doesn't exist or isn't an array + // then return an empty array() + if (!is_array($this->_parameters)) { + return array(); + } + return $this->_parameters; + } + } + + public function setHeaders($headers) { + $formattedHeaders = new Headers(); + // headers don't exist on outboud calls + // so only do this if there are headers + if (is_object($headers)) { + foreach($headers as $name => $value) { + $formattedHeaders->$name = $value; + } + } + return $formattedHeaders; + } + } /** - * Allows Tropo applications to begin recording the current session. - * The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. - * @package TropoPHP_Support - * - */ +* Allows Tropo applications to begin recording the current session. +* The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form. +* @package TropoPHP_Support +* +*/ class StartRecording extends BaseClass { - private $_name; - private $_format; - private $_method; - private $_password; - private $_url; - private $_username; - - /** - * Class constructor - * - * @param string $name - * @param string $format - * @param string $method - * @param string $password - * @param string $url - * @param string $username - */ - public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL) { - $this->_format = $format; - $this->_method = $method; - $this->_password = $password; - $this->_url = $url; - $this->_username = $username; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_format)) { $this->format = $this->_format; } - if(isset($this->_method)) { $this->method = $this->_method; } - if(isset($this->_password)) { $this->password = $this->_password; } - if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_username)) { $this->username = $this->_username; } - return $this->unescapeJSON(json_encode($this)); - } + private $_name; + private $_format; + private $_method; + private $_password; + private $_url; + private $_username; + + /** + * Class constructor + * + * @param string $name + * @param string $format + * @param string $method + * @param string $password + * @param string $url + * @param string $username + */ + public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL) { + $this->_format = $format; + $this->_method = $method; + $this->_password = $password; + $this->_url = $url; + $this->_username = $username; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_format)) { $this->format = $this->_format; } + if(isset($this->_method)) { $this->method = $this->_method; } + if(isset($this->_password)) { $this->password = $this->_password; } + if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_username)) { $this->username = $this->_username; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Stop an already started recording. - * @package TropoPHP_Support - * - */ +* Stop an already started recording. +* @package TropoPHP_Support +* +*/ class StopRecording extends EmptyBaseClass { } /** - * Transcribes spoken text. - * @package TropoPHP_Support - * - */ +* Transcribes spoken text. +* @package TropoPHP_Support +* +*/ class Transcription extends BaseClass { - private $_url; - private $_id; - private $_emailFormat; - - /** - * Class constructor - * - * @param string $url - * @param string $id - * @param string $emailFormat - */ - public function __construct($url, $id=NULL, $emailFormat=NULL) { - $this->_url = $url; - $this->_id = $id; - $this->_emailFormat = $emailFormat; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - if(isset($this->_id)) { $this->id = $this->_id; } - if(isset($this->_url)) { $this->url = $this->_url; } - if(isset($this->_emailFormat)) { $this->emailFormat = $this->_emailFormat; } - return $this->unescapeJSON(json_encode($this)); - } + private $_url; + private $_id; + private $_emailFormat; + + /** + * Class constructor + * + * @param string $url + * @param string $id + * @param string $emailFormat + */ + public function __construct($url, $id=NULL, $emailFormat=NULL) { + $this->_url = $url; + $this->_id = $id; + $this->_emailFormat = $emailFormat; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_id)) { $this->id = $this->_id; } + if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_emailFormat)) { $this->emailFormat = $this->_emailFormat; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Transfers an already answered call to another destination / phone number. - * @package TropoPHP_Support - * - */ +* Transfers an already answered call to another destination / phone number. +* @package TropoPHP_Support +* +*/ class Transfer extends BaseClass { - private $_answerOnMedia; - private $_choices; - private $_from; - private $_on; - private $_ringRepeat; - private $_timeout; - private $_to; - private $_allowSignals; - private $_headers; - - /** - * Class constructor - * - * @param string $to - * @param boolean $answerOnMedia - * @param Choices $choices - * @param Endpoint $from - * @param On $on - * @param int $ringRepeat - * @param int $timeout - * @param string|array $allowSignals - * @param array $headers - */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL) { - $this->_to = $to; - $this->_answerOnMedia = $answerOnMedia; - $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; - $this->_from = $from; - $this->_ringRepeat = $ringRepeat; - $this->_timeout = $timeout; - $this->_on = isset($on) ? sprintf('%s', $on) : null; - $this->_allowSignals = $allowSignals; - $this->_headers = $headers; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - $this->to = $this->_to; - if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } - if(isset($this->_from)) { $this->from = $this->_from; } - if(isset($this->_ringRepeat)) { $this->ringRepeat = $this->_ringRepeat; } - if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_on)) { $this->on = $this->_on; } - if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - if(count($this->_headers)) { $this->headers = $this->_headers; } - return $this->unescapeJSON(json_encode($this)); - } + private $_answerOnMedia; + private $_choices; + private $_from; + private $_on; + private $_ringRepeat; + private $_timeout; + private $_to; + private $_allowSignals; + private $_headers; + + /** + * Class constructor + * + * @param string $to + * @param boolean $answerOnMedia + * @param Choices $choices + * @param Endpoint $from + * @param On $on + * @param int $ringRepeat + * @param int $timeout + * @param string|array $allowSignals + * @param array $headers + */ + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL) { + $this->_to = $to; + $this->_answerOnMedia = $answerOnMedia; + $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; + $this->_from = $from; + $this->_ringRepeat = $ringRepeat; + $this->_timeout = $timeout; + $this->_on = isset($on) ? sprintf('%s', $on) : null; + $this->_allowSignals = $allowSignals; + $this->_headers = $headers; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->to = $this->_to; + if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } + if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_from)) { $this->from = $this->_from; } + if(isset($this->_ringRepeat)) { $this->ringRepeat = $this->_ringRepeat; } + if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } + if(isset($this->_on)) { $this->on = $this->_on; } + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(count($this->_headers)) { $this->headers = $this->_headers; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * Defnies an endoint for transfer and redirects. - * @package TropoPHP_Support - * - */ +* Defnies an endoint for transfer and redirects. +* @package TropoPHP_Support +* +*/ class Endpoint extends BaseClass { - private $_id; - private $_channel; - private $_name = 'unknown'; - private $_network; - - /** - * Class constructor - * - * @param string $id - * @param string $channel - * @param string $name - * @param string $network - */ - public function __construct($id, $channel=NULL, $name=NULL, $network=NULL) { - - $this->_id = $id; - $this->_channel = $channel; - $this->_name = $name; - $this->_network = $network; - } - - /** - * Renders object in JSON format. - * - */ - public function __toString() { - - if(isset($this->_id)) { $this->id = $this->_id; } - if(isset($this->_channel)) { $this->channel = $this->_channel; } - if(isset($this->_name)) { $this->name = $this->_name; } - if(isset($this->_network)) { $this->network = $this->_network; } - return $this->unescapeJSON(json_encode($this)); - } + private $_id; + private $_channel; + private $_name = 'unknown'; + private $_network; + + /** + * Class constructor + * + * @param string $id + * @param string $channel + * @param string $name + * @param string $network + */ + public function __construct($id, $channel=NULL, $name=NULL, $network=NULL) { + + $this->_id = $id; + $this->_channel = $channel; + $this->_name = $name; + $this->_network = $network; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + + if(isset($this->_id)) { $this->id = $this->_id; } + if(isset($this->_channel)) { $this->channel = $this->_channel; } + if(isset($this->_name)) { $this->name = $this->_name; } + if(isset($this->_network)) { $this->network = $this->_network; } + return $this->unescapeJSON(json_encode($this)); + } } /** - * A helper class for wrapping exceptions. Can be modified for custom excpetion handling. - * - */ +* A helper class for wrapping exceptions. Can be modified for custom excpetion handling. +* +*/ class TropoException extends Exception { } /** - * Date Helper class. - * @package TropoPHP_Support - */ +* Date Helper class. +* @package TropoPHP_Support +*/ class Date { - public static $monthDayYear = "mdy"; - public static $dayMonthYear = "dmy"; - public static $yearMonthDay = "ymd"; - public static $yearMonth = "ym"; - public static $monthYear = "my"; - public static $monthDay = "md"; - public static $year = "y"; - public static $month = "m"; - public static $day = "d"; + public static $monthDayYear = "mdy"; + public static $dayMonthYear = "dmy"; + public static $yearMonthDay = "ymd"; + public static $yearMonth = "ym"; + public static $monthYear = "my"; + public static $monthDay = "md"; + public static $year = "y"; + public static $month = "m"; + public static $day = "d"; } /** - * Duration Helper class. - * @package TropoPHP_Support - */ +* Duration Helper class. +* @package TropoPHP_Support +*/ class Duration { - public static $hoursMinutesSeconds = "hms"; - public static $hoursMinutes = "hm"; - public static $hours = "h"; - public static $minutes = "m"; - public static $seconds = "s"; + public static $hoursMinutesSeconds = "hms"; + public static $hoursMinutes = "hm"; + public static $hours = "h"; + public static $minutes = "m"; + public static $seconds = "s"; } /** - * Event Helper class. - * @package TropoPHP_Support - */ +* Event Helper class. +* @package TropoPHP_Support +*/ class Event { - public static $continue = 'continue'; - public static $incomplete = 'incomplete'; - public static $error = 'error'; - public static $hangup = 'hangup'; - public static $join = 'join'; - public static $leave = 'leave'; - public static $ring = 'ring'; + public static $continue = 'continue'; + public static $incomplete = 'incomplete'; + public static $error = 'error'; + public static $hangup = 'hangup'; + public static $join = 'join'; + public static $leave = 'leave'; + public static $ring = 'ring'; } /** - * Format Helper class. - * @package TropoPHP_Support - */ +* Format Helper class. +* @package TropoPHP_Support +*/ class Format { - public $date; - public $duration; - public static $ordinal = "ordinal"; - public static $digits = "digits"; - - public function __construct($date=NULL, $duration=NULL) { - $this->date = $date; - $this->duration = $duration; - } + public $date; + public $duration; + public static $ordinal = "ordinal"; + public static $digits = "digits"; + + public function __construct($date=NULL, $duration=NULL) { + $this->date = $date; + $this->duration = $duration; + } } /** - * SayAs Helper class. - * @package TropoPHP_Support - */ +* SayAs Helper class. +* @package TropoPHP_Support +*/ class SayAs { - public static $date = "DATE"; - public static $digits = "DIGITS"; - public static $number = "NUMBER"; + public static $date = "DATE"; + public static $digits = "DIGITS"; + public static $number = "NUMBER"; } /** - * Network Helper class. - * @package TropoPHP_Support - */ +* Network Helper class. +* @package TropoPHP_Support +*/ class Network { - public static $pstn = "PSTN"; - public static $voip = "VOIP"; - public static $aim = "AIM"; - public static $gtalk = "GTALK"; - public static $jabber = "JABBER"; - public static $msn = "MSN"; - public static $sms = "SMS"; - public static $yahoo = "YAHOO"; - public static $twitter = "TWITTER"; + public static $pstn = "PSTN"; + public static $voip = "VOIP"; + public static $aim = "AIM"; + public static $gtalk = "GTALK"; + public static $jabber = "JABBER"; + public static $msn = "MSN"; + public static $sms = "SMS"; + public static $yahoo = "YAHOO"; + public static $twitter = "TWITTER"; } /** - * Channel Helper class. - * @package TropoPHP_Support - */ +* Channel Helper class. +* @package TropoPHP_Support +*/ class Channel { - public static $voice = "VOICE"; - public static $text = "TEXT"; + public static $voice = "VOICE"; + public static $text = "TEXT"; } /** - * AudioFormat Helper class. - * @package TropoPHP_Support - */ +* AudioFormat Helper class. +* @package TropoPHP_Support +*/ class AudioFormat { - public static $wav = "audio/wav"; - public static $mp3 = "audio/mp3"; + public static $wav = "audio/wav"; + public static $mp3 = "audio/mp3"; } /** - * Voice Helper class. - * @package TropoPHP_Support - */ +* Voice Helper class. +* @package TropoPHP_Support +*/ class Voice { - public static $Castilian_Spanish_male = "jorge"; - public static $Castilian_Spanish_female = "carmen"; - public static $French_male = "bernard"; - public static $French_female = "florence"; - public static $US_English_male = "dave"; - public static $US_English_female = "jill"; - public static $British_English_male = "dave"; - public static $British_English_female = "kate"; - public static $German_male = "stefan"; - public static $German_female = "katrin"; - public static $Italian_male = "luca"; - public static $Italian_female = "paola"; - public static $Dutch_male = "willem"; - public static $Dutch_female = "saskia"; - public static $Mexican_Spanish_male = "carlos"; - public static $Mexican_Spanish_female = "soledad"; + public static $Castilian_Spanish_male = "jorge"; + public static $Castilian_Spanish_female = "carmen"; + public static $French_male = "bernard"; + public static $French_female = "florence"; + public static $US_English_male = "dave"; + public static $US_English_female = "jill"; + public static $British_English_male = "dave"; + public static $British_English_female = "kate"; + public static $German_male = "stefan"; + public static $German_female = "katrin"; + public static $Italian_male = "luca"; + public static $Italian_female = "paola"; + public static $Dutch_male = "willem"; + public static $Dutch_female = "saskia"; + public static $Mexican_Spanish_male = "carlos"; + public static $Mexican_Spanish_female = "soledad"; } /** - * Recognizer Helper class - * @package TropoPHP_Support - * - */ +* Recognizer Helper class +* @package TropoPHP_Support +* +*/ class Recognizer { - public static $German = 'de-de'; - public static $British_English = 'en-gb'; - public static $US_English = 'en-us'; - public static $Castilian_Spanish = 'es-es'; - public static $Mexican_Spanish = 'es-mx'; - public static $French_Canadian = 'fr-ca'; - public static $French = 'fr-fr'; - public static $Italian = 'it-it'; - public static $Polish = 'pl-pl'; - public static $Dutch = 'nl-nl'; + public static $German = 'de-de'; + public static $British_English = 'en-gb'; + public static $US_English = 'en-us'; + public static $Castilian_Spanish = 'es-es'; + public static $Mexican_Spanish = 'es-mx'; + public static $French_Canadian = 'fr-ca'; + public static $French = 'fr-fr'; + public static $Italian = 'it-it'; + public static $Polish = 'pl-pl'; + public static $Dutch = 'nl-nl'; } /** - * SIP Headers Helper class. - * @package TropoPHP_Support - */ +* SIP Headers Helper class. +* @package TropoPHP_Support +*/ class Headers { - public function __set($name, $value) { - if(!strstr($name, "-")) { - $this->$name = $value; - } else { - $name = str_replace("-", "_", $name); - $this->$name = $value; - } - } - + public function __set($name, $value) { + if(!strstr($name, "-")) { + $this->$name = $value; + } else { + $name = str_replace("-", "_", $name); + $this->$name = $value; + } + } } ?> From 82306a10a0674b85542f311e211ee9c7014a101a Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:39:16 -0500 Subject: [PATCH 055/107] Added interdigitTimeout, sensitivity, speechCompleteTimeout and speechIncompleteTimeout to the ask method --- tropo.class.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index a0efbc1..58cbd3a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -95,7 +95,7 @@ public function setLanguage($language) { */ public function ask($ask, Array $params=NULL) { if(!is_object($ask)) { - $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer'); + $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer', 'interdigitTimeout', 'sensitivity', 'speechCompleteTimeout', 'speechIncompleteTimeout'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -116,7 +116,7 @@ public function ask($ask, Array $params=NULL) { $voice = $this->_voice; } $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer); + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer, $interdigitTimeout, $sensitivity, $speechCompleteTimeout, $speechIncompleteTimeout); } $this->ask = sprintf('%s', $ask); } @@ -702,6 +702,10 @@ class Ask extends BaseClass { private $_voice; private $_allowSignals; private $_recognizer; + private $_interdigitTimeout; + private $_sensitivity; + private $_speechCompleteTimeout; + private $_speechIncompleteTimeout; /** * Class constructor @@ -716,8 +720,12 @@ class Ask extends BaseClass { * @param int $timeout * @param string $voice * @param string|array $allowSignals + * @param integer $interdigitTimeout + * @param integer $sensitivity + * @param float $speechCompleteTimeout + * @param float $speechIncompleteTimeout */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL, $interdigitTimeout=NULL, $sensitivity=NULL, $speechCompleteTimeout=NULL, $speechIncompleteTimeout=NULL) { $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; @@ -729,6 +737,10 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL $this->_voice = $voice; $this->_allowSignals = $allowSignals; $this->_recognizer = $recognizer; + $this->_interdigitTimeout = $interdigitTimeout; + $this->_sensitivity = $sensitivity; + $this->_speechCompleteTimeout = $speechCompleteTimeout; + $this->_speechIncompleteTimeout = $speechIncompleteTimeout; } /** @@ -752,6 +764,10 @@ public function __toString() { if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_recognizer)) { $this->recognizer = $this->_recognizer; } + if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } + if(isset($this->_sensitivity)) { $this->sensitivity = $this->_sensitivity; } + if(isset($this->_speechCompleteTimeout)) { $this->speechCompleteTimeout = $this->_speechCompleteTimeout; } + if(isset($this->_speechIncompleteTimeout)) { $this->speechIncompleteTimeout = $this->_speechIncompleteTimeout; } return $this->unescapeJSON(json_encode($this)); } From 9e8e0bbd5a5662c29fe50f002b44c714deb94b4b Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:42:14 -0500 Subject: [PATCH 056/107] Added interdigitTimeout to the conference method --- tropo.class.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 58cbd3a..af17d67 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -152,7 +152,7 @@ public function call($call, Array $params=NULL) { */ public function conference($conference, Array $params=NULL) { if(!is_object($conference)) { - $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals'); + $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -161,7 +161,7 @@ public function conference($conference, Array $params=NULL) { } $id = (empty($id) && !empty($conference)) ? $conference : $id; $name = (empty($name)) ? (string)$id : $name; - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals); + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout); } $this->conference = sprintf('%s', $conference); } @@ -898,6 +898,7 @@ class Conference extends BaseClass { private $_required; private $_terminator; private $_allowSignals; + private $_interdigitTimeout; /** @@ -911,8 +912,9 @@ class Conference extends BaseClass { * @param boolean $required * @param string $terminator * @param string|array $allowSignals + * @param int $interdigitTimeout */ - public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL) { + public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL) { $this->_name = $name; $this->_id = (string) $id; $this->_mute = $mute; @@ -921,6 +923,7 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones $this->_required = $required; $this->_terminator = $terminator; $this->_allowSignals = $allowSignals; + $this->_interdigitTimeout = $interdigitTimeout; } /** @@ -936,6 +939,7 @@ public function __toString() { if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } return $this->unescapeJSON(json_encode($this)); } } From 0a3d40a6757fcb5bcb6a387f9ebcd00c9e122f7f Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:49:25 -0500 Subject: [PATCH 057/107] Added the voice param to the on method --- tropo.class.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index af17d67..c454121 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -208,7 +208,12 @@ public function message($message, Array $params=null) { public function on($on) { if (!is_object($on) && is_array($on)) { $params = $on; - $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; + if ((array_key_exists('say', $params) && ((array_key_exists('voice', $params) || isset($this->_voice))))){ + $v = isset($params["voice"]) ? $params["voice"] : $this->_voice; + $say = new Say($params["say"], null, null, $v); + }else{ + $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; + } $next = (array_key_exists('next', $params)) ? $params["next"] : null; $on = new On($params["event"], $next, $say); } @@ -1021,6 +1026,7 @@ class On extends BaseClass { private $_event; private $_next; private $_say; + private $_voice; /** * Class constructor @@ -1028,11 +1034,13 @@ class On extends BaseClass { * @param string $event * @param string $next * @param Say $say + * @param string $voice */ - public function __construct($event=NULL, $next=NULL, Say $say=NULL) { + public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null) { $this->_event = $event; $this->_next = $next; $this->_say = isset($say) ? sprintf('%s', $say) : null ; + $this->_voice = $voice; } /** @@ -1043,6 +1051,7 @@ public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } if(isset($this->_next)) { $this->next = $this->_next; } if(isset($this->_say)) { $this->say = $this->_say; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } return $this->unescapeJSON(json_encode($this)); } } From 833298c3eb1351437c029d39aa9c48261f8367ba Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:53:02 -0500 Subject: [PATCH 058/107] Added minConfidence and interdigitTimeout to the record method --- tropo.class.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index c454121..9257d55 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -259,14 +259,14 @@ public function record($record) { } else { $transcription = $params["transcription"]; } - $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice'); + $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice); + $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout); } $this->record = sprintf('%s', $record); } @@ -1080,6 +1080,8 @@ class Record extends BaseClass { private $_username; private $_url; private $_voice; + private $_minConfidence; + private $_interdigitTimeout; /** @@ -1100,8 +1102,10 @@ class Record extends BaseClass { * @param string $username * @param string $url * @param string $voice + * @param int $minConfidence + * @param int $interdigitTimeout */ - public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL) { + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL) { $this->_attempts = $attempts; $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; @@ -1121,6 +1125,8 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_username = $username; $this->_url = $url; $this->_voice = $voice; + $this->_minConfidence = $minConfidence; + $this->_interdigitTimeout = $interdigitTimeout; } /** @@ -1144,6 +1150,8 @@ public function __toString() { if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_voice)) { $this->voice = $this->_voice; } + if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; } + if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } return $this->unescapeJSON(json_encode($this)); } } From 8e76a1859c9f4ae1be0c86b3fb1da9e2c5e4f683 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 17:57:16 -0500 Subject: [PATCH 059/107] Added transcriptionID, transcriptionEmailFormat and transcriptionOutURI to the startRecording method --- tropo.class.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 9257d55..df47433 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -335,14 +335,14 @@ public function say($say, Array $params=NULL) { public function startRecording($startRecording) { if(!is_object($startRecording) && is_array($startRecording)) { $params = $startRecording; - $p = array('format', 'method', 'password', 'url', 'username'); + $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $startRecording = new StartRecording($format, $method, $password, $url, $username); + $startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI); } $this->startRecording = sprintf('%s', $startRecording); } @@ -1553,6 +1553,9 @@ class StartRecording extends BaseClass { private $_password; private $_url; private $_username; + private $_transcriptionID; + private $_transcriptionEmailFormat; + private $_transcriptionOutURI; /** * Class constructor @@ -1563,13 +1566,19 @@ class StartRecording extends BaseClass { * @param string $password * @param string $url * @param string $username + * @param string $transcriptionID + * @param string $transcriptionEmailFormat + * @param string $transcriptionOutURI */ - public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL) { + public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL, $transcriptionID=NULL, $transcriptionEmailFormat=NULL, $transcriptionOutURI=NULL) { $this->_format = $format; $this->_method = $method; $this->_password = $password; $this->_url = $url; $this->_username = $username; + $this->_transcriptionID = $transcriptionID; + $this->_transcriptionEmailFormat = $transcriptionEmailFormat; + $this->_transcriptionOutURI = $transcriptionOutURI; } /** @@ -1582,6 +1591,9 @@ public function __toString() { if(isset($this->_password)) { $this->password = $this->_password; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_username)) { $this->username = $this->_username; } + if(isset($this->_transcriptionID)) { $this->transcriptionID = $this->_transcriptionID; } + if(isset($this->_transcriptionEmailFormat)) { $this->transcriptionEmailFormat = $this->_transcriptionEmailFormat; } + if(isset($this->_transcriptionOutURI)) { $this->transcriptionOutURI = $this->_transcriptionOutURI; } return $this->unescapeJSON(json_encode($this)); } } From e14c48d4530dfe2f720c47bc0423479143c9d533 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Fri, 16 Nov 2012 18:03:43 -0500 Subject: [PATCH 060/107] Added the wait method --- tropo.class.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tropo.class.php b/tropo.class.php index df47433..facb342 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -396,6 +396,23 @@ public function transfer($transfer, Array $params=NULL) { } $this->transfer = sprintf('%s', $transfer); } + + /** + * Makes the Tropo sleep an active call in milliseconds + * + * @param Interger $milliseconds + * @param String or Array $allowSignals + * @see https://www.tropo.com/docs/webapi/wait.htm + */ + public function wait($wait) { + if (!is_object($wait) && is_array($wait)){ + $params = $wait; + $signal = isset($params['allowSignals']) ? $params['allowSignals'] : null; + $wait = new Wait($params["milliseconds"], $signal); + } + $this->wait = sprintf('%s', $wait); + + } /** * Launches a new session with the Tropo Session API. @@ -1701,6 +1718,38 @@ public function __toString() { } } +/** +* Defines a time period to sleep in milliseconds +* @package TropoPHP_Support +* +*/ +class Wait extends BaseClass { + + private $_milliseconds; + private $_allowSignals; + + /** + * Class constructor + * + * @param integer $milliseconds + * @param string|array $allowSignals + */ + public function __construct($milliseconds, $allowSignals=NULL) { + $this->_milliseconds = $milliseconds; + $this->_allowSignals = $allowSignals; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + $this->milliseconds = $this->_milliseconds; + if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + return $this->unescapeJSON(json_encode($this)); + } +} + /** * Defnies an endoint for transfer and redirects. * @package TropoPHP_Support From cc86895d47aec089a5fad72097107bec4f4e7d2a Mon Sep 17 00:00:00 2001 From: Adam Kalsey Date: Mon, 18 Feb 2013 07:57:56 -0800 Subject: [PATCH 061/107] Update copyright in license to include Voxeo Labs --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 1bacdbb..3e5f9dc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License -Copyright (c) 2010 Mark Headd +Copyright (c) 2010 Mark Headd, 2011-2013 Voxeo Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 3ae1761c092855c4e2a2cfce566d571ceb1fa8e4 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Thu, 31 Oct 2013 16:23:57 -0400 Subject: [PATCH 062/107] Added the whisper function to the transfer method --- tropo.class.php | 79 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 12 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index facb342..0e0fedd 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -217,7 +217,7 @@ public function on($on) { $next = (array_key_exists('next', $params)) ? $params["next"] : null; $on = new On($params["event"], $next, $say); } - $this->on = sprintf('%s', $on); + $this->on = array(sprintf('%s', $on)); } /** @@ -386,13 +386,40 @@ public function transfer($transfer, Array $params=NULL) { if (is_object($params['on'])) { $on = $params['on']; } else { - if (strtolower($params['on']['event']) != 'ring') { - throw new TropoException("The only event allowed on transfer is 'ring'"); + if (strtolower($params['on']['event']) == 'ring') { + $on = on(array('ring', null, new Say($params['on']['say']), null, null)); + }elseif (strtolower($params['on']['event']) == 'connect') { + + $comma = ""; + $on = ""; + + foreach($params['on']['whisper'] as $key){ + foreach($key as $k => $v){ + + switch($k){ + case 'ask': + $on = $on . $comma . new On('connect', null, null, null,$v,null,null,"ask"); + break; + case 'say': + $on = $on . $comma . new On('connect', null, $v, null,null,null,null,"say"); + break; + case 'wait': + $on = $on . $comma . new On('connect', null, null, null,null,null,$v,"wait"); + break; + case 'message': + $on = $on . $comma . new On('connect', null, null, null,null,$v,null,"message"); + break; + } + $comma = ","; + } + } + + }else{ + throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); } - $on = new On('ring', null, new Say($params['on']['say'])); } } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers); + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, sprintf('%s',$on), $allowSignals, $headers); } $this->transfer = sprintf('%s', $transfer); } @@ -1044,6 +1071,10 @@ class On extends BaseClass { private $_next; private $_say; private $_voice; + private $_ask; + private $_message; + private $_wait; + private $_order; /** * Class constructor @@ -1053,11 +1084,15 @@ class On extends BaseClass { * @param Say $say * @param string $voice */ - public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null) { + public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null, $ask=NULL, Message $message=NULL, Wait $wait=NULL, $order=NULL) { $this->_event = $event; $this->_next = $next; $this->_say = isset($say) ? sprintf('%s', $say) : null ; $this->_voice = $voice; + $this->_ask = isset($ask) ? sprintf('%s', $ask) : null; + $this->_message = isset($message) ? sprintf('%s', $message) : null; + $this->_wait = isset($wait) ? sprintf('%s', $wait) : null; + $this->_order = $order; } /** @@ -1065,11 +1100,31 @@ public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null) * */ public function __toString() { - if(isset($this->_event)) { $this->event = $this->_event; } - if(isset($this->_next)) { $this->next = $this->_next; } - if(isset($this->_say)) { $this->say = $this->_say; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - return $this->unescapeJSON(json_encode($this)); + + if($this->_event == "connect") { + $this->event = $this->_event; + switch($this->_order){ + case 'ask': + $this->ask = $this->_ask; + break; + case 'say': + $this->say = $this->_say; + break; + case 'wait': + $this->ask = $this->_ask; + break; + case 'message': + $this->message = $this->_message; + break; + } + return $this->unescapeJSON(json_encode(($this))); + }else{ + if(isset($this->_event)) { $this->event = $this->_event; } + if(isset($this->_next)) { $this->next = $this->_next; } + if(isset($this->_say)) { $this->say = $this->_say; } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + return $this->unescapeJSON(json_encode($this)); + } } } @@ -1695,7 +1750,7 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr $this->_from = $from; $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; - $this->_on = isset($on) ? sprintf('%s', $on) : null; + $this->_on = isset($on) ? array(sprintf('%s', $on)) : null; $this->_allowSignals = $allowSignals; $this->_headers = $headers; } From d6fe5e24e5cb29efec8681acd68a84eb2e1869e6 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 4 Nov 2013 12:54:33 -0500 Subject: [PATCH 063/107] Added Join/Leave Prompt for the conference method. Also added two exmaples for Join/Leave prompt and whisper --- samples/callWhisper.php | 39 ++++++++++++++++++++++ samples/conferencerJoinLeavePrompt.php | 24 ++++++++++++++ tropo.class.php | 46 ++++++++++++++++++++++---- 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 samples/callWhisper.php create mode 100644 samples/conferencerJoinLeavePrompt.php diff --git a/samples/callWhisper.php b/samples/callWhisper.php new file mode 100644 index 0000000..ef66c89 --- /dev/null +++ b/samples/callWhisper.php @@ -0,0 +1,39 @@ + $a); + +//push the ask to the whisper array +array_push($whisper, $ask); + +//The first method will be a say +$say = array("say" => new Say("You are now being connected to the call.")); + +//Push the say to the whisper array +array_push($whisper, $say); + + +$tropo->say("please hold while you are transferred"); + +//Create the connect whisper on event for the transfer +$on = array("event" => "connect", "whisper" => $whisper); + +$options = array( + 'on' => $on, + 'from' => '14071234321' +); + +//use the connect whisper in the transfer +$tropo->transfer("+14071234321", $options); +$tropo->on(array("event" => "incomplete", "next" => "hangup.php", "say" => "You have opted to not accept this call. Goodbye!")); + +?> \ No newline at end of file diff --git a/samples/conferencerJoinLeavePrompt.php b/samples/conferencerJoinLeavePrompt.php new file mode 100644 index 0000000..12af325 --- /dev/null +++ b/samples/conferencerJoinLeavePrompt.php @@ -0,0 +1,24 @@ +getFrom(); +$callerID = $from["id"]; + +$tropo->say("You are about to enter the conference"); + +$tropo->conference(null, array( + "id"=>"1234", + "joinPrompt" => "$callerID has entered the conference", + "leavePrompt" => "$callerID has left the conference", + "voice" => "Victor", +)); + +$tropo->RenderJson(); + +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 0e0fedd..6ad1e79 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -130,14 +130,14 @@ public function ask($ask, Array $params=NULL) { */ public function call($call, Array $params=NULL) { if(!is_object($call)) { - $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals'); + $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals', 'machineDetection', 'voice'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals); + $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals, $machineDetection, $voice); } $this->call = sprintf('%s', $call); } @@ -152,7 +152,7 @@ public function call($call, Array $params=NULL) { */ public function conference($conference, Array $params=NULL) { if(!is_object($conference)) { - $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout'); + $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -161,7 +161,7 @@ public function conference($conference, Array $params=NULL) { } $id = (empty($id) && !empty($conference)) ? $conference : $id; $name = (empty($name)) ? (string)$id : $name; - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout); + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice); } $this->conference = sprintf('%s', $conference); } @@ -849,6 +849,8 @@ class Call extends BaseClass { private $_headers; private $_recording; private $_allowSignals; + private $_machineDetection; + private $_voice; /** * Class constructor @@ -863,7 +865,7 @@ class Call extends BaseClass { * @param StartRecording $recording * @param string|array $allowSignals */ - public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL) { + public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL, $machineDetection=NULL, $voice=NULL) { $this->_to = $to; $this->_from = $from; $this->_network = $network; @@ -873,6 +875,8 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ $this->_headers = $headers; $this->_recording = isset($recording) ? sprintf('%s', $recording) : null ; $this->_allowSignals = $allowSignals; + $this->_machineDetection = $machineDetection; + $this->_voice = $voice; } /** @@ -889,6 +893,16 @@ public function __toString() { if(count($this->_headers)) { $this->headers = $this->_headers; } if(isset($this->_recording)) { $this->recording = $this->_recording; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_machineDetection)) { + if(is_bool($this->_machineDetection)){ + $this->machineDetection = $this->_machineDetection; + }else{ + $this->machineDetection->introduction = $this->_machineDetection; + if(isset($this->_voice)){ + $this->machineDetection->voice = $this->_voice; + } + } + } return $this->unescapeJSON(json_encode($this)); } } @@ -948,6 +962,9 @@ class Conference extends BaseClass { private $_terminator; private $_allowSignals; private $_interdigitTimeout; + private $_joinPrompt; + private $_leavePrompt; + private $_voice; /** @@ -963,7 +980,7 @@ class Conference extends BaseClass { * @param string|array $allowSignals * @param int $interdigitTimeout */ - public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL) { + public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL, $joinPrompt=NULL, $leavePrompt=NULL, $voice=NULL) { $this->_name = $name; $this->_id = (string) $id; $this->_mute = $mute; @@ -973,6 +990,9 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones $this->_terminator = $terminator; $this->_allowSignals = $allowSignals; $this->_interdigitTimeout = $interdigitTimeout; + $this->_joinPrompt = $joinPrompt; + $this->_leavePrompt = $leavePrompt; + $this->_voice = $voice; } /** @@ -989,6 +1009,18 @@ public function __toString() { if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } + if(isset($this->_joinPrompt)) { + $this->joinPrompt->value = $this->_joinPrompt; + if(isset($this->_voice)) { + $this->joinPrompt->voice = $this->_voice; + } + } + if(isset($this->_leavePrompt)) { + $this->leavePrompt->value = $this->_leavePrompt; + if(isset($this->_voice)) { + $this->leavePrompt->voice = $this->_voice; + } + } return $this->unescapeJSON(json_encode($this)); } } @@ -1436,7 +1468,7 @@ public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSi */ public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } - $this->value = str_replace('%', '%%', $this->_value); + $this->value = $this->_value; if(isset($this->_as)) { $this->as = $this->_as; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } From 5897153192fb7a82bd867e31e8d1447eab396646 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 4 Nov 2013 13:42:38 -0500 Subject: [PATCH 064/107] Removed userType from Session and added it to Result for Machine detection --- tropo.class.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 6ad1e79..86607ed 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1320,6 +1320,7 @@ class Result { private $_confidence; private $_interpretation; private $_concept; + private $_userType; private $_utterance; private $_value; private $_transcription; @@ -1349,6 +1350,7 @@ public function __construct($json=NULL) { $this->_sequence = $result->result->sequence; $this->_complete = $result->result->complete; $this->_error = $result->result->error; + $this->_userType = $result->result->userType; $this->_actions = $result->result->actions; $this->_name = $result->result->actions->name; $this->_attempts = $result->result->actions->attempts; @@ -1389,6 +1391,10 @@ public function getError() { return $this->_error; } + public function getUserType() { + return $this->_userType; + } + public function getActions() { return $this->_actions; } @@ -1489,7 +1495,6 @@ class Session { private $_accountId; private $_callId; private $_timestamp; - private $_userType; private $_initialText; private $_to; private $_from; @@ -1518,7 +1523,6 @@ public function __construct($json=NULL) { $this->_accountId = $session->session->accountId; $this->_callId = $session->session->callId; $this->_timestamp = $session->session->timestamp; - $this->_userType = $session->session->userType; $this->_initialText = $session->session->initialText; $this->_to = isset($session->session->to) ? array( @@ -1568,9 +1572,6 @@ public function getCallId() { public function getTimeStamp() { return $this->_timestamp; } - public function getUserType() { - return $this->_userType; - } public function getInitialText() { return $this->_initialText; From ff93802bb9372f410a7c0f935396f314f45f8daf Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 4 Nov 2013 13:46:07 -0500 Subject: [PATCH 065/107] Added an example for machine detection --- samples/callMachineDetection.php | 31 +++++++++++++++++++ .../{callWhisper.php => transferWhisper.php} | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 samples/callMachineDetection.php rename samples/{callWhisper.php => transferWhisper.php} (94%) diff --git a/samples/callMachineDetection.php b/samples/callMachineDetection.php new file mode 100644 index 0000000..b22a3ac --- /dev/null +++ b/samples/callMachineDetection.php @@ -0,0 +1,31 @@ +call("+14071234321", array( + "machineDetection" => "This is just a test to see if you are a human or a machine. PLease hold while we determine. Almost finished. Thank you!", + "voice" => "Victor" + )); + $tropo->on(array("event" => "continue", "next" => "your_app.php?uri=continue")); + + $tropo->RenderJson(); +} + +dispatch_post('/continue', 'app_continue'); +function app_continue() { + + $tropo = new Tropo(); + @$result = new Result(); + + $userType = $result->getUserType(); + $tropo->say("You are a $userType"); + $tropo->RenderJson(); +} +run(); +?> \ No newline at end of file diff --git a/samples/callWhisper.php b/samples/transferWhisper.php similarity index 94% rename from samples/callWhisper.php rename to samples/transferWhisper.php index ef66c89..0a67a3d 100644 --- a/samples/callWhisper.php +++ b/samples/transferWhisper.php @@ -1,5 +1,5 @@ Date: Mon, 4 Nov 2013 16:50:31 -0500 Subject: [PATCH 066/107] UTF fix --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index 86607ed..654dfb3 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -710,7 +710,7 @@ public function __set($attribute, $value) { * @return string */ public function unescapeJSON($json) { - return str_replace(array("\\", "\"{", "}\""), array("", "{", "}"), $json); + return str_replace(array('\"', "\"{", "}\"", '\\\\\/', '\\\\'), array('"', "{", "}", '/', '\\'), $json); } } From 392ef6997a9e552c8edf1507e27e8bd1ebdd315a Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 25 Nov 2013 12:18:50 -0500 Subject: [PATCH 067/107] Added machine detection to the transfer method --- samples/conferencerJoinLeavePrompt.php | 1 + tropo.class.php | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/samples/conferencerJoinLeavePrompt.php b/samples/conferencerJoinLeavePrompt.php index 12af325..ebf0fad 100644 --- a/samples/conferencerJoinLeavePrompt.php +++ b/samples/conferencerJoinLeavePrompt.php @@ -14,6 +14,7 @@ $tropo->conference(null, array( "id"=>"1234", + "name"=>"joinleave", "joinPrompt" => "$callerID has entered the conference", "leavePrompt" => "$callerID has left the conference", "voice" => "Victor", diff --git a/tropo.class.php b/tropo.class.php index 654dfb3..76aefed 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -372,7 +372,7 @@ public function transfer($transfer, Array $params=NULL) { ? new Choices(null, null, $params["terminator"]) : $choices; $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers'); + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -419,7 +419,8 @@ public function transfer($transfer, Array $params=NULL) { } } } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, sprintf('%s',$on), $allowSignals, $headers); + $on = $on == null ? null : sprintf('%s',$on); + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice); } $this->transfer = sprintf('%s', $transfer); } @@ -1762,6 +1763,8 @@ class Transfer extends BaseClass { private $_to; private $_allowSignals; private $_headers; + private $_machineDetection; + private $_voice; /** * Class constructor @@ -1776,7 +1779,7 @@ class Transfer extends BaseClass { * @param string|array $allowSignals * @param array $headers */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL, $machineDetection=NULL, $voice=NULL) { $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; @@ -1786,6 +1789,8 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr $this->_on = isset($on) ? array(sprintf('%s', $on)) : null; $this->_allowSignals = $allowSignals; $this->_headers = $headers; + $this->_machineDetection = $machineDetection; + $this->_voice = $voice; } /** @@ -1802,6 +1807,16 @@ public function __toString() { if(isset($this->_on)) { $this->on = $this->_on; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(count($this->_headers)) { $this->headers = $this->_headers; } + if(isset($this->_machineDetection)) { + if(is_bool($this->_machineDetection)){ + $this->machineDetection = $this->_machineDetection; + }else{ + $this->machineDetection->introduction = $this->_machineDetection; + if(isset($this->_voice)){ + $this->machineDetection->voice = $this->_voice; + } + } + } return $this->unescapeJSON(json_encode($this)); } } From f891b8ecd9a695f0eb83668d3a7fe6673322715d Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 25 Nov 2013 12:53:30 -0500 Subject: [PATCH 068/107] fixed ring event with connect in transfer --- samples/transferWhisper.php | 10 +++++++--- tropo.class.php | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/samples/transferWhisper.php b/samples/transferWhisper.php index 0a67a3d..59cb7ec 100644 --- a/samples/transferWhisper.php +++ b/samples/transferWhisper.php @@ -1,5 +1,5 @@ say("please hold while you are transferred"); -//Create the connect whisper on event for the transfer -$on = array("event" => "connect", "whisper" => $whisper); +//Create the connect whisper on event for the transfer with a ring event +$on = array("event" => "connect", "whisper" => $whisper, "ring" => "http://www.phono.com/audio/holdmusic.mp3"); + +//Create the connect whisper on event for the transfer without a ring event +$on = array("event" => "connect", "whisper" => $whisper); $options = array( 'on' => $on, @@ -36,4 +39,5 @@ $tropo->transfer("+14071234321", $options); $tropo->on(array("event" => "incomplete", "next" => "hangup.php", "say" => "You have opted to not accept this call. Goodbye!")); +echo $tropo->RenderJson(); ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 76aefed..55292d5 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -392,7 +392,11 @@ public function transfer($transfer, Array $params=NULL) { $comma = ""; $on = ""; - + + if(isset($params['on']['ring'])){ + $on = new On('ring', null, new Say($params['on']['ring']), null, null); + $comma = ","; + } foreach($params['on']['whisper'] as $key){ foreach($key as $k => $v){ From 3bd1b73a93e94b1214cb9303098530c9d52fd08e Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 25 Nov 2013 12:54:09 -0500 Subject: [PATCH 069/107] fixed ring event with connect in transfer --- samples/transferWhisper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/transferWhisper.php b/samples/transferWhisper.php index 59cb7ec..11f0388 100644 --- a/samples/transferWhisper.php +++ b/samples/transferWhisper.php @@ -1,5 +1,5 @@ Date: Mon, 13 Jan 2014 10:06:48 -0500 Subject: [PATCH 070/107] Update tropo.class.php --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index 55292d5..a92aeb7 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -108,7 +108,7 @@ public function ask($ask, Array $params=NULL) { $say[] = new Say($val, $as, $e, $voice); } } - $say[] = new Say($ask, $as, null, $voice); + $say = new Say($ask, $as, null, $voice); $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; From 397787487083b67a842fb05bceca6d7ebc1549bb Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 13 Jan 2014 10:09:49 -0500 Subject: [PATCH 071/107] Update tropo.class.php --- tropo.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index a92aeb7..55292d5 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -108,7 +108,7 @@ public function ask($ask, Array $params=NULL) { $say[] = new Say($val, $as, $e, $voice); } } - $say = new Say($ask, $as, null, $voice); + $say[] = new Say($ask, $as, null, $voice); $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; From 7e0a5cf422d0449d94c712cbc97c026a8693d738 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 17 Sep 2014 14:15:39 -0400 Subject: [PATCH 072/107] Update tropo.class.php --- tropo.class.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 55292d5..c70d40d 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1015,16 +1015,24 @@ public function __toString() { if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } if(isset($this->_joinPrompt)) { - $this->joinPrompt->value = $this->_joinPrompt; - if(isset($this->_voice)) { - $this->joinPrompt->voice = $this->_voice; - } + if($this->_joinPrompt == true || $this->_joinPrompt == false){ + $this->joinPrompt = $this->_joinPrompt; + }else{ + $this->joinPrompt->value = $this->_joinPrompt; + if(isset($this->_voice)) { + $this->joinPrompt->voice = $this->_voice; + } + } } if(isset($this->_leavePrompt)) { - $this->leavePrompt->value = $this->_leavePrompt; - if(isset($this->_voice)) { - $this->leavePrompt->voice = $this->_voice; - } + if($this->_leavePrompt == true || $this->_leavePrompt == false){ + $this->leavePrompt = $this->_leavePrompt; + }else{ + $this->leavePrompt->value = $this->_leavePrompt; + if(isset($this->_voice)) { + $this->leavePrompt->voice = $this->_voice; + } + } } return $this->unescapeJSON(json_encode($this)); } From 066098eef81f9c1a0dc4d60ca2a9ba0c623640ff Mon Sep 17 00:00:00 2001 From: pengxli Date: Tue, 6 Jun 2017 16:53:07 +0800 Subject: [PATCH 073/107] bug TROPO-11280 tropo-webapi-php say --- tests/SayTest.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 154 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 288 insertions(+), 20 deletions(-) create mode 100644 tests/SayTest.php diff --git a/tests/SayTest.php b/tests/SayTest.php new file mode 100644 index 0000000..ef99158 --- /dev/null +++ b/tests/SayTest.php @@ -0,0 +1,154 @@ +say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"name":"say","required":true,"promptLogSecurity":"suppress"}]}]}'); + } + + public function testCreateSayObject1() + { + $tropo = new Tropo(); + $allowSignals = array('exit','quit'); + $params = array( + "name"=>"say", + "as"=>SayAs::$date, + "event"=>"event", + "voice"=>Voice::$US_English_female_allison, + "allowSignals"=>$allowSignals, + "promptLogSecurity"=>"suppress", + "required"=>true); + $tropo->say("Please enter your account number...",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"name":"say","required":true,"promptLogSecurity":"suppress"}]}]}'); + } + + public function testFailsSayWithNoValueParameter() + { + try{ + @ $say = new Say(); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoValueParameter1() + { + try{ + @ $say = new Say(null); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoValueParameter2() + { + try{ + @ $say = new Say(""); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoNameParameter() + { + $tropo = new Tropo(); + try{ + $say = new Say("Please enter your account number..."); + $tropo->say($say); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } + + public function testFailsSayWithNoNameParameter1() + { + $tropo = new Tropo(); + try{ + $say = new Say("Please enter your account number...", null, null, null, null, null, null, null); + $tropo->say($say); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } + + public function testFailsSayWithNoNameParameter2() + { + $tropo = new Tropo(); + try{ + $say = new Say("Please enter your account number...", null, null, null, null, "", null, null); + $tropo->say($say); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } + + public function testFailsSayWithNoValueParameter3() + { + $tropo = new Tropo(); + try{ + @ $tropo->say(); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoValueParameter4() + { + $tropo = new Tropo(); + try{ + @ $tropo->say(null); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoValueParameter5() + { + $tropo = new Tropo(); + try{ + @ $tropo->say(""); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + } + } + + public function testFailsSayWithNoNameParameter3() + { + $tropo = new Tropo(); + try{ + $tropo->say("Please enter your account number..."); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } + + public function testFailsSayWithNoNameParameter4() + { + $tropo = new Tropo(); + $params = array("name"=>null); + try{ + $tropo->say("Please enter your account number...",$params); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } + + public function testFailsSayWithNoNameParameter5() + { + $tropo = new Tropo(); + $params = array("name"=>""); + try{ + $tropo->say("Please enter your account number...",$params); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + } + } +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index c70d40d..7acd3bb 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -311,7 +311,10 @@ public function reject() { */ public function say($say, Array $params=NULL) { if(!is_object($say)) { - $p = array('as', 'format', 'event','voice', 'allowSignals'); + if(!$say) { + throw new Exception("Missing required property: 'value'"); + } + $p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity'); $value = $say; foreach ($p as $option) { $$option = null; @@ -319,8 +322,20 @@ public function say($say, Array $params=NULL) { $$option = $params[$option]; } } + if(!$name) { + throw new Exception("Missing required property: 'name'"); + } $voice = isset($voice) ? $voice : $this->_voice; - $say = new Say($value, $as, $event, $voice, $allowSignals); + $event = null; + $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity); + } else { + $say->setEvent(null); + if(!($say->getValue())) { + throw new Exception("Missing required property: 'value'"); + } + if(!($say->getName())) { + throw new Exception("Missing required property: 'name'"); + } } $this->say = array(sprintf('%s', $say)); } @@ -651,7 +666,8 @@ public function viewExchanges($userid, $password) { * */ public function renderJSON() { - header('Content-type: application/json'); + header('Content-type: application/json;charset=utf8'); + header('WebAPI-Lang-Ver: PHP V15.9.0 SNAPSHOT'); echo $this; } @@ -1460,9 +1476,23 @@ class Say extends BaseClass { private $_value; private $_as; private $_event; - private $_format; private $_voice; private $_allowSignals; + private $_name; + private $_required; + private $_promptLogSecurity; + + public function getValue() { + return $this->_value; + } + + public function getName() { + return $this->_name; + } + + public function setEvent($event) { + $this->_event = $event; + } /** * Class constructor @@ -1473,12 +1503,18 @@ class Say extends BaseClass { * @param string $voice * @param string|array $allowSignals */ - public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL) { + public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL, $name=NULL, $required=NULL, $promptLogSecurity=NULL) { + if(!$value) { + throw new Exception("Missing required property: 'value'"); + } $this->_value = $value; $this->_as = $as; $this->_event = $event; $this->_voice = $voice; $this->_allowSignals = $allowSignals; + $this->_name = $name; + $this->_required = $required; + $this->_promptLogSecurity = $promptLogSecurity; } /** @@ -1491,6 +1527,9 @@ public function __toString() { if(isset($this->_as)) { $this->as = $this->_as; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } + if(isset($this->_name)) { $this->name = $this->_name; } + if(isset($this->_required)) { $this->required = $this->_required; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } return $this->unescapeJSON(json_encode($this)); } } @@ -2021,22 +2060,97 @@ class AudioFormat { * @package TropoPHP_Support */ class Voice { - public static $Castilian_Spanish_male = "jorge"; - public static $Castilian_Spanish_female = "carmen"; - public static $French_male = "bernard"; - public static $French_female = "florence"; - public static $US_English_male = "dave"; - public static $US_English_female = "jill"; - public static $British_English_male = "dave"; - public static $British_English_female = "kate"; - public static $German_male = "stefan"; - public static $German_female = "katrin"; + public static $Arabic_male_maged = "maged"; + public static $Arabic_male_tarik = "tarik"; + public static $Arabic_female = "laila"; + public static $Bahasa_female = "damayanti"; + public static $Basque_female = "miren"; + public static $Bulgarian_female = "daria"; + public static $Cantonese_female = "sin-ji"; + public static $Catalan_female = "montserrat"; + public static $Catalan_male = "jordi"; + public static $Czech_female_iveta = "iveta"; + public static $Czech_female_zuzana = "zuzana"; + public static $Danish_female = "sara"; + public static $Danish_male = "magnus"; + public static $Belgian_Dutch_female = "ellen"; + public static $Australian_English_female = "karen"; + public static $Australian_English_male = "lee"; + public static $Indian_English_female = "veena"; + public static $Irish_English_female = "moira"; + public static $Scottish_English_female = "fiona"; + public static $South_African_English_female = "tessa"; + public static $Uk_English_female_kate = "kate"; + public static $Uk_English_female_serena = "serena"; + public static $Uk_English_male_daniel = "daniel"; + public static $Uk_English_male_oliver = "oliver"; + public static $US_English_female_ava = "ava"; + public static $US_English_female_evelyn = "evelyn"; + public static $US_English_female_samantha = "samantha"; + public static $US_English_female_susan = "susan"; + public static $US_English_female_zoe = "zoe"; + public static $US_English_female_allison = "allison"; + public static $US_English_male_tom = "tom"; + public static $US_English_male_victor = "victor"; + public static $Finnish_female = "satu"; + public static $Finnish_male = "onni"; + public static $French_female_audrey = "audrey"; + public static $French_female_aurelie = "aurelie"; + public static $French_male = "thomas"; + public static $Canadian_French_female_amelie = "amelie"; + public static $Canadian_French_female_chantal = "chantal"; + public static $Canadian_French_male = "nicolas"; + public static $Galician_female = "carmela"; + public static $German_female_anna = "anna"; + public static $German_female_petra = "petra"; + public static $German_male_markus = "markus"; + public static $German_male_yannick = "yannick"; + public static $Greek_female = "melina"; + public static $Greek_male = "nikos"; + public static $Hebrew_female = "carmit"; + public static $Hindi_female = "lekha"; + public static $Hungarian_female = "mariska"; + public static $Italian_female_alice = "alice"; + public static $Italian_female_federica = "federica"; public static $Italian_male = "luca"; - public static $Italian_female = "paola"; - public static $Dutch_male = "willem"; - public static $Dutch_female = "saskia"; - public static $Mexican_Spanish_male = "carlos"; - public static $Mexican_Spanish_female = "soledad"; + public static $Italian_male_paola = "paola"; + public static $Japanese_female = "kyoko"; + public static $Japanese_male = "otoya"; + public static $Korean_female = "sora"; + public static $Mandarin_female = "tian-tian"; + public static $Taiwanese_Mandarin_female = "mei-jia"; + public static $Norwegian_female = "nora"; + public static $Norwegian_male = "henrik"; + public static $Polish_female_ewa = "ewa"; + public static $Polish_female_zosia = "zosia"; + public static $Polish_male = "krzysztof"; + public static $Portuguese_female_catarina = "catarina"; + public static $Portuguese_female_joana = "joana"; + public static $Portuguese_male = "joaquim"; + public static $Brazilian_Portuguese_female = "luciana"; + public static $Brazilian_Portuguese_male = "felipe"; + public static $Russian_female_katya = "katya"; + public static $Russian_female_milena = "milena"; + public static $Russian_male = "yuri"; + public static $Slovak_male = "laura"; + public static $Argentinean_Spanish_male = "diego"; + public static $Castilian_Spanish_female = "monica"; + public static $Castilian_Spanish_male = "jorge"; + public static $Colombian_Spanish_female = "soledad"; + public static $Colombian_Spanish_male = "carlos"; + public static $Mexican_Spanish_female_angelica = "angelica"; + public static $Mexican_Spanish_female_paulina = "paulina"; + public static $Mexican_Spanish_male = "juan"; + public static $Dutch_male = "xander"; + public static $Dutch_female = "claire"; + public static $Swedish_female_alva = "alva"; + public static $Swedish_female_klara = "klara"; + public static $Swedish_male = "oskar"; + public static $Thai_female = "kanya"; + public static $Turkish_female = "yelda"; + public static $Turkish_male = "cem"; + public static $Valencian_female = "empar"; + } /** From 712c3572b0b7f6ff98c25895fdba2f737b9269a0 Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 8 Jun 2017 15:50:09 +0800 Subject: [PATCH 074/107] bug TROPO-11248 tropo-webapi-php on --- tests/OnTest.php | 141 +++++++++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 84 ++++++++++++++-------------- 2 files changed, 182 insertions(+), 43 deletions(-) create mode 100644 tests/OnTest.php diff --git a/tests/OnTest.php b/tests/OnTest.php new file mode 100644 index 0000000..7f8e184 --- /dev/null +++ b/tests/OnTest.php @@ -0,0 +1,141 @@ +on($on); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"on":[{"event":"continue","next":"say.php","say":{"value":"object continue!"}}]}]}'); + } + + public function testCreateOnObject1() { + + $tropo = new Tropo(); + $say = new Say("array continue!", null, null, null, null, null, null, null); + $on = array( + 'event' => 'continue', + 'next' => 'say.php', + 'say' => $say + ); + $tropo->on($on); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"on":[{"event":"continue","next":"say.php","say":{"value":"array continue!"}}]}]}'); + } + + public function testFailsOnWithNoEventParameter() { + $tropo = new Tropo(); + try { + $say = new Say("object continue!", null, null, null, null, null, null, null); + @ $on = new On(); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoEventParameter1() { + $tropo = new Tropo(); + try { + $say = new Say("object continue!", null, null, null, null, null, null, null); + @ $on = new On(null, "say.php", $say); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoEventParameter2() { + $tropo = new Tropo(); + try { + $say = new Say("object continue!", null, null, null, null, null, null, null); + @ $on = new On("", "say.php", $say); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoEventParameter3() { + $tropo = new Tropo(); + try { + $say = new Say("object continue!", null, null, null, null, null, null, null); + $say = new Say("array continue!", null, null, null, null, null, null, null); + @ $on = array( + 'next' => 'say.php', + 'say' => $say + ); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoEventParameter4() { + $tropo = new Tropo(); + try { + $say = new Say("array continue!", null, null, null, null, null, null, null); + $on = array( + 'event' => null, + 'next' => 'say.php', + 'say' => $say + ); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoEventParameter5() { + $tropo = new Tropo(); + try { + $say = new Say("array continue!", null, null, null, null, null, null, null); + $on = array( + 'event' => '', + 'next' => 'say.php', + 'say' => $say + ); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + } + } + + public function testFailsOnWithNoSayParameter() { + $tropo = new Tropo(); + try { + $on = new On("continue", "say.php"); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); + } + } + + public function testFailsOnWithNoSayParameter1() { + $tropo = new Tropo(); + try { + $on = array( + 'event' => 'continue', + 'next' => 'say.php' + ); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); + } + } + + public function testFailsOnWithNoSayParameter2() { + $tropo = new Tropo(); + try { + $on = array( + 'event' => 'continue', + 'next' => 'say.php', + 'say' => null + ); + $tropo->on($on); + } catch (Exception $e) { + $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); + } + } +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 7acd3bb..6567f48 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -206,16 +206,28 @@ public function message($message, Array $params=null) { * @see https://www.tropo.com/docs/webapi/on.htm */ public function on($on) { - if (!is_object($on) && is_array($on)) { - $params = $on; - if ((array_key_exists('say', $params) && ((array_key_exists('voice', $params) || isset($this->_voice))))){ - $v = isset($params["voice"]) ? $params["voice"] : $this->_voice; - $say = new Say($params["say"], null, null, $v); - }else{ - $say = (array_key_exists('say', $params)) ? new Say($params["say"]) : null; + if (!is_object($on)) { + if (is_array($on)) { + $params = $on; + if (!$params["event"]) { + throw new Exception("Missing required property: 'event'"); + } + if (!$params["say"]) { + throw new Exception("Missing required property: 'say'"); + } + if (!is_object($params["say"])) { + throw new Exception("Property 'say' must be a Say object"); + } + $next = (array_key_exists('next', $params)) ? $params["next"] : null; + $on = new On($params["event"], $next, $params["say"]); + } + } else { + if(!($on->getEvent())) { + throw new Exception("Missing required property: 'event'"); + } + if(!($on->getSay())) { + throw new Exception("Missing required property: 'say'"); } - $next = (array_key_exists('next', $params)) ? $params["next"] : null; - $on = new On($params["event"], $next, $say); } $this->on = array(sprintf('%s', $on)); } @@ -1131,11 +1143,16 @@ class On extends BaseClass { private $_event; private $_next; private $_say; - private $_voice; private $_ask; - private $_message; - private $_wait; - private $_order; + private $_post; + + public function getEvent() { + return $this->_event; + } + + public function getSay() { + return $this->_say; + } /** * Class constructor @@ -1145,15 +1162,15 @@ class On extends BaseClass { * @param Say $say * @param string $voice */ - public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null, $ask=NULL, Message $message=NULL, Wait $wait=NULL, $order=NULL) { + public function __construct($event, $next=NULL, Say $say=NULL, Ask $ask=NULL, $post=NULL) { + if(!$event) { + throw new Exception("Missing required property: 'event'"); + } $this->_event = $event; $this->_next = $next; $this->_say = isset($say) ? sprintf('%s', $say) : null ; - $this->_voice = $voice; $this->_ask = isset($ask) ? sprintf('%s', $ask) : null; - $this->_message = isset($message) ? sprintf('%s', $message) : null; - $this->_wait = isset($wait) ? sprintf('%s', $wait) : null; - $this->_order = $order; + $this->_post = $post; } /** @@ -1161,31 +1178,12 @@ public function __construct($event=NULL, $next=NULL, Say $say=NULL, $voice=Null, * */ public function __toString() { - - if($this->_event == "connect") { - $this->event = $this->_event; - switch($this->_order){ - case 'ask': - $this->ask = $this->_ask; - break; - case 'say': - $this->say = $this->_say; - break; - case 'wait': - $this->ask = $this->_ask; - break; - case 'message': - $this->message = $this->_message; - break; - } - return $this->unescapeJSON(json_encode(($this))); - }else{ - if(isset($this->_event)) { $this->event = $this->_event; } - if(isset($this->_next)) { $this->next = $this->_next; } - if(isset($this->_say)) { $this->say = $this->_say; } - if(isset($this->_voice)) { $this->voice = $this->_voice; } - return $this->unescapeJSON(json_encode($this)); - } + if(isset($this->_event)) { $this->event = $this->_event; } + if(isset($this->_next)) { $this->next = $this->_next; } + if(isset($this->_say)) { $this->say = $this->_say; } + if(isset($this->_ask)) { $this->ask = $this->_ask; } + if(isset($this->_post)) { $this->post = $this->_post; } + return $this->unescapeJSON(json_encode($this)); } } From 60ac6a71dcc7e4a5ee9093f558ca03a9b6106770 Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 8 Jun 2017 17:52:20 +0800 Subject: [PATCH 075/107] bug TROPO-11283 tropo-webapi-php session --- tests/SessionTest.php | 32 ++++++++++++++++++++++++++++++++ tropo.class.php | 16 +++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/SessionTest.php diff --git a/tests/SessionTest.php b/tests/SessionTest.php new file mode 100644 index 0000000..80c437f --- /dev/null +++ b/tests/SessionTest.php @@ -0,0 +1,32 @@ +","x-sid":"6f1e3b7b2ace2a7785780b6337641388","User-Agent":"X-Lite release 4.9.7.1 stamp 83369","From":";tag=1bb1ef33","Supported":"replaces","Allow":"SUBSCRIBE\\r\\nNOTIFY\\r\\nINVITE\\r\\nACK\\r\\nCANCEL\\r\\nBYE\\r\\nREFER\\r\\nINFO\\r\\nOPTIONS\\r\\nMESSAGE","Via":"SIP/2.0/UDP 192.168.26.111:5060;branch=z9hG4bK1vxcouwp4r78j;rport=5060\\r\\nSIP/2.0/UDP 192.168.26.1:5678;branch=z9hG4bK-524287-1---b1f649005b368755;rport=5678","Contact":"","To":"","Content-Length":"335","Content-Type":"application/sdp"}}}'; + $session = new Session($strSession); + $this->assertEquals($session->getId(), '35b00c154f2fecacba37fad74e64a7e2'); + $this->assertEquals($session->getAccountId(), '1'); + $this->assertEquals($session->getTimestamp(), '2017-06-08T03:40:19.283Z'); + $this->assertEquals($session->getUserType(), 'HUMAN'); + $this->assertEquals($session->getInitialText(), null); + $this->assertEquals($session->getCallId(), 'c5b298fc0785fda9029f7f3b5aeef7ab'); + $to = $session->getTo(); + $this->assertEquals($to["id"], '9992801029'); + $this->assertEquals($to["e164Id"], '9992801029'); + $this->assertEquals($to["name"], '9992801029'); + $this->assertEquals($to["channel"], 'VOICE'); + $this->assertEquals($to["network"], 'SIP'); + $from = $session->getFrom(); + $this->assertEquals($from["id"], 'pengxli'); + $this->assertEquals($from["e164Id"], 'pengxli'); + $this->assertEquals($from["name"], 'pengxli'); + $this->assertEquals($from["channel"], 'VOICE'); + $this->assertEquals($from["network"], 'SIP'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 6567f48..f597400 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1550,6 +1550,7 @@ class Session { private $_from; private $_headers; private $_parameters; + private $_userType; /** * Class constructor @@ -1574,15 +1575,18 @@ public function __construct($json=NULL) { $this->_callId = $session->session->callId; $this->_timestamp = $session->session->timestamp; $this->_initialText = $session->session->initialText; + $this->_userType = $session->session->userType; $this->_to = isset($session->session->to) ? array( - "id" => $session->session->to->id, + "id" => $session->session->to->id, + "e164Id" => $session->session->to->e164Id, "channel" => $session->session->to->channel, "name" => $session->session->to->name, "network" => $session->session->to->network ) : array( "id" => null, + "e164Id" => null, "channel" => null, "name" => null, "network" => null @@ -1590,12 +1594,14 @@ public function __construct($json=NULL) { $this->_from = isset($session->session->from->id) ? array( "id" => $session->session->from->id, + "e164Id" => $session->session->from->e164Id, "channel" => $session->session->from->channel, "name" => $session->session->from->name, "network" => $session->session->from->network ) : array( "id" => null, + "e164Id" => null, "channel" => null, "name" => null, "network" => null @@ -1611,6 +1617,10 @@ public function getId() { return $this->_id; } + public function getE164Id() { + return $this->_e164Id; + } + public function getAccountID() { return $this->_accountId; } @@ -1647,6 +1657,10 @@ public function getHeaders() { return $this->_headers; } + public function getUserType() { + return $this->_userType; + } + /** * Returns the query string parameters for the session api * From e465930428a93d77393e306f72be9cdafb0913b2 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 12 Jun 2017 10:37:32 +0800 Subject: [PATCH 076/107] bug TROPO-11230 tropo-webapi-php ask --- tests/AskTest.php | 48 ++--------------- tests/OnTest.php | 11 ++-- tropo.class.php | 134 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 108 insertions(+), 85 deletions(-) diff --git a/tests/AskTest.php b/tests/AskTest.php index 2ffb56a..572b21a 100644 --- a/tests/AskTest.php +++ b/tests/AskTest.php @@ -1,5 +1,5 @@ askJson = '{"bargein":true,"choices":{"value":"[5 DIGITS]"},"name":"foo","requied":true,"say":{"value":"Please say your account number"},"timeout":30}'; + $this->askJson = '{"bargein":true,"choices":{"value":"[5 DIGITS]"},"name":"foo","required":true,"say":[{"value":"Please say your account number"}],"timeout":30}'; $this->expected = '{"tropo":[{"ask":' . $this->askJson . '}]}'; } public function testCreateAskObject() { - $say = new Say("Please say your account number"); + $say = array(new Say("Please say your account number")); $choices = new Choices("[5 DIGITS]"); $ask = new Ask(NULL, true, $choices, NULL, "foo", true, $say, 30); $this->assertEquals($this->askJson, sprintf($ask)); @@ -23,52 +23,12 @@ public function testCreateAskObject() public function testAskFromObject() { - $say = new Say("Please say your account number"); + $say = array(new Say("Please say your account number")); $choices = new Choices("[5 DIGITS]"); $ask = new Ask(NULL, true, $choices, NULL, "foo", true, $say, 30); $tropo = new Tropo(); $tropo->Ask($ask); $this->assertEquals($this->expected, sprintf($tropo)); } - - public function testAskWithOptions() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]"); - $options = array( - 'choices' => $choices, - 'say' => $say - ); - $tropo = new Tropo(); - $tropo->Ask($options); - $this->assertEquals($this->expected, sprintf($tropo)); - } - - public function testAskWithOptionsInDifferentOrder() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]"); - $options = array( - 'say' => $say, - 'choices' => $choices - ); - $tropo = new Tropo(); - $tropo->Ask($options); - $this->assertEquals($this->expected, sprintf($tropo)); - } - - public function testAskWithNullOptions() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]"); - $options = array( - 'say' => $say, - 'choices' => $choices, - 'timeout' => NULL - ); - $tropo = new Tropo(); - $tropo->Ask($options); - $this->assertEquals($this->expected, sprintf($tropo)); - } } ?> \ No newline at end of file diff --git a/tests/OnTest.php b/tests/OnTest.php index 7f8e184..aeda0d5 100644 --- a/tests/OnTest.php +++ b/tests/OnTest.php @@ -8,7 +8,7 @@ public function testCreateOnObject() { $tropo = new Tropo(); $say = new Say("object continue!", null, null, null, null, null, null, null); - $on = new On("continue", "say.php", $say); + $on = new On(Event::$continue, "say.php", $say); $tropo->on($on); $this->assertEquals(sprintf($tropo), '{"tropo":[{"on":[{"event":"continue","next":"say.php","say":{"value":"object continue!"}}]}]}'); } @@ -18,7 +18,7 @@ public function testCreateOnObject1() { $tropo = new Tropo(); $say = new Say("array continue!", null, null, null, null, null, null, null); $on = array( - 'event' => 'continue', + 'event' => Event::$continue, 'next' => 'say.php', 'say' => $say ); @@ -59,7 +59,6 @@ public function testFailsOnWithNoEventParameter2() { public function testFailsOnWithNoEventParameter3() { $tropo = new Tropo(); try { - $say = new Say("object continue!", null, null, null, null, null, null, null); $say = new Say("array continue!", null, null, null, null, null, null, null); @ $on = array( 'next' => 'say.php', @@ -104,7 +103,7 @@ public function testFailsOnWithNoEventParameter5() { public function testFailsOnWithNoSayParameter() { $tropo = new Tropo(); try { - $on = new On("continue", "say.php"); + $on = new On(Event::$continue, "say.php"); $tropo->on($on); } catch (Exception $e) { $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); @@ -115,7 +114,7 @@ public function testFailsOnWithNoSayParameter1() { $tropo = new Tropo(); try { $on = array( - 'event' => 'continue', + 'event' => Event::$continue, 'next' => 'say.php' ); $tropo->on($on); @@ -128,7 +127,7 @@ public function testFailsOnWithNoSayParameter2() { $tropo = new Tropo(); try { $on = array( - 'event' => 'continue', + 'event' => Event::$continue, 'next' => 'say.php', 'say' => null ); diff --git a/tropo.class.php b/tropo.class.php index f597400..45ece2a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -93,30 +93,12 @@ public function setLanguage($language) { * @param array $params * @see https://www.tropo.com/docs/webapi/ask.htm */ - public function ask($ask, Array $params=NULL) { - if(!is_object($ask)) { - $p = array('as','event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer', 'interdigitTimeout', 'sensitivity', 'speechCompleteTimeout', 'speechIncompleteTimeout'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - if (is_array($event)) { - // If an event was passed in, add the events to the Ask - foreach ($event as $e => $val){ - $say[] = new Say($val, $as, $e, $voice); - } - } - $say[] = new Say($ask, $as, null, $voice); - $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; - $params["dtmf"] = isset($params["dtmf"]) ? $params["dtmf"] : null; - $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; - if (!isset($voice) && isset($this->_voice)) { - $voice = $this->_voice; - } - $choices = isset($params["choices"]) ? new Choices($params["choices"], $params["mode"], $params["terminator"]) : null; - $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer, $interdigitTimeout, $sensitivity, $speechCompleteTimeout, $speechIncompleteTimeout); + public function ask(Ask $ask) { + if(!($ask->getChoices())) { + throw new Exception("Missing required property: 'choices'"); + } + if(!($ask->getSay())) { + throw new Exception("Missing required property: 'say'"); } $this->ask = sprintf('%s', $ask); } @@ -788,6 +770,17 @@ class Ask extends BaseClass { private $_sensitivity; private $_speechCompleteTimeout; private $_speechIncompleteTimeout; + private $_promptLogSecurity; + private $_asrLogSecurity; + private $_maskTemplate; + + public function getChoices() { + return $this->_choices; + } + + public function getSay() { + return $this->_say; + } /** * Class constructor @@ -807,7 +800,13 @@ class Ask extends BaseClass { * @param float $speechCompleteTimeout * @param float $speechIncompleteTimeout */ - public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL, $minConfidence=NULL, $name=NULL, $required=NULL, $say=NULL, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL, $interdigitTimeout=NULL, $sensitivity=NULL, $speechCompleteTimeout=NULL, $speechIncompleteTimeout=NULL) { + public function __construct($attempts=NULL, $bargein=NULL, Choices $choices, $minConfidence=NULL, $name=NULL, $required=NULL, $say, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL, $interdigitTimeout=NULL, $sensitivity=NULL, $speechCompleteTimeout=NULL, $speechIncompleteTimeout=NULL, $promptLogSecurity=NULL, $asrLogSecurity=NULL, $maskTemplate=NULL) { + if(!$choices) { + throw new Exception("Missing required property: 'choices'"); + } + if(!$say) { + throw new Exception("Missing required property: 'say'"); + } $this->_attempts = $attempts; $this->_bargein = $bargein; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; @@ -823,6 +822,9 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices=NULL $this->_sensitivity = $sensitivity; $this->_speechCompleteTimeout = $speechCompleteTimeout; $this->_speechIncompleteTimeout = $speechIncompleteTimeout; + $this->_promptLogSecurity = $promptLogSecurity; + $this->_asrLogSecurity = $asrLogSecurity; + $this->_maskTemplate = $maskTemplate; } /** @@ -850,6 +852,9 @@ public function __toString() { if(isset($this->_sensitivity)) { $this->sensitivity = $this->_sensitivity; } if(isset($this->_speechCompleteTimeout)) { $this->speechCompleteTimeout = $this->_speechCompleteTimeout; } if(isset($this->_speechIncompleteTimeout)) { $this->speechIncompleteTimeout = $this->_speechIncompleteTimeout; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } + if(isset($this->_asrLogSecurity)) { $this->asrLogSecurity = $this->_asrLogSecurity; } + if(isset($this->_maskTemplate)) { $this->maskTemplate = $this->_maskTemplate; } return $this->unescapeJSON(json_encode($this)); } @@ -1617,10 +1622,6 @@ public function getId() { return $this->_id; } - public function getE164Id() { - return $this->_e164Id; - } - public function getAccountID() { return $this->_accountId; } @@ -2171,16 +2172,79 @@ class Voice { * */ class Recognizer { - public static $German = 'de-de'; - public static $British_English = 'en-gb'; + public static $Afrikaans = 'af-za'; + public static $Arabic = 'ar-ww'; + public static $Jordanian_Arabic = 'ar-jo'; + public static $Assamese = 'as-in'; + public static $Basque = 'eu-es'; + public static $Bengali = 'bn-bd'; + public static $Indian_Bengali = 'bn-bi'; + public static $Bhojpuri = 'bh-in'; + public static $Bulgarian = 'bg-bg'; + public static $Cantonese = 'cn-hk'; + public static $Catalan = 'ca-es'; + public static $Czech = 'cs-cz'; + public static $Danish = 'da-dk'; + public static $Dutch = 'nl-nl'; + public static $Belgian_Dutch = 'nl-be'; + public static $Australian_English = 'en-au'; + public static $Indian_English = 'en-in'; + public static $Singaporean_English = 'en-sg'; + public static $South_African_English = 'en-za'; + public static $UK_English = 'en-gb'; public static $US_English = 'en-us'; - public static $Castilian_Spanish = 'es-es'; - public static $Mexican_Spanish = 'es-mx'; - public static $French_Canadian = 'fr-ca'; + public static $Finnish = 'fi-fi'; public static $French = 'fr-fr'; + public static $Belgian_French = 'fr-be'; + public static $Canadian_French = 'fr-ca'; + public static $Galician = 'gl-es'; + public static $German = 'de-de'; + public static $Austrian_German = 'de-at'; + public static $Swiss_German = 'de-ch'; + public static $Greek = 'el-gr'; + public static $Gujarati = 'gu-in'; + public static $Hebrew = 'he-il'; + public static $Hindi = 'hi-in'; + public static $Hungarian = 'hu-hu'; + public static $Icelandic = 'is-is'; + public static $Indonesian = 'id-id'; public static $Italian = 'it-it'; + public static $Japanese = 'ja-jp'; + public static $Kannada = 'kn-in'; + public static $Korean = 'ko-kr'; + public static $Malay = 'ms-my'; + public static $Malayalam = 'ml-in'; + public static $Mandarin = 'zh-cn'; + public static $Taiwanese_Mandarin = 'zh-tw'; + public static $Marathi = 'mr-in'; + public static $Nepali = 'ne-np'; + public static $Norwegian = 'no-no'; + public static $Oriya = 'or-in'; public static $Polish = 'pl-pl'; - public static $Dutch = 'nl-nl'; + public static $Portuguese = 'pt-pt'; + public static $Brazilian_Portuguese = 'pt-br'; + public static $Punjabi = 'pa-in'; + public static $Romanian = 'ro-ro'; + public static $Russian = 'ru-ru'; + public static $Serbian = 'sr-rs'; + public static $Slovak = 'sk-sk'; + public static $Slovenian = 'sl-si'; + public static $Spanish = 'es-es'; + public static $Argentinian_Spanish = 'es-ar'; + public static $Colombian_Spanish = 'es-co'; + public static $Mexican_Spanish = 'es-us'; + public static $US_Spanish = 'es-us'; + public static $Swedish = 'sv-se'; + public static $Tamil = 'ta-in'; + public static $Telugu = 'te-in'; + public static $Thai = 'th-th'; + public static $Turkish = 'tr-tr'; + public static $Ukrainian = 'uk-ua'; + public static $Indian_Urdu = 'ur-in'; + public static $Pakistani_Urdu = 'ur-pk'; + public static $Valencian = 'va-es'; + public static $Vietnamese = 'vi-vn'; + public static $Welsh = 'cy-gb'; } /** From cf675b1ed7b649207356d0d675b0b1aff6aef0f4 Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 15 Jun 2017 12:10:44 +0800 Subject: [PATCH 077/107] bug TROPO-11245 tropo-webapi-php message --- tests/AskTest.php | 102 +++++++++++++++++----- tests/MessageTest.php | 102 +++++++++++++++------- tropo.class.php | 197 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 330 insertions(+), 71 deletions(-) diff --git a/tests/AskTest.php b/tests/AskTest.php index 572b21a..f75c79c 100644 --- a/tests/AskTest.php +++ b/tests/AskTest.php @@ -4,31 +4,93 @@ class AskTest extends PHPUnit_Framework_TestCase { - public $askJson; - public $expected; - public function AskTest() { - $this->askJson = '{"bargein":true,"choices":{"value":"[5 DIGITS]"},"name":"foo","required":true,"say":[{"value":"Please say your account number"}],"timeout":30}'; - $this->expected = '{"tropo":[{"ask":' . $this->askJson . '}]}'; + public function testAskWithMinOptions() { + $tropo = new Tropo(); + $params = array( + 'choices' => '[1 DIGIT]' + ); + $tropo->ask("Please say a digit.", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"choices":{"value":"[1 DIGIT]"},"say":[{"value":"Please say a digit."}]}}]}'); } - - public function testCreateAskObject() - { - $say = array(new Say("Please say your account number")); - $choices = new Choices("[5 DIGITS]"); - $ask = new Ask(NULL, true, $choices, NULL, "foo", true, $say, 30); - $this->assertEquals($this->askJson, sprintf($ask)); + + public function testAskWithExtraSayOptions() { + $tropo = new Tropo(); + $event = array( + 'timeout' => 'Sorry, I did not hear anything.', + 'nomatch:1' => "Don't think that was a year.", + 'nomatch:2' => 'Nope, still not a year.' + ); + $params = array( + 'choices' => '[4 DIGITS]', + 'event' => $event, + 'attempts' => 3 + ); + $tropo->ask("What is your birth year?", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"attempts":3,"choices":{"value":"[4 DIGITS]"},"say":[{"event":"timeout","value":"Sorry, I did not hear anything."},{"event":"nomatch:1","value":"Don'."'".'t think that was a year."},{"event":"nomatch:2","value":"Nope, still not a year."},{"value":"What is your birth year?"}]}}]}'); } + public function testAskWithAllOptions() { + $tropo = new Tropo(); + $allowSignals = array('quit', 'exit'); + $event = array( + 'timeout' => 'Sorry, I did not hear anything.', + 'nomatch:1' => "Don't think that was a year.", + 'nomatch:2' => 'Nope, still not a year.' + ); + $params = array( + 'choices' => '[4 DIGITS]', + 'mode' => 'dtmf', + 'terminator' => '#', + 'allowSignals' => $allowSignals, + 'attempts' => 3, + 'bargein' => true, + 'interdigitTimeout' => 5.0, + 'minConfidence' => 30.0, + 'name' => 'foo', + 'recognizer' => Recognizer::$US_English, + 'required' => true, + 'event' => $event, + 'sensitivity' => 0.5, + 'speechCompleteTimeout' => 0.5, + 'speechIncompleteTimeout' => 0.5, + 'timeout' => 30.0, + 'voice' => Voice::$US_English_female_allison, + 'promptLogSecurity' => 'suppress', + 'asrLogSecurity' => 'mask', + 'maskTemplate' => 'XXD-' + ); + $tropo->ask("What is your birth year?", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"attempts":3,"bargein":true,"choices":{"value":"[4 DIGITS]","mode":"dtmf","terminator":"#"},"minConfidence":30,"name":"foo","required":true,"say":[{"event":"timeout","value":"Sorry, I did not hear anything."},{"event":"nomatch:1","value":"Don'."'".'t think that was a year."},{"event":"nomatch:2","value":"Nope, still not a year."},{"value":"What is your birth year?"}],"timeout":30,"voice":"allison","allowSignals":["quit","exit"],"recognizer":"en-us","interdigitTimeout":5,"sensitivity":0.5,"speechCompleteTimeout":0.5,"speechIncompleteTimeout":0.5,"promptLogSecurity":"suppress","asrLogSecurity":"mask","maskTemplate":"XXD-"}}]}'); + } - public function testAskFromObject() - { - $say = array(new Say("Please say your account number")); - $choices = new Choices("[5 DIGITS]"); - $ask = new Ask(NULL, true, $choices, NULL, "foo", true, $say, 30); - $tropo = new Tropo(); - $tropo->Ask($ask); - $this->assertEquals($this->expected, sprintf($tropo)); + public function testCreateAskObject() { + $tropo = new Tropo(); + $ask = new Ask(null, null, new Choices('[1 DIGIT]'), null, null, null, array(new Say("Please say a digit."))); + $tropo ->ask($ask); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"choices":{"value":"[1 DIGIT]"},"say":[{"value":"Please say a digit."}]}}]}'); + } + + public function testCreateAskObject1() { + $tropo = new Tropo(); + $ask = new Ask(3, null, new Choices('[4 DIGITS]'), null, null, null, array(new Say("Sorry, I did not hear anything.", null, "timeout"), new Say("Don't think that was a year.", null , "nomatch:1"), new Say("Nope, still not a year.", null, "nomatch:2"), new Say("What is your birth year?"))); + $tropo ->ask($ask); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"attempts":3,"choices":{"value":"[4 DIGITS]"},"say":[{"event":"timeout","value":"Sorry, I did not hear anything."},{"event":"nomatch:1","value":"Don'."'".'t think that was a year."},{"event":"nomatch:2","value":"Nope, still not a year."},{"value":"What is your birth year?"}]}}]}'); + } + + public function testCreateAskObject2() { + $tropo = new Tropo(); + $choices = new Choices("[4 DIGITS]", "dtmf", "#"); + $say = array( + new Say("Sorry, I did not hear anything.", null, "timeout"), + new Say("Don't think that was a year.", null , "nomatch:1"), + new Say("Nope, still not a year.", null, "nomatch:2"), + new Say("What is your birth year?") + ); + $allowSignals = array('quit', 'exit'); + $ask = new Ask(3, true, $choices, 30.0, "foo", true, $say, 30.0, Voice::$US_English_female_allison, $allowSignals, Recognizer::$US_English, 5.0, 0.5, 0.5, 0.5, "suppress", "mask", "XXD-"); + $tropo ->ask($ask); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"ask":{"attempts":3,"bargein":true,"choices":{"value":"[4 DIGITS]","mode":"dtmf","terminator":"#"},"minConfidence":30,"name":"foo","required":true,"say":[{"event":"timeout","value":"Sorry, I did not hear anything."},{"event":"nomatch:1","value":"Don'."'".'t think that was a year."},{"event":"nomatch:2","value":"Nope, still not a year."},{"value":"What is your birth year?"}],"timeout":30,"voice":"allison","allowSignals":["quit","exit"],"recognizer":"en-us","interdigitTimeout":5,"sensitivity":0.5,"speechCompleteTimeout":0.5,"speechIncompleteTimeout":0.5,"promptLogSecurity":"suppress","asrLogSecurity":"mask","maskTemplate":"XXD-"}}]}'); } } ?> \ No newline at end of file diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 86b538a..292cc87 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -1,50 +1,88 @@ partial = '{"say":{"value":"This is an announcement"},"to":"3055195825","channel":"TEXT","network":"SMS","from":"3055551212","voice":"kate","timeout":10,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"}}'; - $this->expected = '{"tropo":[{"message":' . $this->partial . '}]}'; + public function testMessageWithMinOptions() { + $tropo = new Tropo(); + $params = array( + 'to' => 'sip:pengxli@192.168.26.1:5678', + 'name' => 'foo' + ); + $tropo->message("This is an announcement.", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); } - public function testUseAllOptions() - { + public function testMessageWithExtraSayOptiions() { $tropo = new Tropo(); - $options = array( - 'to' => "3055195825", - 'from' => "3055551212", - 'network' => "SMS", - 'channel' => "TEXT", - 'answerOnMedia' => false, - 'timeout' => 10, - 'headers' => array('foo'=>'bar','bling'=>'baz'), - 'voice' => 'kate' + $say = "Remember, you have a meeting at 2 PM."; + $params = array( + 'say' => $say, + 'to' => 'sip:pengxli@192.168.26.1:5678', + 'name' => 'foo' ); - $tropo->message("This is an announcement",$options); - $this->assertEquals($this->expected, sprintf($tropo)); + $tropo->message("This is an announcement.", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); } - public function testUseDifferentOptionsOrder() - { + public function testMessageWithAllOptions() { $tropo = new Tropo(); - $options = array( - 'from' => "3055551212", - 'network' => "SMS", - 'timeout' => 10, - 'headers' => array('foo'=>'bar','bling'=>'baz'), - 'channel' => "TEXT", - 'to' => "3055195825", + $say = array('Remember, you have a meeting at 2 PM.', 'This is tropo.com.'); + $to = array('sip:pengxli@192.168.26.1:5678', 'sip:pengxli@172.16.72.131:5678'); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $params = array( + 'say' => $say, + 'to' => $to, + 'name' => 'foo', 'answerOnMedia' => false, - 'voice' => 'kate' + 'channel' => Channel::$voice, + 'from' => '3055551000', + 'network' => Network::$sip, + 'required' => true, + 'timeout' => 60, + 'voice' => Voice::$US_English_female_allison, + 'promptLogSecurity' => 'suppress', + 'headers' => $headers + ); + $tropo->message("This is an announcement.", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo","channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); + } + + public function testCreateMinObject() { + $tropo = new Tropo(); + $message = new Message(new Say("This is an announcement."), "sip:pengxli@192.168.26.1:5678", "foo"); + $tropo->message($message); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + } + + public function testCreateObject1() { + $tropo = new Tropo(); + $say = array( + new Say("This is an announcement."), + new Say("Remember, you have a meeting at 2 PM.") + ); + $message = new Message($say, "sip:pengxli@192.168.26.1:5678", "foo"); + $tropo->message($message); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + } + + public function testCreateObject2() { + $tropo = new Tropo(); + $say = array( + new Say("This is an announcement."), + new Say("Remember, you have a meeting at 2 PM."), + new Say("This is tropo.com.") + ); + $to = array( + 'sip:pengxli@192.168.26.1:5678', + 'sip:pengxli@172.16.72.131:5678' ); - $tropo->message("This is an announcement",$options); - $this->assertEquals($this->expected, sprintf($tropo)); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $message = new Message($say, $to, "foo", Channel::$voice, Network::$sip, "3055551000", Voice::$US_English_female_allison, 60, false, $headers, true, "suppress"); + $tropo->message($message); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo","channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); } } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 45ece2a..d6f7b07 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -93,12 +93,68 @@ public function setLanguage($language) { * @param array $params * @see https://www.tropo.com/docs/webapi/ask.htm */ - public function ask(Ask $ask) { - if(!($ask->getChoices())) { - throw new Exception("Missing required property: 'choices'"); - } - if(!($ask->getSay())) { - throw new Exception("Missing required property: 'say'"); + public function ask($ask, Array $params=NULL) { + if ($ask instanceof Ask) { + + if(null === $ask->getChoices()) { + throw new Exception("Missing required property: 'choices'"); + } + if(null === $ask->getSay()) { + throw new Exception("Missing required property: 'say'"); + } + + } elseif (is_string($ask) && ($ask !== '')) { + + if (isset($params) && is_array($params)) { + + if (array_key_exists('choices', $params)) { + + if ($params["choices"] instanceof Choices) { + + $choices = $params["choices"]; + + } elseif (is_string($params['choices']) && ($params['choices'] !== '')) { + + $params["mode"] = isset($params["mode"]) ? $params["mode"] : null; + $params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null; + $choices = new Choices($params["choices"], $params["mode"], $params["terminator"]); + + } else { + + throw new Exception("'choices' must be is a string or an instance of Choices."); + + } + } else { + + throw new Exception("Missing required property: 'choices'"); + + } + + $p = array('event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer', 'interdigitTimeout', 'sensitivity', 'speechCompleteTimeout', 'speechIncompleteTimeout', 'promptLogSecurity', 'asrLogSecurity', 'maskTemplate'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + if (is_array($event)) { + foreach ($event as $e => $val){ + $say[] = new Say($val, null, $e); + } + } + $say[] = new Say($ask); + $ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer, $interdigitTimeout, $sensitivity, $speechCompleteTimeout, $speechIncompleteTimeout, $promptLogSecurity, $asrLogSecurity, $maskTemplate); + + } else { + + throw new Exception("When Argument 1 passed to Tropo::ask() is a string, argument 2 passed to Tropo::ask() must be of the type array."); + + } + + } else { + + throw new Exception("Argument 1 passed to Tropo::ask() must be a string or an instance of Ask."); + } $this->ask = sprintf('%s', $ask); } @@ -165,18 +221,81 @@ public function hangup() { * @see https://www.tropo.com/docs/webapi/message.htm */ public function message($message, Array $params=null) { - if(!is_object($message)) { - $say = new Say($message); - $to = $params["to"]; - $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; + + if ($message instanceof Message) { + if(null === $message->getSay()) { + throw new Exception("Missing required property: 'say'"); + } + if(null === $message->getTo()) { + throw new Exception("Missing required property: 'to'"); + } + if(null === $message->getName()) { + throw new Exception("Missing required property: 'name'"); + } + } elseif (is_string($message) && ($message!=='')) { + + if (isset($params) && is_array($params)) { + if (array_key_exists('to', $params)) { + if (is_array($params["to"])) { + foreach ($params["to"] as $value) { + if (is_string($value) && ($value !== '')) { + $to[] = $value; + } else { + throw new Exception("'to' must be is a string or an array of string."); + } + } + } elseif (is_string($params["to"]) && ($params["to"]!=='')) { + $to = $params["to"]; + } else { + throw new Exception("'to' must be is a string or an array of string."); + } + } else { + throw new Exception("Missing required property: 'to'"); + } + + if (array_key_exists('name', $params)) { + if (is_string($params["name"]) && ($params["name"] !=='')) { + $name = $params["name"]; + } else { + throw new Exception("'name' must be is a string."); + } + } else { + throw new Exception("Missing required property: 'name'"); + } + + if (array_key_exists('say', $params)) { + if (is_array($params["say"])) { + $say[] = new Say($message); + foreach ($params["say"] as $value) { + if (is_string($value) && ($value !== '')) { + $say[] = new Say($value); + } + } + } elseif (is_string($params["say"]) && ($params["say"] !== '')) { + $say[] = new Say($message); + $say[] = new Say($params["say"]); + } else { + $say = new Say($message); + } + } else { + $say = new Say($message); } + + $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers','required','promptLogSecurity'); + foreach ($p as $option) { + $$option = null; + if (is_array($params) && array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $message = new Message($say, $to, $name, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers, $required, $promptLogSecurity); + } else { + throw new Exception("When Argument 1 passed to Tropo::message() is a string, argument 2 passed to Tropo::message() must be of the type array."); } - $message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers); + } else { + throw new Exception("Argument 1 passed to Tropo::message() must be a string or an instance of Message."); } + $this->message = sprintf('%s', $message); } @@ -801,10 +920,10 @@ public function getSay() { * @param float $speechIncompleteTimeout */ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices, $minConfidence=NULL, $name=NULL, $required=NULL, $say, $timeout=NULL, $voice=NULL, $allowSignals=NULL, $recognizer=NULL, $interdigitTimeout=NULL, $sensitivity=NULL, $speechCompleteTimeout=NULL, $speechIncompleteTimeout=NULL, $promptLogSecurity=NULL, $asrLogSecurity=NULL, $maskTemplate=NULL) { - if(!$choices) { + if(!isset($choices)) { throw new Exception("Missing required property: 'choices'"); } - if(!$say) { + if(!isset($say)) { throw new Exception("Missing required property: 'say'"); } $this->_attempts = $attempts; @@ -1094,6 +1213,21 @@ class Message extends BaseClass { private $_timeout; private $_answerOnMedia; private $_headers; + private $_name; + private $_required; + private $_promptLogSecurity; + + public function getSay() { + return $this->_say; + } + + public function getTo() { + return $this->_to; + } + + public function getName() { + return $this->_name; + } /** * Class constructor @@ -1108,9 +1242,23 @@ class Message extends BaseClass { * @param boolean $answerOnMedia * @param array $headers */ - public function __construct(Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null) { - $this->_say = isset($say) ? sprintf('%s', $say) : null ; + public function __construct($say, $to, $name, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null, $required=null, $promptLogSecurity=null) { + if(!isset($say)) { + throw new Exception("Missing required property: 'say'"); + } + if(!isset($to)) { + throw new Exception("Missing required property: 'to'"); + } + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } + if ($say instanceof Say) { + $this->_say = sprintf('%s', $say); + } else { + $this->_say = $say; + } $this->_to = $to; + $this->_name = $name; $this->_channel = $channel; $this->_network = $network; $this->_from = $from; @@ -1118,6 +1266,8 @@ public function __construct(Say $say, $to, $channel=null, $network=null, $from=n $this->_timeout = $timeout; $this->_answerOnMedia = $answerOnMedia; $this->_headers = $headers; + $this->_required = $required; + $this->_promptLogSecurity = $promptLogSecurity; } /** @@ -1126,7 +1276,13 @@ public function __construct(Say $say, $to, $channel=null, $network=null, $from=n */ public function __toString() { $this->say = $this->_say; + if (is_array($this->_say)) { + foreach ($this->_say as $k => $v) { + $this->_say[$k] = sprintf('%s', $v); + } + } $this->to = $this->_to; + $this->name = $this->_name; if(isset($this->_channel)) { $this->channel = $this->_channel; } if(isset($this->_network)) { $this->network = $this->_network; } if(isset($this->_from)) { $this->from = $this->_from; } @@ -1134,6 +1290,8 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } if(count($this->_headers)) { $this->headers = $this->_headers; } + if(count($this->_required)) { $this->required = $this->_required; } + if(count($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } return $this->unescapeJSON(json_encode($this)); } } @@ -2048,6 +2206,7 @@ class Network { public static $sms = "SMS"; public static $yahoo = "YAHOO"; public static $twitter = "TWITTER"; + public static $sip = "SIP"; } /** From a54d03ac2bfcb9e0e30df6bb14b9db3ffeb0730f Mon Sep 17 00:00:00 2001 From: pengxli Date: Sat, 17 Jun 2017 11:29:46 +0800 Subject: [PATCH 078/107] bug TROPO-11233 tropo-webapi-php call --- tests/CallTest.php | 148 +++++++++++++++++++++++++++++++-------------- tropo.class.php | 103 +++++++++++++++++++++++++++---- 2 files changed, 194 insertions(+), 57 deletions(-) diff --git a/tests/CallTest.php b/tests/CallTest.php index 023ef57..064aa15 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -1,66 +1,126 @@ call("3055195825"); - $this->assertEquals('{"tropo":[{"call":{"to":"3055195825"}}]}', sprintf($tropo)); + $params = array( + 'name' => 'foo' + ); + $tropo->call("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + } + + public function testCallWithExtraToOptiions() { + $tropo = new Tropo(); + $params = array( + 'to' => 'sip:pengxli@172.16.72.131:5678', + 'name' => 'foo' + ); + $tropo->call("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo"}}]}'); + } + + public function testCallWithExtraToOptiions1() { + $tropo = new Tropo(); + $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); + $params = array( + 'to' => $to, + 'name' => 'foo' + ); + $tropo->call("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"name":"foo"}}]}'); } - - public function testUseAllOptions() - { + + public function testCallWithAllOptions() { $tropo = new Tropo(); - $rec = new StartRecording('recording','audio/mp3','POST','password','http://blah.com/recordings/1234.wav','jose'); - $options = array( - 'from' => "3055551212", - 'network' => "SMS", - 'channel' => "TEXT", - 'answerOnMedia' => false, - 'timeout' => 10, - 'headers' => array('foo'=>'bar','bling'=>'baz'), - 'recording' => $rec + $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); + $allowSignals = array('exit', 'quit'); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $params = array( + 'to' => $to, + 'allowSignals' => $allowSignals, + 'answerOnMedia' => false, + 'channel' => Channel::$voice, + 'from' => '3055551000', + 'headers' => $headers, + 'machineDetection' => false, + 'name' => 'foo', + 'network' => Network::$sip, + 'required' => true, + 'timeout' => 30.0, + 'voice' => Voice::$US_English_female_allison, + 'callbackUrl' => 'http://192.168.26.203/result.php', + 'promptLogSecurity' => 'suppress', + 'label' => 'callLabel' ); - $tropo->call("3055195825",$options); - $this->assertEquals('{"tropo":[{"call":{"to":"3055195825","from":"3055551212","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"recording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}}]}', sprintf($tropo)); + $tropo->call("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } - public function testUseDifferentOptionsOrder() - { + public function testCallWithAllOptions1() { $tropo = new Tropo(); - $rec = new StartRecording('recording','audio/mp3','POST','password','http://blah.com/recordings/1234.wav','jose'); - $options = array( - 'answerOnMedia' => false, - 'timeout' => 10, - 'network' => "SMS", - 'channel' => "TEXT", - 'headers' => array('foo'=>'bar','bling'=>'baz'), - 'recording' => $rec, - 'from' => "3055551212" + $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); + $allowSignals = array('exit', 'quit'); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $params = array( + 'to' => $to, + 'allowSignals' => $allowSignals, + 'answerOnMedia' => false, + 'channel' => Channel::$voice, + 'from' => '3055551000', + 'headers' => $headers, + 'machineDetection' => "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.", + 'name' => 'foo', + 'network' => Network::$sip, + 'required' => true, + 'timeout' => 30.0, + 'voice' => Voice::$US_English_female_allison, + 'callbackUrl' => 'http://192.168.26.203/result.php', + 'promptLogSecurity' => 'suppress', + 'label' => 'callLabel' ); - $tropo->call("3055195825",$options); - $this->assertEquals('{"tropo":[{"call":{"to":"3055195825","from":"3055551212","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"recording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}}]}', sprintf($tropo)); + $tropo->call("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + } + + public function testCreateMinObject() { + $tropo = new Tropo(); + $call = new Call("sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, null, null, null, "foo"); + $tropo->call($call); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); } - public function testCreateCallObject() - { - $rec = new StartRecording('recording','audio/mp3','POST','password','http://blah.com/recordings/1234.wav','jose'); - $call = new Call("3055195825","3055551212","SMS","TEXT",false,10,array('foo'=>'bar','bling'=>'baz'),$rec); - $this->assertEquals('{"to":"3055195825","from":"3055551212","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"recording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}', sprintf($call)); + public function testCreateObject1() { + $tropo = new Tropo(); + $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); + $call = new Call($to, null, null, null, null, null, null, null, null, null, null, "foo"); + $tropo->call($call); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo"}}]}'); } - public function testCallUsingCallObject() - { + public function testCreateObject2() { $tropo = new Tropo(); - $rec = new StartRecording('recording','audio/mp3','POST','password','http://blah.com/recordings/1234.wav','jose'); - $call = new Call("3055195825","3055551212","SMS","TEXT",false,10,array('foo'=>'bar','bling'=>'baz'),$rec); - $tropo->call($call); - $this->assertEquals('{"tropo":[{"call":{"to":"3055195825","from":"3055551212","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"recording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}}]}', sprintf($tropo)); + $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); + $allowSignals = array('exit', 'quit'); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, false, Voice::$US_English_female_allison, "foo", true, "http://192.168.26.203/result.php", "suppress", "callLabel"); + $tropo->call($call); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } + public function testCreateObject3() { + $tropo = new Tropo(); + $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); + $allowSignals = array('exit', 'quit'); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $machineDetection = "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered."; + $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, $machineDetection, Voice::$US_English_female_allison, "foo", true, "http://192.168.26.203/result.php", "suppress", "callLabel"); + $tropo->call($call); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + } } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index d6f7b07..104a8f4 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -167,15 +167,65 @@ public function ask($ask, Array $params=NULL) { * @see https://www.tropo.com/docs/webapi/call.htm */ public function call($call, Array $params=NULL) { - if(!is_object($call)) { - $p = array('to', 'from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'recording', 'allowSignals', 'machineDetection', 'voice'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; + if ($call instanceof Call) { + + if(null === $call->getTo()) { + throw new Exception("Missing required property: 'to'"); + } + if(null === $call->getName()) { + throw new Exception("Missing required property: 'name'"); + } + + } elseif (is_string($call) && ($call !== '')) { + + if (isset($params) && is_array($params)) { + + if (array_key_exists('name', $params)) { + if (is_string($params["name"]) && ($params["name"] !=='')) { + $name = $params["name"]; + } else { + throw new Exception("'name' must be is a string."); + } + } else { + throw new Exception("Missing required property: 'name'"); + } + + if (array_key_exists('to', $params)) { + if (is_array($params["to"])) { + $to[] = $call; + foreach ($params["to"] as $value) { + if (is_string($value) && ($value !== '')) { + $to[] = $value; + } + } + } elseif (is_string($params["to"]) && ($params["to"] !== '')) { + $to[] = $call; + $to[] = $params["to"]; + } else { + $to = $call; + } + } else { + $to = $call; } + + $p = array('from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'allowSignals', 'machineDetection', 'voice', 'required', 'callbackUrl', 'promptLogSecurity', 'label'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $call = new Call($to, $from, $network, $channel, $answerOnMedia, $timeout, $headers, null, $allowSignals, $machineDetection, $voice, $name, $required, $callbackUrl, $promptLogSecurity, $label); + + } else { + + throw new Exception("When Argument 1 passed to Tropo::call() is a string, argument 2 passed to Tropo::call() must be of the type array."); + } - $call = new Call($call, $from, $network, $channel, $answerOnMedia, $timeout, $headers, $recording, $allowSignals, $machineDetection, $voice); + } else { + + throw new Exception("Argument 1 passed to Tropo::call() must be a string or an instance of Call."); + } $this->call = sprintf('%s', $call); } @@ -1004,10 +1054,22 @@ class Call extends BaseClass { private $_answerOnMedia; private $_timeout; private $_headers; - private $_recording; private $_allowSignals; private $_machineDetection; private $_voice; + private $_name; + private $_required; + private $_callbackUrl; + private $_promptLogSecurity; + private $_label; + + public function getTo() { + return $this->_to; + } + + public function getName() { + return $this->_name; + } /** * Class constructor @@ -1022,7 +1084,13 @@ class Call extends BaseClass { * @param StartRecording $recording * @param string|array $allowSignals */ - public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL, $machineDetection=NULL, $voice=NULL) { + public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL, $machineDetection=NULL, $voice=NULL, $name, $required=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { + if(!isset($to)) { + throw new Exception("Missing required property: 'to'"); + } + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } $this->_to = $to; $this->_from = $from; $this->_network = $network; @@ -1030,10 +1098,14 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ $this->_answerOnMedia = $answerOnMedia; $this->_timeout = $timeout; $this->_headers = $headers; - $this->_recording = isset($recording) ? sprintf('%s', $recording) : null ; $this->_allowSignals = $allowSignals; $this->_machineDetection = $machineDetection; $this->_voice = $voice; + $this->_name = $name; + $this->_required = $required; + $this->_callbackUrl = $callbackUrl; + $this->_promptLogSecurity = $promptLogSecurity; + $this->_label = $label; } /** @@ -1048,18 +1120,23 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } if(count($this->_headers)) { $this->headers = $this->_headers; } - if(isset($this->_recording)) { $this->recording = $this->_recording; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_machineDetection)) { if(is_bool($this->_machineDetection)){ $this->machineDetection = $this->_machineDetection; }else{ - $this->machineDetection->introduction = $this->_machineDetection; + $this->machineDetection['introduction'] = $this->_machineDetection; if(isset($this->_voice)){ - $this->machineDetection->voice = $this->_voice; + $this->machineDetection['voice'] = $this->_voice; } } } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + $this->name = $this->_name; + if(isset($this->_required)) { $this->required = $this->_required; } + if(isset($this->_callbackUrl)) { $this->callbackUrl = $this->_callbackUrl; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } + if(isset($this->_label)) { $this->label = $this->_label; } return $this->unescapeJSON(json_encode($this)); } } From b8250d3896c3f3859ae79a95c14306f3fad99b64 Mon Sep 17 00:00:00 2001 From: pengxli Date: Sat, 17 Jun 2017 11:45:44 +0800 Subject: [PATCH 079/107] bug TROPO-11233 tropo-webapi-php call --- tests/CallTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CallTest.php b/tests/CallTest.php index 064aa15..ad30551 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -2,7 +2,7 @@ use PHPUnit\Framework\TestCase; require_once 'tropo.class.php'; -class MessageTest extends PHPUnit_Framework_TestCase +class CallTest extends PHPUnit_Framework_TestCase { public function testCallWithMinOptions() { From c4d2077b92562daa642fab2ab0e711e576023850 Mon Sep 17 00:00:00 2001 From: pengxli Date: Sat, 17 Jun 2017 20:49:14 +0800 Subject: [PATCH 080/107] bug TROPO-11236 tropo-webapi-php conference --- tests/ConferenceTest.php | 135 ++++++++++++++++++++++----------------- tropo.class.php | 73 ++++++++++++++++----- 2 files changed, 133 insertions(+), 75 deletions(-) diff --git a/tests/ConferenceTest.php b/tests/ConferenceTest.php index 55d7ba2..fa055b9 100644 --- a/tests/ConferenceTest.php +++ b/tests/ConferenceTest.php @@ -1,77 +1,94 @@ partial = '{"id":1234,"mute":false,"name":"foo","playTones":false,"terminator":"#"}'; - $this->expected = '{"tropo":[{"conference":' . $this->partial . '}]}'; - } - - public function testCreateConferenceObject() - { - $conference = new Conference(1234, false, "foo", NULL, false, NULL, "#"); - $this->assertEquals($this->partial, sprintf($conference)); - } + public function testConferenceWithMinOptions() { + $tropo = new Tropo(); + $params = array( + 'name' => 'foo' + ); + $tropo->conference("1234", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234"}}]}'); + } - public function testConferenceFromObject() - { - $conference = new Conference(1234, false, "foo", NULL, false, NULL, "#"); + public function testConferenceWithAllOptions() { $tropo = new Tropo(); - $tropo->Conference($conference); - $this->assertEquals($this->expected, sprintf($tropo)); + $allowSignals = array('exit', 'quit'); + $params = array( + 'allowSignals' => $allowSignals, + 'interdigitTimeout' => 5.0, + 'joinPrompt' => true, + 'leavePrompt' => true, + 'mute' => false, + 'name' => 'foo', + 'playTones' => true, + 'required' => true, + 'terminator' => '*', + 'promptLogSecurity' => 'suppress', + ); + $tropo->conference("1234", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); } - - public function testConferenceWithOptions() - { - $options = array( - 'id' => 1234, - 'mute' => 'false', - 'name' => 'foo', - 'playTones' => false, - 'terminator' => '#' + + public function testConferenceWithAllOptions1() { + $tropo = new Tropo(); + $allowSignals = array('exit', 'quit'); + $joinPrompt = array( + 'value' => 'I am coming.', + 'voice' => Voice::$US_English_female_allison + ); + $leavePrompt = array( + 'value' => 'I am leaving.', + 'voice' => Voice::$US_English_female_allison ); + $params = array( + 'allowSignals' => $allowSignals, + 'interdigitTimeout' => 5.0, + 'joinPrompt' => $joinPrompt, + 'leavePrompt' => $leavePrompt, + 'mute' => false, + 'name' => 'foo', + 'playTones' => true, + 'required' => true, + 'terminator' => '*', + 'promptLogSecurity' => 'suppress', + ); + $tropo->conference("1234", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); + } + + public function testCreateMinObject() { $tropo = new Tropo(); - $tropo->Conference($options); - $this->assertEquals($this->expected, sprintf($tropo)); + $conference = new Conference("foo", "1234"); + $tropo->conference($conference); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234"}}]}'); } - - public function testConferenceWithOptionsInDifferentOrder() - { - $options = array( - 'terminator' => '#', - 'playTones' => false, - 'id' => 1234, - 'mute' => 'false', - 'name' => 'foo' - ); + + public function testCreateObject1() { $tropo = new Tropo(); - $tropo->Conference($options); - $this->assertEquals($this->expected, sprintf($tropo)); + $allowSignals = array('exit', 'quit'); + $conference = new Conference("foo", "1234", false, null, true, true, "*", $allowSignals, 5.0, true, true, null, "suppress"); + $tropo->conference($conference); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); } - - public function testConferenceWithOnHandler() { - $say = new Say('Welcome to the conference. Press the pound key to exit.'); - // Set up an On object to handle the event. - // Note - statically calling the properties of the Event object can be used - // as the first parameter to the On Object constructor. - $on = new On(Event::$join, NULL, $say); - $options = array( - 'id' => 1234, - 'mute' => 'false', - 'terminator' => '#', - 'playTones' => false, - 'name' => 'foo', - 'on' => $on - ); + + public function testCreateObject2() { $tropo = new Tropo(); - $tropo->Conference($options); - $this->assertEquals('{"tropo":[{"conference":{"id":1234,"mute":false,"on":{"event":"join","say":{"value":"Welcome to the conference. Press the pound key to exit."},"name":"foo","playTones":false,"terminator":"#"}}]}', sprintf($tropo)); + $allowSignals = array('exit', 'quit'); + $joinPrompt = array( + 'value' => 'I am coming.', + 'voice' => Voice::$US_English_female_allison + ); + $leavePrompt = array( + 'value' => 'I am leaving.', + 'voice' => Voice::$US_English_female_allison + ); + $conference = new Conference("foo", "1234", false, null, true, true, "*", $allowSignals, 5.0, $joinPrompt, $leavePrompt, null, "suppress"); + $tropo->conference($conference); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); } } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 104a8f4..ba510de 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -239,17 +239,47 @@ public function call($call, Array $params=NULL) { * @see https://www.tropo.com/docs/webapi/conference.htm */ public function conference($conference, Array $params=NULL) { - if(!is_object($conference)) { - $p = array('name', 'id', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; + if ($conference instanceof Conference) { + + if(null === $conference->getId()) { + throw new Exception("Missing required property: 'id'"); + } + if(null === $conference->getName()) { + throw new Exception("Missing required property: 'name'"); + } + + } elseif (is_string($conference) && ($conference !== '')) { + + if (isset($params) && is_array($params)) { + + if (array_key_exists('name', $params)) { + if (is_string($params["name"]) && ($params["name"] !=='')) { + $name = $params["name"]; + } else { + throw new Exception("'name' must be is a string."); + } + } else { + throw new Exception("Missing required property: 'name'"); + } + + $p = array('name', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice', 'promptLogSecurity'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } } + $id = $conference; + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice, $promptLogSecurity); + } else { + + throw new Exception("When Argument 1 passed to Tropo::conference() is a string, argument 2 passed to Tropo::conference() must be of the type array."); + } - $id = (empty($id) && !empty($conference)) ? $conference : $id; - $name = (empty($name)) ? (string)$id : $name; - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice); + } else { + + throw new Exception("Argument 1 passed to Tropo::conference() must be a string or an instance of Conference."); + } $this->conference = sprintf('%s', $conference); } @@ -1190,7 +1220,6 @@ class Conference extends BaseClass { private $_id; private $_mute; private $_name; - private $_on; private $_playTones; private $_required; private $_terminator; @@ -1198,7 +1227,14 @@ class Conference extends BaseClass { private $_interdigitTimeout; private $_joinPrompt; private $_leavePrompt; - private $_voice; + private $_promptLogSecurity; + + public function getName() { + return $this->_name; + } + public function getId() { + return $this->_id; + } /** @@ -1214,11 +1250,16 @@ class Conference extends BaseClass { * @param string|array $allowSignals * @param int $interdigitTimeout */ - public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL, $joinPrompt=NULL, $leavePrompt=NULL, $voice=NULL) { + public function __construct($name, $id, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL, $joinPrompt=NULL, $leavePrompt=NULL, $voice=NULL, $promptLogSecurity=NULL) { + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } + if(!isset($id)) { + throw new Exception("Missing required property: 'id'"); + } $this->_name = $name; $this->_id = (string) $id; $this->_mute = $mute; - $this->_on = isset($on) ? sprintf('%s', $on) : null; $this->_playTones = $playTones; $this->_required = $required; $this->_terminator = $terminator; @@ -1226,7 +1267,7 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones $this->_interdigitTimeout = $interdigitTimeout; $this->_joinPrompt = $joinPrompt; $this->_leavePrompt = $leavePrompt; - $this->_voice = $voice; + $this->_promptLogSecurity = $promptLogSecurity; } /** @@ -1235,14 +1276,14 @@ public function __construct($name, $id=NULL, $mute=NULL, On $on=NULL, $playTones */ public function __toString() { $this->name = $this->_name; - if(isset($this->_id)) { $this->id = $this->_id; } + $this->id = $this->_id; if(isset($this->_mute)) { $this->mute = $this->_mute; } - if(isset($this->_on)) { $this->on = $this->_on; } if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } if(isset($this->_joinPrompt)) { if($this->_joinPrompt == true || $this->_joinPrompt == false){ $this->joinPrompt = $this->_joinPrompt; From bb1c9c0b24dfabc5264230905aa5f0d27293978b Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 19 Jun 2017 09:55:21 +0800 Subject: [PATCH 081/107] bug TROPO-11257 tropo-webapi-php record --- tests/RecordTest.php | 99 ++++++++++++++++++++++++++++++-------------- tropo.class.php | 85 +++++++++++++++++++++++++++++++------ 2 files changed, 140 insertions(+), 44 deletions(-) diff --git a/tests/RecordTest.php b/tests/RecordTest.php index 177bab3..d648ba9 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -1,45 +1,80 @@ assertEquals('{"beep":true,"choices":{"value":"[5 DIGITS]","termChar":"#"},"maxSilence":5,"method":"POST","say":{"value":"Please say your account number"}}', sprintf($record)); +{ + + public function testRecordWithMinOptions() { + $tropo = new Tropo(); + $record = array( + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', + 'name' => 'foo' + ); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo"}}]}'); } - - public function testRecordUsingObject() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]", NULL, "#"); - $record = new Record(NULL, NULL, true, $choices, NULL, 5, "POST", NULL, NULL, $say, NULL, NULL); + + public function testRecordWithAllOptions() { $tropo = new Tropo(); - $tropo->Record($record); - $this->assertEquals('{"tropo":[{"record":{"beep":true,"choices":{"value":"[5 DIGITS]","termChar":"#"},"maxSilence":5,"method":"POST","say":{"value":"Please say your account number"}}}]}', sprintf($tropo)); + $allowSignals = array('exit', 'quit'); + $event = array( + 'timeout' => 'Sorry, I did not hear anything. Please call back.' + ); + $transcription = array( + 'id' => '1234', + 'url' => 'mailto:you@yourmail.com', + 'emailFormat' => 'omit' + ); + $record = array( + 'attempts' => 2, + 'asyncUpload' => false, + 'allowSignals' => $allowSignals, + 'bargein' => true, + 'beep' => true, + 'choices' => '*', + 'say' => 'Please leave a message.', + 'event' => $event, + 'format' => AudioFormat::$mp3, + 'maxSilence' => 5.0, + 'maxTime' => 30.0, + 'method' => 'POST', + 'name' => 'foo', + 'required' => true, + 'transcription' => $transcription, + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', + 'password' => '111111', + 'username' => 'root', + 'timeout' => 30.0, + 'interdigitTimeout' => 5.0, + 'voice' => Voice::$US_English_female_allison, + 'promptLogSecurity' => 'suppress' + ); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } - - public function testRecordUsingTropoMethod() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]", NULL, "#"); + + + public function testCreateMinObject() { $tropo = new Tropo(); - $tropo->Record(NULL, NULL, true, $choices, NULL, 5, "POST", NULL, NULL, $say, NULL, NULL); - $this->assertEquals('{"tropo":[{"record":{"beep":true,"choices":{"value":"[5 DIGITS]","termChar":"#"},"maxSilence":5,"method":"POST","say":{"value":"Please say your account number"}}}]}', sprintf($tropo)); + $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo"}}]}'); } - - public function testRecordTranscription() - { - $say = new Say("Please say your account number"); - $choices = new Choices("[5 DIGITS]", NULL, "#"); - $transcription = new Transcription('http://example.com/', 'bling', 'encoded'); + + public function testCreateObject() { $tropo = new Tropo(); - $tropo->Record(NULL, NULL, true, $choices, NULL, 5, "POST", NULL, NULL, $say, NULL, NULL,$transcription); - $this->assertEquals('{"tropo":[{"record":{"beep":true,"choices":{"value":"[5 DIGITS]","termChar":"#"},"maxSilence":5,"method":"POST","say":{"value":"Please say your account number"},"transcription":{"id":"bling","url":"http://example.com/","emailFormat":"encoded"}}}]}', sprintf($tropo)); + $allowSignals = array('exit', 'quit'); + $choices = new Choices(null, null, "*"); + $say = array( + new Say("Please leave a message."), + new Say("Sorry, I did not hear anything. Please call back.", null , "timeout") + ); + $transcription = new Transcription("mailto:you@yourmail.com", "1234", "omit"); + $record = new Record(2, $allowSignals, true, true, $choices, AudioFormat::$mp3, 5.0, 30.0, "POST", "111111", true, $say, 30.0, $transcription, "root", "http://192.168.26.203/tropo-webapi-php/upload_file.php", Voice::$US_English_female_allison, null, 5.0, false, "foo", "suppress"); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } - + } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index ba510de..90bba19 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -424,9 +424,19 @@ public function on($on) { * @see https://www.tropo.com/docs/webapi/record.htm */ public function record($record) { - if(!is_object($record) && is_array($record)) { + if ($record instanceof Record) { + + if(null === $record->getUrl()) { + throw new Exception("Missing required property: 'url'"); + } + if(null === $record->getName()) { + throw new Exception("Missing required property: 'name'"); + } + + } elseif (is_array($record)) { + $params = $record; - $p = array('as', 'voice', 'emailFormat', 'transcription', 'terminator'); + $p = array('voice', 'emailFormat', 'transcription', 'terminator'); foreach ($p as $option) { $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; } @@ -439,7 +449,6 @@ public function record($record) { if (!isset($params['voice'])) { $params['voice'] = $this->_voice; } - $say = new Say($params["say"], $params["as"], null, null); if (is_array($params['transcription'])) { $p = array('url', 'id', 'emailFormat'); foreach ($p as $option) { @@ -452,14 +461,37 @@ public function record($record) { } else { $transcription = $params["transcription"]; } - $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout'); + $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout', 'asyncUpload', 'name', 'promptLogSecurity'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { $$option = $params[$option]; } } - $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout); + if (array_key_exists('say', $params)) { + if (is_string($params["say"]) && ($params["say"] !== '')) { + + $say[] = new Say($params["say"]); + + } + } + if (array_key_exists('event', $params)) { + + if (is_array($params["event"])) { + foreach ($params["event"] as $e => $value) { + $say[] = new Say($value, null, $e); + } + } + } + if (!isset($say)) { + $say = null; + } + $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout, $asyncUpload, $name, $promptLogSecurity); + + } else { + + throw new Exception("Argument 1 passed to Tropo::record() must be a array or an instance of Record."); + } $this->record = sprintf('%s', $record); } @@ -1492,8 +1524,18 @@ class Record extends BaseClass { private $_username; private $_url; private $_voice; - private $_minConfidence; private $_interdigitTimeout; + private $_asyncUpload; + private $_name; + private $_promptLogSecurity; + + public function getUrl() { + return $this->_url; + } + + public function getName() { + return $this->_name; + } /** @@ -1517,7 +1559,13 @@ class Record extends BaseClass { * @param int $minConfidence * @param int $interdigitTimeout */ - public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url=NULL, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL) { + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL, $asyncUpload=NULL, $name, $promptLogSecurity=NULL) { + if(!isset($url)) { + throw new Exception("Missing required property: 'url'"); + } + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } $this->_attempts = $attempts; $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; @@ -1528,17 +1576,21 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_maxTime = $maxTime; $this->_method = $method; $this->_password = $password; - if (!is_object($say)) { - $say = new Say($say); + $this->_required = $required; + if ($say instanceof Say) { + $this->_say = sprintf('%s', $say); + } else { + $this->_say = $say; } - $this->_say = isset($say) ? sprintf('%s', $say) : null; $this->_timeout = $timeout; $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; $this->_username = $username; $this->_url = $url; $this->_voice = $voice; - $this->_minConfidence = $minConfidence; $this->_interdigitTimeout = $interdigitTimeout; + $this->_asyncUpload = $asyncUpload; + $this->_name = $name; + $this->_promptLogSecurity = $promptLogSecurity; } /** @@ -1556,14 +1608,22 @@ public function __toString() { if(isset($this->_maxTime)) { $this->maxTime = $this->_maxTime; } if(isset($this->_method)) { $this->method = $this->_method; } if(isset($this->_password)) { $this->password = $this->_password; } + if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_say)) { $this->say = $this->_say; } + if (is_array($this->_say)) { + foreach ($this->_say as $k => $v) { + $this->_say[$k] = sprintf('%s', $v); + } + } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_voice)) { $this->voice = $this->_voice; } - if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } + if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } + if(isset($this->_name)) { $this->name = $this->_name; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } return $this->unescapeJSON(json_encode($this)); } } @@ -2343,6 +2403,7 @@ class Channel { class AudioFormat { public static $wav = "audio/wav"; public static $mp3 = "audio/mp3"; + public static $au = "audio/au"; } /** From 05853d3bfe07a026f829385b804c36c5818f2f03 Mon Sep 17 00:00:00 2001 From: pengxli Date: Wed, 21 Jun 2017 13:31:19 +0800 Subject: [PATCH 082/107] bug TROPO-11292 tropo-webapi-php transfer --- tests/TransferTest.php | 225 +++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 201 ++++++++++++++++++++++++++---------- 2 files changed, 373 insertions(+), 53 deletions(-) create mode 100644 tests/TransferTest.php diff --git a/tests/TransferTest.php b/tests/TransferTest.php new file mode 100644 index 0000000..b7215bd --- /dev/null +++ b/tests/TransferTest.php @@ -0,0 +1,225 @@ + 'foo' + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","name":"foo"}}]}'); + } + + public function testTransferWithMinOptions1() { + $tropo = new Tropo(); + $params = array( + 'name' => 'foo' + ); + $tropo->transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"name":"foo"}}]}'); + } + + public function testTransferWithChoicesOptions() { + $tropo = new Tropo(); + $choices = new Choices(null, null, "#"); + $params = array( + 'name' => 'foo', + 'choices' => $choices, + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"},"name":"foo"}}]}'); + } + + public function testTransferWithChoicesOptions1() { + $tropo = new Tropo(); + $params = array( + 'name' => 'foo', + 'choices' => '#', + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"},"name":"foo"}}]}'); + } + + public function testTransferWithChoicesAndTerminatorOptions() { + $tropo = new Tropo(); + $choices = new Choices(null, null, "#"); + $params = array( + 'name' => 'foo', + 'choices' => $choices, + 'terminator' => '*', + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"},"name":"foo"}}]}'); + } + + public function testTransferWithChoicesAndTerminatorOptions1() { + $tropo = new Tropo(); + $params = array( + 'name' => 'foo', + 'choices' => '#', + 'terminator' => '*', + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"},"name":"foo"}}]}'); + } + + public function testTransferWithOnOptions() { + $tropo = new Tropo(); + $on = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $params = array( + 'name' => 'foo', + 'ringRepeat' => 2, + 'on' => $on, + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},"name":"foo"}}]}'); + } + + public function testTransferWithAllOptions() { + $tropo = new Tropo(); + $allowSignals = array("exit", "quit"); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $onRing = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $say = array( + new Say("Sorry. Please enter you 5 digit account number again.", null, "nomatch"), + new Say("Sorry, I did not hear anything.", null , "timeout"), + new Say("Please enter 5 digit account number.") + ); + $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); + $onConnect = new On("connect", null, null, $ask); + $on = array($onRing, $onConnect); + $params = array( + 'from' => '14155551212', + 'timeout' => 30.0, + 'answerOnMedia' => false, + 'name' => 'foo', + 'required' => true, + 'allowSignals' => $allowSignals, + 'machineDetection' => false, + 'terminator' => '#', + 'headers' => $headers, + 'interdigitTimeout' => 5.0, + 'ringRepeat' => 2, + 'playTones' => true, + 'on' => $on, + 'voice' => Voice::$US_English_female_allison, + 'callbackUrl' => 'http://192.168.26.203/result.php', + 'promptLogSecurity' => 'suppress', + 'label' => 'transferLabel' + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + } + + public function testTransferWithAllOptions1() { + $tropo = new Tropo(); + $allowSignals = array("exit", "quit"); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $onRing = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $say = array( + new Say("Sorry. Please enter you 5 digit account number again.", null, "nomatch"), + new Say("Sorry, I did not hear anything.", null , "timeout"), + new Say("Please enter 5 digit account number.") + ); + $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); + $onConnect = new On("connect", null, null, $ask); + $on = array($onRing, $onConnect); + $params = array( + 'from' => '14155551212', + 'timeout' => 30.0, + 'answerOnMedia' => false, + 'name' => 'foo', + 'required' => true, + 'allowSignals' => $allowSignals, + 'machineDetection' => "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.", + 'terminator' => '#', + 'headers' => $headers, + 'interdigitTimeout' => 5.0, + 'ringRepeat' => 2, + 'playTones' => true, + 'on' => $on, + 'voice' => Voice::$US_English_female_allison, + 'callbackUrl' => 'http://192.168.26.203/result.php', + 'promptLogSecurity' => 'suppress', + 'label' => 'transferLabel' + ); + $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + } + + + public function testCreateMinObject() { + $tropo = new Tropo(); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", null, null, null, null, null, null, null, null, null, null, "foo"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","name":"foo"}}]}'); + } + + public function testCreateObject() { + $tropo = new Tropo(); + $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), null, null, null, null, null, null, null, null, null, null, "foo"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"name":"foo"}}]}'); + } + + public function testCreateObject1() { + $tropo = new Tropo(); + $choices = new Choices(null, null, "#"); + $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), null, $choices, null, null, null, null, null, null, null, null, "foo"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"choices":{"terminator":"#"},"name":"foo"}}]}'); + } + + public function testCreateObject2() { + $tropo = new Tropo(); + $on = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", null, null, null, 2, null, $on, null, null, null, null, "foo"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},"name":"foo"}}]}'); + } + + public function testCreateObject3() { + $tropo = new Tropo(); + $choices = new Choices(null, null, "#"); + $allowSignals = array("exit", "quit"); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $onRing = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $say = array( + new Say("Sorry. Please enter you 5 digit account number again.", null, "nomatch"), + new Say("Sorry, I did not hear anything.", null , "timeout"), + new Say("Please enter 5 digit account number.") + ); + $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); + $onConnect = new On("connect", null, null, $ask); + $on = array($onRing, $onConnect); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, false, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + } + + public function testCreateObject4() { + $tropo = new Tropo(); + $choices = new Choices(null, null, "#"); + $allowSignals = array("exit", "quit"); + $headers = array('foo' => 'bar', 'bling' => 'baz'); + $machineDetection = "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered."; + $onRing = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); + $say = array( + new Say("Sorry. Please enter you 5 digit account number again.", null, "nomatch"), + new Say("Sorry, I did not hear anything.", null , "timeout"), + new Say("Please enter 5 digit account number.") + ); + $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); + $onConnect = new On("connect", null, null, $ask); + $on = array($onRing, $onConnect); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, $machineDetection, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); + $tropo->transfer($transfer); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 90bba19..2e332e6 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -606,65 +606,101 @@ public function stopRecording() { * @see https://www.tropo.com/docs/webapi/transfer.htm */ public function transfer($transfer, Array $params=NULL) { - if(!is_object($transfer)) { - $choices = isset($params["choices"]) ? $params["choices"] : null; - $choices = isset($params["terminator"]) - ? new Choices(null, null, $params["terminator"]) - : $choices; - $to = isset($params["to"]) ? $params["to"] : $transfer; - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice'); - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; + if ($transfer instanceof Transfer) {//$transfer is an instance of Transfer + + if(null === $transfer->getTo()) { + throw new Exception("Missing required property: 'to'"); + } + if(null === $transfer->getName()) { + throw new Exception("Missing required property: 'name'"); + } + + } elseif (is_array($transfer) || (is_string($transfer) && ($transfer !== ''))) {//$transfer is a non-empty string or a non-empty string of array + + if (is_array($transfer)) { + $to = null; + foreach ($transfer as $value) { + if (is_string($value) && ($value !== '')) { + $to[] = $value; + } + } + if (null === $to) { + + throw new Exception("Argument 1 passed to Tropo::transfer() must be a string or a string of array or an instance of Transfer."); + } + } else { + $to = $transfer; } - $on = null; - if (array_key_exists('playvalue', $params) && isset($params['playvalue'])) { - $on = new On('ring', null, new Say($params['playvalue'])); - } elseif (array_key_exists('on', $params) && isset($params['on'])) { - if (is_object($params['on'])) { - $on = $params['on']; + + if (isset($params) && is_array($params)) { + + if (array_key_exists('name', $params)) { + if (is_string($params["name"]) && ($params["name"] !=='')) { + $name = $params["name"]; + } else { + throw new Exception("'name' must be is a string."); + } } else { - if (strtolower($params['on']['event']) == 'ring') { - $on = on(array('ring', null, new Say($params['on']['say']), null, null)); - }elseif (strtolower($params['on']['event']) == 'connect') { - - $comma = ""; - $on = ""; - - if(isset($params['on']['ring'])){ - $on = new On('ring', null, new Say($params['on']['ring']), null, null); - $comma = ","; + throw new Exception("Missing required property: 'name'"); + } + + $choices = null; + if (array_key_exists('choices', $params)) { + + if ($params["choices"] instanceof Choices) { + $choices = $params["choices"]; + } elseif (is_string($params["choices"]) && ($params["choices"] !== '')) { + $choices = new Choices(null, null, $params["choices"]); + } else { + $choices = null; + } + } + + if (array_key_exists('terminator', $params)) { + if (is_string($params["terminator"]) && ($params["terminator"] !== '')) { + $choices = new Choices(null, null, $params["terminator"]); + } + } + + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice', 'required', 'interdigitTimeout', 'playTones', 'callbackUrl', 'promptLogSecurity', 'label'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $on = null; + if (array_key_exists('on', $params)) { + if ($params['on'] instanceof On) { + if (is_string($params['on']->getEvent()) && ((strtolower($params['on']->getEvent()) == 'ring') || (strtolower($params['on']->getEvent()) == 'connect'))) { + $on = $params['on']; + } else { + throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); } - foreach($params['on']['whisper'] as $key){ - foreach($key as $k => $v){ - - switch($k){ - case 'ask': - $on = $on . $comma . new On('connect', null, null, null,$v,null,null,"ask"); - break; - case 'say': - $on = $on . $comma . new On('connect', null, $v, null,null,null,null,"say"); - break; - case 'wait': - $on = $on . $comma . new On('connect', null, null, null,null,null,$v,"wait"); - break; - case 'message': - $on = $on . $comma . new On('connect', null, null, null,null,$v,null,"message"); - break; + } elseif (is_array($params['on'])) { + foreach ($params['on'] as $value) { + if ($value instanceof On) { + if (is_string($value->getEvent()) && ((strtolower($value->getEvent()) == 'ring') || (strtolower($value->getEvent()) == 'connect'))) { + $on[] = $value; + } else { + throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); } - $comma = ","; } } - - }else{ - throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); } } + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice, $name, $required, $interdigitTimeout, $playTones, $callbackUrl, $promptLogSecurity, $label); + } else { + + throw new Exception("When Argument 1 passed to Tropo::transfer() is a string or a string of array, argument 2 passed to Tropo::transfer() must be of the type array."); + } - $on = $on == null ? null : sprintf('%s',$on); - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice); + + } else { + + throw new Exception("Argument 1 passed to Tropo::transfer() must be a string or a string of array or an instance of Transfer."); + } $this->transfer = sprintf('%s', $transfer); } @@ -1213,6 +1249,18 @@ class Choices extends BaseClass { private $_mode; private $_terminator; + public function getValue() { + return $this->_value; + } + + public function getMode() { + return $this->_mode; + } + + public function getTerminator() { + return $this->_terminator; + } + /** * Class constructor * @@ -2165,6 +2213,20 @@ class Transfer extends BaseClass { private $_headers; private $_machineDetection; private $_voice; + private $_name; + private $_required; + private $_interdigitTimeout; + private $_playTones; + private $_callbackUrl; + private $_promptLogSecurity; + private $_label; + + public function getTo() { + return $this->_to; + } + public function getName() { + return $this->_name; + } /** * Class constructor @@ -2179,18 +2241,38 @@ class Transfer extends BaseClass { * @param string|array $allowSignals * @param array $headers */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL, $machineDetection=NULL, $voice=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL, $machineDetection=NULL, $voice=NULL, $name, $required=NULL, $interdigitTimeout=NULL, $playTones=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { + if(!isset($to)) { + throw new Exception("Missing required property: 'to'"); + } + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; $this->_from = $from; $this->_ringRepeat = $ringRepeat; $this->_timeout = $timeout; - $this->_on = isset($on) ? array(sprintf('%s', $on)) : null; + $this->_on = null; + if (isset($on)) { + if ($on instanceof On) { + $this->_on = sprintf('%s', $on); + } else { + $this->_on = $on; + } + } $this->_allowSignals = $allowSignals; $this->_headers = $headers; $this->_machineDetection = $machineDetection; $this->_voice = $voice; + $this->_name = $name; + $this->_required = $required; + $this->_interdigitTimeout = $interdigitTimeout; + $this->_playTones = $playTones; + $this->_callbackUrl = $callbackUrl; + $this->_promptLogSecurity = $promptLogSecurity; + $this->_label = $label; } /** @@ -2205,18 +2287,31 @@ public function __toString() { if(isset($this->_ringRepeat)) { $this->ringRepeat = $this->_ringRepeat; } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_on)) { $this->on = $this->_on; } + if (is_array($this->_on)) { + foreach ($this->_on as $k => $v) { + $this->_on[$k] = sprintf('%s', $v); + } + } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(count($this->_headers)) { $this->headers = $this->_headers; } if(isset($this->_machineDetection)) { if(is_bool($this->_machineDetection)){ $this->machineDetection = $this->_machineDetection; }else{ - $this->machineDetection->introduction = $this->_machineDetection; + $this->machineDetection['introduction'] = $this->_machineDetection; if(isset($this->_voice)){ - $this->machineDetection->voice = $this->_voice; + $this->machineDetection['voice'] = $this->_voice; } } } + if(isset($this->_voice)) { $this->voice = $this->_voice; } + $this->name = $this->_name; + if(isset($this->_required)) { $this->required = $this->_required; } + if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } + if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } + if(isset($this->_callbackUrl)) { $this->callbackUrl = $this->_callbackUrl; } + if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } + if(isset($this->_label)) { $this->label = $this->_label; } return $this->unescapeJSON(json_encode($this)); } } From 2273c6e407987e23cfca7ff18317f91383489a8e Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 22 Jun 2017 11:34:28 +0800 Subject: [PATCH 083/107] bug TROPO-11286 tropo-webapi-php startRecoring --- tests/StartRecordingTest.php | 65 ++++++++++++++++++++++++++++-------- tropo.class.php | 37 ++++++++++++++++---- 2 files changed, 82 insertions(+), 20 deletions(-) diff --git a/tests/StartRecordingTest.php b/tests/StartRecordingTest.php index 3ed9292..fad3506 100644 --- a/tests/StartRecordingTest.php +++ b/tests/StartRecordingTest.php @@ -1,23 +1,60 @@ assertEquals('{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}', sprintf($recording)); - $tropo = new Tropo(); - $tropo->StartRecording($recording); - $this->assertEquals('{"tropo":[{"startRecording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}]}', sprintf($tropo)); + public function testStartRecordingWithMinOptions() { + $tropo = new Tropo(); + $startRecording = array( + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', + ); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } - - public function testStartRecordingWithParameters() - { - $tropo = new Tropo(); - $tropo->StartRecording('recording','audio/mp3','POST','password','http://blah.com/recordings/1234.wav','jose'); - $this->assertEquals('{"tropo":[{"recording":{"format":"audio/mp3","method":"POST","password":"password","url":"http://blah.com/recordings/1234.wav","username":"jose"}}]}', sprintf($tropo)); + + public function testRecordWithAllOptions() { + $tropo = new Tropo(); + $startRecording = array( + 'asyncUpload' => false, + 'format' => AudioFormat::$au, + 'method' => 'POST', + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', + 'username' => 'root', + 'password' => '111111', + 'transcriptionOutURI' => 'mailto:you@yourmail.com', + 'transcriptionEmailFormat' => 'plain', + 'transcriptionID' => '1234' + ); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/au","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + } + + public function testCreateMinObject() { + $tropo = new Tropo(); + $startRecording = new StartRecording(null, null, null, 'http://192.168.26.203/tropo-webapi-php/upload_file.php'); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } + + public function testCreateObject() { + $tropo = new Tropo(); + $startRecording = new StartRecording(AudioFormat::$mp3, "POST", "111111", "http://192.168.26.203/tropo-webapi-php/upload_file.php", "root", "1234", "plain", "mailto:you@yourmail.com", false); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/mp3","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + } + } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 2e332e6..a3e4c52 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -573,16 +573,32 @@ public function say($say, Array $params=NULL) { * @see https://www.tropo.com/docs/webapi/startrecording.htm */ public function startRecording($startRecording) { - if(!is_object($startRecording) && is_array($startRecording)) { + if ($startRecording instanceof StartRecording) { + + if(null === $startRecording->getUrl()) { + throw new Exception("Missing required property: 'url'"); + } + + } elseif (is_array($startRecording)) { + + if (!array_key_exists('url', $startRecording)) { + throw new Exception("Missing required property: 'url'"); + } + $params = $startRecording; - $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI'); + $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload'); foreach ($p as $option) { $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { + if (array_key_exists($option, $params)) { $$option = $params[$option]; } } - $startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI); + $startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI, $asyncUpload); + + } else { + + throw new Exception("Argument 1 passed to Tropo::startRecording() must be a array or an instance of StartRecording."); + } $this->startRecording = sprintf('%s', $startRecording); } @@ -2101,7 +2117,6 @@ public function setHeaders($headers) { */ class StartRecording extends BaseClass { - private $_name; private $_format; private $_method; private $_password; @@ -2110,6 +2125,11 @@ class StartRecording extends BaseClass { private $_transcriptionID; private $_transcriptionEmailFormat; private $_transcriptionOutURI; + private $_asyncUpload; + + public function getUrl() { + return $this->_url; + } /** * Class constructor @@ -2124,7 +2144,10 @@ class StartRecording extends BaseClass { * @param string $transcriptionEmailFormat * @param string $transcriptionOutURI */ - public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NULL, $username=NULL, $transcriptionID=NULL, $transcriptionEmailFormat=NULL, $transcriptionOutURI=NULL) { + public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $username=NULL, $transcriptionID=NULL, $transcriptionEmailFormat=NULL, $transcriptionOutURI=NULL, $asyncUpload=NULL) { + if(!isset($url)) { + throw new Exception("Missing required property: 'url'"); + } $this->_format = $format; $this->_method = $method; $this->_password = $password; @@ -2133,6 +2156,7 @@ public function __construct($format=NULL, $method=NULL, $password=NULL, $url=NUL $this->_transcriptionID = $transcriptionID; $this->_transcriptionEmailFormat = $transcriptionEmailFormat; $this->_transcriptionOutURI = $transcriptionOutURI; + $this->_asyncUpload = $asyncUpload; } /** @@ -2148,6 +2172,7 @@ public function __toString() { if(isset($this->_transcriptionID)) { $this->transcriptionID = $this->_transcriptionID; } if(isset($this->_transcriptionEmailFormat)) { $this->transcriptionEmailFormat = $this->_transcriptionEmailFormat; } if(isset($this->_transcriptionOutURI)) { $this->transcriptionOutURI = $this->_transcriptionOutURI; } + if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } return $this->unescapeJSON(json_encode($this)); } } From 63ec13639fd0365cd40deb5fadd741332f9c4427 Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 23 Jun 2017 11:49:11 +0800 Subject: [PATCH 084/107] bug TROPO-11277 tropo-webapi-php result --- tests/ResultTest.php | 70 ++++++++++++++++++++++++++++++++++ tropo.class.php | 90 +++++++++++++++++++++++++++++--------------- 2 files changed, 130 insertions(+), 30 deletions(-) create mode 100644 tests/ResultTest.php diff --git a/tests/ResultTest.php b/tests/ResultTest.php new file mode 100644 index 0000000..b41fdfb --- /dev/null +++ b/tests/ResultTest.php @@ -0,0 +1,70 @@ +assertEquals($result->getSessionId(), '26a5aa37ff9d14d11990a48865abb5d2'); + $this->assertEquals($result->getCallId(), 'b11948c7073e815a768fd3f393cc92a6'); + $this->assertEquals($result->getState(), 'ANSWERED'); + $this->assertEquals($result->getSessionDuration(), 158); + $this->assertEquals($result->getSequence(), 1); + $this->assertEquals($result->isComplete(), true); + $this->assertEquals($result->getError(), null); + $this->assertEquals($result->getCalledid(), '9992801029'); + $this->assertEquals($result->getUserType(), null); + $actions = $result->getActions(); + $action = $actions; + $this->assertEquals($result->getName($action), 'foo'); + $this->assertEquals($result->getAttempts($action), null); + $this->assertEquals($result->getDisposition($action), 'SUCCESS'); + $this->assertEquals($result->getConfidence($action), null); + $this->assertEquals($result->getInterpretation($action), null); + $this->assertEquals($result->getUtterance($action), null); + $this->assertEquals($result->getValue($action), null); + $this->assertEquals($result->getConcept($action), null); + $this->assertEquals($result->getXml($action), null); + $this->assertEquals($result->getUploadStatus($action), null); + $this->assertEquals($result->getDuration($action), 149); + $this->assertEquals($result->getConnectedDuration($action), 145); + $this->assertEquals($result->getUrl($action), null); + $this->assertEquals($result->getActionUserType($action), 'HUMAN'); + $this->assertEquals($result->getTranscription($action), null); + } + + public function testResult2() { + $json = '{"result":{"sessionId":"349e753764123e3b90245162a0f7758a","callId":"925f666b73ea1e41211d624489deea17","state":"ANSWERED","sessionDuration":13,"sequence":1,"complete":true,"error":null,"calledid":"9992801029","actions":[{"name":"foo","attempts":1,"disposition":"SUCCESS","confidence":100,"interpretation":"1234","utterance":"1234","value":"1234","xml":"\\r\\n\\r\\n \\r\\n \\r\\n 1234<\\/input>\\r\\n <\\/interpretation>\\r\\n<\\/result>\\r\\n"},{"name":"foo","attempts":1,"disposition":"SUCCESS","confidence":100,"interpretation":"1234","utterance":"1234","value":"1234","xml":"\\r\\n\\r\\n \\r\\n \\r\\n 1234<\\/input>\\r\\n <\\/interpretation>\\r\\n<\\/result>\\r\\n"}]}}'; + $result = new Result($json); + $this->assertEquals($result->getSessionId(), '349e753764123e3b90245162a0f7758a'); + $this->assertEquals($result->getCallId(), '925f666b73ea1e41211d624489deea17'); + $this->assertEquals($result->getState(), 'ANSWERED'); + $this->assertEquals($result->getSessionDuration(), 13); + $this->assertEquals($result->getSequence(), 1); + $this->assertEquals($result->isComplete(), true); + $this->assertEquals($result->getError(), null); + $this->assertEquals($result->getCalledid(), '9992801029'); + $this->assertEquals($result->getUserType(), null); + $actions = $result->getActions(); + $action = $actions[0]; + $this->assertEquals($result->getName($action), 'foo'); + $this->assertEquals($result->getAttempts($action), 1); + $this->assertEquals($result->getDisposition($action), 'SUCCESS'); + $this->assertEquals($result->getConfidence($action), 100); + $this->assertEquals($result->getInterpretation($action), '1234'); + $this->assertEquals($result->getUtterance($action), '1234'); + $this->assertEquals($result->getValue($action), '1234'); + $this->assertEquals($result->getConcept($action), null); + $this->assertEquals($result->getXml($action), "\r\n\r\n \r\n \r\n 1234\r\n \r\n\r\n"); + $this->assertEquals($result->getUploadStatus($action), null); + $this->assertEquals($result->getDuration($action), null); + $this->assertEquals($result->getConnectedDuration($action), null); + $this->assertEquals($result->getUrl($action), null); + $this->assertEquals($result->getActionUserType($action), null); + $this->assertEquals($result->getTranscription($action), null); + } +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index a3e4c52..511e55f 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1745,6 +1745,8 @@ class Result { private $_sequence; private $_complete; private $_error; + private $_calledid; + private $_userType; private $_actions; private $_name; private $_attempts; @@ -1752,7 +1754,6 @@ class Result { private $_confidence; private $_interpretation; private $_concept; - private $_userType; private $_utterance; private $_value; private $_transcription; @@ -1782,17 +1783,18 @@ public function __construct($json=NULL) { $this->_sequence = $result->result->sequence; $this->_complete = $result->result->complete; $this->_error = $result->result->error; - $this->_userType = $result->result->userType; - $this->_actions = $result->result->actions; - $this->_name = $result->result->actions->name; - $this->_attempts = $result->result->actions->attempts; - $this->_disposition = $result->result->actions->disposition; - $this->_confidence = $result->result->actions->confidence; - $this->_interpretation = $result->result->actions->interpretation; - $this->_utterance = $result->result->actions->utterance; - $this->_value = $result->result->actions->value; - $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; - $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; + $this->_calledid = $result->result->calledid; + $this->_userType = isset($result->result->userType) ? $result->result->userType : null; + $this->_actions = isset($result->result->actions) ? $result->result->actions : null; + // $this->_name = $result->result->actions->name; + // $this->_attempts = $result->result->actions->attempts; + // $this->_disposition = $result->result->actions->disposition; + // $this->_confidence = $result->result->actions->confidence; + // $this->_interpretation = $result->result->actions->interpretation; + // $this->_utterance = $result->result->actions->utterance; + // $this->_value = $result->result->actions->value; + // $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; + // $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; } public function getSessionId() { @@ -1823,6 +1825,10 @@ public function getError() { return $this->_error; } + public function getCalledid() { + return $this->_calledid; + } + public function getUserType() { return $this->_userType; } @@ -1831,40 +1837,64 @@ public function getActions() { return $this->_actions; } - public function getName() { - return $this->_name; + public function getName($action) { + return isset($action->name) ? $action->name : null; } - public function getAttempts() { - return $this->_attempts; + public function getAttempts($action) { + return isset($action->attempts) ? $action->attempts : null; } - public function getDisposition() { - return $this->_disposition; + public function getDisposition($action) { + return isset($action->disposition) ? $action->disposition : null; } - public function getConfidence() { - return $this->_confidence; + public function getConfidence($action) { + return isset($action->confidence) ? $action->confidence : null; } - public function getInterpretation() { - return $this->_interpretation; + public function getInterpretation($action) { + return isset($action->interpretation) ? $action->interpretation : null; } - public function getConcept() { - return $this->_concept; + public function getUtterance($action) { + return isset($action->utterance) ? $action->utterance : null; } - public function getUtterance() { - return $this->_utterance; + public function getValue($action) { + return isset($action->value) ? $action->value : null; } - public function getValue() { - return $this->_value; + public function getConcept($action) { + return isset($action->concept) ? $action->concept : null; + } + + public function getXml($action) { + return isset($action->xml) ? $action->xml : null; + } + + public function getUploadStatus($action) { + return isset($action->uploadStatus) ? $action->uploadStatus : null; + } + + public function getDuration($action) { + return isset($action->duration) ? $action->duration : null; + } + + public function getConnectedDuration($action) { + return isset($action->connectedDuration) ? $action->connectedDuration : null; + } + + public function getUrl($action) { + return isset($action->url) ? $action->url : null; + } + + public function getActionUserType($action) { + return isset($action->userType) ? $action->userType : null; } - public function getTranscription() { - return $this->_transcription; + public function getTranscription($action) { + return isset($action->transcription) ? $action->transcription : null; } } From 6970fd358ed1acd9dcadbcb60e727ba8f6dbb82c Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 23 Jun 2017 12:38:05 +0800 Subject: [PATCH 085/107] bug TROPO-11239 tropo-webapi-php generalLogSecurity --- tests/GeneralLogSecurityTest.php | 20 ++++++++++++++++++++ tropo.class.php | 13 +++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/GeneralLogSecurityTest.php diff --git a/tests/GeneralLogSecurityTest.php b/tests/GeneralLogSecurityTest.php new file mode 100644 index 0000000..660d864 --- /dev/null +++ b/tests/GeneralLogSecurityTest.php @@ -0,0 +1,20 @@ +generalLogSecurity("suppress"); + $say = new Say("this is not logged.", null, null, null, null, "nolog"); + $tropo->say($say); + $tropo->generalLogSecurity("none"); + $say = new Say("this will be logged.", null, null, null, null, "log"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"generalLogSecurity":"suppress"},{"say":[{"value":"this is not logged.","name":"nolog"}]},{"generalLogSecurity":"none"},{"say":[{"value":"this will be logged.","name":"log"}]}]}'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 511e55f..641d3f8 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -738,6 +738,19 @@ public function wait($wait) { } + public function generalLogSecurity($state) { + + if (is_string($state) && ($state !== '')) { + + $this->generalLogSecurity = $state; + + } else { + + throw new Exception("Argument 1 passed to Tropo::generalLogSecurity() must be a string."); + + } + } + /** * Launches a new session with the Tropo Session API. * (Pass through to SessionAPI class.) From b8fd167318a340c660f274a2f7a1c62e5680db2a Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 23 Jun 2017 16:26:18 +0800 Subject: [PATCH 086/107] bug TROPO-11268 tropo-webapi-php redirect --- tests/RedirectTest.php | 28 +++++++++++++++++ tropo.class.php | 71 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 tests/RedirectTest.php diff --git a/tests/RedirectTest.php b/tests/RedirectTest.php new file mode 100644 index 0000000..3de9b9e --- /dev/null +++ b/tests/RedirectTest.php @@ -0,0 +1,28 @@ + 'foo', + 'required' => true + ); + + $tropo->redirect("sip:pengxli@192.168.26.1:5678", $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo","required":true}}]}'); + } + + public function testRedirect1() + { + $tropo = new Tropo(); + $redirect = new Redirect("sip:pengxli@192.168.26.1:5678", null, "foo", true); + $tropo->redirect($redirect); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo","required":true}}]}'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 641d3f8..3d3d22a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -506,11 +506,47 @@ public function record($record) { * @see https://www.tropo.com/docs/webapi/redirect.htm */ public function redirect($redirect, Array $params=NULL) { - if(!is_object($redirect)) { - $to = isset($params["to"]) ? $params["to"]: null; - $from = isset($params["from"]) ? $params["from"] : null; - $redirect = new Redirect($to, $from); - } + if ($redirect instanceof Redirect) { + + if(null === $redirect->getTo()) { + throw new Exception("Missing required property: 'to'"); + } + if(null === $redirect->getName()) { + throw new Exception("Missing required property: 'name'"); + } + + } elseif (is_string($redirect) && ($redirect !== '')) { + + if (isset($params) && is_array($params)) { + + if (array_key_exists('name', $params)) { + if (is_string($params["name"]) && ($params["name"] !=='')) { + $name = $params["name"]; + } else { + throw new Exception("'name' must be is a string."); + } + } else { + throw new Exception("Missing required property: 'name'"); + } + + $required = null; + if (array_key_exists('required', $params)) { + $required = $params["required"]; + } + + $redirect = new Redirect($redirect, null, $name, $required); + + } else { + + throw new Exception("When Argument 1 passed to Tropo::redirect() is a string, argument 2 passed to Tropo::redirect() must be of the type array."); + + } + + } else { + + throw new Exception("Argument 1 passed to Tropo::redirect() must be a string or an instance of Redirect."); + + } $this->redirect = sprintf('%s', $redirect); } @@ -1713,7 +1749,16 @@ public function __toString() { class Redirect extends BaseClass { private $_to; - private $_from; + private $_name; + private $_required; + + public function getTo() { + return $this->_to; + } + + public function getName() { + return $this->_name; + } /** * Class constructor @@ -1721,9 +1766,16 @@ class Redirect extends BaseClass { * @param Endpoint $to * @param Endpoint $from */ - public function __construct($to=NULL, $from=NULL) { + public function __construct($to, $from=NULL, $name, $required) { + if(!isset($to)) { + throw new Exception("Missing required property: 'to'"); + } + if(!isset($name)) { + throw new Exception("Missing required property: 'name'"); + } $this->_to = sprintf('%s', $to); - $this->_from = isset($from) ? sprintf('%s', $from) : null; + $this->_name = sprintf('%s', $name); + $this->_required = $required; } /** @@ -1732,7 +1784,8 @@ public function __construct($to=NULL, $from=NULL) { */ public function __toString() { $this->to = $this->_to; - if(isset($this->_from)) { $this->from = $this->_from; } + $this->name = $this->_name; + if(isset($this->_required)) { $this->required = $this->_required; } return $this->unescapeJSON(json_encode($this)); } } From a0c49ccff33194e445dcd8a9255620857e877416 Mon Sep 17 00:00:00 2001 From: pengxli Date: Tue, 27 Jun 2017 17:21:07 +0800 Subject: [PATCH 087/107] bug TROPO-11295 tropo-webapi-php wait --- tests/WaitTest.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 29 +++++++++++++++++++--- 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 tests/WaitTest.php diff --git a/tests/WaitTest.php b/tests/WaitTest.php new file mode 100644 index 0000000..d12432e --- /dev/null +++ b/tests/WaitTest.php @@ -0,0 +1,61 @@ +say($say); + $wait = array( + 'milliseconds' => 8000 + ); + $tropo->wait($wait); + $say = new Say("Bye!", null, null, null, null, "bye"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Connected!","name":"connected"}]},{"wait":{"milliseconds":8000}},{"say":[{"value":"Bye!","name":"bye"}]}]}'); + } + + public function testWaitWithAllOption() + { + $tropo = new Tropo(); + $say = new Say("Connected!", null, null, null, null, "connected"); + $tropo->say($say); + $wait = array( + 'milliseconds' => 8000, + 'allowSignals' => array('exit','quit') + ); + $tropo->wait($wait); + $say = new Say("Bye!", null, null, null, null, "bye"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Connected!","name":"connected"}]},{"wait":{"milliseconds":8000,"allowSignals":["exit","quit"]}},{"say":[{"value":"Bye!","name":"bye"}]}]}'); + } + + public function testWaitObject() + { + $tropo = new Tropo(); + $say = new Say("Connected!", null, null, null, null, "connected"); + $tropo->say($say); + $wait = new Wait(8000); + $tropo->wait($wait); + $say = new Say("Bye!", null, null, null, null, "bye"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Connected!","name":"connected"}]},{"wait":{"milliseconds":8000}},{"say":[{"value":"Bye!","name":"bye"}]}]}'); + } + + public function testWaitObject1() + { + $tropo = new Tropo(); + $say = new Say("Connected!", null, null, null, null, "connected"); + $tropo->say($say); + $wait = new Wait(8000, array('exit','quit')); + $tropo->wait($wait); + $say = new Say("Bye!", null, null, null, null, "bye"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Connected!","name":"connected"}]},{"wait":{"milliseconds":8000,"allowSignals":["exit","quit"]}},{"say":[{"value":"Bye!","name":"bye"}]}]}'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 3d3d22a..6de0944 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -765,10 +765,24 @@ public function transfer($transfer, Array $params=NULL) { * @see https://www.tropo.com/docs/webapi/wait.htm */ public function wait($wait) { - if (!is_object($wait) && is_array($wait)){ - $params = $wait; - $signal = isset($params['allowSignals']) ? $params['allowSignals'] : null; - $wait = new Wait($params["milliseconds"], $signal); + if ($wait instanceof Wait) { + + if(null === $wait->getMilliseconds()) { + throw new Exception("Missing required property: 'milliseconds'"); + } + + } elseif(is_array($wait)) { + + $params = $wait; + if (!array_key_exists("milliseconds", $params)) { + throw new Exception("Missing required property: 'milliseconds'"); + } + $signal = isset($params['allowSignals']) ? $params['allowSignals'] : null; + $wait = new Wait($params["milliseconds"], $signal); + } else { + + throw new Exception("Argument 1 passed to Tropo::wait() must be a array or an instance of Wait."); + } $this->wait = sprintf('%s', $wait); @@ -2447,6 +2461,10 @@ class Wait extends BaseClass { private $_milliseconds; private $_allowSignals; + public function getMilliseconds() { + return $this->_milliseconds; + } + /** * Class constructor * @@ -2454,6 +2472,9 @@ class Wait extends BaseClass { * @param string|array $allowSignals */ public function __construct($milliseconds, $allowSignals=NULL) { + if(!isset($milliseconds)) { + throw new Exception("Missing required property: 'milliseconds'"); + } $this->_milliseconds = $milliseconds; $this->_allowSignals = $allowSignals; } From 755e38e28f30af6acc6e19d76ed72a19616926f8 Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 29 Jun 2017 17:00:23 +0800 Subject: [PATCH 088/107] modify result --- tests/ResultTest.php | 87 ++++++++++++++++++++----------- tropo.class.php | 121 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 154 insertions(+), 54 deletions(-) diff --git a/tests/ResultTest.php b/tests/ResultTest.php index b41fdfb..d022511 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -6,6 +6,34 @@ class ResultTest extends PHPUnit_Framework_TestCase { public function testResult() { + $json = '{"result":{"sessionId":"26a5aa37ff9d14d11990a48865abb5d2","callId":"b11948c7073e815a768fd3f393cc92a6","state":"ANSWERED","sessionDuration":158,"sequence":1,"complete":true,"error":null,"calledid":"9992801029","actions":{"name":"foo","duration":149,"connectedDuration":145,"disposition":"SUCCESS","timestamp":"2017-06-23T02:45:22.849Z","calledid":"pengxli","userType":"HUMAN"}}}'; + $result = new Result($json); + $this->assertEquals($result->getSessionId(), '26a5aa37ff9d14d11990a48865abb5d2'); + $this->assertEquals($result->getCallId(), 'b11948c7073e815a768fd3f393cc92a6'); + $this->assertEquals($result->getState(), 'ANSWERED'); + $this->assertEquals($result->getSessionDuration(), 158); + $this->assertEquals($result->getSequence(), 1); + $this->assertEquals($result->isComplete(), true); + $this->assertEquals($result->getError(), null); + $this->assertEquals($result->getCalledid(), '9992801029'); + $this->assertEquals($result->getUserType(), null); + $this->assertEquals($result->getName(), 'foo'); + $this->assertEquals($result->getAttempts(), null); + $this->assertEquals($result->getDisposition(), 'SUCCESS'); + $this->assertEquals($result->getConfidence(), null); + $this->assertEquals($result->getInterpretation(), null); + $this->assertEquals($result->getUtterance(), null); + $this->assertEquals($result->getValue(), null); + $this->assertEquals($result->getConcept(), null); + $this->assertEquals($result->getXml(), null); + $this->assertEquals($result->getUploadStatus(), null); + $this->assertEquals($result->getDuration(), 149); + $this->assertEquals($result->getConnectedDuration(), 145); + $this->assertEquals($result->getUrl(), null); + $this->assertEquals($result->getActionUserType(), 'HUMAN'); + } + + public function testResult1() { $json = '{"result":{"sessionId":"26a5aa37ff9d14d11990a48865abb5d2","callId":"b11948c7073e815a768fd3f393cc92a6","state":"ANSWERED","sessionDuration":158,"sequence":1,"complete":true,"error":null,"calledid":"9992801029","actions":{"name":"foo","duration":149,"connectedDuration":145,"disposition":"SUCCESS","timestamp":"2017-06-23T02:45:22.849Z","calledid":"pengxli","userType":"HUMAN"}}}'; $result = new Result($json); $this->assertEquals($result->getSessionId(), '26a5aa37ff9d14d11990a48865abb5d2'); @@ -19,21 +47,20 @@ public function testResult() { $this->assertEquals($result->getUserType(), null); $actions = $result->getActions(); $action = $actions; - $this->assertEquals($result->getName($action), 'foo'); - $this->assertEquals($result->getAttempts($action), null); - $this->assertEquals($result->getDisposition($action), 'SUCCESS'); - $this->assertEquals($result->getConfidence($action), null); - $this->assertEquals($result->getInterpretation($action), null); - $this->assertEquals($result->getUtterance($action), null); - $this->assertEquals($result->getValue($action), null); - $this->assertEquals($result->getConcept($action), null); - $this->assertEquals($result->getXml($action), null); - $this->assertEquals($result->getUploadStatus($action), null); - $this->assertEquals($result->getDuration($action), 149); - $this->assertEquals($result->getConnectedDuration($action), 145); - $this->assertEquals($result->getUrl($action), null); - $this->assertEquals($result->getActionUserType($action), 'HUMAN'); - $this->assertEquals($result->getTranscription($action), null); + $this->assertEquals($result->getNameFromAction($action), 'foo'); + $this->assertEquals($result->getAttemptsFromAction($action), null); + $this->assertEquals($result->getDispositionFromAction($action), 'SUCCESS'); + $this->assertEquals($result->getConfidenceFromAction($action), null); + $this->assertEquals($result->getInterpretationFromAction($action), null); + $this->assertEquals($result->getUtteranceFromAction($action), null); + $this->assertEquals($result->getValueFromAction($action), null); + $this->assertEquals($result->getConceptFromAction($action), null); + $this->assertEquals($result->getXmlFromAction($action), null); + $this->assertEquals($result->getUploadStatusFromAction($action), null); + $this->assertEquals($result->getDurationFromAction($action), 149); + $this->assertEquals($result->getConnectedDurationFromAction($action), 145); + $this->assertEquals($result->getUrlFromAction($action), null); + $this->assertEquals($result->getActionUserTypeFromAction($action), 'HUMAN'); } public function testResult2() { @@ -50,21 +77,21 @@ public function testResult2() { $this->assertEquals($result->getUserType(), null); $actions = $result->getActions(); $action = $actions[0]; - $this->assertEquals($result->getName($action), 'foo'); - $this->assertEquals($result->getAttempts($action), 1); - $this->assertEquals($result->getDisposition($action), 'SUCCESS'); - $this->assertEquals($result->getConfidence($action), 100); - $this->assertEquals($result->getInterpretation($action), '1234'); - $this->assertEquals($result->getUtterance($action), '1234'); - $this->assertEquals($result->getValue($action), '1234'); - $this->assertEquals($result->getConcept($action), null); - $this->assertEquals($result->getXml($action), "\r\n\r\n \r\n \r\n 1234\r\n \r\n\r\n"); - $this->assertEquals($result->getUploadStatus($action), null); - $this->assertEquals($result->getDuration($action), null); - $this->assertEquals($result->getConnectedDuration($action), null); - $this->assertEquals($result->getUrl($action), null); - $this->assertEquals($result->getActionUserType($action), null); - $this->assertEquals($result->getTranscription($action), null); + $this->assertEquals($result->getNameFromAction($action), 'foo'); + $this->assertEquals($result->getAttemptsFromAction($action), 1); + $this->assertEquals($result->getDispositionFromAction($action), 'SUCCESS'); + $this->assertEquals($result->getConfidenceFromAction($action), 100); + $this->assertEquals($result->getInterpretationFromAction($action), '1234'); + $this->assertEquals($result->getUtteranceFromAction($action), '1234'); + $this->assertEquals($result->getValueFromAction($action), '1234'); + $this->assertEquals($result->getConceptFromAction($action), null); + $this->assertEquals($result->getXmlFromAction($action), "\r\n\r\n \r\n \r\n 1234\r\n \r\n\r\n"); + $this->assertEquals($result->getUploadStatusFromAction($action), null); + $this->assertEquals($result->getDurationFromAction($action), null); + $this->assertEquals($result->getConnectedDurationFromAction($action), null); + $this->assertEquals($result->getUrlFromAction($action), null); + $this->assertEquals($result->getActionUserTypeFromAction($action), null); + $this->assertEquals($result->getTranscriptionFromAction($action), null); } } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 6de0944..0ce8ee7 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1836,6 +1836,12 @@ class Result { private $_concept; private $_utterance; private $_value; + private $_xml; + private $_uploadStatus; + private $_duration; + private $_connectedDuration; + private $_url; + private $_actionUserType; private $_transcription; /** @@ -1866,15 +1872,22 @@ public function __construct($json=NULL) { $this->_calledid = $result->result->calledid; $this->_userType = isset($result->result->userType) ? $result->result->userType : null; $this->_actions = isset($result->result->actions) ? $result->result->actions : null; - // $this->_name = $result->result->actions->name; - // $this->_attempts = $result->result->actions->attempts; - // $this->_disposition = $result->result->actions->disposition; - // $this->_confidence = $result->result->actions->confidence; - // $this->_interpretation = $result->result->actions->interpretation; - // $this->_utterance = $result->result->actions->utterance; - // $this->_value = $result->result->actions->value; - // $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; - // $this->_transcription = isset($result->result->transcription) ? $result->result->transcription : null; + if (is_object($this->_actions)) { + $this->_name = isset($result->result->actions->name) ? $result->result->actions->name : null; + $this->_attempts = isset($result->result->actions->attempts) ? $result->result->actions->attempts : null; + $this->_disposition = isset($result->result->actions->disposition) ? $result->result->actions->disposition : null; + $this->_confidence = isset($result->result->actions->confidence) ? $result->result->actions->confidence : null; + $this->_interpretation = isset($result->result->actions->interpretation) ? $result->result->actions->interpretation : null; + $this->_utterance = isset($result->result->actions->utterance) ? $result->result->actions->utterance : null; + $this->_value = isset($result->result->actions->value) ? $result->result->actions->value : null; + $this->_concept = isset($result->result->actions->concept) ? $result->result->actions->concept : null; + $this->_xml = isset($result->result->actions->xml) ? $result->result->actions->xml : null; + $this->_uploadStatus = isset($result->result->actions->uploadStatus) ? $result->result->actions->uploadStatus : null; + $this->_duration = isset($result->result->actions->duration) ? $result->result->actions->duration : null; + $this->_connectedDuration = isset($result->result->actions->connectedDuration) ? $result->result->actions->connectedDuration : null; + $this->_url = isset($result->result->actions->url) ? $result->result->actions->url : null; + $this->_actionUserType = isset($result->result->actions->userType) ? $result->result->actions->userType : null; + } } public function getSessionId() { @@ -1917,63 +1930,123 @@ public function getActions() { return $this->_actions; } - public function getName($action) { + public function getName() { + return $this->_name; + } + + public function getNameFromAction($action) { return isset($action->name) ? $action->name : null; } - public function getAttempts($action) { + public function getAttempts() { + return $this->_attempts; + } + + public function getAttemptsFromAction($action) { return isset($action->attempts) ? $action->attempts : null; } - public function getDisposition($action) { + public function getDisposition() { + return $this->_disposition; + } + + public function getDispositionFromAction($action) { return isset($action->disposition) ? $action->disposition : null; } - public function getConfidence($action) { + public function getConfidence() { + return $this->_confidence; + } + + public function getConfidenceFromAction($action) { return isset($action->confidence) ? $action->confidence : null; } - public function getInterpretation($action) { + public function getInterpretation() { + return $this->_interpretation; + } + + public function getInterpretationFromAction($action) { return isset($action->interpretation) ? $action->interpretation : null; } - public function getUtterance($action) { + public function getUtterance() { + return $this->_utterance; + } + + public function getUtteranceFromAction($action) { return isset($action->utterance) ? $action->utterance : null; } - public function getValue($action) { + public function getValue() { + return $this->_utterance; + } + + public function getValueFromAction($action) { return isset($action->value) ? $action->value : null; } - public function getConcept($action) { + public function getConcept() { + return $this->_concept; + } + + public function getConceptFromAction($action) { return isset($action->concept) ? $action->concept : null; } - public function getXml($action) { + public function getXml() { + return $this->_xml; + } + + public function getXmlFromAction($action) { return isset($action->xml) ? $action->xml : null; } - public function getUploadStatus($action) { + public function getUploadStatus() { + return $this->_uploadStatus; + } + + public function getUploadStatusFromAction($action) { return isset($action->uploadStatus) ? $action->uploadStatus : null; } - public function getDuration($action) { + public function getDuration() { + return $this->_duration; + } + + public function getDurationFromAction($action) { return isset($action->duration) ? $action->duration : null; } - public function getConnectedDuration($action) { + public function getConnectedDuration() { + return $this->_connectedDuration; + } + + public function getConnectedDurationFromAction($action) { return isset($action->connectedDuration) ? $action->connectedDuration : null; } - public function getUrl($action) { + public function getUrl() { + return $this->_url; + } + + public function getUrlFromAction($action) { return isset($action->url) ? $action->url : null; } - public function getActionUserType($action) { + public function getActionUserType() { + return $this->_actionUserType; + } + + public function getActionUserTypeFromAction($action) { return isset($action->userType) ? $action->userType : null; } - public function getTranscription($action) { + public function getTranscription() { + return $this->_transcription; + } + + public function getTranscriptionFromAction($action) { return isset($action->transcription) ? $action->transcription : null; } } From 9cc99124c5904387db0fd1c8f3b3f9be06ea9b17 Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 30 Jun 2017 17:26:35 +0800 Subject: [PATCH 089/107] review say --- tests/SayTest.php | 12 ++++---- tropo.class.php | 76 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/tests/SayTest.php b/tests/SayTest.php index ef99158..176ba30 100644 --- a/tests/SayTest.php +++ b/tests/SayTest.php @@ -95,7 +95,7 @@ public function testFailsSayWithNoValueParameter3() try{ @ $tropo->say(); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + $this->assertEquals($e->getMessage(), "Argument 1 passed to Tropo::say() must be a string or an instance of Say."); } } @@ -105,7 +105,7 @@ public function testFailsSayWithNoValueParameter4() try{ @ $tropo->say(null); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + $this->assertEquals($e->getMessage(), "Argument 1 passed to Tropo::say() must be a string or an instance of Say."); } } @@ -115,7 +115,7 @@ public function testFailsSayWithNoValueParameter5() try{ @ $tropo->say(""); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); + $this->assertEquals($e->getMessage(), "Argument 1 passed to Tropo::say() must be a string or an instance of Say."); } } @@ -125,7 +125,7 @@ public function testFailsSayWithNoNameParameter3() try{ $tropo->say("Please enter your account number..."); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + $this->assertEquals($e->getMessage(), "When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); } } @@ -136,7 +136,7 @@ public function testFailsSayWithNoNameParameter4() try{ $tropo->say("Please enter your account number...",$params); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + $this->assertEquals($e->getMessage(), "Required property: 'name' must be a string."); } } @@ -147,7 +147,7 @@ public function testFailsSayWithNoNameParameter5() try{ $tropo->say("Please enter your account number...",$params); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); + $this->assertEquals($e->getMessage(), "Required property: 'name' must be a string."); } } } diff --git a/tropo.class.php b/tropo.class.php index 0ce8ee7..000e9dc 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -571,32 +571,64 @@ public function reject() { * @see https://www.tropo.com/docs/webapi/say.htm */ public function say($say, Array $params=NULL) { - if(!is_object($say)) { - if(!$say) { + if ($say instanceof Say) { + + if(!(is_string($say->getValue()) && ($say->getValue() != ''))) { + throw new Exception("Missing required property: 'value'"); + } - $p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity'); - $value = $say; - foreach ($p as $option) { - $$option = null; - if (is_array($params) && array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - if(!$name) { + if(!(is_string($say->getName()) && ($say->getName() != ''))) { + throw new Exception("Missing required property: 'name'"); + } - $voice = isset($voice) ? $voice : $this->_voice; - $event = null; - $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity); - } else { + $say->setEvent(null); - if(!($say->getValue())) { - throw new Exception("Missing required property: 'value'"); - } - if(!($say->getName())) { - throw new Exception("Missing required property: 'name'"); + + } elseif (is_string($say) && ($say != '')) { + + if (isset($params) && is_array($params)) { + + if (array_key_exists('name', $params)) { + + if (is_string($params['name']) && ($params['name'] != '')) { + + $name = $params['name']; + + } else { + + throw new Exception("Required property: 'name' must be a string."); + + } + + $p = array('as', 'event','voice', 'allowSignals', 'required', 'promptLogSecurity'); + $value = $say; + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $voice = isset($voice) ? $voice : $this->_voice; + $event = null; + $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity); + + } else { + + throw new Exception("Missing required property: 'name'"); + + } + + } else { + + throw new Exception("When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); + } + } else { + + throw new Exception("Argument 1 passed to Tropo::say() must be a string or an instance of Say."); + } $this->say = array(sprintf('%s', $say)); } @@ -2090,7 +2122,7 @@ public function setEvent($event) { * @param string|array $allowSignals */ public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL, $name=NULL, $required=NULL, $promptLogSecurity=NULL) { - if(!$value) { + if(!(is_string($value) && ($value != ''))) { throw new Exception("Missing required property: 'value'"); } $this->_value = $value; @@ -2116,7 +2148,7 @@ public function __toString() { if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From bcbc771bd193f1aafbd61d2736f225e28f8e82a6 Mon Sep 17 00:00:00 2001 From: pengxli Date: Sun, 2 Jul 2017 16:20:41 +0800 Subject: [PATCH 090/107] review on --- tests/OnTest.php | 22 ++++----- tropo.class.php | 117 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 106 insertions(+), 33 deletions(-) diff --git a/tests/OnTest.php b/tests/OnTest.php index aeda0d5..30da151 100644 --- a/tests/OnTest.php +++ b/tests/OnTest.php @@ -7,7 +7,7 @@ class OnTest extends TestCase{ public function testCreateOnObject() { $tropo = new Tropo(); - $say = new Say("object continue!", null, null, null, null, null, null, null); + $say = new Say("object continue!"); $on = new On(Event::$continue, "say.php", $say); $tropo->on($on); $this->assertEquals(sprintf($tropo), '{"tropo":[{"on":[{"event":"continue","next":"say.php","say":{"value":"object continue!"}}]}]}'); @@ -16,7 +16,7 @@ public function testCreateOnObject() { public function testCreateOnObject1() { $tropo = new Tropo(); - $say = new Say("array continue!", null, null, null, null, null, null, null); + $say = new Say("array continue!"); $on = array( 'event' => Event::$continue, 'next' => 'say.php', @@ -29,7 +29,7 @@ public function testCreateOnObject1() { public function testFailsOnWithNoEventParameter() { $tropo = new Tropo(); try { - $say = new Say("object continue!", null, null, null, null, null, null, null); + $say = new Say("object continue!"); @ $on = new On(); } catch (Exception $e) { $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); @@ -39,7 +39,7 @@ public function testFailsOnWithNoEventParameter() { public function testFailsOnWithNoEventParameter1() { $tropo = new Tropo(); try { - $say = new Say("object continue!", null, null, null, null, null, null, null); + $say = new Say("object continue!"); @ $on = new On(null, "say.php", $say); } catch (Exception $e) { $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); @@ -49,7 +49,7 @@ public function testFailsOnWithNoEventParameter1() { public function testFailsOnWithNoEventParameter2() { $tropo = new Tropo(); try { - $say = new Say("object continue!", null, null, null, null, null, null, null); + $say = new Say("object continue!"); @ $on = new On("", "say.php", $say); } catch (Exception $e) { $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); @@ -59,7 +59,7 @@ public function testFailsOnWithNoEventParameter2() { public function testFailsOnWithNoEventParameter3() { $tropo = new Tropo(); try { - $say = new Say("array continue!", null, null, null, null, null, null, null); + $say = new Say("array continue!"); @ $on = array( 'next' => 'say.php', 'say' => $say @@ -73,7 +73,7 @@ public function testFailsOnWithNoEventParameter3() { public function testFailsOnWithNoEventParameter4() { $tropo = new Tropo(); try { - $say = new Say("array continue!", null, null, null, null, null, null, null); + $say = new Say("array continue!"); $on = array( 'event' => null, 'next' => 'say.php', @@ -81,14 +81,14 @@ public function testFailsOnWithNoEventParameter4() { ); $tropo->on($on); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + $this->assertEquals($e->getMessage(), "Required property: 'event' must be a string."); } } public function testFailsOnWithNoEventParameter5() { $tropo = new Tropo(); try { - $say = new Say("array continue!", null, null, null, null, null, null, null); + $say = new Say("array continue!"); $on = array( 'event' => '', 'next' => 'say.php', @@ -96,7 +96,7 @@ public function testFailsOnWithNoEventParameter5() { ); $tropo->on($on); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); + $this->assertEquals($e->getMessage(), "Required property: 'event' must be a string."); } } @@ -133,7 +133,7 @@ public function testFailsOnWithNoSayParameter2() { ); $tropo->on($on); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); + $this->assertEquals($e->getMessage(), "Required property: 'say' must be a Say of array or an instance of Say."); } } } diff --git a/tropo.class.php b/tropo.class.php index 000e9dc..6c2b25e 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -387,28 +387,85 @@ public function message($message, Array $params=null) { * @see https://www.tropo.com/docs/webapi/on.htm */ public function on($on) { - if (!is_object($on)) { - if (is_array($on)) { - $params = $on; - if (!$params["event"]) { - throw new Exception("Missing required property: 'event'"); - } - if (!$params["say"]) { - throw new Exception("Missing required property: 'say'"); - } - if (!is_object($params["say"])) { - throw new Exception("Property 'say' must be a Say object"); - } - $next = (array_key_exists('next', $params)) ? $params["next"] : null; - $on = new On($params["event"], $next, $params["say"]); + if ($on instanceof On) { + + if(!(is_string($on->getEvent()) && ($on->getEvent() != ''))) { + throw new Exception("Missing required property: 'event'"); } - } else { - if(!($on->getEvent())) { + if(null === $on->getSay()) { + throw new Exception("Missing required property: 'say'"); + } + + } elseif (is_array($on)) { + + if (array_key_exists('event', $on)) { + + if (is_string($on['event']) && ($on['event'] != '')) { + + $event = $on['event']; + + } else { + + throw new Exception("Required property: 'event' must be a string."); + + } + } else { + throw new Exception("Missing required property: 'event'"); + } - if(!($on->getSay())) { + + if (array_key_exists('say', $on)) { + + if ($on['say'] instanceof Say) { + + if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) { + + throw new Exception("The value of say must be a string."); + + } else { + + $say = $on['say']; + } + + } elseif (is_array($on['say'])) { + + foreach ($on['say'] as $value) { + + if ($value instanceof Say) { + + if(!(is_string($value->getValue()) && ($value->getValue() != ''))) { + + throw new Exception("The value of say must be a string."); + + } + + } else { + + throw new Exception("Required property: 'say' must be a Say of array or an instance of Say."); + + } + } + + $say = $on['say']; + + } else { + + throw new Exception("Required property: 'say' must be a Say of array or an instance of Say."); + } + } else { + throw new Exception("Missing required property: 'say'"); + } + + $next = (array_key_exists('next', $on)) ? $on["next"] : null; + //$event, $next=NULL, Say $say=NULL, $voice=Null, Ask $ask=NULL, Message $message=NULL, Wait $wait=NULL, $order=NULL, $post=NULL + $on = new On($event, $next, $say); + } else { + + throw new Exception("Argument 1 passed to Tropo::on() must be a array or an instance of On."); + } $this->on = array(sprintf('%s', $on)); } @@ -1634,13 +1691,29 @@ public function getSay() { * @param Say $say * @param string $voice */ - public function __construct($event, $next=NULL, Say $say=NULL, Ask $ask=NULL, $post=NULL) { - if(!$event) { + public function __construct($event, $next=NULL, $say=NULL, $voice=Null, Ask $ask=NULL, Message $message=NULL, Wait $wait=NULL, $order=NULL, $post=NULL) { + if(!(is_string($event) && ($event != ''))) { throw new Exception("Missing required property: 'event'"); } $this->_event = $event; $this->_next = $next; - $this->_say = isset($say) ? sprintf('%s', $say) : null ; + if (isset($say)) { + if ($say instanceof Say) { + $this->_say = sprintf('%s', $say); + } elseif (is_array($say)) { + foreach ($say as $key => $value) { + $this->_say[$key] = sprintf('%s', $value); + } + foreach ($this->_say as $key => $value) { + $this->_say[$key] = json_decode($value); + } + $this->_say = json_encode($this->_say); + } else { + $this->_say = null; + } + } else { + $this->_say = null; + } $this->_ask = isset($ask) ? sprintf('%s', $ask) : null; $this->_post = $post; } @@ -1652,10 +1725,10 @@ public function __construct($event, $next=NULL, Say $say=NULL, Ask $ask=NULL, $p public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } if(isset($this->_next)) { $this->next = $this->_next; } - if(isset($this->_say)) { $this->say = $this->_say; } + if(isset($this->_say)) { $this->say = json_decode($this->_say); } if(isset($this->_ask)) { $this->ask = $this->_ask; } if(isset($this->_post)) { $this->post = $this->_post; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From f58991a02e6df37c51def66017647870aa2d795f Mon Sep 17 00:00:00 2001 From: pengxli Date: Sun, 2 Jul 2017 17:34:52 +0800 Subject: [PATCH 091/107] review ask --- tropo.class.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 6c2b25e..ee8b2ef 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -1244,11 +1244,26 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices, $mi } $this->_attempts = $attempts; $this->_bargein = $bargein; - $this->_choices = isset($choices) ? sprintf('%s', $choices) : null ; + $this->_choices = sprintf('%s', $choices); $this->_minConfidence = $minConfidence; $this->_name = $name; $this->_required = $required; - $this->_say = isset($say) ? $say : null; + if ($say instanceof Say) { + $this->_say = sprintf('%s', $say); + } elseif (is_array($say)) { + foreach ($say as $key => $value) { + if (!($value instanceof Say)) { + throw new Exception("Required property: 'say' must be an instance of Say or a Say of array."); + } + $this->_say[$key] = sprintf('%s', $value); + } + foreach ($this->_say as $key => $value) { + $this->_say[$key] = json_decode($value); + } + $this->_say = json_encode($this->_say); + } else { + throw new Exception("Required property: 'say' must be an instance of Say or a Say of array."); + } $this->_timeout = $timeout; $this->_voice = $voice; $this->_allowSignals = $allowSignals; @@ -1269,16 +1284,11 @@ public function __construct($attempts=NULL, $bargein=NULL, Choices $choices, $mi public function __toString() { if(isset($this->_attempts)) { $this->attempts = $this->_attempts; } if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_choices)) { $this->choices = json_decode($this->_choices); } if(isset($this->_minConfidence)) { $this->minConfidence = $this->_minConfidence; } if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } - if(isset($this->_say)) { $this->say = $this->_say; } - if (is_array($this->_say)) { - foreach ($this->_say as $k => $v) { - $this->_say[$k] = sprintf('%s', $v); - } - } + if(isset($this->_say)) { $this->say = json_decode($this->_say); } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } @@ -1290,7 +1300,7 @@ public function __toString() { if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } if(isset($this->_asrLogSecurity)) { $this->asrLogSecurity = $this->_asrLogSecurity; } if(isset($this->_maskTemplate)) { $this->maskTemplate = $this->_maskTemplate; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } /** @@ -1450,7 +1460,7 @@ public function __toString() { if(isset($this->_value)){ $this->value = $this->_value; } if(isset($this->_mode)) { $this->mode = $this->_mode; } if(isset($this->_terminator)) { $this->terminator = $this->_terminator; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } @@ -1702,6 +1712,9 @@ public function __construct($event, $next=NULL, $say=NULL, $voice=Null, Ask $ask $this->_say = sprintf('%s', $say); } elseif (is_array($say)) { foreach ($say as $key => $value) { + if (!($value instanceof Say)) { + throw new Exception("Required property: 'say' must be a Say of array or an instance of Say."); + } $this->_say[$key] = sprintf('%s', $value); } foreach ($this->_say as $key => $value) { From d0575a9d716f067b61afa8bfa9e471b08994d689 Mon Sep 17 00:00:00 2001 From: pengxli Date: Sun, 2 Jul 2017 19:59:27 +0800 Subject: [PATCH 092/107] review message --- tests/MessageTest.php | 6 ++--- tropo.class.php | 59 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 292cc87..58b1218 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -52,7 +52,7 @@ public function testMessageWithAllOptions() { public function testCreateMinObject() { $tropo = new Tropo(); - $message = new Message(new Say("This is an announcement."), "sip:pengxli@192.168.26.1:5678", "foo"); + $message = new Message(new Say("This is an announcement."), "sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, "foo"); $tropo->message($message); $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); } @@ -63,7 +63,7 @@ public function testCreateObject1() { new Say("This is an announcement."), new Say("Remember, you have a meeting at 2 PM.") ); - $message = new Message($say, "sip:pengxli@192.168.26.1:5678", "foo"); + $message = new Message($say, "sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, "foo"); $tropo->message($message); $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); } @@ -80,7 +80,7 @@ public function testCreateObject2() { 'sip:pengxli@172.16.72.131:5678' ); $headers = array('foo' => 'bar', 'bling' => 'baz'); - $message = new Message($say, $to, "foo", Channel::$voice, Network::$sip, "3055551000", Voice::$US_English_female_allison, 60, false, $headers, true, "suppress"); + $message = new Message($say, $to, Channel::$voice, Network::$sip, "3055551000", Voice::$US_English_female_allison, 60, false, $headers, "foo", true, "suppress"); $tropo->message($message); $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo","channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); } diff --git a/tropo.class.php b/tropo.class.php index ee8b2ef..5d46c6a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -312,6 +312,19 @@ public function message($message, Array $params=null) { if(null === $message->getName()) { throw new Exception("Missing required property: 'name'"); } + if (is_string($message->getTo()) && ($message->getTo() != '')) { + } elseif (is_array($message->getTo())) { + foreach ($message->getTo() as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if(!(is_string($message->getName()) && ($message->getName() != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } } elseif (is_string($message) && ($message!=='')) { if (isset($params) && is_array($params)) { @@ -368,7 +381,7 @@ public function message($message, Array $params=null) { $$option = $params[$option]; } } - $message = new Message($say, $to, $name, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers, $required, $promptLogSecurity); + $message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers, $name, $required, $promptLogSecurity); } else { throw new Exception("When Argument 1 passed to Tropo::message() is a string, argument 2 passed to Tropo::message() must be of the type array."); } @@ -1618,7 +1631,8 @@ public function getName() { * @param boolean $answerOnMedia * @param array $headers */ - public function __construct($say, $to, $name, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null, $required=null, $promptLogSecurity=null) { + // Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null + public function __construct($say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null, $name, $required=null, $promptLogSecurity=null) { if(!isset($say)) { throw new Exception("Missing required property: 'say'"); } @@ -1630,11 +1644,37 @@ public function __construct($say, $to, $name, $channel=null, $network=null, $fro } if ($say instanceof Say) { $this->_say = sprintf('%s', $say); + } elseif (is_array($say)) { + foreach ($say as $key => $value) { + if (!($value instanceof Say)) { + throw new Exception("Required property: 'say' must be an instance of Say or a Say of array."); + } + $this->_say[$key] = sprintf('%s', $value); + } + foreach ($this->_say as $key => $value) { + $this->_say[$key] = json_decode($value); + } + $this->_say = json_encode($this->_say); } else { - $this->_say = $say; + throw new Exception("Required property: 'say' must be an instance of Say or a Say of array."); + } + if (is_string($to) && ($to != '')) { + $this->_to = $to; + } elseif (is_array($to)) { + foreach ($to as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + $this->_to = $to; + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if (is_string($name) && ($name != '')) { + $this->_name = $name; + } else { + throw new Exception("Required property: 'name' must be a string."); } - $this->_to = $to; - $this->_name = $name; $this->_channel = $channel; $this->_network = $network; $this->_from = $from; @@ -1651,12 +1691,7 @@ public function __construct($say, $to, $name, $channel=null, $network=null, $fro * */ public function __toString() { - $this->say = $this->_say; - if (is_array($this->_say)) { - foreach ($this->_say as $k => $v) { - $this->_say[$k] = sprintf('%s', $v); - } - } + if(isset($this->_say)) { $this->say = json_decode($this->_say); } $this->to = $this->_to; $this->name = $this->_name; if(isset($this->_channel)) { $this->channel = $this->_channel; } @@ -1668,7 +1703,7 @@ public function __toString() { if(count($this->_headers)) { $this->headers = $this->_headers; } if(count($this->_required)) { $this->required = $this->_required; } if(count($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From f6da102138e616112d2bd6b63c5e77f0dc4b9b25 Mon Sep 17 00:00:00 2001 From: pengxli Date: Sun, 2 Jul 2017 21:23:04 +0800 Subject: [PATCH 093/107] review call --- tests/CallTest.php | 35 +++++++++---------------- tropo.class.php | 64 +++++++++++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/tests/CallTest.php b/tests/CallTest.php index ad30551..86101c0 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -16,28 +16,17 @@ public function testCallWithMinOptions() { public function testCallWithExtraToOptiions() { $tropo = new Tropo(); + $call = array('sip:pengxli@192.168.26.1:5678', 'sip:pengxli@192.168.26.206:5678'); $params = array( - 'to' => 'sip:pengxli@172.16.72.131:5678', 'name' => 'foo' ); - $tropo->call("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo"}}]}'); - } - - public function testCallWithExtraToOptiions1() { - $tropo = new Tropo(); - $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); - $params = array( - 'to' => $to, - 'name' => 'foo' - ); - $tropo->call("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"name":"foo"}}]}'); + $tropo->call($call, $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"name":"foo"}}]}'); } public function testCallWithAllOptions() { $tropo = new Tropo(); - $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); + $call = array('sip:pengxli@192.168.26.1:5678', 'sip:pengxli@192.168.26.206:5678'); $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); $params = array( @@ -57,13 +46,13 @@ public function testCallWithAllOptions() { 'promptLogSecurity' => 'suppress', 'label' => 'callLabel' ); - $tropo->call("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $tropo->call($call, $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } public function testCallWithAllOptions1() { $tropo = new Tropo(); - $to = array('sip:pengxli@172.16.72.131:5678', 'sip:pengxli@192.168.26.206:5678'); + $call = array('sip:pengxli@192.168.26.1:5678', 'sip:pengxli@192.168.26.206:5678'); $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); $params = array( @@ -73,7 +62,7 @@ public function testCallWithAllOptions1() { 'channel' => Channel::$voice, 'from' => '3055551000', 'headers' => $headers, - 'machineDetection' => "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.", + 'machineDetection' => 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.', 'name' => 'foo', 'network' => Network::$sip, 'required' => true, @@ -83,8 +72,8 @@ public function testCallWithAllOptions1() { 'promptLogSecurity' => 'suppress', 'label' => 'callLabel' ); - $tropo->call("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $tropo->call($call, $params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } public function testCreateMinObject() { @@ -117,10 +106,10 @@ public function testCreateObject3() { $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); - $machineDetection = "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered."; + $machineDetection = 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.'; $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, $machineDetection, Voice::$US_English_female_allison, "foo", true, "http://192.168.26.203/result.php", "suppress", "callLabel"); $tropo->call($call); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 5d46c6a..c421a67 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -175,8 +175,37 @@ public function call($call, Array $params=NULL) { if(null === $call->getName()) { throw new Exception("Missing required property: 'name'"); } + if (is_string($call->getTo()) && ($call->getTo() != '')) { + } elseif (is_array($call->getTo())) { + foreach ($call->getTo() as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if (!(is_string($call->getName()) && ($call->getName() != ''))) { + throw new Exception("Required property: 'to' must be a string."); + } - } elseif (is_string($call) && ($call !== '')) { + } elseif (is_array($call) || (is_string($call) && ($call !== ''))) { + + if (is_array($call)) { + $to = null; + foreach ($call as $value) { + if (is_string($value) && ($value !== '')) { + $to[] = $value; + } + } + if (null === $to) { + + throw new Exception("Argument 1 passed to Tropo::call() must be a string or a string of array or an instance of Call."); + + } + } else { + $to = $call; + } if (isset($params) && is_array($params)) { @@ -190,24 +219,6 @@ public function call($call, Array $params=NULL) { throw new Exception("Missing required property: 'name'"); } - if (array_key_exists('to', $params)) { - if (is_array($params["to"])) { - $to[] = $call; - foreach ($params["to"] as $value) { - if (is_string($value) && ($value !== '')) { - $to[] = $value; - } - } - } elseif (is_string($params["to"]) && ($params["to"] !== '')) { - $to[] = $call; - $to[] = $params["to"]; - } else { - $to = $call; - } - } else { - $to = $call; - } - $p = array('from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'allowSignals', 'machineDetection', 'voice', 'required', 'callbackUrl', 'promptLogSecurity', 'label'); foreach ($p as $option) { $$option = null; @@ -1380,6 +1391,19 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ if(!isset($name)) { throw new Exception("Missing required property: 'name'"); } + if (is_string($to) && ($to != '')) { + } elseif (is_array($to)) { + foreach ($to as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if (!(is_string($name) && ($name != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } $this->_to = $to; $this->_from = $from; $this->_network = $network; @@ -1426,7 +1450,7 @@ public function __toString() { if(isset($this->_callbackUrl)) { $this->callbackUrl = $this->_callbackUrl; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } if(isset($this->_label)) { $this->label = $this->_label; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From 4845c83bfa0149440743b9828bed423718cfd884 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 3 Jul 2017 09:53:42 +0800 Subject: [PATCH 094/107] review conference --- tropo.class.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index c421a67..68f2392 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -258,6 +258,12 @@ public function conference($conference, Array $params=NULL) { if(null === $conference->getName()) { throw new Exception("Missing required property: 'name'"); } + if (!(is_string($conference->getName()) && ($conference->getName() != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } + if (!(is_string($conference->getId()) && ($conference->getId() != ''))) { + throw new Exception("Required property: 'id' must be a string."); + } } elseif (is_string($conference) && ($conference !== '')) { @@ -1552,6 +1558,12 @@ public function __construct($name, $id, $mute=NULL, On $on=NULL, $playTones=NULL if(!isset($id)) { throw new Exception("Missing required property: 'id'"); } + if (!(is_string($name) && ($name != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } + if (!(is_string($id) && ($id != ''))) { + throw new Exception("Required property: 'id' must be a string."); + } $this->_name = $name; $this->_id = (string) $id; $this->_mute = $mute; @@ -1599,7 +1611,7 @@ public function __toString() { } } } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From fb35ccd8167770653f09b2214c22561e36cb7ba2 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 3 Jul 2017 10:45:07 +0800 Subject: [PATCH 095/107] review record --- tropo.class.php | 60 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 68f2392..f05e772 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -519,10 +519,28 @@ public function record($record) { if(null === $record->getName()) { throw new Exception("Missing required property: 'name'"); } + if (!(is_string($record->getUrl()) && ($record->getUrl() != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } + if (!(is_string($record->getName()) && ($record->getName() != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } } elseif (is_array($record)) { $params = $record; + if (!array_key_exists('url', $params)) { + throw new Exception("Missing required property: 'url'"); + } + if (!array_key_exists('name', $params)) { + throw new Exception("Missing required property: 'name'"); + } + if (!(is_string($params['url']) && ($params['url'] != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } + if (!(is_string($params['name']) && ($params['name'] != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } $p = array('voice', 'emailFormat', 'transcription', 'terminator'); foreach ($p as $option) { $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; @@ -1882,6 +1900,12 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ if(!isset($name)) { throw new Exception("Missing required property: 'name'"); } + if (!(is_string($url) && ($url != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } + if (!(is_string($name) && ($name != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } $this->_attempts = $attempts; $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; @@ -1893,10 +1917,23 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_method = $method; $this->_password = $password; $this->_required = $required; - if ($say instanceof Say) { - $this->_say = sprintf('%s', $say); - } else { - $this->_say = $say; + if (isset($say)) { + if ($say instanceof Say) { + $this->_say = sprintf('%s', $say); + } elseif (is_array($say)) { + foreach ($say as $key => $value) { + if (!($value instanceof Say)) { + throw new Exception("Property: 'say' must be an instance of Say or a Say of array."); + } + $this->_say[$key] = sprintf('%s', $value); + } + foreach ($this->_say as $key => $value) { + $this->_say[$key] = json_decode($value); + } + $this->_say = json_encode($this->_say); + } else { + throw new Exception("Property: 'say' must be an instance of Say or a Say of array."); + } } $this->_timeout = $timeout; $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; @@ -1918,21 +1955,16 @@ public function __toString() { if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(isset($this->_bargein)) { $this->bargein = $this->_bargein; } if(isset($this->_beep)) { $this->beep = $this->_beep; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_choices)) { $this->choices = json_decode($this->_choices); } if(isset($this->_format)) { $this->format = $this->_format; } if(isset($this->_maxSilence)) { $this->maxSilence = $this->_maxSilence; } if(isset($this->_maxTime)) { $this->maxTime = $this->_maxTime; } if(isset($this->_method)) { $this->method = $this->_method; } if(isset($this->_password)) { $this->password = $this->_password; } if(isset($this->_required)) { $this->required = $this->_required; } - if(isset($this->_say)) { $this->say = $this->_say; } - if (is_array($this->_say)) { - foreach ($this->_say as $k => $v) { - $this->_say[$k] = sprintf('%s', $v); - } - } + if(isset($this->_say)) { $this->say = json_decode($this->_say); } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_transcription)) { $this->transcription = $this->_transcription; } + if(isset($this->_transcription)) { $this->transcription = json_decode($this->_transcription); } if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_voice)) { $this->voice = $this->_voice; } @@ -1940,7 +1972,7 @@ public function __toString() { if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } @@ -2588,7 +2620,7 @@ public function __toString() { if(isset($this->_id)) { $this->id = $this->_id; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_emailFormat)) { $this->emailFormat = $this->_emailFormat; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From 711959a0492b1c842b2f7fab50bb5d782a730ec6 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 3 Jul 2017 12:14:40 +0800 Subject: [PATCH 096/107] review transfer --- tests/TransferTest.php | 20 ++++++++-------- tropo.class.php | 52 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/tests/TransferTest.php b/tests/TransferTest.php index b7215bd..9040982 100644 --- a/tests/TransferTest.php +++ b/tests/TransferTest.php @@ -90,7 +90,7 @@ public function testTransferWithAllOptions() { new Say("Please enter 5 digit account number.") ); $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); - $onConnect = new On("connect", null, null, $ask); + $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); $params = array( 'from' => '14155551212', @@ -112,7 +112,7 @@ public function testTransferWithAllOptions() { 'label' => 'transferLabel' ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } public function testTransferWithAllOptions1() { @@ -126,7 +126,7 @@ public function testTransferWithAllOptions1() { new Say("Please enter 5 digit account number.") ); $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); - $onConnect = new On("connect", null, null, $ask); + $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); $params = array( 'from' => '14155551212', @@ -135,7 +135,7 @@ public function testTransferWithAllOptions1() { 'name' => 'foo', 'required' => true, 'allowSignals' => $allowSignals, - 'machineDetection' => "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.", + 'machineDetection' => 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.', 'terminator' => '#', 'headers' => $headers, 'interdigitTimeout' => 5.0, @@ -148,7 +148,7 @@ public function testTransferWithAllOptions1() { 'label' => 'transferLabel' ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } @@ -194,11 +194,11 @@ public function testCreateObject3() { new Say("Please enter 5 digit account number.") ); $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); - $onConnect = new On("connect", null, null, $ask); + $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, false, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } public function testCreateObject4() { @@ -206,7 +206,7 @@ public function testCreateObject4() { $choices = new Choices(null, null, "#"); $allowSignals = array("exit", "quit"); $headers = array('foo' => 'bar', 'bling' => 'baz'); - $machineDetection = "For the most accurate results, the 'introduction' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered."; + $machineDetection = 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.'; $onRing = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); $say = array( new Say("Sorry. Please enter you 5 digit account number again.", null, "nomatch"), @@ -214,11 +214,11 @@ public function testCreateObject4() { new Say("Please enter 5 digit account number.") ); $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); - $onConnect = new On("connect", null, null, $ask); + $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, $machineDetection, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http:\/\/www.phono.com\/audio\/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \'introduction\' should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } } diff --git a/tropo.class.php b/tropo.class.php index f05e772..d09b942 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -803,6 +803,19 @@ public function transfer($transfer, Array $params=NULL) { if(null === $transfer->getName()) { throw new Exception("Missing required property: 'name'"); } + if (is_string($transfer->getTo()) && ($transfer->getTo() != '')) { + } elseif (is_array($transfer->getTo())) { + foreach ($transfer->getTo() as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if (!(is_string($transfer->getName()) && ($transfer->getName() != ''))) { + throw new Exception("Required property: 'to' must be a string."); + } } elseif (is_array($transfer) || (is_string($transfer) && ($transfer !== ''))) {//$transfer is a non-empty string or a non-empty string of array @@ -1828,7 +1841,7 @@ public function __toString() { if(isset($this->_event)) { $this->event = $this->_event; } if(isset($this->_next)) { $this->next = $this->_next; } if(isset($this->_say)) { $this->say = json_decode($this->_say); } - if(isset($this->_ask)) { $this->ask = $this->_ask; } + if(isset($this->_ask)) { $this->ask = json_decode($this->_ask); } if(isset($this->_post)) { $this->post = $this->_post; } return json_encode($this); } @@ -2677,6 +2690,19 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr if(!isset($name)) { throw new Exception("Missing required property: 'name'"); } + if (is_string($to) && ($to != '')) { + } elseif (is_array($to)) { + foreach ($to as $value) { + if (!(is_string($value) && ($value != ''))) { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + } + } else { + throw new Exception("Required property: 'to' must be a string or a string of array."); + } + if (!(is_string($name) && ($name != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; @@ -2687,8 +2713,19 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr if (isset($on)) { if ($on instanceof On) { $this->_on = sprintf('%s', $on); + } elseif (is_array($on)) { + foreach ($on as $key => $value) { + if (!($value instanceof On)) { + throw new Exception("Property: 'on' must be a On of array or an instance of On."); + } + $this->_on[$key] = sprintf('%s', $value); + } + foreach ($this->_on as $key => $value) { + $this->_on[$key] = json_decode($value); + } + $this->_on = json_encode($this->_on); } else { - $this->_on = $on; + $this->_on = null; } } $this->_allowSignals = $allowSignals; @@ -2711,16 +2748,11 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr public function __toString() { $this->to = $this->_to; if(isset($this->_answerOnMedia)) { $this->answerOnMedia = $this->_answerOnMedia; } - if(isset($this->_choices)) { $this->choices = $this->_choices; } + if(isset($this->_choices)) { $this->choices = json_decode($this->_choices); } if(isset($this->_from)) { $this->from = $this->_from; } if(isset($this->_ringRepeat)) { $this->ringRepeat = $this->_ringRepeat; } if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } - if(isset($this->_on)) { $this->on = $this->_on; } - if (is_array($this->_on)) { - foreach ($this->_on as $k => $v) { - $this->_on[$k] = sprintf('%s', $v); - } - } + if(isset($this->_on)) { $this->on = json_decode($this->_on); } if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } if(count($this->_headers)) { $this->headers = $this->_headers; } if(isset($this->_machineDetection)) { @@ -2741,7 +2773,7 @@ public function __toString() { if(isset($this->_callbackUrl)) { $this->callbackUrl = $this->_callbackUrl; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } if(isset($this->_label)) { $this->label = $this->_label; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From 106724c55032ff1992d0135848519fdf9ac06d33 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 3 Jul 2017 12:35:18 +0800 Subject: [PATCH 097/107] review startRecording --- tropo.class.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tropo.class.php b/tropo.class.php index d09b942..917a531 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -751,12 +751,18 @@ public function startRecording($startRecording) { if(null === $startRecording->getUrl()) { throw new Exception("Missing required property: 'url'"); } + if (!(is_string($startRecording->getUrl()) && ($startRecording->getUrl() != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } } elseif (is_array($startRecording)) { if (!array_key_exists('url', $startRecording)) { throw new Exception("Missing required property: 'url'"); } + if (!(is_string($startRecording['url']) && ($startRecording['url'] != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } $params = $startRecording; $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload'); @@ -2565,6 +2571,9 @@ public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $u if(!isset($url)) { throw new Exception("Missing required property: 'url'"); } + if (!(is_string($url) && ($url != ''))) { + throw new Exception("Required property: 'url' must be a string."); + } $this->_format = $format; $this->_method = $method; $this->_password = $password; @@ -2590,7 +2599,7 @@ public function __toString() { if(isset($this->_transcriptionEmailFormat)) { $this->transcriptionEmailFormat = $this->_transcriptionEmailFormat; } if(isset($this->_transcriptionOutURI)) { $this->transcriptionOutURI = $this->_transcriptionOutURI; } if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From 55b98be04fde72cdce380d1ab1403a766bee594c Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 3 Jul 2017 12:53:46 +0800 Subject: [PATCH 098/107] review all --- tropo.class.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index 917a531..0f304f9 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -619,6 +619,12 @@ public function redirect($redirect, Array $params=NULL) { if(null === $redirect->getName()) { throw new Exception("Missing required property: 'name'"); } + if (!(is_string($redirect->getTo()) && ($redirect->getTo() != ''))) { + throw new Exception("Required property: 'to' must be a string."); + } + if (!(is_string($redirect->getName()) && ($redirect->getName() != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } } elseif (is_string($redirect) && ($redirect !== '')) { @@ -2027,6 +2033,12 @@ public function __construct($to, $from=NULL, $name, $required) { if(!isset($name)) { throw new Exception("Missing required property: 'name'"); } + if (!(is_string($to) && ($to != ''))) { + throw new Exception("Required property: 'to' must be a string."); + } + if (!(is_string($name) && ($name != ''))) { + throw new Exception("Required property: 'name' must be a string."); + } $this->_to = sprintf('%s', $to); $this->_name = sprintf('%s', $name); $this->_required = $required; @@ -2040,7 +2052,7 @@ public function __toString() { $this->to = $this->_to; $this->name = $this->_name; if(isset($this->_required)) { $this->required = $this->_required; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } @@ -2821,7 +2833,7 @@ public function __construct($milliseconds, $allowSignals=NULL) { public function __toString() { $this->milliseconds = $this->_milliseconds; if(isset($this->_allowSignals)) { $this->allowSignals = $this->_allowSignals; } - return $this->unescapeJSON(json_encode($this)); + return json_encode($this); } } From 7229e8fca1b12315391430beabc0fd06b828658a Mon Sep 17 00:00:00 2001 From: pengxli Date: Tue, 4 Jul 2017 17:06:03 +0800 Subject: [PATCH 099/107] bug SUP-3199 tropo-webapi-php on --- tests/OnTest.php | 14 ++------------ tropo.class.php | 49 +++++++++++++++++++----------------------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/tests/OnTest.php b/tests/OnTest.php index 30da151..cdbed79 100644 --- a/tests/OnTest.php +++ b/tests/OnTest.php @@ -100,16 +100,6 @@ public function testFailsOnWithNoEventParameter5() { } } - public function testFailsOnWithNoSayParameter() { - $tropo = new Tropo(); - try { - $on = new On(Event::$continue, "say.php"); - $tropo->on($on); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); - } - } - public function testFailsOnWithNoSayParameter1() { $tropo = new Tropo(); try { @@ -119,7 +109,7 @@ public function testFailsOnWithNoSayParameter1() { ); $tropo->on($on); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'say'"); + $this->assertEquals($e->getMessage(), "Property: 'say' must be a Say of array or an instance of Say."); } } @@ -133,7 +123,7 @@ public function testFailsOnWithNoSayParameter2() { ); $tropo->on($on); } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Required property: 'say' must be a Say of array or an instance of Say."); + $this->assertEquals($e->getMessage(), "Property: 'say' must be a Say of array or an instance of Say."); } } } diff --git a/tropo.class.php b/tropo.class.php index 0f304f9..da616d0 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -422,9 +422,6 @@ public function on($on) { if(!(is_string($on->getEvent()) && ($on->getEvent() != ''))) { throw new Exception("Missing required property: 'event'"); } - if(null === $on->getSay()) { - throw new Exception("Missing required property: 'say'"); - } } elseif (is_array($on)) { @@ -445,52 +442,44 @@ public function on($on) { } - if (array_key_exists('say', $on)) { - - if ($on['say'] instanceof Say) { + if ($on['say'] instanceof Say) { - if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) { + if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) { - throw new Exception("The value of say must be a string."); + throw new Exception("The value of say must be a string."); - } else { + } else { - $say = $on['say']; - } + $say = $on['say']; + } - } elseif (is_array($on['say'])) { + } elseif (is_array($on['say'])) { - foreach ($on['say'] as $value) { - - if ($value instanceof Say) { + foreach ($on['say'] as $value) { + + if ($value instanceof Say) { - if(!(is_string($value->getValue()) && ($value->getValue() != ''))) { + if(!(is_string($value->getValue()) && ($value->getValue() != ''))) { - throw new Exception("The value of say must be a string."); + throw new Exception("The value of say must be a string."); - } - - } else { + } + + } else { - throw new Exception("Required property: 'say' must be a Say of array or an instance of Say."); + throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); - } } + } - $say = $on['say']; - - } else { + $say = $on['say']; - throw new Exception("Required property: 'say' must be a Say of array or an instance of Say."); - } } else { - throw new Exception("Missing required property: 'say'"); - + throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); } $next = (array_key_exists('next', $on)) ? $on["next"] : null; - //$event, $next=NULL, Say $say=NULL, $voice=Null, Ask $ask=NULL, Message $message=NULL, Wait $wait=NULL, $order=NULL, $post=NULL $on = new On($event, $next, $say); } else { From d92d046de433b40c7e8a7be1cb435f9a5668f309 Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 10 Jul 2017 08:16:46 +0800 Subject: [PATCH 100/107] modify on --- tropo.class.php | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/tropo.class.php b/tropo.class.php index da616d0..c008530 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -442,41 +442,49 @@ public function on($on) { } - if ($on['say'] instanceof Say) { + if (array_key_exists('say', $on)) { - if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) { + if ($on['say'] instanceof Say) { - throw new Exception("The value of say must be a string."); + if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) { - } else { + throw new Exception("The value of say must be a string."); - $say = $on['say']; - } + } else { - } elseif (is_array($on['say'])) { + $say = $on['say']; + } - foreach ($on['say'] as $value) { - - if ($value instanceof Say) { + } elseif (is_array($on['say'])) { - if(!(is_string($value->getValue()) && ($value->getValue() != ''))) { + foreach ($on['say'] as $value) { + + if ($value instanceof Say) { - throw new Exception("The value of say must be a string."); + if(!(is_string($value->getValue()) && ($value->getValue() != ''))) { - } - - } else { + throw new Exception("The value of say must be a string."); - throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); + } + + } else { + throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); + + } } - } - $say = $on['say']; + $say = $on['say']; + + } else { + + throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); + } } else { - throw new Exception("Property: 'say' must be a Say of array or an instance of Say."); + $say = null; + } $next = (array_key_exists('next', $on)) ? $on["next"] : null; From ba7533e296ad4a2bb53460f85399ed17cb20830f Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 14 Jul 2017 10:54:16 +0800 Subject: [PATCH 101/107] feature TROPO-11947 add sensitivity in record api --- tests/RecordTest.php | 18 ++++++++++++++++++ tropo.class.php | 9 ++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/RecordTest.php b/tests/RecordTest.php index d648ba9..e06ddfc 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -76,5 +76,23 @@ public function testCreateObject() { $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } + public function testRecordWithSensitivity() { + $tropo = new Tropo(); + $record = array( + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', + 'name' => 'foo', + 'sensitivity' => 0.5 + ); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo","sensitivity":0.5}}]}'); + } + + public function testCreateObjectWithSensitivity() { + $tropo = new Tropo(); + $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null, 0.5); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo","sensitivity":0.5}}]}'); + } + } ?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index c008530..b16158a 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -563,7 +563,7 @@ public function record($record) { } else { $transcription = $params["transcription"]; } - $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout', 'asyncUpload', 'name', 'promptLogSecurity'); + $p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout', 'asyncUpload', 'name', 'promptLogSecurity', 'sensitivity'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -588,7 +588,7 @@ public function record($record) { if (!isset($say)) { $say = null; } - $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout, $asyncUpload, $name, $promptLogSecurity); + $record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout, $asyncUpload, $name, $promptLogSecurity, $sensitivity); } else { @@ -1884,6 +1884,7 @@ class Record extends BaseClass { private $_asyncUpload; private $_name; private $_promptLogSecurity; + private $_sensitivity; public function getUrl() { return $this->_url; @@ -1915,7 +1916,7 @@ public function getName() { * @param int $minConfidence * @param int $interdigitTimeout */ - public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL, $asyncUpload=NULL, $name, $promptLogSecurity=NULL) { + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL, $asyncUpload=NULL, $name, $promptLogSecurity=NULL, $sensitivity=NULL) { if(!isset($url)) { throw new Exception("Missing required property: 'url'"); } @@ -1966,6 +1967,7 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_asyncUpload = $asyncUpload; $this->_name = $name; $this->_promptLogSecurity = $promptLogSecurity; + $this->_sensitivity = $sensitivity; } /** @@ -1994,6 +1996,7 @@ public function __toString() { if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } + if(isset($this->_sensitivity)) { $this->sensitivity = $this->_sensitivity; } return json_encode($this); } } From edd4f77400a8df168ccc7837e9ac51dff8da5a1c Mon Sep 17 00:00:00 2001 From: pengxli Date: Mon, 21 Aug 2017 12:52:29 +0800 Subject: [PATCH 102/107] New parameter transcriptionLanguage is added for record/startRecording api for PHP --- readme.markdown | 4 ++-- tests/CallTest.php | 2 -- tests/HangupTest.php | 2 +- tests/OnTest.php | 23 ----------------------- tests/RecordTest.php | 9 +++++---- tests/SayTest.php | 19 ------------------- tests/SessionTest.php | 2 +- tests/StartRecordingTest.php | 9 +++++---- tropo.class.php | 20 ++++++++++++++------ 9 files changed, 28 insertions(+), 62 deletions(-) diff --git a/readme.markdown b/readme.markdown index 0873a23..2174c4f 100644 --- a/readme.markdown +++ b/readme.markdown @@ -6,7 +6,7 @@ TropoPHP is a set of PHP classes for working with [Tropo's cloud communication s Requirements ============ - * PHP 5.3.0 or greater (5.4 not yet supported) + * PHP 5.6 or greater * PHP Notices disabled (All error reporting disabled is recommended for production use) Usage @@ -19,7 +19,7 @@ Answer the phone, say something, and hang up. $tropo = new Tropo(); // Use Tropo's text to speech to say a phrase. - $tropo->say('Yes, Tropo is this easy.'); + $tropo->say('Yes, Tropo is this easy.', array('name' => 'sayName')); // Render the JSON back to Tropo. $tropo->renderJSON(); diff --git a/tests/CallTest.php b/tests/CallTest.php index 86101c0..38ef5c6 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -30,7 +30,6 @@ public function testCallWithAllOptions() { $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); $params = array( - 'to' => $to, 'allowSignals' => $allowSignals, 'answerOnMedia' => false, 'channel' => Channel::$voice, @@ -56,7 +55,6 @@ public function testCallWithAllOptions1() { $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); $params = array( - 'to' => $to, 'allowSignals' => $allowSignals, 'answerOnMedia' => false, 'channel' => Channel::$voice, diff --git a/tests/HangupTest.php b/tests/HangupTest.php index deecca2..9f64c10 100644 --- a/tests/HangupTest.php +++ b/tests/HangupTest.php @@ -1,5 +1,5 @@ assertEquals(sprintf($tropo), '{"tropo":[{"on":[{"event":"continue","next":"say.php","say":{"value":"array continue!"}}]}]}'); } - public function testFailsOnWithNoEventParameter() { - $tropo = new Tropo(); - try { - $say = new Say("object continue!"); - @ $on = new On(); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'event'"); - } - } - public function testFailsOnWithNoEventParameter1() { $tropo = new Tropo(); try { @@ -100,19 +90,6 @@ public function testFailsOnWithNoEventParameter5() { } } - public function testFailsOnWithNoSayParameter1() { - $tropo = new Tropo(); - try { - $on = array( - 'event' => Event::$continue, - 'next' => 'say.php' - ); - $tropo->on($on); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Property: 'say' must be a Say of array or an instance of Say."); - } - } - public function testFailsOnWithNoSayParameter2() { $tropo = new Tropo(); try { diff --git a/tests/RecordTest.php b/tests/RecordTest.php index e06ddfc..519e770 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -24,7 +24,8 @@ public function testRecordWithAllOptions() { $transcription = array( 'id' => '1234', 'url' => 'mailto:you@yourmail.com', - 'emailFormat' => 'omit' + 'emailFormat' => 'omit', + 'language' => 'en-uk' ); $record = array( 'attempts' => 2, @@ -51,7 +52,7 @@ public function testRecordWithAllOptions() { 'promptLogSecurity' => 'suppress' ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } @@ -70,10 +71,10 @@ public function testCreateObject() { new Say("Please leave a message."), new Say("Sorry, I did not hear anything. Please call back.", null , "timeout") ); - $transcription = new Transcription("mailto:you@yourmail.com", "1234", "omit"); + $transcription = new Transcription("mailto:you@yourmail.com", "1234", "omit", "en-uk"); $record = new Record(2, $allowSignals, true, true, $choices, AudioFormat::$mp3, 5.0, 30.0, "POST", "111111", true, $say, 30.0, $transcription, "root", "http://192.168.26.203/tropo-webapi-php/upload_file.php", Voice::$US_English_female_allison, null, 5.0, false, "foo", "suppress"); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } public function testRecordWithSensitivity() { diff --git a/tests/SayTest.php b/tests/SayTest.php index 176ba30..76b3174 100644 --- a/tests/SayTest.php +++ b/tests/SayTest.php @@ -29,15 +29,6 @@ public function testCreateSayObject1() $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"name":"say","required":true,"promptLogSecurity":"suppress"}]}]}'); } - public function testFailsSayWithNoValueParameter() - { - try{ - @ $say = new Say(); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'value'"); - } - } - public function testFailsSayWithNoValueParameter1() { try{ @@ -89,16 +80,6 @@ public function testFailsSayWithNoNameParameter2() } } - public function testFailsSayWithNoValueParameter3() - { - $tropo = new Tropo(); - try{ - @ $tropo->say(); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Argument 1 passed to Tropo::say() must be a string or an instance of Say."); - } - } - public function testFailsSayWithNoValueParameter4() { $tropo = new Tropo(); diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 80c437f..b0c8d82 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -1,6 +1,6 @@ '111111', 'transcriptionOutURI' => 'mailto:you@yourmail.com', 'transcriptionEmailFormat' => 'plain', - 'transcriptionID' => '1234' + 'transcriptionID' => '1234', + 'transcriptionLanguage' => 'pt-br' ); $tropo->startRecording($startRecording); $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/au","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/au","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } public function testCreateMinObject() { @@ -48,12 +49,12 @@ public function testCreateMinObject() { public function testCreateObject() { $tropo = new Tropo(); - $startRecording = new StartRecording(AudioFormat::$mp3, "POST", "111111", "http://192.168.26.203/tropo-webapi-php/upload_file.php", "root", "1234", "plain", "mailto:you@yourmail.com", false); + $startRecording = new StartRecording(AudioFormat::$mp3, "POST", "111111", "http://192.168.26.203/tropo-webapi-php/upload_file.php", "root", "1234", "plain", "mailto:you@yourmail.com", false, "pt-br"); $tropo->startRecording($startRecording); $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/mp3","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/mp3","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } } diff --git a/tropo.class.php b/tropo.class.php index b16158a..3591ec9 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -552,14 +552,14 @@ public function record($record) { $params['voice'] = $this->_voice; } if (is_array($params['transcription'])) { - $p = array('url', 'id', 'emailFormat'); + $p = array('url', 'id', 'emailFormat', 'language'); foreach ($p as $option) { $$option = null; if (!is_array($params["transcription"]) || !array_key_exists($option, $params["transcription"])) { $params["transcription"][$option] = null; } } - $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"]); + $transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"],$params["transcription"]["language"]); } else { $transcription = $params["transcription"]; } @@ -768,14 +768,14 @@ public function startRecording($startRecording) { } $params = $startRecording; - $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload'); + $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload', 'transcriptionLanguage'); foreach ($p as $option) { $$option = null; if (array_key_exists($option, $params)) { $$option = $params[$option]; } } - $startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI, $asyncUpload); + $startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI, $asyncUpload, $transcriptionLanguage); } else { @@ -2560,6 +2560,7 @@ class StartRecording extends BaseClass { private $_transcriptionID; private $_transcriptionEmailFormat; private $_transcriptionOutURI; + private $_transcriptionLanguage; private $_asyncUpload; public function getUrl() { @@ -2578,8 +2579,9 @@ public function getUrl() { * @param string $transcriptionID * @param string $transcriptionEmailFormat * @param string $transcriptionOutURI + * @param string $transcriptionLanguage */ - public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $username=NULL, $transcriptionID=NULL, $transcriptionEmailFormat=NULL, $transcriptionOutURI=NULL, $asyncUpload=NULL) { + public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $username=NULL, $transcriptionID=NULL, $transcriptionEmailFormat=NULL, $transcriptionOutURI=NULL, $asyncUpload=NULL, $transcriptionLanguage=NULL) { if(!isset($url)) { throw new Exception("Missing required property: 'url'"); } @@ -2594,6 +2596,7 @@ public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $u $this->_transcriptionID = $transcriptionID; $this->_transcriptionEmailFormat = $transcriptionEmailFormat; $this->_transcriptionOutURI = $transcriptionOutURI; + $this->_transcriptionLanguage = $transcriptionLanguage; $this->_asyncUpload = $asyncUpload; } @@ -2610,6 +2613,7 @@ public function __toString() { if(isset($this->_transcriptionID)) { $this->transcriptionID = $this->_transcriptionID; } if(isset($this->_transcriptionEmailFormat)) { $this->transcriptionEmailFormat = $this->_transcriptionEmailFormat; } if(isset($this->_transcriptionOutURI)) { $this->transcriptionOutURI = $this->_transcriptionOutURI; } + if(isset($this->_transcriptionLanguage)) { $this->transcriptionLanguage = $this->_transcriptionLanguage; } if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } return json_encode($this); } @@ -2632,6 +2636,7 @@ class Transcription extends BaseClass { private $_url; private $_id; private $_emailFormat; + private $_language; /** * Class constructor @@ -2639,11 +2644,13 @@ class Transcription extends BaseClass { * @param string $url * @param string $id * @param string $emailFormat + * @param string $language */ - public function __construct($url, $id=NULL, $emailFormat=NULL) { + public function __construct($url, $id=NULL, $emailFormat=NULL, $language=NULL) { $this->_url = $url; $this->_id = $id; $this->_emailFormat = $emailFormat; + $this->_language = $language; } /** @@ -2654,6 +2661,7 @@ public function __toString() { if(isset($this->_id)) { $this->id = $this->_id; } if(isset($this->_url)) { $this->url = $this->_url; } if(isset($this->_emailFormat)) { $this->emailFormat = $this->_emailFormat; } + if(isset($this->_language)) { $this->language = $this->_language; } return json_encode($this); } } From d56e70b64e04ba2ea473a25a45b0e4e33c5d6632 Mon Sep 17 00:00:00 2001 From: pengxli Date: Wed, 30 Aug 2017 11:32:28 +0800 Subject: [PATCH 103/107] Add answer to TROPO JSON helper library with support for RPID parameters - WebAPI PHP --- tests/AnswerTest.php | 60 ++++++++++++++++++++++++++++++++++++++++++++ tropo.class.php | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/AnswerTest.php diff --git a/tests/AnswerTest.php b/tests/AnswerTest.php new file mode 100644 index 0000000..62e750c --- /dev/null +++ b/tests/AnswerTest.php @@ -0,0 +1,60 @@ +answer(); + $params = array("name"=>"say"); + $tropo->say("Hello, you were the first to answer.",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}'); + } + + public function testCallWithMinOptions1() { + $tropo = new Tropo(); + $tropo->answer(null); + $params = array("name"=>"say"); + $tropo->say("Hello, you were the first to answer.",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}'); + } + + public function testCallWithExtraToOptiions() { + $tropo = new Tropo(); + $params = array( + 'headers' => array( + 'P-Header' => 'value goes here', + 'Remote-Party-ID' => '"John Doe";party=calling;id-type=subscriber;privacy=full;screen=yes' + )); + $tropo->answer($params); + $params = array("name"=>"say"); + $tropo->say("Hello, you were the first to answer.",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{"headers":{"P-Header":"value goes here","Remote-Party-ID":"\"John Doe\";party=calling;id-type=subscriber;privacy=full;screen=yes"}}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}'); + } + + public function testCreateMinObject() { + $tropo = new Tropo(); + $answer = new Answer(); + $tropo->answer($answer); + $params = array("name"=>"say"); + $tropo->say("Hello, you were the first to answer.",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}'); + } + + public function testCreateMinObject1() { + $tropo = new Tropo(); + $headers = array( + 'P-Header' => 'value goes here', + 'Remote-Party-ID' => '"John Doe";party=calling;id-type=subscriber;privacy=full;screen=yes' + ); + $answer = new Answer($headers); + $tropo->answer($answer); + $params = array("name"=>"say"); + $tropo->say("Hello, you were the first to answer.",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{"headers":{"P-Header":"value goes here","Remote-Party-ID":"\"John Doe\";party=calling;id-type=subscriber;privacy=full;screen=yes"}}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}'); + } + +} +?> \ No newline at end of file diff --git a/tropo.class.php b/tropo.class.php index 3591ec9..0797019 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -960,6 +960,31 @@ public function generalLogSecurity($state) { } } + public function answer($answer=NULL) { + if (!isset($answer)) { + $answer = "{}"; + } elseif ($answer instanceof Answer) { + } elseif (is_array($answer)) { + + $params = $answer; + $p = array('headers'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } + } + $answer = new Answer($headers); + + } else { + + throw new Exception("Argument 1 passed to Tropo::answer() must be a array or an instance of Answer."); + + } + $this->answer = sprintf('%s', $answer); + + } + /** * Launches a new session with the Tropo Session API. * (Pass through to SessionAPI class.) @@ -2845,6 +2870,29 @@ public function __toString() { } } +class Answer extends BaseClass { + + private $_headers; + + /** + * Class constructor + * + * @param array $headers + */ + public function __construct(Array $headers=NULL) { + $this->_headers = $headers; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(count($this->_headers)) { $this->headers = $this->_headers; } + return json_encode($this); + } +} + /** * Defnies an endoint for transfer and redirects. * @package TropoPHP_Support From 58453e915c3131bf5375d9bd26a14b86d5ea8cfa Mon Sep 17 00:00:00 2001 From: pengxli Date: Thu, 21 Dec 2017 18:14:44 +0800 Subject: [PATCH 104/107] TROPO-12567 tropo-webapi-php record url --- tests/RecordTest.php | 37 +++++++++++++++++---- tropo.class.php | 76 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 94 insertions(+), 19 deletions(-) diff --git a/tests/RecordTest.php b/tests/RecordTest.php index 519e770..bc041ea 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -12,7 +12,32 @@ public function testRecordWithMinOptions() { 'name' => 'foo' ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo"}}]}'); + } + + public function testRecordWithMinOptions1() { + $tropo = new Tropo(); + $url = new Url('http://192.168.26.203/tropo-webapi-php/upload_file.php', 'root', '111111', 'POST'); + $record = array( + 'url' => $url, + 'name' => 'foo' + ); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"name":"foo"}}]}'); + } + + public function testRecordWithMinOptions2() { + $tropo = new Tropo(); + $url = array( + new Url('http://192.168.26.203/tropo-webapi-php/upload_file.php', 'root', '111111', 'POST'), + new Url('http://192.168.26.204/tropo-webapi-php/upload_file.php') + ); + $record = array( + 'url' => $url, + 'name' => 'foo' + ); + $tropo->record($record); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":[{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},{"url":"http://192.168.26.204/tropo-webapi-php/upload_file.php"}],"name":"foo"}}]}'); } public function testRecordWithAllOptions() { @@ -52,7 +77,7 @@ public function testRecordWithAllOptions() { 'promptLogSecurity' => 'suppress' ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } @@ -60,7 +85,7 @@ public function testCreateMinObject() { $tropo = new Tropo(); $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo"}}]}'); } public function testCreateObject() { @@ -74,7 +99,7 @@ public function testCreateObject() { $transcription = new Transcription("mailto:you@yourmail.com", "1234", "omit", "en-uk"); $record = new Record(2, $allowSignals, true, true, $choices, AudioFormat::$mp3, 5.0, 30.0, "POST", "111111", true, $say, 30.0, $transcription, "root", "http://192.168.26.203/tropo-webapi-php/upload_file.php", Voice::$US_English_female_allison, null, 5.0, false, "foo", "suppress"); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"method":"POST","password":"111111","required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"username":"root","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); } public function testRecordWithSensitivity() { @@ -85,14 +110,14 @@ public function testRecordWithSensitivity() { 'sensitivity' => 0.5 ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo","sensitivity":0.5}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo","sensitivity":0.5}}]}'); } public function testCreateObjectWithSensitivity() { $tropo = new Tropo(); $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null, 0.5); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","name":"foo","sensitivity":0.5}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo","sensitivity":0.5}}]}'); } } diff --git a/tropo.class.php b/tropo.class.php index 0797019..da55683 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -516,9 +516,9 @@ public function record($record) { if(null === $record->getName()) { throw new Exception("Missing required property: 'name'"); } - if (!(is_string($record->getUrl()) && ($record->getUrl() != ''))) { - throw new Exception("Required property: 'url' must be a string."); - } + // if (!(is_string($record->getUrl()) && ($record->getUrl() != ''))) { + // throw new Exception("Required property: 'url' must be a string."); + // } if (!(is_string($record->getName()) && ($record->getName() != ''))) { throw new Exception("Required property: 'name' must be a string."); } @@ -532,9 +532,9 @@ public function record($record) { if (!array_key_exists('name', $params)) { throw new Exception("Missing required property: 'name'"); } - if (!(is_string($params['url']) && ($params['url'] != ''))) { - throw new Exception("Required property: 'url' must be a string."); - } + // if (!(is_string($params['url']) && ($params['url'] != ''))) { + // throw new Exception("Required property: 'url' must be a string."); + // } if (!(is_string($params['name']) && ($params['name'] != ''))) { throw new Exception("Required property: 'name' must be a string."); } @@ -1948,8 +1948,23 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ if(!isset($name)) { throw new Exception("Missing required property: 'name'"); } - if (!(is_string($url) && ($url != ''))) { - throw new Exception("Required property: 'url' must be a string."); + if (is_string($url) && ($url != '')) { + $this->_url = sprintf('%s', new Url($url, $username, $password, $method)); + } else if ($url instanceof Url) { + $this->_url = sprintf('%s', $url); + } else if (is_array($url) && count($url) > 0) { + foreach ($url as $key => $value) { + if (!($value instanceof Url)) { + throw new Exception("Property: 'url' must be a valid string, an instance of Url or an array of Urls."); + } + $this->_url[$key] = sprintf('%s', $value); + } + foreach ($this->_url as $key => $value) { + $this->_url[$key] = json_decode($value); + } + $this->_url = json_encode($this->_url); + } else { + throw new Exception("Property: 'url' must be a valid string, an instance of Url or an array of Urls."); } if (!(is_string($name) && ($name != ''))) { throw new Exception("Required property: 'name' must be a string."); @@ -1962,8 +1977,8 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ $this->_format = $format; $this->_maxSilence = $maxSilence; $this->_maxTime = $maxTime; - $this->_method = $method; - $this->_password = $password; + //$this->_method = $method; + //$this->_password = $password; $this->_required = $required; if (isset($say)) { if ($say instanceof Say) { @@ -1985,8 +2000,8 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ } $this->_timeout = $timeout; $this->_transcription = isset($transcription) ? sprintf('%s', $transcription) : null; - $this->_username = $username; - $this->_url = $url; + //$this->_username = $username; + $this->_voice = $voice; $this->_interdigitTimeout = $interdigitTimeout; $this->_asyncUpload = $asyncUpload; @@ -2015,7 +2030,7 @@ public function __toString() { if(isset($this->_timeout)) { $this->timeout = $this->_timeout; } if(isset($this->_transcription)) { $this->transcription = json_decode($this->_transcription); } if(isset($this->_username)) { $this->username = $this->_username; } - if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_url)) { $this->url = json_decode($this->_url); } if(isset($this->_voice)) { $this->voice = $this->_voice; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } if(isset($this->_asyncUpload)) { $this->asyncUpload = $this->_asyncUpload; } @@ -2893,6 +2908,41 @@ public function __toString() { } } +class Url extends BaseClass { + + private $_url; + private $_username; + private $_password; + private $_method; + + /** + * Class constructor + * + * @param string $url + * @param string $username + * @param string $password + * @param string $method + */ + public function __construct($url, $username=NULL, $password=NULL, $method=NULL) { + $this->_url = $url; + $this->_username = $username; + $this->_password = $password; + $this->_method = $method; + } + + /** + * Renders object in JSON format. + * + */ + public function __toString() { + if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_username)) { $this->username = $this->_username; } + if(isset($this->_password)) { $this->password = $this->_password; } + if(isset($this->_method)) { $this->method = $this->_method; } + return json_encode($this); + } +} + /** * Defnies an endoint for transfer and redirects. * @package TropoPHP_Support From 63d3725d41cc77af8c9c91c01a343e1819f89b23 Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 22 Dec 2017 18:34:15 +0800 Subject: [PATCH 105/107] bug TROPO-12571 tropo-webapi-php startRecording url --- tests/StartRecordingTest.php | 37 ++++++++++++++++++++++++++++---- tropo.class.php | 41 ++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/tests/StartRecordingTest.php b/tests/StartRecordingTest.php index 0e7a462..0b0072c 100644 --- a/tests/StartRecordingTest.php +++ b/tests/StartRecordingTest.php @@ -13,7 +13,36 @@ public function testStartRecordingWithMinOptions() { $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + } + + public function testStartRecordingWithMinOptions1() { + $tropo = new Tropo(); + $url = new Url('http://192.168.26.203/tropo-webapi-php/upload_file.php'); + $startRecording = array( + 'url' => $url + ); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + } + + public function testStartRecordingWithMinOptions2() { + $tropo = new Tropo(); + $url = array( + new Url('http://192.168.26.203/tropo-webapi-php/upload_file.php', 'root', '111111', 'POST'), + new Url('http://192.168.26.204/tropo-webapi-php/upload_file.php') + ); + $startRecording = array( + 'url' => $url + ); + $tropo->startRecording($startRecording); + $say = new Say("I am now recording!", null, null, null, null, "say"); + $tropo->say($say); + $tropo->stopRecording(); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":[{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},{"url":"http://192.168.26.204/tropo-webapi-php/upload_file.php"}]}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } public function testRecordWithAllOptions() { @@ -34,7 +63,7 @@ public function testRecordWithAllOptions() { $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/au","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/au","url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } public function testCreateMinObject() { @@ -44,7 +73,7 @@ public function testCreateMinObject() { $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } public function testCreateObject() { @@ -54,7 +83,7 @@ public function testCreateObject() { $say = new Say("I am now recording!", null, null, null, null, "say"); $tropo->say($say); $tropo->stopRecording(); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/mp3","method":"POST","password":"111111","url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"startRecording":{"format":"audio/mp3","url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"transcriptionID":"1234","transcriptionEmailFormat":"plain","transcriptionOutURI":"mailto:you@yourmail.com","transcriptionLanguage":"pt-br","asyncUpload":false}},{"say":[{"value":"I am now recording!","name":"say"}]},{"stopRecording":"null"}]}'); } } diff --git a/tropo.class.php b/tropo.class.php index da55683..f85bd30 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -754,18 +754,18 @@ public function startRecording($startRecording) { if(null === $startRecording->getUrl()) { throw new Exception("Missing required property: 'url'"); } - if (!(is_string($startRecording->getUrl()) && ($startRecording->getUrl() != ''))) { - throw new Exception("Required property: 'url' must be a string."); - } + // if (!(is_string($startRecording->getUrl()) && ($startRecording->getUrl() != ''))) { + // throw new Exception("Required property: 'url' must be a string."); + // } } elseif (is_array($startRecording)) { if (!array_key_exists('url', $startRecording)) { throw new Exception("Missing required property: 'url'"); } - if (!(is_string($startRecording['url']) && ($startRecording['url'] != ''))) { - throw new Exception("Required property: 'url' must be a string."); - } + // if (!(is_string($startRecording['url']) && ($startRecording['url'] != ''))) { + // throw new Exception("Required property: 'url' must be a string."); + // } $params = $startRecording; $p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload', 'transcriptionLanguage'); @@ -2625,14 +2625,29 @@ public function __construct($format=NULL, $method=NULL, $password=NULL, $url, $u if(!isset($url)) { throw new Exception("Missing required property: 'url'"); } - if (!(is_string($url) && ($url != ''))) { - throw new Exception("Required property: 'url' must be a string."); + if (is_string($url) && ($url != '')) { + $this->_url = sprintf('%s', new Url($url, $username, $password, $method)); + } else if ($url instanceof Url) { + $this->_url = sprintf('%s', $url); + } else if (is_array($url) && count($url) > 0) { + foreach ($url as $key => $value) { + if (!($value instanceof Url)) { + throw new Exception("Property: 'url' must be a valid string, an instance of Url or an array of Urls."); + } + $this->_url[$key] = sprintf('%s', $value); + } + foreach ($this->_url as $key => $value) { + $this->_url[$key] = json_decode($value); + } + $this->_url = json_encode($this->_url); + } else { + throw new Exception("Property: 'url' must be a valid string, an instance of Url or an array of Urls."); } $this->_format = $format; - $this->_method = $method; - $this->_password = $password; - $this->_url = $url; - $this->_username = $username; + //$this->_method = $method; + //$this->_password = $password; + //$this->_url = $url; + //$this->_username = $username; $this->_transcriptionID = $transcriptionID; $this->_transcriptionEmailFormat = $transcriptionEmailFormat; $this->_transcriptionOutURI = $transcriptionOutURI; @@ -2648,7 +2663,7 @@ public function __toString() { if(isset($this->_format)) { $this->format = $this->_format; } if(isset($this->_method)) { $this->method = $this->_method; } if(isset($this->_password)) { $this->password = $this->_password; } - if(isset($this->_url)) { $this->url = $this->_url; } + if(isset($this->_url)) { $this->url = json_decode($this->_url); } if(isset($this->_username)) { $this->username = $this->_username; } if(isset($this->_transcriptionID)) { $this->transcriptionID = $this->_transcriptionID; } if(isset($this->_transcriptionEmailFormat)) { $this->transcriptionEmailFormat = $this->_transcriptionEmailFormat; } From af88e523c67d39931572010d9f1d1e7da3cf2c85 Mon Sep 17 00:00:00 2001 From: pengxli Date: Wed, 24 Jan 2018 11:07:19 +0800 Subject: [PATCH 106/107] bug TROPO-12574 name shouldn't be a necessary parameter in WebAPI SDK --- tests/CallTest.php | 36 ++--- tests/ConferenceTest.php | 25 ++- tests/MessageTest.php | 25 ++- tests/RecordTest.php | 33 ++-- tests/RedirectTest.php | 7 +- tests/SayTest.php | 84 ++-------- tests/TransferTest.php | 55 +++---- tropo.class.php | 326 ++++++++++++--------------------------- 8 files changed, 185 insertions(+), 406 deletions(-) diff --git a/tests/CallTest.php b/tests/CallTest.php index 38ef5c6..5ded659 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -7,21 +7,15 @@ class CallTest extends PHPUnit_Framework_TestCase public function testCallWithMinOptions() { $tropo = new Tropo(); - $params = array( - 'name' => 'foo' - ); - $tropo->call("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $tropo->call("sip:pengxli@192.168.26.1:5678"); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testCallWithExtraToOptiions() { $tropo = new Tropo(); $call = array('sip:pengxli@192.168.26.1:5678', 'sip:pengxli@192.168.26.206:5678'); - $params = array( - 'name' => 'foo' - ); - $tropo->call($call, $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"name":"foo"}}]}'); + $tropo->call($call); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"]}}]}'); } public function testCallWithAllOptions() { @@ -36,7 +30,6 @@ public function testCallWithAllOptions() { 'from' => '3055551000', 'headers' => $headers, 'machineDetection' => false, - 'name' => 'foo', 'network' => Network::$sip, 'required' => true, 'timeout' => 30.0, @@ -46,7 +39,7 @@ public function testCallWithAllOptions() { 'label' => 'callLabel' ); $tropo->call($call, $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } public function testCallWithAllOptions1() { @@ -61,7 +54,6 @@ public function testCallWithAllOptions1() { 'from' => '3055551000', 'headers' => $headers, 'machineDetection' => 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.', - 'name' => 'foo', 'network' => Network::$sip, 'required' => true, 'timeout' => 30.0, @@ -71,22 +63,22 @@ public function testCallWithAllOptions1() { 'label' => 'callLabel' ); $tropo->call($call, $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@192.168.26.206:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } public function testCreateMinObject() { $tropo = new Tropo(); - $call = new Call("sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, null, null, null, "foo"); + $call = new Call("sip:pengxli@192.168.26.1:5678"); $tropo->call($call); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testCreateObject1() { $tropo = new Tropo(); $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); - $call = new Call($to, null, null, null, null, null, null, null, null, null, null, "foo"); + $call = new Call($to); $tropo->call($call); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"]}}]}'); } public function testCreateObject2() { @@ -94,9 +86,9 @@ public function testCreateObject2() { $to = array("sip:pengxli@192.168.26.1:5678", "sip:pengxli@172.16.72.131:5678"); $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); - $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, false, Voice::$US_English_female_allison, "foo", true, "http://192.168.26.203/result.php", "suppress", "callLabel"); + $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, false, Voice::$US_English_female_allison, null, true, "http://192.168.26.203/result.php", "suppress", "callLabel"); $tropo->call($call); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":false,"voice":"allison","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } public function testCreateObject3() { @@ -105,9 +97,9 @@ public function testCreateObject3() { $allowSignals = array('exit', 'quit'); $headers = array('foo' => 'bar', 'bling' => 'baz'); $machineDetection = 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.'; - $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, $machineDetection, Voice::$US_English_female_allison, "foo", true, "http://192.168.26.203/result.php", "suppress", "callLabel"); + $call = new Call($to, "3055551000", Network::$sip, Channel::$voice, false, 30.0, $headers, null, $allowSignals, $machineDetection, Voice::$US_English_female_allison, null, true, "http://192.168.26.203/result.php", "suppress", "callLabel"); $tropo->call($call); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"call":{"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"from":"3055551000","network":"SIP","channel":"VOICE","timeout":30,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"allowSignals":["exit","quit"],"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","required":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"callLabel"}}]}'); } } ?> \ No newline at end of file diff --git a/tests/ConferenceTest.php b/tests/ConferenceTest.php index fa055b9..0ed5cf5 100644 --- a/tests/ConferenceTest.php +++ b/tests/ConferenceTest.php @@ -7,11 +7,8 @@ class ConferenceTest extends PHPUnit_Framework_TestCase public function testConferenceWithMinOptions() { $tropo = new Tropo(); - $params = array( - 'name' => 'foo' - ); - $tropo->conference("1234", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234"}}]}'); + $tropo->conference("1234"); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234"}}]}'); } public function testConferenceWithAllOptions() { @@ -23,14 +20,13 @@ public function testConferenceWithAllOptions() { 'joinPrompt' => true, 'leavePrompt' => true, 'mute' => false, - 'name' => 'foo', 'playTones' => true, 'required' => true, 'terminator' => '*', 'promptLogSecurity' => 'suppress', ); $tropo->conference("1234", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); } public function testConferenceWithAllOptions1() { @@ -50,29 +46,28 @@ public function testConferenceWithAllOptions1() { 'joinPrompt' => $joinPrompt, 'leavePrompt' => $leavePrompt, 'mute' => false, - 'name' => 'foo', 'playTones' => true, 'required' => true, 'terminator' => '*', 'promptLogSecurity' => 'suppress', ); $tropo->conference("1234", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); } public function testCreateMinObject() { $tropo = new Tropo(); - $conference = new Conference("foo", "1234"); + $conference = new Conference(null, "1234"); $tropo->conference($conference); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234"}}]}'); } public function testCreateObject1() { $tropo = new Tropo(); $allowSignals = array('exit', 'quit'); - $conference = new Conference("foo", "1234", false, null, true, true, "*", $allowSignals, 5.0, true, true, null, "suppress"); + $conference = new Conference(null, "1234", false, null, true, true, "*", $allowSignals, 5.0, true, true, null, "suppress"); $tropo->conference($conference); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":true,"leavePrompt":true}}]}'); } public function testCreateObject2() { @@ -86,9 +81,9 @@ public function testCreateObject2() { 'value' => 'I am leaving.', 'voice' => Voice::$US_English_female_allison ); - $conference = new Conference("foo", "1234", false, null, true, true, "*", $allowSignals, 5.0, $joinPrompt, $leavePrompt, null, "suppress"); + $conference = new Conference(null, "1234", false, null, true, true, "*", $allowSignals, 5.0, $joinPrompt, $leavePrompt, null, "suppress"); $tropo->conference($conference); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"conference":{"id":"1234","mute":false,"playTones":true,"required":true,"terminator":"*","allowSignals":["exit","quit"],"interdigitTimeout":5,"promptLogSecurity":"suppress","joinPrompt":{"value":"I am coming.","voice":"allison"},"leavePrompt":{"value":"I am leaving.","voice":"allison"}}}]}'); } } ?> \ No newline at end of file diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 58b1218..7912160 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -8,11 +8,10 @@ class MessageTest extends PHPUnit_Framework_TestCase public function testMessageWithMinOptions() { $tropo = new Tropo(); $params = array( - 'to' => 'sip:pengxli@192.168.26.1:5678', - 'name' => 'foo' + 'to' => 'sip:pengxli@192.168.26.1:5678' ); $tropo->message("This is an announcement.", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testMessageWithExtraSayOptiions() { @@ -20,11 +19,10 @@ public function testMessageWithExtraSayOptiions() { $say = "Remember, you have a meeting at 2 PM."; $params = array( 'say' => $say, - 'to' => 'sip:pengxli@192.168.26.1:5678', - 'name' => 'foo' + 'to' => 'sip:pengxli@192.168.26.1:5678' ); $tropo->message("This is an announcement.", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testMessageWithAllOptions() { @@ -35,7 +33,6 @@ public function testMessageWithAllOptions() { $params = array( 'say' => $say, 'to' => $to, - 'name' => 'foo', 'answerOnMedia' => false, 'channel' => Channel::$voice, 'from' => '3055551000', @@ -47,14 +44,14 @@ public function testMessageWithAllOptions() { 'headers' => $headers ); $tropo->message("This is an announcement.", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo","channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); } public function testCreateMinObject() { $tropo = new Tropo(); - $message = new Message(new Say("This is an announcement."), "sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, "foo"); + $message = new Message(new Say("This is an announcement."), "sip:pengxli@192.168.26.1:5678"); $tropo->message($message); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testCreateObject1() { @@ -63,9 +60,9 @@ public function testCreateObject1() { new Say("This is an announcement."), new Say("Remember, you have a meeting at 2 PM.") ); - $message = new Message($say, "sip:pengxli@192.168.26.1:5678", null, null, null, null, null, null, null, "foo"); + $message = new Message($say, "sip:pengxli@192.168.26.1:5678"); $tropo->message($message); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."}],"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } public function testCreateObject2() { @@ -80,9 +77,9 @@ public function testCreateObject2() { 'sip:pengxli@172.16.72.131:5678' ); $headers = array('foo' => 'bar', 'bling' => 'baz'); - $message = new Message($say, $to, Channel::$voice, Network::$sip, "3055551000", Voice::$US_English_female_allison, 60, false, $headers, "foo", true, "suppress"); + $message = new Message($say, $to, Channel::$voice, Network::$sip, "3055551000", Voice::$US_English_female_allison, 60, false, $headers, null, true, "suppress"); $tropo->message($message); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"name":"foo","channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":[{"value":"This is an announcement."},{"value":"Remember, you have a meeting at 2 PM."},{"value":"This is tropo.com."}],"to":["sip:pengxli@192.168.26.1:5678","sip:pengxli@172.16.72.131:5678"],"channel":"VOICE","network":"SIP","from":"3055551000","voice":"allison","timeout":60,"answerOnMedia":false,"headers":{"foo":"bar","bling":"baz"},"required":true,"promptLogSecurity":"suppress"}}]}'); } } ?> \ No newline at end of file diff --git a/tests/RecordTest.php b/tests/RecordTest.php index bc041ea..56505cc 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -8,22 +8,20 @@ class RecordTest extends PHPUnit_Framework_TestCase public function testRecordWithMinOptions() { $tropo = new Tropo(); $record = array( - 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', - 'name' => 'foo' + 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php' ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}}}]}'); } public function testRecordWithMinOptions1() { $tropo = new Tropo(); $url = new Url('http://192.168.26.203/tropo-webapi-php/upload_file.php', 'root', '111111', 'POST'); $record = array( - 'url' => $url, - 'name' => 'foo' + 'url' => $url ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"}}}]}'); } public function testRecordWithMinOptions2() { @@ -33,11 +31,10 @@ public function testRecordWithMinOptions2() { new Url('http://192.168.26.204/tropo-webapi-php/upload_file.php') ); $record = array( - 'url' => $url, - 'name' => 'foo' + 'url' => $url ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":[{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},{"url":"http://192.168.26.204/tropo-webapi-php/upload_file.php"}],"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":[{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},{"url":"http://192.168.26.204/tropo-webapi-php/upload_file.php"}]}}]}'); } public function testRecordWithAllOptions() { @@ -65,7 +62,6 @@ public function testRecordWithAllOptions() { 'maxSilence' => 5.0, 'maxTime' => 30.0, 'method' => 'POST', - 'name' => 'foo', 'required' => true, 'transcription' => $transcription, 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', @@ -77,15 +73,15 @@ public function testRecordWithAllOptions() { 'promptLogSecurity' => 'suppress' ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"promptLogSecurity":"suppress"}}]}'); } public function testCreateMinObject() { $tropo = new Tropo(); - $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null); + $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php"); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"}}}]}'); } public function testCreateObject() { @@ -97,27 +93,26 @@ public function testCreateObject() { new Say("Sorry, I did not hear anything. Please call back.", null , "timeout") ); $transcription = new Transcription("mailto:you@yourmail.com", "1234", "omit", "en-uk"); - $record = new Record(2, $allowSignals, true, true, $choices, AudioFormat::$mp3, 5.0, 30.0, "POST", "111111", true, $say, 30.0, $transcription, "root", "http://192.168.26.203/tropo-webapi-php/upload_file.php", Voice::$US_English_female_allison, null, 5.0, false, "foo", "suppress"); + $record = new Record(2, $allowSignals, true, true, $choices, AudioFormat::$mp3, 5.0, 30.0, "POST", "111111", true, $say, 30.0, $transcription, "root", "http://192.168.26.203/tropo-webapi-php/upload_file.php", Voice::$US_English_female_allison, null, 5.0, false, null, "suppress"); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"name":"foo","promptLogSecurity":"suppress"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"attempts":2,"allowSignals":["exit","quit"],"bargein":true,"beep":true,"choices":{"terminator":"*"},"format":"audio/mp3","maxSilence":5,"maxTime":30,"required":true,"say":[{"value":"Please leave a message."},{"event":"timeout","value":"Sorry, I did not hear anything. Please call back."}],"timeout":30,"transcription":{"id":"1234","url":"mailto:you@yourmail.com","emailFormat":"omit","language":"en-uk"},"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php","username":"root","password":"111111","method":"POST"},"voice":"allison","interdigitTimeout":5,"asyncUpload":false,"promptLogSecurity":"suppress"}}]}'); } public function testRecordWithSensitivity() { $tropo = new Tropo(); $record = array( 'url' => 'http://192.168.26.203/tropo-webapi-php/upload_file.php', - 'name' => 'foo', 'sensitivity' => 0.5 ); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo","sensitivity":0.5}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"sensitivity":0.5}}]}'); } public function testCreateObjectWithSensitivity() { $tropo = new Tropo(); - $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, "foo", null, 0.5); + $record = new Record(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "http://192.168.26.203/tropo-webapi-php/upload_file.php", null, null, null, null, null, null, 0.5); $tropo->record($record); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"name":"foo","sensitivity":0.5}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"record":{"url":{"url":"http://192.168.26.203/tropo-webapi-php/upload_file.php"},"sensitivity":0.5}}]}'); } } diff --git a/tests/RedirectTest.php b/tests/RedirectTest.php index 3de9b9e..2f1355d 100644 --- a/tests/RedirectTest.php +++ b/tests/RedirectTest.php @@ -8,20 +8,19 @@ public function testRedirect() { $tropo = new Tropo(); $params = array( - 'name' => 'foo', 'required' => true ); $tropo->redirect("sip:pengxli@192.168.26.1:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo","required":true}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","required":true}}]}'); } public function testRedirect1() { $tropo = new Tropo(); - $redirect = new Redirect("sip:pengxli@192.168.26.1:5678", null, "foo", true); + $redirect = new Redirect("sip:pengxli@192.168.26.1:5678", null, null, true); $tropo->redirect($redirect); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","name":"foo","required":true}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"redirect":{"to":"sip:pengxli@192.168.26.1:5678","required":true}}]}'); } } diff --git a/tests/SayTest.php b/tests/SayTest.php index 76b3174..7b8bc83 100644 --- a/tests/SayTest.php +++ b/tests/SayTest.php @@ -4,13 +4,25 @@ class SayTest extends TestCase { + public function testSayWithMinOptions() { + $tropo = new Tropo(); + $tropo->say("Please enter your account number..."); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number..."}]}]}'); + } + + public function testSayWithOptions() { + $tropo = new Tropo(); + $tropo->say("Please enter your account number...",array('name' => 'say')); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","name":"say"}]}]}'); + } + public function testCreateSayObject() { $tropo = new Tropo(); $allowSignals = array('exit','quit'); - $say = new Say("Please enter your account number...", SayAs::$date, null, Voice::$US_English_female_allison, $allowSignals, "say", true, "suppress"); + $say = new Say("Please enter your account number...", SayAs::$date, null, Voice::$US_English_female_allison, $allowSignals, null, true, "suppress"); $tropo->say($say); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"name":"say","required":true,"promptLogSecurity":"suppress"}]}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress"}]}]}'); } public function testCreateSayObject1() @@ -18,7 +30,6 @@ public function testCreateSayObject1() $tropo = new Tropo(); $allowSignals = array('exit','quit'); $params = array( - "name"=>"say", "as"=>SayAs::$date, "event"=>"event", "voice"=>Voice::$US_English_female_allison, @@ -26,7 +37,7 @@ public function testCreateSayObject1() "promptLogSecurity"=>"suppress", "required"=>true); $tropo->say("Please enter your account number...",$params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"name":"say","required":true,"promptLogSecurity":"suppress"}]}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress"}]}]}'); } public function testFailsSayWithNoValueParameter1() @@ -47,39 +58,6 @@ public function testFailsSayWithNoValueParameter2() } } - public function testFailsSayWithNoNameParameter() - { - $tropo = new Tropo(); - try{ - $say = new Say("Please enter your account number..."); - $tropo->say($say); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); - } - } - - public function testFailsSayWithNoNameParameter1() - { - $tropo = new Tropo(); - try{ - $say = new Say("Please enter your account number...", null, null, null, null, null, null, null); - $tropo->say($say); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); - } - } - - public function testFailsSayWithNoNameParameter2() - { - $tropo = new Tropo(); - try{ - $say = new Say("Please enter your account number...", null, null, null, null, "", null, null); - $tropo->say($say); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Missing required property: 'name'"); - } - } - public function testFailsSayWithNoValueParameter4() { $tropo = new Tropo(); @@ -99,37 +77,5 @@ public function testFailsSayWithNoValueParameter5() $this->assertEquals($e->getMessage(), "Argument 1 passed to Tropo::say() must be a string or an instance of Say."); } } - - public function testFailsSayWithNoNameParameter3() - { - $tropo = new Tropo(); - try{ - $tropo->say("Please enter your account number..."); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); - } - } - - public function testFailsSayWithNoNameParameter4() - { - $tropo = new Tropo(); - $params = array("name"=>null); - try{ - $tropo->say("Please enter your account number...",$params); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Required property: 'name' must be a string."); - } - } - - public function testFailsSayWithNoNameParameter5() - { - $tropo = new Tropo(); - $params = array("name"=>""); - try{ - $tropo->say("Please enter your account number...",$params); - } catch (Exception $e) { - $this->assertEquals($e->getMessage(), "Required property: 'name' must be a string."); - } - } } ?> \ No newline at end of file diff --git a/tests/TransferTest.php b/tests/TransferTest.php index 9040982..199b881 100644 --- a/tests/TransferTest.php +++ b/tests/TransferTest.php @@ -7,76 +7,65 @@ class TransferTest extends PHPUnit_Framework_TestCase public function testTransferWithMinOptions() { $tropo = new Tropo(); - $params = array( - 'name' => 'foo' - ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678"}}]}'); } public function testTransferWithMinOptions1() { $tropo = new Tropo(); - $params = array( - 'name' => 'foo' - ); $tropo->transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"]}}]}'); } public function testTransferWithChoicesOptions() { $tropo = new Tropo(); $choices = new Choices(null, null, "#"); $params = array( - 'name' => 'foo', 'choices' => $choices, ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"}}}]}'); } public function testTransferWithChoicesOptions1() { $tropo = new Tropo(); $params = array( - 'name' => 'foo', 'choices' => '#', ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"#"}}}]}'); } public function testTransferWithChoicesAndTerminatorOptions() { $tropo = new Tropo(); $choices = new Choices(null, null, "#"); $params = array( - 'name' => 'foo', 'choices' => $choices, 'terminator' => '*', ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"}}}]}'); } public function testTransferWithChoicesAndTerminatorOptions1() { $tropo = new Tropo(); $params = array( - 'name' => 'foo', 'choices' => '#', 'terminator' => '*', ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","choices":{"terminator":"*"}}}]}'); } public function testTransferWithOnOptions() { $tropo = new Tropo(); $on = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); $params = array( - 'name' => 'foo', 'ringRepeat' => 2, 'on' => $on, ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}}}}]}'); } public function testTransferWithAllOptions() { @@ -96,7 +85,6 @@ public function testTransferWithAllOptions() { 'from' => '14155551212', 'timeout' => 30.0, 'answerOnMedia' => false, - 'name' => 'foo', 'required' => true, 'allowSignals' => $allowSignals, 'machineDetection' => false, @@ -112,7 +100,7 @@ public function testTransferWithAllOptions() { 'label' => 'transferLabel' ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } public function testTransferWithAllOptions1() { @@ -132,7 +120,6 @@ public function testTransferWithAllOptions1() { 'from' => '14155551212', 'timeout' => 30.0, 'answerOnMedia' => false, - 'name' => 'foo', 'required' => true, 'allowSignals' => $allowSignals, 'machineDetection' => 'For the most accurate results, the "introduction" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.', @@ -148,38 +135,38 @@ public function testTransferWithAllOptions1() { 'label' => 'transferLabel' ); $tropo->transfer("sip:pengxli@172.16.72.131:5678", $params); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } public function testCreateMinObject() { $tropo = new Tropo(); - $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", null, null, null, null, null, null, null, null, null, null, "foo"); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678"); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678"}}]}'); } public function testCreateObject() { $tropo = new Tropo(); - $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), null, null, null, null, null, null, null, null, null, null, "foo"); + $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678")); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"]}}]}'); } public function testCreateObject1() { $tropo = new Tropo(); $choices = new Choices(null, null, "#"); - $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), null, $choices, null, null, null, null, null, null, null, null, "foo"); + $transfer = new Transfer(array("sip:pengxli@172.16.72.131:5678", "sip:pengxli@192.168.26.1:5678"), null, $choices); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"choices":{"terminator":"#"},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":["sip:pengxli@172.16.72.131:5678","sip:pengxli@192.168.26.1:5678"],"choices":{"terminator":"#"}}}]}'); } public function testCreateObject2() { $tropo = new Tropo(); $on = new On("ring", null, new Say("http://www.phono.com/audio/holdmusic.mp3")); - $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", null, null, null, 2, null, $on, null, null, null, null, "foo"); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", null, null, null, 2, null, $on); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},"name":"foo"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","ringRepeat":2,"on":{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}}}}]}'); } public function testCreateObject3() { @@ -196,9 +183,9 @@ public function testCreateObject3() { $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); - $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, false, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, false, Voice::$US_English_female_allison, null, true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":false,"voice":"allison","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } public function testCreateObject4() { @@ -216,9 +203,9 @@ public function testCreateObject4() { $ask = new Ask(3, true, new Choices("[5 DIGITS]", "dtmf", null), null, "ask", true, $say); $onConnect = new On("connect", null, null, null, $ask); $on = array($onRing, $onConnect); - $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, $machineDetection, Voice::$US_English_female_allison, "foo", true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); + $transfer = new Transfer("sip:pengxli@172.16.72.131:5678", false, $choices, "14155551212", 2, 30.0, $on, $allowSignals, $headers, $machineDetection, Voice::$US_English_female_allison, null, true, 5.0, true, "http://192.168.26.203/result.php", "suppress", "transferLabel"); $tropo->transfer($transfer); - $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","name":"foo","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"transfer":{"to":"sip:pengxli@172.16.72.131:5678","answerOnMedia":false,"choices":{"terminator":"#"},"from":"14155551212","ringRepeat":2,"timeout":30,"on":[{"event":"ring","say":{"value":"http://www.phono.com/audio/holdmusic.mp3"}},{"event":"connect","ask":{"attempts":3,"bargein":true,"choices":{"value":"[5 DIGITS]","mode":"dtmf"},"name":"ask","required":true,"say":[{"event":"nomatch","value":"Sorry. Please enter you 5 digit account number again."},{"event":"timeout","value":"Sorry, I did not hear anything."},{"value":"Please enter 5 digit account number."}]}}],"allowSignals":["exit","quit"],"headers":{"foo":"bar","bling":"baz"},"machineDetection":{"introduction":"For the most accurate results, the \"introduction\" should be long enough to give Tropo time to detect a human or machine. The longer the introduction, the more time we have to determine how the call was answered.","voice":"allison"},"voice":"allison","required":true,"interdigitTimeout":5,"playTones":true,"callbackUrl":"http://192.168.26.203/result.php","promptLogSecurity":"suppress","label":"transferLabel"}}]}'); } } diff --git a/tropo.class.php b/tropo.class.php index f85bd30..95329ce 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -172,9 +172,6 @@ public function call($call, Array $params=NULL) { if(null === $call->getTo()) { throw new Exception("Missing required property: 'to'"); } - if(null === $call->getName()) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($call->getTo()) && ($call->getTo() != '')) { } elseif (is_array($call->getTo())) { foreach ($call->getTo() as $value) { @@ -185,9 +182,6 @@ public function call($call, Array $params=NULL) { } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if (!(is_string($call->getName()) && ($call->getName() != ''))) { - throw new Exception("Required property: 'to' must be a string."); - } } elseif (is_array($call) || (is_string($call) && ($call !== ''))) { @@ -207,31 +201,22 @@ public function call($call, Array $params=NULL) { $to = $call; } - if (isset($params) && is_array($params)) { + if (isset($params)) { - if (array_key_exists('name', $params)) { - if (is_string($params["name"]) && ($params["name"] !=='')) { - $name = $params["name"]; - } else { - throw new Exception("'name' must be is a string."); + if (is_array($params)) { + $p = array('from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'allowSignals', 'machineDetection', 'voice', 'name', 'required', 'callbackUrl', 'promptLogSecurity', 'label'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } } + $call = new Call($to, $from, $network, $channel, $answerOnMedia, $timeout, $headers, null, $allowSignals, $machineDetection, $voice, $name, $required, $callbackUrl, $promptLogSecurity, $label); } else { - throw new Exception("Missing required property: 'name'"); + throw new Exception("When Argument 1 passed to Tropo::call() is a string, argument 2 passed to Tropo::call() must be of the type array."); } - - $p = array('from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'allowSignals', 'machineDetection', 'voice', 'required', 'callbackUrl', 'promptLogSecurity', 'label'); - foreach ($p as $option) { - $$option = null; - if (array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $call = new Call($to, $from, $network, $channel, $answerOnMedia, $timeout, $headers, null, $allowSignals, $machineDetection, $voice, $name, $required, $callbackUrl, $promptLogSecurity, $label); - } else { - - throw new Exception("When Argument 1 passed to Tropo::call() is a string, argument 2 passed to Tropo::call() must be of the type array."); - + $call = new Call($to); } } else { @@ -255,42 +240,30 @@ public function conference($conference, Array $params=NULL) { if(null === $conference->getId()) { throw new Exception("Missing required property: 'id'"); } - if(null === $conference->getName()) { - throw new Exception("Missing required property: 'name'"); - } - if (!(is_string($conference->getName()) && ($conference->getName() != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } if (!(is_string($conference->getId()) && ($conference->getId() != ''))) { throw new Exception("Required property: 'id' must be a string."); } } elseif (is_string($conference) && ($conference !== '')) { - if (isset($params) && is_array($params)) { + $id = $conference; + if (isset($params)) { - if (array_key_exists('name', $params)) { - if (is_string($params["name"]) && ($params["name"] !=='')) { - $name = $params["name"]; - } else { - throw new Exception("'name' must be is a string."); + if (is_array($params)) { + $p = array('name', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice', 'promptLogSecurity'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; + } } + $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice, $promptLogSecurity); } else { - throw new Exception("Missing required property: 'name'"); + throw new Exception("When Argument 1 passed to Tropo::conference() is a string, argument 2 passed to Tropo::conference() must be of the type array."); } - - $p = array('name', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice', 'promptLogSecurity'); - foreach ($p as $option) { - $$option = null; - if (array_key_exists($option, $params)) { - $$option = $params[$option]; - } - } - $id = $conference; - $conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice, $promptLogSecurity); } else { - throw new Exception("When Argument 1 passed to Tropo::conference() is a string, argument 2 passed to Tropo::conference() must be of the type array."); + $conference = new Conference(null, $id); } } else { @@ -326,9 +299,6 @@ public function message($message, Array $params=null) { if(null === $message->getTo()) { throw new Exception("Missing required property: 'to'"); } - if(null === $message->getName()) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($message->getTo()) && ($message->getTo() != '')) { } elseif (is_array($message->getTo())) { foreach ($message->getTo() as $value) { @@ -339,9 +309,6 @@ public function message($message, Array $params=null) { } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if(!(is_string($message->getName()) && ($message->getName() != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } } elseif (is_string($message) && ($message!=='')) { if (isset($params) && is_array($params)) { @@ -363,16 +330,6 @@ public function message($message, Array $params=null) { throw new Exception("Missing required property: 'to'"); } - if (array_key_exists('name', $params)) { - if (is_string($params["name"]) && ($params["name"] !=='')) { - $name = $params["name"]; - } else { - throw new Exception("'name' must be is a string."); - } - } else { - throw new Exception("Missing required property: 'name'"); - } - if (array_key_exists('say', $params)) { if (is_array($params["say"])) { $say[] = new Say($message); @@ -391,7 +348,7 @@ public function message($message, Array $params=null) { $say = new Say($message); } - $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers','required','promptLogSecurity'); + $p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers','name','required','promptLogSecurity'); foreach ($p as $option) { $$option = null; if (is_array($params) && array_key_exists($option, $params)) { @@ -513,15 +470,9 @@ public function record($record) { if(null === $record->getUrl()) { throw new Exception("Missing required property: 'url'"); } - if(null === $record->getName()) { - throw new Exception("Missing required property: 'name'"); - } // if (!(is_string($record->getUrl()) && ($record->getUrl() != ''))) { // throw new Exception("Required property: 'url' must be a string."); // } - if (!(is_string($record->getName()) && ($record->getName() != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } } elseif (is_array($record)) { @@ -529,15 +480,9 @@ public function record($record) { if (!array_key_exists('url', $params)) { throw new Exception("Missing required property: 'url'"); } - if (!array_key_exists('name', $params)) { - throw new Exception("Missing required property: 'name'"); - } // if (!(is_string($params['url']) && ($params['url'] != ''))) { // throw new Exception("Required property: 'url' must be a string."); // } - if (!(is_string($params['name']) && ($params['name'] != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } $p = array('voice', 'emailFormat', 'transcription', 'terminator'); foreach ($p as $option) { $params[$option] = array_key_exists($option, $params) ? $params[$option] : null; @@ -613,40 +558,31 @@ public function redirect($redirect, Array $params=NULL) { if(null === $redirect->getTo()) { throw new Exception("Missing required property: 'to'"); } - if(null === $redirect->getName()) { - throw new Exception("Missing required property: 'name'"); - } if (!(is_string($redirect->getTo()) && ($redirect->getTo() != ''))) { throw new Exception("Required property: 'to' must be a string."); } - if (!(is_string($redirect->getName()) && ($redirect->getName() != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } } elseif (is_string($redirect) && ($redirect !== '')) { - if (isset($params) && is_array($params)) { + if (isset($params)) { - if (array_key_exists('name', $params)) { - if (is_string($params["name"]) && ($params["name"] !=='')) { + if (is_array($params)) { + $required = null; + if (array_key_exists('required', $params)) { + $required = $params["required"]; + } + $name = null; + if (array_key_exists('name', $params)) { $name = $params["name"]; - } else { - throw new Exception("'name' must be is a string."); } - } else { - throw new Exception("Missing required property: 'name'"); - } - $required = null; - if (array_key_exists('required', $params)) { - $required = $params["required"]; + $redirect = new Redirect($redirect, null, $name, $required); + } else { + throw new Exception("When Argument 1 passed to Tropo::redirect() is a string, argument 2 passed to Tropo::redirect() must be of the type array."); } - - $redirect = new Redirect($redirect, null, $name, $required); - } else { - throw new Exception("When Argument 1 passed to Tropo::redirect() is a string, argument 2 passed to Tropo::redirect() must be of the type array."); + $redirect = new Redirect($redirect); } @@ -685,33 +621,18 @@ public function say($say, Array $params=NULL) { throw new Exception("Missing required property: 'value'"); - } - if(!(is_string($say->getName()) && ($say->getName() != ''))) { - - throw new Exception("Missing required property: 'name'"); - } $say->setEvent(null); } elseif (is_string($say) && ($say != '')) { - if (isset($params) && is_array($params)) { - - if (array_key_exists('name', $params)) { + $value = $say; - if (is_string($params['name']) && ($params['name'] != '')) { + if (isset($params)) { - $name = $params['name']; - - } else { - - throw new Exception("Required property: 'name' must be a string."); - - } - - $p = array('as', 'event','voice', 'allowSignals', 'required', 'promptLogSecurity'); - $value = $say; + if (is_array($params)) { + $p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity'); foreach ($p as $option) { $$option = null; if (array_key_exists($option, $params)) { @@ -721,16 +642,12 @@ public function say($say, Array $params=NULL) { $voice = isset($voice) ? $voice : $this->_voice; $event = null; $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity); - } else { - - throw new Exception("Missing required property: 'name'"); - + throw new Exception("When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); } - } else { - throw new Exception("When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); + $say = new Say($value); } } else { @@ -809,9 +726,6 @@ public function transfer($transfer, Array $params=NULL) { if(null === $transfer->getTo()) { throw new Exception("Missing required property: 'to'"); } - if(null === $transfer->getName()) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($transfer->getTo()) && ($transfer->getTo() != '')) { } elseif (is_array($transfer->getTo())) { foreach ($transfer->getTo() as $value) { @@ -822,9 +736,6 @@ public function transfer($transfer, Array $params=NULL) { } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if (!(is_string($transfer->getName()) && ($transfer->getName() != ''))) { - throw new Exception("Required property: 'to' must be a string."); - } } elseif (is_array($transfer) || (is_string($transfer) && ($transfer !== ''))) {//$transfer is a non-empty string or a non-empty string of array @@ -844,67 +755,61 @@ public function transfer($transfer, Array $params=NULL) { $to = $transfer; } - if (isset($params) && is_array($params)) { + if (isset($params)) { - if (array_key_exists('name', $params)) { - if (is_string($params["name"]) && ($params["name"] !=='')) { - $name = $params["name"]; - } else { - throw new Exception("'name' must be is a string."); - } - } else { - throw new Exception("Missing required property: 'name'"); - } - - $choices = null; - if (array_key_exists('choices', $params)) { + if (is_array($params)) { + $choices = null; + if (array_key_exists('choices', $params)) { - if ($params["choices"] instanceof Choices) { - $choices = $params["choices"]; - } elseif (is_string($params["choices"]) && ($params["choices"] !== '')) { - $choices = new Choices(null, null, $params["choices"]); - } else { - $choices = null; + if ($params["choices"] instanceof Choices) { + $choices = $params["choices"]; + } elseif (is_string($params["choices"]) && ($params["choices"] !== '')) { + $choices = new Choices(null, null, $params["choices"]); + } else { + $choices = null; + } } - } - if (array_key_exists('terminator', $params)) { - if (is_string($params["terminator"]) && ($params["terminator"] !== '')) { - $choices = new Choices(null, null, $params["terminator"]); - } - } - - $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice', 'required', 'interdigitTimeout', 'playTones', 'callbackUrl', 'promptLogSecurity', 'label'); - foreach ($p as $option) { - $$option = null; - if (array_key_exists($option, $params)) { - $$option = $params[$option]; + if (array_key_exists('terminator', $params)) { + if (is_string($params["terminator"]) && ($params["terminator"] !== '')) { + $choices = new Choices(null, null, $params["terminator"]); + } } - } - $on = null; - if (array_key_exists('on', $params)) { - if ($params['on'] instanceof On) { - if (is_string($params['on']->getEvent()) && ((strtolower($params['on']->getEvent()) == 'ring') || (strtolower($params['on']->getEvent()) == 'connect'))) { - $on = $params['on']; - } else { - throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); + + $p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice', 'name', 'required', 'interdigitTimeout', 'playTones', 'callbackUrl', 'promptLogSecurity', 'label'); + foreach ($p as $option) { + $$option = null; + if (array_key_exists($option, $params)) { + $$option = $params[$option]; } - } elseif (is_array($params['on'])) { - foreach ($params['on'] as $value) { - if ($value instanceof On) { - if (is_string($value->getEvent()) && ((strtolower($value->getEvent()) == 'ring') || (strtolower($value->getEvent()) == 'connect'))) { - $on[] = $value; - } else { - throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); + } + $on = null; + if (array_key_exists('on', $params)) { + if ($params['on'] instanceof On) { + if (is_string($params['on']->getEvent()) && ((strtolower($params['on']->getEvent()) == 'ring') || (strtolower($params['on']->getEvent()) == 'connect'))) { + $on = $params['on']; + } else { + throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); + } + } elseif (is_array($params['on'])) { + foreach ($params['on'] as $value) { + if ($value instanceof On) { + if (is_string($value->getEvent()) && ((strtolower($value->getEvent()) == 'ring') || (strtolower($value->getEvent()) == 'connect'))) { + $on[] = $value; + } else { + throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'"); + } } } } } + $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice, $name, $required, $interdigitTimeout, $playTones, $callbackUrl, $promptLogSecurity, $label); + } else { + throw new Exception("When Argument 1 passed to Tropo::transfer() is a string or a string of array, argument 2 passed to Tropo::transfer() must be of the type array."); } - $transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice, $name, $required, $interdigitTimeout, $playTones, $callbackUrl, $promptLogSecurity, $label); } else { - throw new Exception("When Argument 1 passed to Tropo::transfer() is a string or a string of array, argument 2 passed to Tropo::transfer() must be of the type array."); + $transfer = new Transfer($to); } @@ -1455,13 +1360,10 @@ public function getName() { * @param StartRecording $recording * @param string|array $allowSignals */ - public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL, $machineDetection=NULL, $voice=NULL, $name, $required=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { + public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answerOnMedia=NULL, $timeout=NULL, Array $headers=NULL, StartRecording $recording=NULL, $allowSignals=NULL, $machineDetection=NULL, $voice=NULL, $name=NULL, $required=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { if(!isset($to)) { throw new Exception("Missing required property: 'to'"); } - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($to) && ($to != '')) { } elseif (is_array($to)) { foreach ($to as $value) { @@ -1472,9 +1374,6 @@ public function __construct($to, $from=NULL, $network=NULL, $channel=NULL, $answ } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if (!(is_string($name) && ($name != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } $this->_to = $to; $this->_from = $from; $this->_network = $network; @@ -1516,7 +1415,7 @@ public function __toString() { } } if(isset($this->_voice)) { $this->voice = $this->_voice; } - $this->name = $this->_name; + if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_callbackUrl)) { $this->callbackUrl = $this->_callbackUrl; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } @@ -1616,16 +1515,10 @@ public function getId() { * @param string|array $allowSignals * @param int $interdigitTimeout */ - public function __construct($name, $id, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL, $joinPrompt=NULL, $leavePrompt=NULL, $voice=NULL, $promptLogSecurity=NULL) { - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } + public function __construct($name=NULL, $id, $mute=NULL, On $on=NULL, $playTones=NULL, $required=NULL, $terminator=NULL, $allowSignals=NULL, $interdigitTimeout=NULL, $joinPrompt=NULL, $leavePrompt=NULL, $voice=NULL, $promptLogSecurity=NULL) { if(!isset($id)) { throw new Exception("Missing required property: 'id'"); } - if (!(is_string($name) && ($name != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } if (!(is_string($id) && ($id != ''))) { throw new Exception("Required property: 'id' must be a string."); } @@ -1647,7 +1540,7 @@ public function __construct($name, $id, $mute=NULL, On $on=NULL, $playTones=NULL * */ public function __toString() { - $this->name = $this->_name; + if(isset($this->_name)) { $this->name = $this->_name; } $this->id = $this->_id; if(isset($this->_mute)) { $this->mute = $this->_mute; } if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } @@ -1733,16 +1626,13 @@ public function getName() { * @param array $headers */ // Say $say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null - public function __construct($say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null, $name, $required=null, $promptLogSecurity=null) { + public function __construct($say, $to, $channel=null, $network=null, $from=null, $voice=null, $timeout=null, $answerOnMedia=null, Array $headers=null, $name=null, $required=null, $promptLogSecurity=null) { if(!isset($say)) { throw new Exception("Missing required property: 'say'"); } if(!isset($to)) { throw new Exception("Missing required property: 'to'"); } - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } if ($say instanceof Say) { $this->_say = sprintf('%s', $say); } elseif (is_array($say)) { @@ -1771,11 +1661,7 @@ public function __construct($say, $to, $channel=null, $network=null, $from=null, } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if (is_string($name) && ($name != '')) { - $this->_name = $name; - } else { - throw new Exception("Required property: 'name' must be a string."); - } + $this->_name = $name; $this->_channel = $channel; $this->_network = $network; $this->_from = $from; @@ -1794,7 +1680,7 @@ public function __construct($say, $to, $channel=null, $network=null, $from=null, public function __toString() { if(isset($this->_say)) { $this->say = json_decode($this->_say); } $this->to = $this->_to; - $this->name = $this->_name; + if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_channel)) { $this->channel = $this->_channel; } if(isset($this->_network)) { $this->network = $this->_network; } if(isset($this->_from)) { $this->from = $this->_from; } @@ -1941,13 +1827,10 @@ public function getName() { * @param int $minConfidence * @param int $interdigitTimeout */ - public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL, $asyncUpload=NULL, $name, $promptLogSecurity=NULL, $sensitivity=NULL) { + public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $beep=NULL, Choices $choices=NULL, $format=NULL, $maxSilence=NULL, $maxTime=NULL, $method=NULL, $password=NULL, $required=NULL, $say=NULL, $timeout=NULL, Transcription $transcription=NULL, $username=NULL, $url, $voice=NULL, $minConfidence=NULL, $interdigitTimeout=NULL, $asyncUpload=NULL, $name=NULL, $promptLogSecurity=NULL, $sensitivity=NULL) { if(!isset($url)) { throw new Exception("Missing required property: 'url'"); } - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($url) && ($url != '')) { $this->_url = sprintf('%s', new Url($url, $username, $password, $method)); } else if ($url instanceof Url) { @@ -1966,9 +1849,6 @@ public function __construct($attempts=NULL, $allowSignals=NULL, $bargein=NULL, $ } else { throw new Exception("Property: 'url' must be a valid string, an instance of Url or an array of Urls."); } - if (!(is_string($name) && ($name != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } $this->_attempts = $attempts; $this->_allowSignals = $allowSignals; $this->_bargein = $bargein; @@ -2066,21 +1946,15 @@ public function getName() { * @param Endpoint $to * @param Endpoint $from */ - public function __construct($to, $from=NULL, $name, $required) { + public function __construct($to, $from=NULL, $name=NULL, $required) { if(!isset($to)) { throw new Exception("Missing required property: 'to'"); } - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } if (!(is_string($to) && ($to != ''))) { throw new Exception("Required property: 'to' must be a string."); } - if (!(is_string($name) && ($name != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } $this->_to = sprintf('%s', $to); - $this->_name = sprintf('%s', $name); + $this->_name = $name; $this->_required = $required; } @@ -2090,7 +1964,7 @@ public function __construct($to, $from=NULL, $name, $required) { */ public function __toString() { $this->to = $this->_to; - $this->name = $this->_name; + if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } return json_encode($this); } @@ -2767,13 +2641,10 @@ public function getName() { * @param string|array $allowSignals * @param array $headers */ - public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL, $machineDetection=NULL, $voice=NULL, $name, $required=NULL, $interdigitTimeout=NULL, $playTones=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { + public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $from=NULL, $ringRepeat=NULL, $timeout=NULL, $on=NULL, $allowSignals=NULL, Array $headers=NULL, $machineDetection=NULL, $voice=NULL, $name=NULL, $required=NULL, $interdigitTimeout=NULL, $playTones=NULL, $callbackUrl=NULL, $promptLogSecurity=NULL, $label=NULL) { if(!isset($to)) { throw new Exception("Missing required property: 'to'"); } - if(!isset($name)) { - throw new Exception("Missing required property: 'name'"); - } if (is_string($to) && ($to != '')) { } elseif (is_array($to)) { foreach ($to as $value) { @@ -2784,9 +2655,6 @@ public function __construct($to, $answerOnMedia=NULL, Choices $choices=NULL, $fr } else { throw new Exception("Required property: 'to' must be a string or a string of array."); } - if (!(is_string($name) && ($name != ''))) { - throw new Exception("Required property: 'name' must be a string."); - } $this->_to = $to; $this->_answerOnMedia = $answerOnMedia; $this->_choices = isset($choices) ? sprintf('%s', $choices) : null; @@ -2850,7 +2718,7 @@ public function __toString() { } } if(isset($this->_voice)) { $this->voice = $this->_voice; } - $this->name = $this->_name; + if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_interdigitTimeout)) { $this->interdigitTimeout = $this->_interdigitTimeout; } if(isset($this->_playTones)) { $this->playTones = $this->_playTones; } From 91448370ce15fc81f0e117da6be4139009a9f1a3 Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 10 Aug 2018 15:50:10 +0800 Subject: [PATCH 107/107] TROPO-13424 add MMS support --- tests/MessageTest.php | 16 +++++++++++ tests/SayTest.php | 62 +++++++++++++++++++++++++++++++++++++++++++ tests/SessionTest.php | 14 +++++++++- tropo.class.php | 44 +++++++++++++++++++++++++++--- 4 files changed, 132 insertions(+), 4 deletions(-) diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 7912160..af775d5 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -14,6 +14,22 @@ public function testMessageWithMinOptions() { $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is an announcement."},"to":"sip:pengxli@192.168.26.1:5678"}}]}'); } + public function testMessageWithMMS() { + $tropo = new Tropo(); + $say = new Say("This is the subject",null, null, null, null, null,null,null,"http://user:pass@server.com/1.jpg"); + $message = new Message($say,'sip:pengxli@192.168.26.1:5678',null, Network::$mms); + $tropo->message($message); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is the subject","media":"http://user:pass@server.com/1.jpg"},"to":"sip:pengxli@192.168.26.1:5678","network":"MMS"}}]}'); + } + + public function testMessageWithMMS1() { + $tropo = new Tropo(); + $say = new Say("This is the subject",null, null, null, null, null,null,null,array("http://server.com/1.jpg", "this is a inline text content", "http://filehosting.tropo.com/account/1/2.text")); + $message = new Message($say,'sip:pengxli@192.168.26.1:5678',null, Network::$mms); + $tropo->message($message); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"message":{"say":{"value":"This is the subject","media":["http://server.com/1.jpg","this is a inline text content","http://filehosting.tropo.com/account/1/2.text"]},"to":"sip:pengxli@192.168.26.1:5678","network":"MMS"}}]}'); + } + public function testMessageWithExtraSayOptiions() { $tropo = new Tropo(); $say = "Remember, you have a meeting at 2 PM."; diff --git a/tests/SayTest.php b/tests/SayTest.php index 7b8bc83..e8f7813 100644 --- a/tests/SayTest.php +++ b/tests/SayTest.php @@ -16,6 +16,18 @@ public function testSayWithOptions() { $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","name":"say"}]}]}'); } + public function testSayWithOptions1() { + $tropo = new Tropo(); + $tropo->say("Please enter your account number...",array('name' => 'say','media' => 'http://user:pass@server.com/1.jpg')); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","name":"say","media":"http://user:pass@server.com/1.jpg"}]}]}'); + } + + public function testSayWithOptions2() { + $tropo = new Tropo(); + $tropo->say("Please enter your account number...",array('name' => 'say','media' => array('http://server.com/1.jpg', 'this is a inline text content', 'http://filehosting.tropo.com/account/1/2.text'))); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","name":"say","media":["http://server.com/1.jpg","this is a inline text content","http://filehosting.tropo.com/account/1/2.text"]}]}]}'); + } + public function testCreateSayObject() { $tropo = new Tropo(); @@ -40,6 +52,56 @@ public function testCreateSayObject1() $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress"}]}]}'); } + public function testCreateSayObject2() + { + $tropo = new Tropo(); + $allowSignals = array('exit','quit'); + $say = new Say("Please enter your account number...", SayAs::$date, null, Voice::$US_English_female_allison, $allowSignals, null, true, "suppress", "http://user:pass@server.com/1.jpg"); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress","media":"http://user:pass@server.com/1.jpg"}]}]}'); + } + + public function testCreateSayObject3() + { + $tropo = new Tropo(); + $allowSignals = array('exit','quit'); + $say = new Say("Please enter your account number...", SayAs::$date, null, Voice::$US_English_female_allison, $allowSignals, null, true, "suppress", array('http://server.com/1.jpg', 'this is a inline text content', 'http://filehosting.tropo.com/account/1/2.text')); + $tropo->say($say); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress","media":["http://server.com/1.jpg","this is a inline text content","http://filehosting.tropo.com/account/1/2.text"]}]}]}'); + } + + public function testCreateSayObject4() + { + $tropo = new Tropo(); + $allowSignals = array('exit','quit'); + $params = array( + "as"=>SayAs::$date, + "event"=>"event", + "voice"=>Voice::$US_English_female_allison, + "allowSignals"=>$allowSignals, + "promptLogSecurity"=>"suppress", + "required"=>true, + "media"=>"http://user:pass@server.com/1.jpg"); + $tropo->say("Please enter your account number...",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress","media":"http://user:pass@server.com/1.jpg"}]}]}'); + } + + public function testCreateSayObject5() + { + $tropo = new Tropo(); + $allowSignals = array('exit','quit'); + $params = array( + "as"=>SayAs::$date, + "event"=>"event", + "voice"=>Voice::$US_English_female_allison, + "allowSignals"=>$allowSignals, + "promptLogSecurity"=>"suppress", + "required"=>true, + "media"=>array('http://server.com/1.jpg', 'this is a inline text content', 'http://filehosting.tropo.com/account/1/2.text')); + $tropo->say("Please enter your account number...",$params); + $this->assertEquals(sprintf($tropo), '{"tropo":[{"say":[{"value":"Please enter your account number...","as":"DATE","voice":"allison","allowSignals":["exit","quit"],"required":true,"promptLogSecurity":"suppress","media":["http://server.com/1.jpg","this is a inline text content","http://filehosting.tropo.com/account/1/2.text"]}]}]}'); + } + public function testFailsSayWithNoValueParameter1() { try{ diff --git a/tests/SessionTest.php b/tests/SessionTest.php index b0c8d82..4676996 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -6,13 +6,25 @@ class OnTest extends TestCase{ public function testCreateOnObject() { - $strSession = '{"session":{"id":"35b00c154f2fecacba37fad74e64a7e2","accountId":"1","applicationId":"1","timestamp":"2017-06-08T03:40:19.283Z","userType":"HUMAN","initialText":null,"callId":"c5b298fc0785fda9029f7f3b5aeef7ab","to":{"id":"9992801029","e164Id":"9992801029","name":"9992801029","channel":"VOICE","network":"SIP"},"from":{"id":"pengxli","e164Id":"pengxli","name":"pengxli","channel":"VOICE","network":"SIP"},"headers":{"Call-ID":"83369NTAxNDI2NDA4MWMzYTBiNzBiNmM0ZTVlMTQ4NjRlNmY","CSeq":"1 INVITE","Max-Forwards":"69","Request URI":"sip:9992801029@10.140.254.38;x-rt=0","Record-Route":"","x-sid":"6f1e3b7b2ace2a7785780b6337641388","User-Agent":"X-Lite release 4.9.7.1 stamp 83369","From":";tag=1bb1ef33","Supported":"replaces","Allow":"SUBSCRIBE\\r\\nNOTIFY\\r\\nINVITE\\r\\nACK\\r\\nCANCEL\\r\\nBYE\\r\\nREFER\\r\\nINFO\\r\\nOPTIONS\\r\\nMESSAGE","Via":"SIP/2.0/UDP 192.168.26.111:5060;branch=z9hG4bK1vxcouwp4r78j;rport=5060\\r\\nSIP/2.0/UDP 192.168.26.1:5678;branch=z9hG4bK-524287-1---b1f649005b368755;rport=5678","Contact":"","To":"","Content-Length":"335","Content-Type":"application/sdp"}}}'; + $strSession = '{"session":{"id":"35b00c154f2fecacba37fad74e64a7e2","accountId":"1","applicationId":"1","timestamp":"2017-06-08T03:40:19.283Z","userType":"HUMAN","initialText":null,"subject": "Inbound MMS subject","initialMedia":[{"status":"success","media": "http://filehosting.tropo.com/account/1.jpg"},{"status":"success","text": "this is text"},{"status":"failure","disposition": "Failed to upload: 500 Internal Error","media": "2.jpg"}],"callId":"c5b298fc0785fda9029f7f3b5aeef7ab","to":{"id":"9992801029","e164Id":"9992801029","name":"9992801029","channel":"VOICE","network":"SIP"},"from":{"id":"pengxli","e164Id":"pengxli","name":"pengxli","channel":"VOICE","network":"SIP"},"headers":{"Call-ID":"83369NTAxNDI2NDA4MWMzYTBiNzBiNmM0ZTVlMTQ4NjRlNmY","CSeq":"1 INVITE","Max-Forwards":"69","Request URI":"sip:9992801029@10.140.254.38;x-rt=0","Record-Route":"","x-sid":"6f1e3b7b2ace2a7785780b6337641388","User-Agent":"X-Lite release 4.9.7.1 stamp 83369","From":";tag=1bb1ef33","Supported":"replaces","Allow":"SUBSCRIBE\\r\\nNOTIFY\\r\\nINVITE\\r\\nACK\\r\\nCANCEL\\r\\nBYE\\r\\nREFER\\r\\nINFO\\r\\nOPTIONS\\r\\nMESSAGE","Via":"SIP/2.0/UDP 192.168.26.111:5060;branch=z9hG4bK1vxcouwp4r78j;rport=5060\\r\\nSIP/2.0/UDP 192.168.26.1:5678;branch=z9hG4bK-524287-1---b1f649005b368755;rport=5678","Contact":"","To":"","Content-Length":"335","Content-Type":"application/sdp"}}}'; $session = new Session($strSession); $this->assertEquals($session->getId(), '35b00c154f2fecacba37fad74e64a7e2'); $this->assertEquals($session->getAccountId(), '1'); $this->assertEquals($session->getTimestamp(), '2017-06-08T03:40:19.283Z'); $this->assertEquals($session->getUserType(), 'HUMAN'); $this->assertEquals($session->getInitialText(), null); + $initialMedia = $session->getInitialMedia(); + $this->assertEquals($session->getStatusFromMedia($initialMedia[0]), "success"); + $this->assertEquals($session->getMediaFromMedia($initialMedia[0]), "http://filehosting.tropo.com/account/1.jpg"); + + $this->assertEquals($session->getStatusFromMedia($initialMedia[1]), "success"); + $this->assertEquals($session->getTextFromMedia($initialMedia[1]), "this is text"); + + $this->assertEquals($session->getStatusFromMedia($initialMedia[2]), "failure"); + $this->assertEquals($session->getDispositionFromMedia($initialMedia[2]), "Failed to upload: 500 Internal Error"); + $this->assertEquals($session->getMediaFromMedia($initialMedia[2]), "2.jpg"); + + $this->assertEquals($session->getSubject(), "Inbound MMS subject"); $this->assertEquals($session->getCallId(), 'c5b298fc0785fda9029f7f3b5aeef7ab'); $to = $session->getTo(); $this->assertEquals($to["id"], '9992801029'); diff --git a/tropo.class.php b/tropo.class.php index 95329ce..6e69aca 100644 --- a/tropo.class.php +++ b/tropo.class.php @@ -632,7 +632,7 @@ public function say($say, Array $params=NULL) { if (isset($params)) { if (is_array($params)) { - $p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity'); + $p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity', 'media'); foreach ($p as $option) { $$option = null; if (array_key_exists($option, $params)) { @@ -641,7 +641,7 @@ public function say($say, Array $params=NULL) { } $voice = isset($voice) ? $voice : $this->_voice; $event = null; - $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity); + $say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity, $media); } else { throw new Exception("When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array."); } @@ -2233,6 +2233,7 @@ class Say extends BaseClass { private $_name; private $_required; private $_promptLogSecurity; + private $_media; public function getValue() { return $this->_value; @@ -2255,7 +2256,7 @@ public function setEvent($event) { * @param string $voice * @param string|array $allowSignals */ - public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL, $name=NULL, $required=NULL, $promptLogSecurity=NULL) { + public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSignals=NULL, $name=NULL, $required=NULL, $promptLogSecurity=NULL, $media=NULL) { if(!(is_string($value) && ($value != ''))) { throw new Exception("Missing required property: 'value'"); } @@ -2267,6 +2268,7 @@ public function __construct($value, $as=NULL, $event=NULL, $voice=NULL, $allowSi $this->_name = $name; $this->_required = $required; $this->_promptLogSecurity = $promptLogSecurity; + $this->_media = $media; } /** @@ -2282,6 +2284,7 @@ public function __toString() { if(isset($this->_name)) { $this->name = $this->_name; } if(isset($this->_required)) { $this->required = $this->_required; } if(isset($this->_promptLogSecurity)) { $this->promptLogSecurity = $this->_promptLogSecurity; } + if(isset($this->_media)) { $this->media = $this->_media; } return json_encode($this); } } @@ -2305,6 +2308,8 @@ class Session { private $_headers; private $_parameters; private $_userType; + private $_subject; + private $_initialMedia; /** * Class constructor @@ -2330,6 +2335,14 @@ public function __construct($json=NULL) { $this->_timestamp = $session->session->timestamp; $this->_initialText = $session->session->initialText; $this->_userType = $session->session->userType; + $this->_subject = isset($session->session->subject) ? $session->session->subject : null; + $this->_initialMedia = isset($session->session->initialMedia) ? $session->session->initialMedia : null; + if (is_object($this->_initialMedia)) { + $this->_text = isset($session->session->initialMedia->text) ? $session->session->initialMedia->text : null; + $this->_media = isset($session->session->initialMedia->media) ? $session->session->initialMedia->media : null; + $this->_status = isset($session->session->initialMedia->status) ? $session->session->initialMedia->status : null; + $this->_disposition = isset($session->session->initialMedia->disposition) ? $session->session->initialMedia->disposition : null; + } $this->_to = isset($session->session->to) ? array( "id" => $session->session->to->id, @@ -2411,6 +2424,30 @@ public function getUserType() { return $this->_userType; } + public function getSubject() { + return $this->_subject; + } + + public function getInitialMedia() { + return $this->_initialMedia; + } + + public function getTextFromMedia($media) { + return isset($media->text) ? $media->text : null; + } + + public function getMediaFromMedia($media) { + return isset($media->media) ? $media->media : null; + } + + public function getStatusFromMedia($media) { + return isset($media->status) ? $media->status : null; + } + + public function getDispositionFromMedia($media) { + return isset($media->disposition) ? $media->disposition : null; + } + /** * Returns the query string parameters for the session api * @@ -2958,6 +2995,7 @@ class Network { public static $yahoo = "YAHOO"; public static $twitter = "TWITTER"; public static $sip = "SIP"; + public static $mms = "MMS"; } /**