From 91448370ce15fc81f0e117da6be4139009a9f1a3 Mon Sep 17 00:00:00 2001 From: pengxli Date: Fri, 10 Aug 2018 15:50:10 +0800 Subject: [PATCH] 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"; } /**