text
stringlengths
0
152
—Eddie Vedder
The Asterisk dialplan has evolved into a simple yet powerful programming interface
for call handling. Many people, however, especially those with a programming back‐
ground, prefer to implement call handling in a traditional programming language.
The Asterisk Gateway Interface (AGI) allows for the development of first-party call
control in the programming language of your choice.
Quick Start
This section gives a quick example of using the AGI.
First, let’s create the script that we’re going to run. AGI scripts are usually placed
in /var/lib/asterisk/agi-bin.
$ cd /var/lib/asterisk/agi-bin
$ vim hello-world.sh
#!/bin/bash
# Consume all variables sent by Asterisk
while read VAR && [ -n ${VAR} ] ; do : ; done
# Answer the call.
echo "ANSWER"
read RESPONSE
# Say the letters of "Hello World"
echo 'SAY ALPHA "Hello World" ""'
read RESPONSE
exit 0
315
$ chown asterisk:asterisk hello-world.sh
$ chmod 700 hello-world.sh
Now, add the following line to /etc/asterisk/extensions.conf, in your [sets] context:
exten => 237,1,AGI(hello-world.sh)
Save and reload your dialplan, and when you call extension 237 you should hear Alli‐
son spell out “Hello World.”
AGI Variants
There are a few variants of AGI that differ primarily in the method used to communi‐
cate with Asterisk. It is good to be aware of all the options so you can make the best
choice based on the needs of your application.
Process-Based AGI
Process-based AGI is the simplest variant of AGI. The quick-start example at the
beginning of this chapter is an example of a process-based AGI script. The script is
invoked using the AGI() application from the Asterisk dialplan. The application to
run is specified as the first argument to AGI(). Unless a full path is specified, the
application is expected to be in the /var/lib/asterisk/agi-bin directory. Arguments to
be passed to your AGI application can be specified as additional arguments to the
AGI() application in the Asterisk dialplan. The syntax is:
AGI(command[,arg1[,arg2[,...]]])
Ensure that your application has the proper permissions set so that
the Asterisk process user has permissions to execute it. Otherwise,
AGI() will fail.
Once Asterisk executes your AGI application, communication between Asterisk and
your application will take place over stdin and stdout. More details about this com‐
munication will be covered in “AGI Communication Overview” on page 319. For
more details about invoking AGI() from the dialplan, check the documentation built
into Asterisk:
*CLI> core show application AGI
Pros of process-based AGI
It is the simplest form of AGI to implement.
316
|
Chapter 18: Asterisk Gateway Interface
Cons of process-based AGI
It is the least efficient form of AGI with regard to resource consumption. Systems
with high load should consider FastAGI, discussed in “FastAGI—AGI over TCP”
on page 317, instead.
EAGI
EAGI (Enhanced AGI) is a slight variant on AGI(). It is invoked in the Asterisk
dialplan as EAGI(). The difference is that in addition to the communication on stdin
and stdout, Asterisk also provides a unidirectional stream of audio coming from the
channel on file descriptor 3. For more details on how to invoke EAGI() from the
Asterisk dialplan, check the documentation built into Asterisk:
*CLI> core show application EAGI