digium switchvox is nifty GIU software that runs on top of asterisk, an open source PBX. you can download it for free here: www1.digium.com/en/products/switchvox/support/switchvox-home-edition asterisk
once switchvox is up and running, you can use their admin control panel to view reports of usage. here’s the cool thing– if you use firebug and click on the net pane you can see the ajax JSON calls from those reports. turn the JSON into XML and you can call those very same API functions to build your own reporting tool.
here are some helpful digium API links:
http://developers.digium.com/switchvox/wiki/index.php/WebService_methods
http://developers.digium.com/switchvox/?pageView=phpLibrary
the first thing you’ll notice about their php API is they have a send method, but it’s a little wonky with nested arrays, so you’ll want to add your own that accepts straight XML: (in SwitchvoxRequest.php):
public function XMLsend($in_method, $postBody) { $request_options = array ( 'httpauthtype' => HTTP_AUTH_DIGEST, 'httpauth' => $this->username.":".$this->password, 'headers' => array ( "Content-Type" => "application/xml" ) ); $request = new HttpRequest($this->uri, HTTP_METH_POST, $request_options); $request->setRawPostData($postBody); $response = $request->send(); return new SwitchvoxResponse($request); }
now for some helpful scripts:
1- get all extensions:
<?php require_once("SwitchvoxRequest.php"); $request = new SwitchvoxRequest("switchvoxip", "username", "password"); $requestParams = array( 'sort_field' => 'extension', 'sort_order' => 'ASC', 'items_per_page' => '400', 'page_number' => '1'); $response = $request->send("switchvox.extensions.getVoicemailInfo",$requestParams); $responseArray=(array)$response; $arrayKeys=array('apiStatus','apiErrors','apiResult','rawXML'); $index=0; foreach ($responseArray as $key=>$value) { $responseCleanedArray[$arrayKeys[$index++]]=$value; } echo('<pre>'); foreach($responseCleanedArray['apiResult']['extensions']['extension'] as $extension) { echo($extension['number'].' ['.$extension['type'].'] - '.$extension['first_name'].' '.$extension['last_name'].' account_id: '.$extension['account_id']."\n"); } echo('</pre>');
and this one lists all queues:
<?php require_once("SwitchvoxRequest.php"); $request = new SwitchvoxRequest("switchvoxip", "username", "password"); $requestParams = array( ); $response = $request->send("switchvox.extensions.search",$requestParams); $responseArray=(array)$response; $arrayKeys=array('apiStatus','apiErrors','apiResult','rawXML'); $index=0; foreach ($responseArray as $key=>$value) { $responseCleanedArray[$arrayKeys[$index++]]=$value; } echo('<pre>LIST OF CALL QUEUES'); foreach($responseCleanedArray['apiResult']['extensions']['extension'] as $extension) { if ($extension['type']=='call_queue') { echo("\n".$extension['number'].' ' .$extension['call_queue_name'].' account_id: '.$extension['account_id']); } } echo('</pre>');
and finally a nifty web script to list how long your customer service reps talk on the phone. it uses our XMLsend function we added above:
<html><head><title>time on phone</title></head><body> <h1>time on phone</h1> <?php require_once("include/SwitchvoxRequest.php"); echo('<form action="'.$_SERVER['PHP_SELF'].'" method="get">'); $personsOfInterest=array('CS_REP1'=>'1001','CS_REP2'=>'1002','CS_REP3'=>'1003'); $invertedPersons=array_flip($personsOfInterest); echo('person(s): <select multiple="multiple" name="persons[]" size='.count($personsOfInterest).'>'); foreach($personsOfInterest as $persons=>$account_id) { $selected=in_array($account_id,$_REQUEST['persons'])?'selected="selected"':''; echo('<option '.$selected.' value="'.$account_id.'">'.$persons.'</option>'."\n"); } ?> </select><br /> start date: <input type="text" name="start_date" value="<?php echo($_REQUEST['start_date']); ?>"/><i>(format YYYY-MM-DD)</i><br /> end date: <input type="text" name="end_date" value="<?php echo($_REQUEST['end_date']); ?>"/><br /> <input type="submit" name="submit" value="run report" /> </form> <?php function getCallStats($account_id,$start_date,$end_date) { $request2 = new SwitchvoxRequest("switchvoxip", "username", "password"); $xmlRequest='<?xml version="1.0"?> <request method="switchvox.callReports.search"> <parameters> <start_date>'.$start_date.' 00:00:00</start_date> <end_date>'.$end_date.' 23:59:59</end_date> <breakdown>by_day</breakdown> <type>phones</type> <items_per_page>9999</items_per_page> <report_fields> <report_field>total_calls</report_field> <report_field>total_incoming_calls</report_field> <report_field>total_outgoing_calls</report_field> <report_field>talking_duration</report_field> <report_field>call_duration</report_field> <report_field>avg_talking_duration</report_field> <report_field>avg_call_duration</report_field> </report_fields> <ignore_weekends>1</ignore_weekends> <account_ids> <account_id>'.$account_id.'</account_id> </account_ids> </parameters> </request>'; $response2 = $request2->XMLsend("switchvox.callReports.search",$xmlRequest); $responseArray2=(array)$response2; $arrayKeys2=array('apiStatus','apiErrors','apiResult','rawXML'); $index2=0; foreach ($responseArray2 as $key=>$value) { $responseCleanedArray2[$arrayKeys2[$index2++]]=$value; } echo('<h3>Summary Stats</h3>'); $summaryFields=array('total_calls','total_incoming_calls','total_outgoing_calls','talking_duration','call_duration','avg_calls','avg_incoming','avg_outgoing','avg_talk_time','avg_call_time','avg_talk_time_per_call','avg_call_time_per_call'); echo('<table border="1" cellpadding="1" cellspacing="1"><tr>'); foreach($summaryFields as $field) { echo('<th>'.$field.'</th>'); } echo('</tr><tr>'); foreach($summaryFields as $field) { echo('<td>'.$responseCleanedArray2['apiResult']['rows'][$field].'</td>'); } echo('</tr></table>'); echo('<h3>Detail Stats</h3>'); $detailFields=array('date','total_calls','total_incoming_calls','total_outgoing_calls','talking_duration','call_duration','avg_talking_duration','avg_call_duration'); echo('<table border="1" cellpadding="1" cellspacing="1"><tr>'); foreach($detailFields as $field) { echo('<th>'.$field.'</th>'); } echo('</tr>'); foreach($responseCleanedArray2['apiResult']['rows']['row'] as $row) { echo('<tr>'); foreach($detailFields as $field) { echo('<td>'.$row[$field].'</td>'); } echo('</tr>'); } echo('</table>'); } if (isset($_REQUEST['submit'])) { foreach ($_REQUEST['persons'] as $account_id) { echo('<h2>'.$invertedPersons[$account_id].'</h2>'); getCallStats($account_id,$_REQUEST['start_date'],$_REQUEST['end_date']); } }
enjoy.