转自网络,备忘:
web link:http://astbook.asteriskdocs.org/en/2nd_Edition/asterisk-book-html-chunk/asterisk-CHP-9-SECT-2.html
Writing AGI Scripts in Perl | ||
---|---|---|
Prev | Chapter 9. The Asterisk Gateway Interface (AGI) | Next |
Writing AGI Scripts in Perl
Asterisk 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;
use strict
tells Perl to act, well, strict about possible programming errors, such as undeclared variables. While not absolutely necessary, enabling this will help you avoid common programming pitfalls.
$|=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;
Warning
You 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 AGI
, which is used to store the variables that Asterisk passes to our script at the beginning of the AGI session. The next three are scalar values, used to count the total number of tests, the number of failed tests, and the number of passed tests, respectively.
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 AGI
. They can be used later in the program or simply ignored, but they should always be read from STDIN
before continuing on with the logic of the program.
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 AGI
hash to STDERR
. This is useful for debugging the AGI script, as STDERR
is printed to the Asterisk console.[120]
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 STREAM FILE
command. The STREAM FILE
command tells Asterisk to play a sound file to the caller, just as the Background()
application does. In this case, we’re telling Asterisk to play a file called beep.gsm.[121]
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.
Warning
You 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 \n
on the end of the print
statement) at the end of the command.
After sending the STREAM FILE
command, this test reads the result from STDIN
and calls the checkresult
subroutine to determine if Asterisk was able to play the file. The STREAM FILE
command takes three arguments, two of which are required:
-
The name of the sound file to play back
-
The digits that may interrupt the playback
-
The position at which to start playing the sound, specified in number of samples (optional)
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 SEND TEXT
command, which is similar to the SendText()
application. This command will send the specified text to the caller, if the caller’s channel type supports the sending of text.
The SEND TEXT
command takes one argument: the text to send to the channel. If the text contains spaces (as in the previous code block), the argument should be encapsulated with quotes, so that Asterisk will know that the entire text string is a single argument to the command. Again, notice that the quotation marks are escaped, as they must be sent to Asterisk, not used to terminate the string in Perl.
print STDERR "3. Testing 'sendimage'..."; print "SEND IMAGE asterisk-image\n"; my $result = <STDIN>; &checkresult($result);
This test calls the SEND IMAGE
command, which is similar to the SendImage()
application. Its single argument is the name of an image file to send to the caller. As with the SEND TEXT
command, this command works only if the calling channel supports the receiving images.
print STDERR "4. Testing 'saynumber'..."; print "SAY NUMBER 192837465 \"\"\n"; my $result = <STDIN>; &checkresult($result);
This test sends Asterisk the SAY NUMBER
command. This command behaves identically to the SayNumber()
dialplan application. It takes two arguments:
-
The number to say
-
The digits that may interrupt the command
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 1000\n"; my $result = <STDIN>; &checkresult($result);
This test shows the WAIT FOR DIGIT
command. This command waits the specified number of milliseconds for the caller to enter a DTMF digit. If you want the command to wait indefinitely for a digit, use -1
as the timeout. This application returns the decimal ASCII value of the digit that was pressed.
print STDERR "6. Testing 'record'..."; print "RECORD FILE testagi gsm 1234 3000\n"; my $result = <STDIN>; &checkresult($result);
This section of code shows us the RECORD FILE
command. This command is used to record the call audio, similar to the Record()
dialplan application. RECORD FILE
takes seven arguments, the last three of which are optional:
-
The filename of the recorded file.
-
The format in which to record the audio.
-
The digits that may interrupt the recording.
-
The timeout (maximum recording time) in milliseconds, or
-1
for no timeout. -
The number of samples to skip before starting the recording (optional).
-
The word
BEEP
, if you’d like Asterisk to beep before the recording starts (optional). -
The number of seconds before Asterisk decides that the user is done with the recording and returns, even though the timeout hasn’t been reached and no DTMF digits have been entered (optional). This argument must be preceded by
s=
.
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 STREAM FILE
command. We’ve already covered STREAM FILE
, so this section of code needs no further explanation.
print STDERR "================== Complete ======================\n"; print STDERR "$tests tests completed, $pass passed, $fail failed\n"; print STDERR "==================================================\n";
At the end of the AGI script, a summary of the tests is printed to STDERR
, which should end up on the Asterisk console.
In summary, you should remember the following when writing AGI programs in Perl:
-
Turn on strict language checking with the
use strict
command.[122] -
Turn off output buffering by setting
$|=1
. -
Data from Asterisk is received using a
while(<STDIN>)
loop. -
Write values with the
print
command. -
Use the
print STDERR
command to write debug information to the Asterisk console.
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 ended\n”;
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, 0 if playback completes without a digit being pressed, 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, 0 if playback completes without a digit being pressed, 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 Crashed\n”, 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’);