Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created January 6, 2010 23:01
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/270776 to your computer and use it in GitHub Desktop.
Save mheadd/270776 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", "call_logs/");
define("COUCH_DB_DESIGN_DOCUMENT_NAME", "");
define("COUCH_DB_SHOW_FUNCTION_NAME", "");
// Base URL for accessing Couch DB instance on remote host.
define("COUCH_DB_BASE_URL", "http://%USER%:%PASSWORD%@%USER%.my_couchdb_host.com:5984/");
// Function to lookup phone number by extension.
function getPhoneNumberByExtension(&$ch, $ext) {
return "18888888888"; // For now, hard code a number to dial.
}
// Function to log call record
function logCallRecord(&$ch, $callLog) {
$baseURL = str_replace(array('%USER%', '%PASSWORD%'), array(COUCH_DB_USER, COUCH_DB_PASSWORD), COUCH_DB_BASE_URL);
$fullURL = $baseURL.COUCH_DB_NAME;
curl_setopt($ch, CURLOPT_URL, $fullURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $callLog);
curl_exec($ch);
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();
// 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." ***");
$calledPartyInfo = getPhoneNumberByExtension(&$ch, $event->value); // Get the number to transfer to basaed on the extension selected.
prompt("Please hold while your call is transferred.");
$tran = transfer("tel:+".$calledPartyInfo,
array(
"answerOnMedia" => false,
"callerID" => "9999999999",
"timeout" => 60,
"method" => "bridged",
"choices" => "1,2,3,4,5,6,7,8,9,0,*,#",
"playvalue" => "La la la la...",
"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 ***");')
));
}
// Log the call record when transfer is over.
$callLog = json_encode(array("number_called" => $calledPartyInfo, "caller_id" => $currentCall.callerID, "time_stamp" => time()));
if (logCallRecord(&$ch, $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