从Asterisk Dialplan传递变量到AGI perl脚本



我试图通过一个变量从星号拨号计划到使用AGI的perl脚本。我现在对此感到非常困惑,这到底是如何工作的。现在在我的拨号计划中,我有这个:

exten=>1122,1,Answer
exten=>1122,n,Read(digit)
exten=>1122,n,agi(/home/steve/Desktop/testperlping.pl,${digit})
exten=>1122,n,Hangup()

我想要一个用户拨分机1122,然后输入1-10的数字,并使用AGI将他们输入的数字传递给perl。

我的perl脚本如下:
#!/usr/bin/perl -w
$|=1;
use Net::Ping;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
***I think I need something here***
$AGI->verbose("$numbertheytypedintophone"); #This will display the entered number back to the CLI.

我正在使用Asterisk::FastAGI(推荐用于perl集成)和代码看起来像这样:

my $dst = $self->param('dst');

对于Asterisk::AGI它已经是(我刚刚读AGI。点来源):

my $digits=$input{'arg_1'};

我强烈建议你阅读模块的源代码,如果你有任何问题,将会快得多。

考虑作为dialplan的一部分

exten => _X.,1,NoOp(${PJSIP_HEADER(read,To)})
exten => _X.,n,Set(FirstTo=${CUT(PJSIP_HEADER(read,To),:,2)})
exten => _X.,n,Set(TO_DID=${CUT(FirstTo,@,1)})
exten => _X.,n,Set(client_number=${CALLERID(num)})
exten => _X.,n,AGI(check_nr.agi,,${client_number})
exten => _X.,n,Set(First_check=${first_ani})

注意agi脚本上的双逗号

使用client_number值的脚本部分

  (my $empty_field,  my $data ) = @ARGV;
   my @data_array = split /--/, $data;                  
   my $callerid   = $data_array[0]; 

分割部分是如果你有多个参数,如

AGI(check_nr.agi,,${client_number}--${second}--${third}) 

在这种情况下是

  (my $empty_field,  my $data ) = @ARGV;
   my @data_array = split /--/, $data;                  
   my $callerid   = $data_array[0]; 
   my $second     = $data_array[1]; 
   my $third      = $data_array[2]; 

要将变量传递回星号dialplan,您应该使用:

print "SET VARIABLE first_ani $first_anin"; 

完整的perl脚本

#!/usr/bin/perl
# Modules to use
use strict ;
use warnings;
use Config::IniFiles;
use DBI;
use sigtrap;
use sigtrap qw(handler my_handler normal-signals stack-trace error-signals);

                   my $cfg = Config::IniFiles -> new( -file => '/etc/my_file.ini' );
                   my $dbinst = $cfg -> val('AST', 'DBINST');
                   my $dbhost = $cfg -> val('AST', 'DBHOST');
                   my $dbuser = $cfg -> val('AST', 'DBUSER');
                   my $dbpass = $cfg -> val('AST', 'DBPASS');
                   
                   my $dbhA  = DBI->connect( "dbi:mysql:$dbinst:$dbhost",$dbuser,$dbpass, {RaiseError => 0,PrintError => 1,AutoCommit => 0,}) or die $DBI::errstr;     

########################################### Pass data from asterisk channel to perl script ##########################################
  (my $empty_field,  my $data ) = @ARGV;
   my @data_array = split /--/, $data;                  
   my $callerid   = $data_array[0];
########################################### Pass data from asterisk channel to perl script ##########################################

        my $sthA = $dbhA->prepare( "select  ani,
                                            numero,
                                            data_ultima_modifica
                                    from my_table
                                    where numero = ?
                                    ORDER BY data_ultima_modifica DESC "); 
           $sthA->bind_param( 1, $callerid ); 
           $sthA->execute();
        my $sthArows=$sthA->rows;
            if ($sthArows > 0) 
            {
              my @aryA                  = $sthA->fetchrow_array;
              my $ani                   = $aryA[0];
              my $numero                = $aryA[1];
              my $data_ultima_modifica  = $aryA[2];
            
                   print "SET VARIABLE first_ani  $first_ani n";
             }
            $sthA->finish();    

 
################## Close DB Connection##################
                warn $DBI::errstr if $DBI::err;
                $dbhA->commit(); 
                $dbhA->disconnect();
################## Close DB Connection##################

相关内容

  • 没有找到相关文章

最新更新