Many thanks to J.P. for sending us this PHP script and enabling us to share it with our community.

Essentially this little script enables anyone using Twilio to easily integrate Humbug Analytics and make use of our telecom reporting and fraud detection platform. Twilio already comes with neat statistics built-in, but sometimes you may be looking for a dedicated reporting platform that provides you with the drill-down capabilites and customizations that you need to better understand your traffic. For example, you may want to keep tabs on the amount of calls/duration that each of your inbound phone numbers receive per day for marketing metrics, or you may want to receive an alert if we find that your callers are dialing destinations that appear on our community blacklist.

To setup, make sure that your Twilio application sends “Status Callback URL” requests to the function below, then make sure you modify the “gateway” parameter to be one of the API Keys you received from Humbug. As always, obtaining a Humbug API key is free and can be used for upto 2,500 calls/month for free.

 

<?php
function Twilio_status_callback(){
/*
 * a status callback $_REQUEST arrives here
 */

$call_disposition = $_REQUEST['CallStatus'];
switch ($call_disposition){
	case 'completed':
	        $call_disposition = "ANSWER";
	        break;
	case 'busy':
        	$call_disposition = "BUSY";
	        break;
    	case 'failed':
        	$call_disposition = "FAILED";
	        break;
	case 'no-answer':
		$call_disposition = "NO ANSWER";
       		break;
	default: //call is still in progress
		return;
}

$end_time     = time(); //time of hangup
$answer_time  = strtotime('-'.$_REQUEST['CallDuration'].' seconds',$end_time); //actual talk time
$start_time   = strtotime('-'.$_REQUEST['Duration'].' minutes',$end_time); //call duration in minutes from twilio billing 

//prepare the Humbug CDR request
$array = array (
	'gateway'            => 'your-humbug-apikey-goes-here',
	'encoded'            => '0',
	'Event'              => 'Cdr',
	'Source'             => $_REQUEST['From'],
	'Destination'        => $_REQUEST['To'],
	'DestinationContext' => 'twilio-inbound',
	'CallerID'           => $_REQUEST['Caller'],
	'Channel'            => 'Twilio/Twilio-inbound',
	'DestinationChannel' => 'Twilio/Twilio-inbound',
	'LastApplication'    => '',
	'LastData'           => '',
	'StartTime'          => date('Y-m-d H:i:s',$start_time),
	'AnswerTime'         => date('Y-m-d H:i:s',$answer_time),
	'EndTime'            => date('Y-m-d H:i:s',$end_time),
	'Duration'           => $_REQUEST['Duration']*60, //converted to seconds 
	'BillableSeconds'    => $_REQUEST['CallDuration'],
	'Disposition'        => $call_disposition,
	'UniqueID'           => $_REQUEST['CallSid'],
	'UserField'          => '',
	'event_time'         => date('Y-m-d h:i:s',time())
);
 
//turn the array into a key=value string 
$params    = "";
foreach($array as $key => $value) {
$params .= $key.'='.$value.'&';
}
rtrim($params,'&');

$url              = 'http://api.humbuglabs.org/receiver.php';
 
//send the data
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($array));
curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
}
?>

Have more integration ideas? Share your code with us at support@humbuglabs.org