[Asterisk-Users] AGI ?

Jon Scottorn jscottorn at possibilityforge.com
Tue May 23 10:09:24 MST 2006


Hi All,

   I have been attempting to get an AGI LCRdialout script to work.
Basically what I need to have happen is when someone dials out a number
the script check to see if it is local if so, go out the ZAP channel. If
the ZAP channel is busy, go out the IAX channels, if IAX is all busy, go
out the SIP channels.  Here is a sample of what I have in my script.  


#!/usr/bin/perl

use strict;
use Asterisk::AGI;

my $callerId = "MyNumber";
my $log = "/var/log/asterisk/dialout.log";
my $gIaxUser = "MyIAXPROVIDER";
my $gIaxPass = "MYIAXPASS";
my $gIaxHost = "myIAXHost";
my $gSipHost = "MYSIPHost";

########### SYSTEM VARIABLES #####################
my $AGI = new Asterisk::AGI;
my $gDialedNo = shift;
my $gNumber = "";
my $gPrefix = "";
my $gAreaCode = "";

### Sets Outbound CallerID ###
$AGI->set_callerid($callerId);

open(LOG, ">>$log") or die("Couldn't open $log\n");

### Log Statement for Troubleshooting ###
#print LOG "(". localtime() .") Got Dialed number: $gDialedNo\n";

### Calls the function parseDialed to seperate the dialed number into
areacode, ###
### prefix, and number and assigns it to the num array ###
my @num = parseDialed($gDialedNo);
if(!@num) {
        ### Log Statement for Troubleshooting ###
        #print LOG "(". localtime() .") ERROR: Number not Valid!\n";

        ### If num is empty DIE ###
        return -1;
} else {
        ### Otherwise grab the items from the array ###
        $gNumber = pop(@num);
        $gPrefix = pop(@num);
        $gAreaCode = pop(@num);

        $gPrefix = pop(@num);
        $gAreaCode = pop(@num);

        ### Log Statement for Troubleshooting ###
        #print LOG "(". localtime() .") Set Areacode: $gAreaCode -
Prefix: $gPrefix - Number: $gNumber\n";
}

### Calls the function genDialString to create the actual dial string
depending on what was dialed ###
my $retDialStr = genDialString($gAreaCode,$gPrefix,$gNumber,$gIaxUser,
$gIaxPass,$gIaxHost,$gSipHost);
if(!$retDialStr) {
        ### Log Statement for Troubleshooting ###
        #print LOG "(". localtime() .") ERROR: No Dial String!\n";

        ### If DialString is empty then DIE ###
        return -1;
} else {
        ### Log Statement for Troubleshooting ###
        print LOG "(". localtime() .") Dialing: $retDialStr\n";

        ### Otherwise Dial the number over the specified channel ###
        $AGI->exec("$retDialStr");
        my $res = shift;

        print LOG "(". localtime() .") Result: $res\n";
}
close(LOG);
sub parseDialed {
        my $dialedNo = shift;
        my @number;

        ### If the dialed number is 12 digits parse into seperate
areacode, prefix, and number ###
        if(length($dialedNo) == 12) {
                push(@number, substr($dialedNo, 2, 3));
                push(@number, substr($dialedNo, 5, 3));
                push(@number, substr($dialedNo, 5));
                return @number;
        ### Else Assume it is in our areacode and grab the rest of the
digits. ###
        } elsif(length($dialedNo) == 8) {
                push(@number, "435");
                push(@number, substr($dialedNo, 1, 3));
                push(@number, substr($dialedNo, 1));
                return @number;
        ### Else assume it is an invalid number or not enough digits and
bread out fail safe ###
                return @number;
        ### Else assume it is an invalid number or not enough digits and
bread out fail safe ###
        } else {
                return @number;
        }
        ### Final catch all for invalid number ###
        return @number;
}
sub genDialString {
        my $areaCode = shift;
        my $prefix = shift;
        my $number = shift;
        my $iaxUser = shift;
        my $iaxPass = shift;
        my $iaxHost = shift;
        my $sipHost = shift;
        my $one = "1";
        my $dialStr = "";
        my $callStatus = "";
        my $channel = "";

        ### If they dialed toll free dial on local PSTN ###
        if($areaCode eq "800" || $areaCode eq "866" || $areaCode eq
"877" || $areaCode eq "888") {
                $dialStr = "Dial Zap/g2/$one$areaCode$number|25|TW";

                ### Log Statement for Troubleshooting ###
                #print LOG "(". localtime() .") Toll Free: $dialStr\n";

                return $dialStr;
        ### Else if it is a 435 areacode ###
        } elsif($areaCode eq "435") {
                ### If areacode is 435 and the prefix is local to us,
Dial out over ZAP/PSTN line ###
                if($prefix eq "215" || $prefix eq "229" || $prefix eq
"236" || $prefix eq "251" || $prefix eq "256" || $prefix eq "275" ||
                   $prefix eq "313" || $prefix eq "467" || $prefix eq
"619" || $prefix eq "627" || $prefix eq "628" || $prefix eq "632" ||
                   $prefix eq "634" || $prefix eq "635" || $prefix eq
"652" || $prefix eq "656" || $prefix eq "668" || $prefix eq "669" ||
                   $prefix eq "673" || $prefix eq "674" || $prefix eq
"680" || $prefix eq "688" || $prefix eq "703" || $prefix eq "705" ||
                   $prefix eq "772" || $prefix eq "773" || $prefix eq
"862" || $prefix eq "986") {
                        $dialStr = "Dial Zap/g2/$number|25|TW";

                        ### Log Statement for Troubleshooting ###
                        #print LOG "(". localtime() .") Local Call:
$dialStr\n";

                        return $dialStr;
                ### Else it is not local to us, Dial out over IAX
Provider ###
                } else {
                        $dialStr = "Dial IAX2/$iaxUser\:$iaxPass\@
$iaxHost/$one$areaCode$number|25|TW";

                        ### Log Statement for Troubleshooting ###
                        #print LOG "(". localtime() .") Non-Local Call:
$dialStr\n";

                        return $dialStr;
                }
        ### Else it is not local to us, Dial out over IAX Provider ###
        } else {
                $dialStr = "Dial IAX2/$iaxUser\:$iaxPass\@$iaxHost/$one
$areaCode$number|25|TW";

                ### Log Statement for Troubleshooting ###
                #print LOG "(". localtime() .") Non-Local Call: $dialStr
\n";

                return $dialStr;
        }
        ### Final catch all for invalid number ###
        return $dialStr;
}


Can someone give me some advice as to how to get a channel status of
what the $AGI just dialed.


Thanks in advance,
Jon Scottorn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digium.com/pipermail/asterisk-users/attachments/20060523/8cb0a7c7/attachment.htm


More information about the asterisk-users mailing list