text
stringlengths
0
152
Ending an AGI Session
An AGI session ends when your AGI application is ready for it to end. The details
about how this happens depend on whether your application is using process-based
AGI, FastAGI, or async AGI.
Process-based AGI/FastAGI
Your AGI application may exit or close its connection at any time. As long as the
channel has not hung up before your application ends, dialplan execution will
continue.
If channel hangup occurs while your AGI session is still active, Asterisk will provide
notification that this has occurred so that your application can adjust its operation as
appropriate.
If a channel hangs up while your AGI application is still executing, a couple of things
will happen. If an AGI command is in the middle of executing, you may receive a
result code of -1. You should not depend on this, though, since not all AGI
AGI Communication Overview
|
325
commands require channel interaction. If the command being executed does not
require channel interaction, the result will not reflect the hangup.
The next thing that happens after a channel hangs up is that an explicit notification of
the hangup is sent to your application. For process-based AGI, the signal SIGHUP will
be sent to the process to notify it of the hangup. For a FastAGI connection, Asterisk
will send a line containing the word HANGUP.
If you would like to disable having Asterisk send the SIGHUP signal to your process-
based AGI application or the HANGUP string to your FastAGI server, you can do so by
setting the AGISIGHUP channel variable, as demonstrated in this short example:
; no SIGHUP (AGI) or HANGUP (FastAGI)
exten => 237,1,Set(AGISIGHUP=no)
same => n,AGI(hello-world.sh)
Once the hangup has happened, the only AGI commands that may be used are those
that do not require channel interaction. The documentation for the AGI commands
built into Asterisk includes an indication of whether or not each command can be
used once the channel has been hung up.
Async AGI
When you’re using async AGI, the manager interface provides mechanisms to notify
you about channel hangups. When you would like to end an async AGI session for a
channel, you must execute the ASYNCAGI BREAK command. When the async AGI ses‐
sion ends, Asterisk will send an AsyncAGI manager event with a SubEvent of End. The
following is an example of ending an async AGI session:
Action: AGI
Channel: SIP/0004F2060EB4-0000001b
ActionID: my-action-id
CommandID: my-command-id
Command: ASYNCAGI BREAK
Response: Success
ActionID: my-action-id
Message: Added AGI command to queue
Event: AsyncAGI
Privilege: agi,all
SubEvent: End
Channel: SIP/0004F2060EB4-0000001b
At this point, the channel returns to the next step in the Asterisk dialplan (assuming
it has not yet been hung up).
326
|
Chapter 18: Asterisk Gateway Interface
Example: Account Database Access
Example 18-1 is an example of an AGI script. To run this script you would first place
it in the /var/lib/asterisk/agi-bin directory. Then you would execute it from the Aster‐
isk dialplan like this:
exten => 241,1,AGI(account-lookup.py)
same => n,Hangup()
This example is written in Python and is very sparsely documented for brevity. It
demonstrates how an AGI script interfaces with Asterisk using stdin and stdout.
The script prompts a user to enter an account number, and then plays back a value
associated with that number. In the interest of brevity, we have hardcoded a few fake
accounts into the script—this would obviously be something normally handled by a
database connection.
The script is intentionally terse, since we are interested in briefly showing some AGI
functions without filling this book with pages of code.
Example 18-1. account-lookup.py