Here’s a PHP AGI script I wrote so that when someone calls 799 they it will ring a group of desk phones if it’s business hours or retrieve the phone number(s) from the ‘Description’ field of the calendar entry and attempt to call them. If no one is reached it will then call the IT Manager. If that is not answered either it will send an email to the group.
The phpagi library is used as the interface to Asterisk, which is a little out of date and I had to make a tweak to; leave a comment if you’d like the details. There’s also a logger class I wrote which I can post if anyone is interested. The dialplan needs an entry like this:
exten => 799,1,AGI(oncall.php)
#!/usr/bin/php
<?php
//*******************
// Oncall.php
//
// Read an event from a Google Calendar
// and redirect the phone call to that
// number, or default to the IT Mgr.
//
// Based on perl script by Brian
// by Steve Schoeneman 07.27.2010
$debug=0;
require_once(‘lib/LogFile.php’);
require_once(‘phpagi/branches/phpagi_php5/phpagi.php’);
// Deskphones to ring during the day.
if($debug){
$deskphones_us = (‘SIP/steves’);
}else{
$deskphones_us = (‘SIP/joe&SIP/steves&SIP/pete&SIP/scott&SIP/mark&’.
‘IAX/london/517&IAX/london/589′);
}
if($debug){
$deskphones_uk = (‘IAX/london/635′);
}else{
$deskphones_uk = (‘IAX/london/517&IAX/london/589′);
}
$log = new LogFile(‘/var/log/asterisk/oncall.log’);
$answer = 0;
$cid = “IT Support/”;
$confirmed = ‘http://schemas.google.com/g/2005#event.confirmed’;
date_default_timezone_set(‘America/Chicago’);
$asm = new AGI;
$cid_array = $asm->get_variable(‘CALLERIDNAME’);
$callerid = $cid_array['data'];
$cid = $cid.$cid_array['data'];
$asm->exec(‘SetCIDName’,'”‘.$cid.’”‘);
$asm->Verbose(‘cid name: ‘.$cid);
if(is_workhours_uk() == TRUE){
$asm->stream_file(‘tts/ringing_it_support’);
$log->lwrite(“Calling “.$deskphones_uk.”;”);
$asm->exec(‘Dial’,$deskphones_uk.’|15′);
$call = $asm->get_variable(‘DIALSTATUS’);
$asm->Verbose(‘DIALSTATUS: ‘.print_r($call,true));
if($call['data'] == ‘ANSWER’){
$log->lwrite(“Answered;\n”);
$asm->hangup();
}else{
$log->lwrite(“Not answered;”);
}
}
if(is_workhours_us() == TRUE){
$asm->stream_file(‘tts/ringing_it_support’);
$log->lwrite(“Calling “.$deskphones_us.”;”);
$asm->exec(‘Dial’,$deskphones_us.’|15′);
$call = $asm->get_variable(‘DIALSTATUS’);
$asm->Verbose(‘DIALSTATUS: ‘.print_r($call,true));
if($call['data'] == ‘ANSWER’){
$log->lwrite(“Answered;\n”);
$asm->hangup();
}else{
$log->lwrite(“Not answered;”);
}
}
$offset=date(“P”);
$tz=preg_replace(‘/\+/’,'%2b’,$offset);
if($debug){
$feed = ‘https://www.google.com/calendar/feeds/mydomain.com_your_private_calendar_xml _address_calendar.google.com/private-(*&*HUH*&H*HU/full?’.
‘orderby=starttime&singleevents=true&start-min=’ . date(“Y-m-d\Th:i:s”, time()).$tz.
‘&start-max=’ . date(“Y-m-d\Th:i:s”, time()).$tz;
} else {
$feed = ‘https://www.google.com/calendar/feeds/mydomain.com__your_private_calendar_xml _address_group.calendar.google.com/private-98732428jHJH82903′.
‘/full?orderby=starttime&singleevents=true&start-min=’ . date(“Y-m-d\Th:i:s”, time()).$tz.
‘&start-max=’ . date(“Y-m-d\Th:i:s”, time()).$tz;
}
//if($debug) $log->lwrite($feed);
// This is the XML document object
$doc = new DOMDocument();
$log->lwrite(date(“F j, Y,g:i a”).”;”);
$doc->load( $feed );
if($debug) $log->lwrite(“Loaded feed;”);
$entries = $doc->getElementsByTagName( “entry” );
// length of 0 means no entries were fetched
if($entries->length != 0) {
$x = 0;
$asm->verbose(“Total entries: $entries->length”);
foreach ( $entries as $entry ) {
$asm->verbose(“x = $x”);
$status = $entry->getElementsByTagName( “eventStatus” );
$eventStatus = $status->item(0)->getAttributeNode(“value”)->value;
if($eventStatus == $confirmed && ($call['data'] != ‘ANSWER’ || is_workhours_us() == FALSE)){
$log->lwrite(“Ringing on call number(s);”);
$titles = $entry->getElementsByTagName( “title” );
$title = $titles->item(0)->nodeValue;
$content = $entry->getElementsByTagName( “content” );
$what = $content->item(0)->nodeValue;
$log->lwrite($title.”;”);
$log->lwrite($what.”;”);
if($what != “”){
//Strip misc crap and white space from phone number
$number[$x] = preg_replace(‘/[-\.\s\(\)]/’,”,$what);
if($debug) $asm->verbose(“After junk removal ” . $number[$x]);
$number[$x] = preg_replace(‘/^1512/’, ”, $number[$x]);
if($debug) $asm->verbose(“Remove 1 if 512 ” . $number[$x]);
if(preg_match(‘/^[2-46-9]/’, $number[$x]) > 0){
$number[$x] = “1″ . $number[$x];
}
if(strlen($number[$x] == 7)){
$number[$x] = “512″.$number[$x];
}
if($number_sum != “”) $number_sum = $number_sum . “&”;
$number_sum = $number_sum . “Zap/g1/” . $number[$x];
}
}
$x++;
}
if($number_sum == “”){
//IT Manager’s mobile number
$asm->stream_file(‘tts/ringing_it_manager’);
$log->lwrite(“Failed to fetch schedule, using 15555551313;”);
if($debug){
$number = ’15555551212′;
}else{
$number = ’15555551313′;
}
$asm->Verbose(“Failed to get number!\n”);
}
if($debug) $log->lwrite(“\nnumber_sum: $number_sum\n”);
$asm->verbose(“About to call $number_sum”);
//Have a number, now make a call
$channel=”Zap/g1/”;
$asm->stream_file(‘tts/ringing_it_oncall’);
$log->lwrite(“Calling “.$number_sum.”;”);
$asm->exec(‘Dial’,$number_sum.’|90′);
$call = $asm->get_variable(‘DIALSTATUS’);
$asm->Verbose(‘DIALSTATUS: ‘.$call);
if($call['data'] == ‘ANSWER’){
$log->lwrite(“Answered;\n”);
$asm->hangup();
}else{
$log->lwrite(“Not answered;”);
if($debug){
$number = ’15555551212′;
}else{
$number = ’15555551313′;
}
$asm->stream_file(‘tts/ringing_it_manager’);
$log->lwrite(“Calling “.$channel.$number.”;”);
$asm->exec(‘Dial’,$channel.$number.’|90′);
$call = $asm->get_variable(‘DIALSTATUS’);
$asm->Verbose(‘DIALSTATUS: ‘.$call);
if($call == ‘ANSWERED’){
$log->lwrite(“Answered;\n”);
$asm->hangup();
}else{
$log->lwrite(“No one was reached;\n”);
mail_admin($callerdid);
$asm->hangup();
}
}
}else{
$log->lwrite(“no on-call entries in calendar;”);
$channel=”Zap/g1/”;
if($debug){
$number = ’15555551212′;
}else{
$number = ’15555551313′;
}
$asm->stream_file(‘tts/ringing_it_manager’);
$log->lwrite(“Calling “.$channel.$number.”;”);
$asm->exec(‘Dial’,$channel.$number.’|90′);
$call = $asm->get_variable(‘DIALSTATUS’);
$asm->Verbose(‘DIALSTATUS: ‘.$call);
if($call == ‘ANSWERED’){
$log->lwrite(“Answered;\n”);
$asm->hangup();
}else{
$log->lwrite(“No one was reached;\n”);
mail_admin($callerdid);
$asm->hangup();
}
}
//***********************
// is_workhours_us()
function is_workhours_us(){
if($debug) return FALSE;
$workhours = array(’7′,’8′,’9′,’10′,’11′,’12′,’13′,’14′,’15′,’16′,’17′);
$workdays = array(‘Mon’,'Tue’,'Wed’,'Thu’,'Fri’);
$day=date(‘D’);
$hour=date(‘G’);
if(in_array($day,$workdays) && in_array($hour,$workhours)){
return TRUE;
}else{
return FALSE;
}
}
//***********************
// is_workhours_uk()
function is_workhours_uk(){
if($debug) return FALSE;
$workhours = array(’1′,’2′,’3′,’4′,’5′,’6′);
$workdays = array(‘Mon’,'Tue’,'Wed’,'Thu’,'Fri’);
$day=date(‘D’);
$hour=date(‘G’);
if(in_array($day,$workdays) && in_array($hour,$workhours)){
return TRUE;
}else{
return FALSE;
}
}
//**********************
// mail_admin($callerid)
function mail_admin($callerid){
$to = “admin@mydomain.com”;
$subject = “IT Hotline Missed Call”;
$message = “There was an unanswered call from $callerid to the IT Hotline.”;
$headers = “From: admin@mydomain.com”;
mail($to,$subject,$message,$headers);
}
?>