Added dialout sample scripts.
git-svn-id: http://voip.null.ro/svn/yate@2142 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
parent
26db12ec37
commit
dd6e4a30fa
|
@ -0,0 +1,173 @@
|
|||
#!/usr/bin/php -q
|
||||
<?
|
||||
/* Dialout IVR for the Yate PHP interface
|
||||
It will be started from dialout-scheduler.php
|
||||
*/
|
||||
require_once("libyate.php");
|
||||
|
||||
/* Always the first action to do */
|
||||
Yate::Init();
|
||||
|
||||
/* Uncomment next line to get debugging messages */
|
||||
//Yate::Debug(true);
|
||||
|
||||
$prompt = "";
|
||||
//$prompt = "wave/play/sounds/dialoutprompt.au";
|
||||
|
||||
$running = true;
|
||||
$done = false;
|
||||
$ourcallid = "dialout/" . uniqid(rand(),1);
|
||||
$partycallid = "";
|
||||
|
||||
$caller = "";
|
||||
$callername = "";
|
||||
$called = "";
|
||||
|
||||
// Initialization code called after the called party presses a key
|
||||
function startRealJob()
|
||||
{
|
||||
global $partycallid;
|
||||
global $caller;
|
||||
global $called;
|
||||
|
||||
// NOTE: put your state init code here
|
||||
}
|
||||
|
||||
// Got a DTMF
|
||||
function gotDTMF($text)
|
||||
{
|
||||
global $done;
|
||||
if (!$done) {
|
||||
$done = true;
|
||||
startRealJob();
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE: put your DTMF handling code here
|
||||
}
|
||||
|
||||
// Call failed
|
||||
function callFailed($error,$reason = "")
|
||||
{
|
||||
global $running;
|
||||
global $done;
|
||||
if ($done)
|
||||
return;
|
||||
if ($error == "")
|
||||
$error = $reason;
|
||||
if ($error == "Normal Clearing")
|
||||
$error = "";
|
||||
Yate::Debug("callFailed: '$error'");
|
||||
$running = false;
|
||||
$done = true;
|
||||
// NOTE: You must place your real handling code here
|
||||
}
|
||||
|
||||
// Succeeded call.execute, prepare for more activity
|
||||
function callExecuting($peerid)
|
||||
{
|
||||
global $ourcallid;
|
||||
global $partycallid;
|
||||
Yate::Debug("callExecuting: '$peerid'");
|
||||
$partycallid = $peerid;
|
||||
// Install handlers for the wave end and dtmf notify messages
|
||||
Yate::Install("chan.dtmf",90,"peerid",$ourcallid);
|
||||
Yate::Install("call.answered",90,"peerid",$ourcallid);
|
||||
Yate::Install("chan.notify",90,"id",$ourcallid);
|
||||
}
|
||||
|
||||
// Called party answered, ask it to press a number key to check it's human
|
||||
function callAnswered()
|
||||
{
|
||||
global $ourcallid;
|
||||
global $prompt;
|
||||
global $done;
|
||||
Yate::Debug("callAnswered");
|
||||
if ($prompt != "") {
|
||||
$m = new Yate("chan.attach");
|
||||
$m->id = "";
|
||||
$m->SetParam("id",$ourcallid);
|
||||
$m->SetParam("source",$prompt);
|
||||
$m->SetParam("single",true);
|
||||
$m->SetParam("notify",$ourcallid);
|
||||
$m->Dispatch();
|
||||
}
|
||||
else
|
||||
$done = true;
|
||||
}
|
||||
|
||||
Yate::SetLocal("id",$ourcallid);
|
||||
Yate::SetLocal("disconnected","true");
|
||||
Yate::Install("chan.disconnected",90,"id",$ourcallid);
|
||||
|
||||
// The main loop. We pick events and handle them
|
||||
while ($running) {
|
||||
$ev=Yate::GetEvent();
|
||||
if ($ev === false)
|
||||
break;
|
||||
if ($ev === true)
|
||||
continue;
|
||||
switch ($ev->type) {
|
||||
case "incoming":
|
||||
switch ($ev->name) {
|
||||
case "call.execute":
|
||||
$caller = $ev->GetValue("caller");
|
||||
$callername = $ev->GetValue("callername");
|
||||
$called = $ev->GetValue("called");
|
||||
$ev->handled = true;
|
||||
|
||||
// Dispatch outgoing call.execute before acknowledging this one
|
||||
$m = new Yate("call.execute");
|
||||
$m->SetParam("id",$ourcallid);
|
||||
$m->SetParam("callto",$ev->GetValue("direct"));
|
||||
$m->SetParam("caller",$caller);
|
||||
$m->SetParam("callername",$callername);
|
||||
$m->SetParam("called",$called);
|
||||
// No need to track us, this is an utility channel
|
||||
$m->SetParam("cdrtrack",false);
|
||||
// Active DTMF detector on outgoing call leg
|
||||
$m->SetParam("tonedetect_out",true);
|
||||
$m->Dispatch();
|
||||
break;
|
||||
case "call.answered":
|
||||
callAnswered();
|
||||
$ev->handled = true;
|
||||
break;
|
||||
case "chan.notify":
|
||||
callFailed("noanswer");
|
||||
$ev->handled = true;
|
||||
break;
|
||||
case "chan.dtmf":
|
||||
gotDTMF($ev->GetValue("text"));
|
||||
$ev->handled = true;
|
||||
break;
|
||||
case "chan.disconnected":
|
||||
callFailed($ev->GetValue("reason"));
|
||||
/* Put it back running to avoid double destruction... */
|
||||
$running = true;
|
||||
$ev->handled = true;
|
||||
break;
|
||||
}
|
||||
/* This is extremely important.
|
||||
We MUST let messages return, handled or not */
|
||||
if ($ev)
|
||||
$ev->Acknowledge();
|
||||
break;
|
||||
case "answer":
|
||||
if ($ev->name == "call.execute") {
|
||||
if ($ev->handled) {
|
||||
callExecuting($ev->GetValue("peerid"));
|
||||
continue;
|
||||
}
|
||||
callFailed($ev->GetValue("error"),$ev->GetValue("reason"));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Yate::Debug("PHP Event: " . $ev->type);
|
||||
}
|
||||
}
|
||||
|
||||
Yate::Debug("PHP: bye!");
|
||||
|
||||
/* vi: set ts=8 sw=4 sts=4 noet: */
|
||||
?>
|
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/php -q
|
||||
<?
|
||||
/* Sample dialout scheduler script
|
||||
To use add in extmodule.conf
|
||||
|
||||
dialout-scheduler.php=
|
||||
*/
|
||||
require_once("libyate.php");
|
||||
|
||||
$caller = "12345";
|
||||
$callername = "Sample dialout";
|
||||
|
||||
/* Always the first action to do */
|
||||
Yate::Init();
|
||||
|
||||
/* Uncomment next line to get debugging messages */
|
||||
//Yate::Debug(true);
|
||||
|
||||
// Start routing a single call
|
||||
function startRouting($called)
|
||||
{
|
||||
global $caller;
|
||||
global $callername;
|
||||
Yate::Debug("Routing dialout number='$called'");
|
||||
$m = new Yate("call.route");
|
||||
$m->SetParam("caller",$caller);
|
||||
$m->SetParam("callername",$callername);
|
||||
$m->SetParam("called",$called);
|
||||
$m->Dispatch();
|
||||
}
|
||||
|
||||
// Initiate a call once we know the target
|
||||
function callInitiate($target,$ev)
|
||||
{
|
||||
Yate::Debug("Initiating dialout call to '$target'");
|
||||
$m = new Yate("call.execute");
|
||||
$m->id = "";
|
||||
$m->SetParam("callto","external/nodata/dialout-dialer.php");
|
||||
$m->SetParam("direct",$target);
|
||||
$m->SetParam("caller",$ev->GetValue("caller"));
|
||||
$m->SetParam("callername",$ev->GetValue("callername"));
|
||||
$m->SetParam("called",$ev->GetValue("called"));
|
||||
$m->Dispatch();
|
||||
}
|
||||
|
||||
// Routing failed, the number may be invalid
|
||||
function routeFailure($error,$ev)
|
||||
{
|
||||
$number = $ev->GetValue("called");
|
||||
Yate::Output("Failed routing dialout to '$number' with error '$error'");
|
||||
}
|
||||
|
||||
// Check if we have any scheduled calls to start
|
||||
// NOTE: You must place your real algorithm here
|
||||
function checkSchedule()
|
||||
{
|
||||
// pretend we get a number to dial only ever 3rd attempt
|
||||
if (rand(0,99) > 33)
|
||||
return;
|
||||
// generate a random number and route to it
|
||||
$number = rand(10000,30000);
|
||||
startRouting($number);
|
||||
}
|
||||
|
||||
// Only install a handler for the timer message
|
||||
Yate::Install("engine.timer");
|
||||
// Ask Yate to restart this script if it dies unexpectedly
|
||||
Yate::SetLocal("restart",true);
|
||||
|
||||
// The main loop. We pick events and handle them
|
||||
for (;;) {
|
||||
$ev=Yate::GetEvent();
|
||||
if ($ev === false)
|
||||
break;
|
||||
if ($ev === true)
|
||||
continue;
|
||||
switch ($ev->type) {
|
||||
case "incoming":
|
||||
// We are sure it's the timer message
|
||||
$ev->Acknowledge();
|
||||
// Do the processing after letting the message return
|
||||
switch (substr($ev->GetValue("time"),-1)) {
|
||||
/* Only check when second ends in 2 or 7 */
|
||||
case "2":
|
||||
case "7":
|
||||
checkSchedule();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "answer":
|
||||
// Use the return of the routing message
|
||||
if ($ev->name == "call.route") {
|
||||
if ($ev->handled && ($ev->retval != "") && ($ev->retval != "-") && ($ev->retval != "error"))
|
||||
callInitiate($ev->retval,$ev);
|
||||
else
|
||||
routeFailure($ev->GetValue("error"),$ev);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Yate::Debug("PHP Event: " . $ev->type);
|
||||
}
|
||||
}
|
||||
|
||||
Yate::Debug("PHP: bye!");
|
||||
|
||||
/* vi: set ts=8 sw=4 sts=4 noet: */
|
||||
?>
|
Loading…
Reference in New Issue