Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created January 12, 2010 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mheadd/275266 to your computer and use it in GitHub Desktop.
Save mheadd/275266 to your computer and use it in GitHub Desktop.
<?php
// User defined constants.
define("COUCH_DB_USER", "");
define("COUCH_DB_PASSWORD", "");
define("COUCH_DB_NAME_LOGS", "call_logs/");
define("COUCH_DB_NAME_EXT", "call_settings/");
define("COUCH_DB_DESIGN_DOCUMENT_NAME", "");
define("COUCH_DB_SHOW_FUNCTION_NAME", "");
// Base URL for accessing Couch DB instance on CloudAnt.
define("COUCH_DB_BASE_URL", "http://%USER%:%PASSWORD%@%USER%.my_couchdb_host.com:5984/");
// Function to lookup phone number by extension.
function getPhoneNumberByExtension(&$ch, $baseURL, $ext) {
$fullURL = $baseURL.COUCH_DB_NAME_EXT.$ext;
curl_setopt($ch, CURLOPT_URL, $fullURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
// HTTP response on a GET should be '200' for success, 400 class response on error.
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
return NULL;
}
return $output;
}
// Function to log call record
function logCallRecord(&$ch, $baseURL, $callLog) {
$fullURL = $baseURL.COUCH_DB_NAME_LOGS;
curl_setopt($ch, CURLOPT_URL, $fullURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $callLog);
curl_exec($ch);
// HTTP response on POST should be '201' (created) if successful, 400 class on error.
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '201') {
return false;
}
return true;
}
// A CURL handle that will be used to connect to our CouchDB instance.
$ch = curl_init();
// Create the base URL that will be used to connect to our CouchDB instance
$baseURL = str_replace(array('%USER%', '%PASSWORD%'), array(COUCH_DB_USER, COUCH_DB_PASSWORD), COUCH_DB_BASE_URL);
// Start the call.
answer(30);
prompt("Hello, welcome to my sample cloud telephone application.", array("bargein" => false));
// Prompt the caller for the extension they want to call.
$event = prompt("Please select an extension.", array("choices" => "[4 DIGITS]", "choiceMode" => "dtmf", "repeat" => "3", "timeout" => "5"));
if($event->name == 'choice') {
_log("*** User selected ".$event->value." ***");
// Get the number to transfer to based on the extension selected.
$calledPartyInfo = getPhoneNumberByExtension(&$ch, $baseURL, $event->value);
if(!is_null($calledPartyInfo)) {
_log("*** Extension information for ".$event->value." successfully retrieved from CouchDB. ***");
// Decode the JSON response from CouchDB.
$extensionDetails = json_decode($calledPartyInfo, true);
// Transfer the call.
prompt("Please hold while your call is transferred to ".$extensionDetails["first_name"]." ".$extensionDetails["last_name"].", ".$extensionDetails["title"]);
$tran = transfer("tel:+".$extensionDetails["phone"],
array("answerOnMedia" => false,
"callerID" => $currentCall->callerID,
"timeout" => 60,
"method" => "bridged",
"choices" => "1,2,3,4,5,6,7,8,9,0,*,#",
"playvalue" => $extensionDetails["ring_tone"],
"playrepeat" => 10,
"onSuccess" => create_function('$event', '_log("*** Caller Transfered to: '. $event->value->calledId.');'),
"onError" => create_function('$event', '_log("*** Transfer error ***");'),
"onTimeout" => create_function('$event', '_log("*** Transfer timeout ***");'),
"onCallFailure" => create_function('$event', '_log("*** Transfer failed ***");')
));
}
// If the user selected an invalid extension, just hang up. This can obviously be tailored to make it more user friendly.
else {
prompt("That was not a valid extension.", array("bargein" => false));
hangup();
}
}
// Log the call record when transfer is over.
$callLog = json_encode(array("sesion_id" => $currentCall->getHeader("Call-ID"), "number_called" => $extensionDetails["phone"], "caller_id" => $currentCall->callerID, "call_timestamp" => time()));
if (logCallRecord(&$ch, $baseURL, $callLog)) {
_log("*** Call record successfuly logged. ***");
}
else {
_log("*** ERROR: Could not log call record. ***");
}
// End the call.
curl_close($ch);
prompt("The call is now over. Goodbye.", array("bargein" => false));
hangup();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment