Integration with external applications using AGI

Recently we had a customer that requested for integration with their CRM web application (based on .NET framework and MSSQL). They wanted to get the caller id of the caller and the number which they have called. Here is a simple approach to get this done. In this example, I have used AGI to get the CLID and channel number and then I use the cURL method of PHP to POST the 2 parameters to the URL on the other side.


Step1- Create a new extension in /etc/asterisk/extensions_custom.conf

[custom-passCLID]

exten => s,1,NoOp(CallerID is ${CALLERID(num)})
exten => s,n,AGI(pass_clid.php)
exten => s,n,goto(ivr-9,s,1)

Step2- cd /var/lib/asterisk/agi-bin
Step3- touch pass_clid.php
step4- chown asterisk:asterisk pass_clid.php
step5- chmod 775 pass_clid.php
step6- This is the content of pass_clid.php

#!/usr/bin/php -q
<?php

require_once "phpagi.php";

$AGI = new AGI();

$cidnum  = $AGI->request['agi_callerid'];
$cidname = $AGI->request['agi_calleridname'];
$chan    = $AGI->request['agi_channel'];
$slashpos= strpos('z'.$chan,'/');
$dashpos = strpos('z'.$chan,'-') - 1;

$chanel  = substr($chan,$slashpos,$dashpos-$slashpos);

if (is_numeric($chanel))
{
switch ($chanel) {
case '1':
$chan_line = "0378037098";
break;
case '2':
$chan_line = "0162103949";
break;
case '3':
$chan_line = "0162103949";
break;
default :
}
debug("****************************************************************");
debug("CALLID: $cidnum | chan: $chan | Chan_No: $chanel | Chan_Line: $chan_line");
$whois = passID($cidnum,$chan_line);
debug("Name: $whois | callerId: $cidnum");
debug("****************************************************************");

}
else debug("$chan is not a dahdi channel");

exit( 0 );

function debug($string, $level=3) {
global $AGI;
$AGI->verbose($string, $level);
}

function passID($callid,$chan_line){
$CONNECT = curl_init();
$url = "http://192.168.1.168/ibmsHMS-v3/views/caller/process_callers.php";

$post_str = "callerid=$callid&chan_line=$chan_line";
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_str,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30
);

curl_setopt_array($CONNECT, $options);

if($tmp = curl_exec($CONNECT)){
return $tmp;
}
else return 'Curl error: '.curl_error($CONNECT);
curl_close($CONNECT);
}
?>

Step7- in PBX go to Tools > custom destinations, add a custom destination and set it as below:



Step8- in PBX go to setup > inbound routes. select each channel and set the destination to custom context:




Step9- Go to asterisk CLI, make a call and see if the AGI runs properly. it should be something like this:


 
 

You may also like