Upgrade 3CX to v18 and get it hosted free!

PERL-ZAPINCOMMING-AGI

Author image

!/usr/bin/perl 
//  by imran 


use Asterisk::AGI; 
my $ModuelPath = "/var/www/cgi-bin/modules"; 
my $ivrPath = "/var/www/html/ivrSounds"; 
use lib "/var/www/cgi-bin/modules"; 

use Db; 

$AGI = new Asterisk::AGI; 
my %input = $AGI->ReadParse(); 
my $callerid="0"; 
$callerid= $input{callerid}; 
$|=1; 
$AGI->answer(); 

($calleridnum, $phoneno, $ZapChannel) = @ARGV; 


my $dial_up_to_seconds=30; 
my $dialstr; 
my $DialedNo; 
my $d_path = "/var/www/html/recordings"; 
my $gsm_file; 
my $wav_file; 
my %g_DATA; 
my $ChannalName=""; 
my $isExt; 
my $info; 
my $CALLSTATUS; 

if ( $calleridnum eq '' ) 
{ 
$calleridnum ='unknown'; 
} 

$ChannalName = $AGI->get_variable("CHANNEL"); 

$ChannalName : Zap/1-1 

$ChannalName =~ /Zap\/(.*?)-/gsi; 

my $lineno =$1; 

my $strSQL = " 
select is_ext,info from codetail where co_no='$lineno'"; 

my @Result = Db->ExecuteQuery($strSQL); 

if($Result10 eq "") 
{ 
$AGI->stream_file("$ivrPath/welcomt2ippx","0123456789"); 
sleep(3); 
exit; 
} 

$isExt =$Result10; 
$info =$Result11; 


when Extension are defined 
if($isExt) 
{ 
# in $info= "100023-100024" out $info= "100023&SIP/100024&SIP/" 
my @DATA = split (/-/,$info); 
for ( my $nIndex=0; $nIndex <$#DATA; $nIndex++ ) 
{ 
if ( $DATA$nIndex ne '' ) 
{ 
$DialedNo = $DATA$nIndex; 
last; 
} 
} 
if ( $DialedNo eq '' ) 
{ 
# extension is not defined 
sleep(3); 
exit; 
} 

%g_DATA=getAccessOfCalledExten($DialedNo); 

$info =~ s/\-/&SIP\//g; 
$dialstr = "SIP/$info|$dial_up_to_seconds|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 

} 

else 
{ 
my $strSQL = " 
select 0map,1map ,2map ,3map ,path from ivr where id='$info'"; 

my @Result = Db->ExecuteQuery($strSQL); 

if($Result14 eq "") 
{ 
$AGI->stream_file("$ivrPath/welcomt2ippx","0123456789"); 
sleep(3); 
exit; 
} 

$n0map =$Result10; 
$n1map =$Result11; 
$n2map =$Result12; 
$n3map =$Result13; 
$path =$Result14; 

# in = $path ="woman.gsm" out = $path = "woman"; 
$path =~ s/\..*$//g; 

for($i=0;$i<3;$i++) 
{ 
$DialedNo=""; 
$DialedNo= $AGI->get_data("$ivrPath/$path", 5000, 3); 

if($DialedNo eq "") 
{ 
if($i == 2){$DialedNo=$n0map;} 
else {next;} 
} 
elsif( $DialedNo ==0 ) 
{ 
$DialedNo=$n0map; 
} 
elsif($DialedNo ==1) 
{ 
$DialedNo=$n1map; 
} 
elsif($DialedNo ==2) 
{ 
$DialedNo=$n2map; 
} 
elsif($DialedNo ==3) 
{ 
$DialedNo=$n3map; 
} 
else 
{ 
if(!(length($DialedNo)==3)) 
{ 
next; 
} 
} 

%g_DATA=getAccessOfCalledExten($DialedNo); 
$dialstr = "SIP/".$DialedNo . "|$dial_up_to_seconds|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
last; 

} 
} 


if( $g_DATA{MINEXT} <= $DialedNo && $g_DATA{MAXEXT} >= $DialedNo ) 
{ 
#valid Extension 
$AGI->stream_file("pls-wait-connect-call"); 
} 
else 
{ 
#Sorry Invalid Extension 
$AGI->stream_file('you-dialed-wrong-number'); 
$AGI->hangup(); 
exit; 
} 

if ( $g_DATA{CALLRECORDING} eq 'Y' ) 
{ 
callrecording($DialedNo); 
} 

$res = $AGI->exec("DIAL $dialstr"); 

$CALLSTATUS = $AGI->get_variable(DIALSTATUS); 

if ( $CALLSTATUS ne 'ANSWER' && $CALLSTATUS ne 'ANSWERED') 
{ 

if ( $g_DATA{CALLFORWARDING} eq 'Y' ) 
{ 

$dialstr = callforwording(); 

if( $dialstr eq '' ) 
{ 
goto LABLE2; 
} 
$res = $AGI->exec("DIAL $dialstr"); 
$CALLSTATUS = $AGI->get_variable(DIALSTATUS); 
if ( $CALLSTATUS eq 'ANSWER' or $CALLSTATUS eq 'ANSWERED') 
{ 
goto LABLE; 
} 
} 

LABLE2: 

if ( $g_DATA{VOICEMAIL} eq 'Y' ) 
{ 
$res = $AGI->exec("VoiceMail u$DialedNo"); 
sleep(1); 
system("chmod +777 /var/spool/asterisk/voicemail/default/$phoneno/ -R"); 
} 
else 
{ 
$AGI->stream_file('nbdy-avail-to-take-call'); #disconnected #$AGI->stream_file('agent-loggedoff'); 
sleep(1); 
$AGI->stream_file('call-terminated'); 
sleep(1); 
$AGI->stream_file('thanks-for-using'); 
} 
} 
  
 LABLE: 
  
if ( $g_DATA{CALLRECORDING} eq 'Y' ) 
{ 
my $C = $AGI->get_variable(CHANNEL); 
my $t = $AGI->exec("StopMonitor $C"); 
$AGI->hangup(); 
sleep(3); 
system("sox $d_path/$wav_file -r 8000 $d_path/$gsm_file resample -ql"); 
} 
else 
{ 
$AGI->hangup(); 
} 

exit; 


sub myVerbose 
{ 
my ($pStr)=@_; 
return; 


$pStr =~ s/\n/ /ige; 

$AGI->verbose("$pStr\n", 3); 

return; 
} 

sub callrecording 
{ 
my ($phoneno) =@_; 
my $CalledNumber = $phoneno; 
my $CurDate; 

$CurDate = $AGI->get_variable("DATETIME"); 
$CurDate =~ s/://gsi; 
$CurDate=substr($CurDate,4,4) . substr($CurDate,2,2) .substr($CurDate,0,2) .substr($CurDate,8,7); 
$gsm_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.gsm"; 
$wav_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.WAV"; 
$res = $AGI->exec("Monitor WAV|$d_path/Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate|m"); 
} 

sub getAccessOfCalledExten 
{ 
my ( $ext )=@_; 
my %g_DATA; 
my $strSQL = " 
select 
call_recording, 
local_calling, 
international_calling, 
international_calling_ww35, 
international_calling_all, 
call_forwarding, 

extension_busy, 
extension_answered, 
extension_logged, 

caller_id, 
summary_call, 
call_waiting, 
voice_mail, 
seconds, 
evoice 

from buddies_relation 
where name=\"$ext\" 
"; 
my @Result = Db->ExecuteQuery($strSQL); 

$g_DATA{CALLRECORDING} = $Result10; 
$g_DATA{LOCALCALLING} = $Result11; 
$g_DATA{INTCALL} = $Result12; 
$g_DATA{INTCALLWW35} = $Result13; 
$g_DATA{INTCALLALL} = $Result14; 
$g_DATA{CALLFORWARDING}= $Result15; 
$g_DATA{EXTBUAY} = $Result16; 
$g_DATA{EXTANSWER} = $Result17; 
$g_DATA{EXTLOG} = $Result18; 

$g_DATA{CALLERID} = $Result19; 
$g_DATA{SUMMARYCALL} = $Result110; 
$g_DATA{CALLWAITING} = $Result111; 
$g_DATA{VOICEMAIL} = $Result112; 
$g_DATA{SECONDS} = $Result113; 

if ( $g_DATA{SECONDS} ne '' && $g_DATA{VOICEMAIL} eq 'Y') 
{ 
$dial_up_to_seconds =$g_DATA{SECONDS}; 
} 

my @r = Db->ExecuteQuery("select min(name), max(name) from sip_buddies"); 
$g_DATA{MINEXT} = $r10; 
$g_DATA{MAXEXT} = $r11; 

return %g_DATA; 

} 



NOTE: this function needs your customiezed call forwading functionality depends call status BUSY, NOANSWER, NOAVAILABLE etc 

sub callforwording 
{ 
my $dial=""; 

if ( $CALLSTATUS eq 'BUSY' ) 
{ 
if ( $g_DATA{MINEXT} <= $g_DATA{EXTBUAY} && $g_DATA{MAXEXT} >= $g_DATA{EXTBUAY} ) 
{ 
# make here SIP/IAX/ZAP dailstring according to the dype of $g_DATA{EXTBUAY} number 

if( it is sip call ) 
{ 
$dial = = "SIP/" . $g_DATA{EXTBUAY} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( it is sip iax call ) 
{ 
$dial = = "IAX2/admin:admin123\@203.103.65.2158c4a440fa8888b84abf019118cd912/".$g_DATA{EXTBUAY} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( it is zap call ) 
{ 
$dial = = "ZAP/g1/" . $g_DATA{EXTBUAY} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 
return $dial; 
} 

return $dial; 
} 


if ( $CALLSTATUS eq 'NOANSWER' ) 
{ 
if ( $g_DATA{MINEXT} <= $g_DATA{EXTANSWER} && $g_DATA{MAXEXT} >= $g_DATA{EXTANSWER} ) 
{ 
# make here SIP/IAX/ZAP dailstring according to the dype of $g_DATA{EXTANSWER} number 

if( apply checks here if it is sip call ) 
{ 
$dial = = "SIP/" . $g_DATA{EXTANSWER} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( apply checks here if it is sip iax call ) 
{ 
$dial = = "IAX2/admin:admin123\@203.103.65.2158c4a440fa8888b84abf019118cd912/" . $g_DATA{EXTANSWER} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( apply checks here if it is zap call ) 
{ 
$dial = = "ZAP/g1/" . $g_DATA{EXTANSWER} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 
return $dial; 
} 

return $dial; 
} 

if ( $CALLSTATUS eq 'NOAVAILABLE' ) 
{ 
if ( $g_DATA{MINEXT} <= $g_DATA{EXTLOG} && $g_DATA{MAXEXT} >= $g_DATA{EXTLOG} ) 
{ 
# make here SIP/IAX/ZAP dailstring according to the dype of $g_DATA{EXTLOG} number 

if( apply checks here if it is sip call ) 
{ 
$dial = = "SIP/" . $g_DATA{EXTLOG} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( apply checks here if it is sip iax call ) 
{ 
$dial = = "IAX2/admin:admin123\@203.103.65.2158c4a440fa8888b84abf019118cd912/" . $g_DATA{EXTLOG} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 

else if( apply checks here if it is zap call ) 
{ 
$dial = = "ZAP/g1/" . $g_DATA{EXTLOG} . "|20|HL(" . (100 * 60 * 1000) . ":60000:30000)|tr"; 
} 
return $dial; 
} 

return $dial; 
} 

return $dial; 
}


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.