Search:
     
3CX Phone System for Windows Download the Free Edition

PERL-ZAPOUTGOING-AGI

#!/usr/bin/perl
// zap call outging by imran ( Pakistan )

use Asterisk::AGI;
use lib "/var/www/cgi-bin/modules";
use Db;

$AGI = new Asterisk::AGI;
$|=1;
my %input = $AGI->ReadParse();
($calleridnum, $phoneno, $quiet) = @ARGV;

$phoneno =~ s/^9//gsi;

my @Result=Db->ExecuteQuery("select co_no,outgoing_prefix from codetail");
my %CoPrHash;
my $ZapStr;
my $g_Cents;
my $d_path = "/var/www/html/recordings";
my $gsm_file;
my $wav_file;
my $CALLSTATUS;
my $callstart;
my $answeredtime;
my %g_DATA;
my $AvailChan;
my $Co;
my $prefix;

// NOTE DIAL STRING SHOULD BE AS >>>>>/zap/1&Zap/2&Zap

for($i=1;$i<=$#Result;$i++)
{
$Co =$Result$i0;
$prefix =$Result$i1;

$CoPrHash{$Co} = $prefix;

$ZapStr .= "Zap/$Co";
if($i != $#Result)
{
$ZapStr .= "&" ;
}

}

$res = $AGI->exec("ChanIsAvail $ZapStr");

$AvailChan = $AGI->get_variable("AVAILCHAN");


if($AvailChan !~ /Zap/gsi)
{
$AGI->stream_file('all-circuits-busy-now');
exit;
}

my ($OrderId,$callrate) = setup_billing($calleridnum);
if ( $OrderId eq '-1')
{
#No Talk time balance in ur account
$AGI->stream_file('not-enough-credit'); #disconnected
$AGI->hangup();
exit;
}


$AvailChan =~s/-(.*)$//gsi;

$dialstr = "$AvailChan/" . $CoPrHash{$LineNo} . $phoneno ;


  1. .... gloable variable
my $strSQL = "
select
call_recording,
local_calling,
international_calling,
international_calling_ww35,
international_calling_all,
call_forwarding,

extension_busy,
extension_answered,
extension_logged

from buddies_relation
where name=\"$calleridnum\"
";
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;

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


if( ( $g_DATA{LOCALCALLING} eq 'Y' ) && not( $g_DATA{MINEXT} <= $phoneno && $g_DATA{MAXEXT} >= $phoneno ) )#local calling
{
if ( $g_DATA{CALLRECORDING} eq 'Y' )
{
callrecording($phoneno);
}

# execute local call
$res = $AGI->exec("DIAL $dialstr");

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

if ( not( $CALLSTATUS eq 'ANSWER' || $CALLSTATUS eq 'ANSWERED' ) )
{
# the number u have dialed is not responding please call later
$AGI->stream_file('pls-try-call-later');

}
else
{
$answeredtime = $AGI->get_variable(ANSWEREDTIME);
#savecdr($calleridnum, $phoneno, "", $CALLSTATUS, $answeredtime, "",$callstart,"");
#CutBalance($OrderId,$callrate,$answeredtime);
}

if ( $g_DATA{CALLRECORDING} eq 'Y' )
{
my $C = $AGI->get_variable(CHANNEL);
my $t = $AGI->exec("StopMonitor $C");
$AGI->hangup();
sleep(2);
system("sox $d_path/$gsm_file $d_path/$wav_file");
}
}
else
{
# sorry you r not authoriezed to have a local call
$AGI->stream_file('not-auth-pstn');
$AGI->hangup();
}

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

$callstart= currentDbDate();
$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 gsm|$d_path/Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate|m");
#system("sox $d_path/$wav_file -r 8000 $d_path/$gsm_file resample -ql");
#$AGI->verbose("imran sox = ( sox $d_path/$wav_file -r 8000 $d_path/$gsm_file )\n",3);
}

sub currentDbDate
{

my $strSQL = "
select curdate()

";

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

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

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

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

}

sub savecdr()
{
my ($callerid, $callednum, $trunk, $disposition, $billseconds, $billcost,$callstart,$resellerrate) = @_;
my $strSQL = "
INSERT INTO cdrs (callerid,callednum,trunk,disposition,billseconds,billcost,callstart,resellerrate) VALUES
(
\"$callerid\",
\"$callednum\",
\"$trunk\",
\"$disposition\",
\"$billseconds\",
\"$billcost\",
\"$callstart\",
\"$resellerrate\"
)
";
myVerbose($strSQL);

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

}

sub setup_billing
{
my ( $Exten ) =@_;

my @Result = Db->ExecuteQuery("select id,talktime from buddies_relation where name='$Exten'");
my $Cents = $Result11;
$g_Cents = $Result11;
if($Cents<=0)
{
$Cents=0;
return ('-1','-1');
}

my $bal=$Cents/100;
my $DollarBal =sprintf("%d",$bal);
my $CentsBal =sprintf("NaVd",$bal*100)%100));
my $Totalcents =$DollarBal*100 + $CentsBal;

my %Hash;

$Hash{Rates} =0.002;

my $totalCents =$Totalcents ;
my $callrate =sprintf("%d",$Hash{Rates}*100);
my $totalMinutes=sprintf("%d",$totalCents/$callrate);
my $totalSeconds=sprintf("NaV$callrate)/$callrate)*60;


return ($Result10,$callrate);
}

sub CutBalance
{

my ($id,$Rate,$CallSec) = @_;
my $min=sprintf("%d",$CallSec/60);
my $Sec=sprintf("NaV60);
if($Sec>0)
{
$min++;
}

my $Cut= $min*$Rate;

my $amount = $g_Cents - $Cut;

my $strSQL = "Update buddies_relation set talktime =\"$amount\" where id=\"$id\" ";

Db->ExecuteQuery($strSQL);
}

Created by: ddx,Last modification on Fri 26 of Aug, 2005 [07:39 UTC]


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - support@voip-info.org

Page Changes | Comments

 





Search: