asterisk agi For Perl
?转自网络,备忘: web link:http://astbook.asteriskdocs.org/en/2nd_Edition/asterisk-book-html-chunk/asterisk-CHP-9-SECT-2.html
? Writing AGI Scripts in PerlAsterisk comes with a sample AGI script called agi-test.agi. Let’s step through the file while we cover the core concepts of AGI programming. While this particular script is written in Perl,please remember that your own AGI programs may be written in almost any programming language. Just to prove it,we’re going to cover AGI programming in a couple of other languages later in the chapter. Let’s get started! We’ll look at each section of the code in turn,and describe what it does: #!/usr/bin/perl This line tells the system that this particular script is written in Perl,so it should use the Perl interpreter to execute the script. If you’ve done much Linux or Unix scripting,this line should be familiar to you. This line assumes,of course,that your Perl binary is located in the /usr/bin/ directory. Change this to match the location of your Perl interpreter. use strict;
$|=1; This line tells Perl not to buffer its output—in other words,that it should write any data immediately,instead of waiting for a block of data before outputting it. You’ll see this as a recurring theme throughout the chapter. # Set up some variables my %AGI; my $tests = 0; my $fail = 0; my $pass = 0; WarningYou should always use unbuffered output when writing AGI scripts. Otherwise,your AGI may not work as expected,because Asterisk may be waiting for the output of your program,while your program thinks it has sent the output to Asterisk and is waiting for a response. Here,we set up four variables. The first is a hash called while(<STDIN>) { chomp; last unless length($_); if (/^agi_(w+):s+(.*)$/) { $AGI{$1} = $2; } } As we explained earlier,Asterisk sends a group of variables to the AGI program at startup. This loop simply takes all of these variables and stores them in the hash named print STDERR "AGI Environment Dump:n"; foreach my $i (sort keys %AGI) { print STDERR " -- $i = $AGI{$i}n"; } This loop simply writes each of the values that we stored in the sub checkresult { my ($res) = @_; my $retval; $tests++; chomp $res; if ($res =~ /^200/) { $res =~ /result=(-?d+)/; if (!length($1)) { print STDERR "FAIL ($res)n"; $fail++; } else { print STDERR "PASS ($1)n"; $pass++; } } else { print STDERR "FAIL (unexpected result '$res')n"; $fail++; } This subroutine reads in the result of an AGI command from Asterisk and decodes the result to determine whether the command passes or fails. Now that the preliminaries are out of the way,we can get to the core logic of the AGI script: print STDERR "1. Testing 'sendfile'..."; print "STREAM FILE beep ""n"; my $result = <STDIN>; &checkresult($result); This first test shows how to use the You will notice that the second argument is passed by putting in a set of double quotes,escaped by backslashes. Without the double quotes to indicate the second argument,this command does not work correctly. WarningYou must pass all required arguments to the AGI commands. If you want to skip a required argument,you must send empty quotes (properly escaped in your particular programming language),as shown above. If you don’t pass the required number of arguments,your AGI script will not work. You should also make sure you pass a line feed (the After sending the
In short,this test told Asterisk to play back the file named beep.gsm,and then it checked the result to make sure the command was successfully executed by Asterisk. print STDERR "2. Testing 'sendtext'..."; print "SEND TEXT "hello world"n"; my $result = <STDIN>; &checkresult($result); This test shows us how to call the The print STDERR "3. Testing 'sendimage'..."; print "SEND IMAGE asterisk-imagen"; my $result = <STDIN>; &checkresult($result); This test calls the print STDERR "4. Testing 'saynumber'..."; print "SAY NUMBER 192837465 ""n"; my $result = <STDIN>; &checkresult($result); This test sends Asterisk the
Again,since we’re not passing in any digits as the second argument,we need to pass in an empty set of quotes. print STDERR "5. Testing 'waitdtmf'..."; print "WAIT FOR DIGIT 1000n"; my $result = <STDIN>; &checkresult($result); This test shows the print STDERR "6. Testing 'record'..."; print "RECORD FILE testagi gsm 1234 3000n"; my $result = <STDIN>; &checkresult($result); This section of code shows us the
In this particular case,we’re recording a file called testagi (in the GSM format),with any of the DTMF digits 1 through 4 terminating the recording,and a maximum recording time of 3,000 milliseconds. print STDERR "6a. Testing 'record' playback..."; print "STREAM FILE testagi ""n"; my $result = <STDIN>; &checkresult($result); The second part of this test plays back the audio that was recorded earlier,using the print STDERR "================== Complete ======================n"; print STDERR "$tests tests completed,$pass passed,$fail failedn"; print STDERR "==================================================n"; At the end of the AGI script,a summary of the tests is printed to In summary,you should remember the following when writing AGI programs in Perl:
? Perl agi functions: ? 现将perl agi的模块转进来,以供日后参考。 以下为原文: Asterisk perl agi Asterisk::AGI perl module documentation NAME Asterisk::AGI – Simple Asterisk Gateway Interface Class SYNOPSIS use Asterisk::AGI; $AGI = new Asterisk::AGI; # pull AGI variables into %input %input = $AGI->ReadParse(); # say the number 1984 $AGI->say_number(1984); DESCRIPTION This module should make it easier to write scripts that interact with the asterisk open source pbx via AGI (aster???isk gateway interface) CALLBACKS Callbacks provide a handy way receiving events like hangups. To use the callback function,simply define a function to handle the callback and then tell Perl AGI which funtion it is by sending it a reference. Here is an example: use Asterisk::AGI; # the AGI object my $agi = new Asterisk::AGI; # send callback reference $agi->setcallback(&;callback); # our callback function sub callback(){ warn “The call has endedn”; set_context($context); exit; } AGI COMMANDS * $AGI->stream_file($filename,$digits) Executes AGI Command “STREAM FILE $filename $digits” This command instructs Asterisk to play the given sound file and listen for the given dtmf digits. The fileextension must not be used in the filename because Asterisk will find the most appropriate file type. Example: $AGI->stream_file(‘demo-echotest’,’0123′); Returns: -1 on error or hangup,0 if playback completes without a digit being pressed,or the ASCII numerical value of the digit if a digit was pressed. * $AGI->send_text($text) Executes AGI Command “SEND TEXT “$text” Sends the given text on a channel. Most channels do not support the transmission of text. Example: $AGI->send_text(‘You’ve got mail!’); Returns: -1 on error or hangup,0 if the text was sent or if the channel does not support text transmission. * $AGI->send_image($image) Executes AGI Command “SEND IMAGE $image Sends the given image on a channel. Most channels do not support the transmission of images. Example: $AGI->send_image(‘image.png’); Returns: -1 on error or hangup,0 if the image was sent or if the channel does not support image transmission. * $AGI->say_number($number,$digits) Executes AGI Command “SAY NUMBER $number $digits” Says the given $number,returning early if any of the $digits are received. Example: $AGI->say_number(’98765′); Returns: -1 on error or hangup,or the ASCII numerical value of the digit of one was pressed. * $AGI->say_digits($number,$digits) Executes AGI Command “SAY DIGITS $number $digits” Says the given digit string $number,returning early if any of the $digits are received. Example: $AGI->say_digits(’8675309′); Returns: -1 on error or hangup,or the ASCII numerical value of the digit of one was pressed. * $AGI->answer() Executes AGI Command “ANSWER” Answers channel if not already in answer state. Example: $AGI->answer(); Returns: -1 on channel failure,or 0 if successful. * $AGI->get_data($filename,$timeout,$maxdigits) Executes AGI Command “GET DATA $filename $timeout $maxdigits” Streams $filename and returns when $maxdigits have been received or when $timeout has been reached. Timeout is specified in ms. Example: $AGI->get_data(‘demo-welcome’,15000,5); * $AGI->set_callerid($number) Executes AGI Command “SET CALLERID $number” Changes the callerid of the current channel to <number>. Example: $AGI->set_callerid(’9995551212′); Returns: Always returns 1. * $AGI->set_context($context) Executes AGI Command “SET CONTEXT $context” Changes the context for continuation upon exiting the agi application. Example: $AGI->set_context(‘dialout’); Returns: Always returns 0. * $AGI->set_extension($extension) Executes AGI Command “SET EXTENSION $extension” Changes the extension for continuation upon exiting the agi application. Example: $AGI->set_extension(’7′); Returns: Always returns 0. * $AGI->set_priority($priority) Executes AGI Command “SET PRIORITY $priority” Changes the priority for continuation upon exiting the agi application. Example: $AGI->set_priority(1); Returns: Always returns 0. * $AGI->hangup($channel) Executes AGI Command “HANGUP $channel” Hangs up the passed $channel,or the current channel if $channel is not passed. It is left to the AGI script to exit properly,otherwise you could end up with zombies. Example: $AGI->hangup(); Returns: Always returns 1. * $AGI->exec($app,$options) Executes AGI Command “EXEC $app $options” The most powerful AGI command. Executes the given application passing the given options. Example: $AGI->exec(‘Dial’,‘Zap/g2/8005551212′); Returns: -2 on failure to find application,or what ever the given application returns. * $AGI->set_variable($variable,$value) Executes AGI Command “SET VARIABLE $variable $value” Sets the channel variable <variablename> to <value>. Example: $AGI->set_variable(‘status’,‘authorized’); Returns: Always returns 1. * $AGI->get_variable($variable) Executes AGI Command “GET VARIABLE $variablename” Gets the channel variable <variablename>. Example: $AGI->get_variable(‘status’); Returns: The value of the variable,or undef if variable does not exist. * $AGI->verbose($message,$level) Executes AGI Command “VERBOSE $message $level” Logs $message with verboselevel $level. Example: $AGI->verbose(“System Crashedn”,1); Returns: Always returns 1. * $AGI->database_get($family,$key) Executes AGI Command “DATABASE GET $family $key” Example: $var = $AGI->database_get(‘test’,‘status’); Returns: The value of the variable,or undef if variable does not exist. * $AGI->database_put($family,$key,$value) Executes AGI Command “DATABASE PUT $family $key $value” Set/modifes database entry <family>/<key> to <value>. Example: $AGI->database_put(‘test’,‘status’,‘authorized’); Returns: 1 on success,0 otherwise. * $AGI->database_del($family,$key) Executes AGI Command “DATABASE DEL $family $key” Removes database entry <family>/<key>. Example: $AGI->database_del(‘test’,‘status’); Returns: 1 on success,0 otherwise. * $AGI->database_deltree($family,$key) Executes AGI Command “DATABASE DELTREE $family $key” Deletes a family or specific keytree within a family in the Asterisk database. Example: $AGI->database_deltree(‘test’,‘status’); Example: $AGI->database_deltree(‘test’); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |