Wednesday, June 24, 2009

Getting the result of Netborder Call Progress Analyzer to an Asterisk variable

/* Parse contact header for continued conversation */
/* When we get 200 OK, we know which device (and IP) to contact for this call */
/* This is important when we have a SIP proxy between us and the phone */
if (outgoing) {
int cpdstart=0;
const char *cpdresult = __get_header(req,"CPD-Result",&cpdstart);

if (!ast_strlen_zero(cpdresult))
pbx_builtin_setvar_helper(p->owner, "CPDRESULT", cpdresult);
update_call_counter(p, DEC_CALL_RINGING);

Add the above bold code in chan_sip.c and you will get the Call Progress Detection result in asterisk Dial plan.

If you originate a call through AMI, with the dialplan,

exten => _8.,1,Answer
exten => _8.,2,Noop(${CPDRESULT})
exten => _8.,3,Echo


You will see in the asterisk console similar to this,


-- Executing [8dfgdfgdfgdfgdgdgdfgd@something:1] Answer("SIP/963258-10058d50", "") in new stack
-- Executing [8dfgdfgdfgdfgdgdgdfgd@something:2] NoOp("SIP/963258-10058d50", "Answering-Machine") in new stack
-- Executing [8dfgdfgdfgdfgdgdgdfgd@something:3] Echo("SIP/963258-10058d50", "") in new stack


In this case it is Answering-Machine.

Asterisk How to Capture Events and Handle it with PHP easily, works with all 1.2, 1.4 & 1.6

PHP makes the life easily when it comes to string parsing. Just enjoy to handle the events from asterisk.

#!/usr/bin/php -q

ob_implicit_flush(false);

$socket = fsockopen("hostname or ip","port", $errornum, $errorstr);
if(!$socket) {
print "Couldn't open socket. Error #" . $errornum . ": " . $errorstr;
} else {
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: username\r\n");
fputs($socket, "Secret: password\r\n\r\n");
fputs($socket, "Action: Events\r\n");
fputs($socket, "EventMask: on\r\n\r\n");

fgets($socket); // Ignore the welcome message

$change = false;
$agentid = NULL;
$agentchannel = NULL;
$login = NULL;

while(true)
{
while(!feof($socket) )
{

$readbuf = "";
$resp = fread($socket,8192);
$readbuf .= $resp;
$allevents = split("\r\n\r\n",$readbuf);
foreach($allevents as $event)
{
$eventdetails = split("\r\n",$event);
$event_assoc = "";
foreach($eventdetails as $value ) {
$namevalue = split(": ",$value);
$event_assoc[$namevalue[0]] = $namevalue[1];
}
switch( $event_assoc['Event'] )
{
case 'Link': // Calls getting bridged
break;

default:
if( !empty($event_assoc['Event']) )
print_r($event_assoc);
}
}
fputs($socket, "Action: Ping\r\n\r\n");
usleep(1000000);
}

}

}

?>