Skip to content

Instantly share code, notes, and snippets.

@iadvize
Created January 17, 2011 21:48
Show Gist options
  • Save iadvize/783548 to your computer and use it in GitHub Desktop.
Save iadvize/783548 to your computer and use it in GitHub Desktop.
tropo app test in php
<?php
//header('Content-type:application/json');
header("Content-type: text/html; charset=utf-8");
@include '../../inc/env.inc.php';
include '../../inc/db.inc.php';
include '../../inc/functions.inc.php';
session_start();
require('FileDebugger.inc.php');
$debugger = new FileDebugger( '/home/iadvize/dev/jonathan/www/tropo/tropo.log' );
$debugger->debug( "======= DEBUG ======= " , file_get_contents("php://input") );
//$debugger->debug( "phone: " . $_POST['phoneNumber'] , false );
//$debugger->debug( "POST VALUES: " . implode(",",$_POST), false );
// dev token
$token = '5a0e28264a6f594a90f01770d713c55eee7482916f8bd3676aa941caf5c41daf3bc90046e0836210e55c8f6b';
// production token
//$token = "89586985f06d854ab81c848c471d48965841861c59f38c7033e9627bcb53b423878b3e54a51c043aa35391c3";
// Include the Tropo library and create a Tropo object
require('/home/iadvize/dev/jonathan/inc/libs/tropo/TropoClasses.php');
$tropo = new Tropo();
function unescapeJSON($json) {
return str_replace(array("\\", "\"{", "}\""), array("", "{", "}"), $json);
}
function sendRestMsg ($request) {
// Create a stream context so that we can POST the REST request to $url
$context = stream_context_create (array ('http' => array ('method' => 'POST'
,'header' => "Host: localhost:5280\nContent-Type: text/html; charset=utf-8\nContent-Length: ".strlen($request)
,'content' => $request)));
$result = file_get_contents("http://localhost:5280/rest", false, $context);
return $result;
}
if (isset($_REQUEST['go'])) {
$go = $_REQUEST['go'];
}
if ($go == 'incomplete'){
//$tropo->say('Une erreur est survenue, Votre appel ne peut aboutir.', array('voice' => 'florence'));
sendRestMsg('<message type="chat" to="devjon7@iadvize.com" from="idzjonjon@iadvize.com"><body>!c2c error</body></message>');
$tropo->hangup();
$tropo->renderJSON();
exit(1);
}
if ($go == 'hangup'){
$debugger->debug( "call is terminated !" , false );
$result = new Result();
$debugger->debug( "session duration: " . $result->getSessionDuration(), false );
$tropo->renderJSON();
exit(1);
}
if ($go == 'firstcall_completed'){
$debugger->debug( "=== operator answered ===", false);
sendRestMsg('<message type="chat" to="devjon7@iadvize.com" from="idzjonjon@iadvize.com"><body>!c2c opanswered</body></message>');
sendRestMsg('<message type="chat" to="idzjonjon@iadvize.com" from="devjon7@iadvize.com"><body>IDZ_OBSERVE</body></message>');
// start recording call (because i can't record after the transfer :( )
$tropo->startRecording(array("format" => "audio/mp3"
, "method" => "POST"
, "url" => "http://jon.iadvize.com/tropo/recorder.php"
, "method" => "POST"));
// transfer operator to visitor
$tropo->transfer($_SESSION['c2c_cphone'], array(
'on' => unescapeJSON(json_encode(array("event" => "ring", "say" => array("value" => "http://phono.com/audio/holdmusic.mp3")))),
'required' => true,
'terminator' => '*',
'ringRepeat' => 5
));
// wait for events but they never fire when transfer is ok (only when someone hangup)
$tropo->on(array("event" => "continue", "next" => "index.php?go=transfer_completed"));
$tropo->on(array("event" => "incomplete", "next" => "index.php?go=incomplete"));
$tropo->on(array("event" => "error", "next" => "?go=error"));
$tropo->on(array("event" => "hangup", "next" => "index.php?go=hangup"));
$tropo->renderJSON();
exit(1);
}
if ($go == 'transfer_completed'){
$debugger->debug( "======= TRANSFERT COMPLETED !! ======= " , false );
}
if ($go == 'start') {
// When the the session object is created, it tries
// to load the json that Tropo posts when reciving or
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".
// call our operator
$debugger->debug( "=== call operator at " . $session->getParameters("opPhone"), false);
$tropo->call($session->getParameters("opPhone"));
$_SESSION['c2c_opphone'] = $session->getParameters("opPhone");
$_SESSION['c2c_cphone'] = $session->getParameters("cPhone");
// tell operator to wait while we connect him with the visitor
$tropo->say('Merci de patienter le temps que votre correspondant décroche', array('voice' => 'florence'
, 'required' => true));
// wait for events
$tropo->on(array("event" => "continue", "next" => "index.php?go=firstcall_completed"));
$tropo->on(array("event" => "incomplete", "next" => "index.php?go=incomplete"));
$tropo->on(array("event" => "error", "next" => "index.php?go=error"));
$tropo->on(array("event" => "hangup", "next" => "index.php?go=hangup"));
}
$tropo->renderJSON();
} catch (TropoException $e) {
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment