Asterisk Gateway Interface (AGI) : - CHAPTER 21
Asterisk Gateway Interface (AGI) : - CHAPTER 21
t
^ Action Response
i
Figure20-2. Manager actions
AMI Client | Asterisk
i
i
i
i
i
:
Manager Event
>
1
1
1
1
Manager Event
i
*
i
1
'
Manager Event
' S
i
i
i
Action Response
Figure20-3. Manager actions that return a list of data
Message Encoding
All AMI messages, including manager events, manager actions, and manager action
responses, are encoded in the same way. The messages are text-based, with lines ter-
minated by a carriage return and a line-feed character. A message is terminated by a
blank line:
Headerl : Thi s i s the f i rst header<CR><LF>
Header2: Thi s i s the second header<CR><LF>
Header3: Thi s i s t he l ast header of thi s message<CR><LF>
<CRxLF>
466 | Chapter 20: Asterisk Manager Interface (AMI)
Events
Manager events always have an Event header and a Pri vi l ege header. The Event header
gives the ame of the event, while the Pri vi l ege header lists the privilege levels asso-
ciated with the event. Any other headers included with the event are specific to the
event type. Heres an example:
Event: Hangup
Pri vi l ege: cal i , al l
Channel : SI P/ 0004F2060EB4- 00000000
Uni quei d: 1283174108. 0
Cal l erI DNum: 2565551212
Cal l erl DName: Russel l Bryant
Cause: 16
Cause- t xt : Normal Cl eari ng
Actions
When executing a manager action, it must include the Action header. The Action header
identifies which manager action is being executed. The rest of the headers are argu-
ments to the manager action. Some headers are required.
m\
I To get a list of the headers associated with a particular manager action,
type manager show command <Action> at the Asterisk command line.
" il-y To get a full list of manager actions supported by the versin of Asterisk
you are running, enter manager show commands at the Asterisk CLI.
The final response to a manager action is typically a message that includes the
Response header. The valu of the Response header will be Success if the manager action
was successfully executed. If the manager action was not successfully executed, the
valu of the Response header will be Error. For example:
Acti on: Logi n
Username: russel l
Secret: russel l
Response: Success
Message: Aut hent i cat i on accept ed
AMI over HTTP
In addition to the native TCP interface, it is also possible to access the Asterisk Manager
Interface over HTTP. Programmers with previous experience writing applications that
use web APIs will likely prefer this over the native TCP connectivity.
Authentication and session handling
There are two methods of performing authentication against the AMI over HTTP. The
first is to use the Login action, similar to authentication with the native TCP interface.
Protocol Overview | 467
This is the method that was used in the quick-start example, as seen in AMI over
HTTP on page 459. The second authentication option is HTTP digest authentica-
tion.* The next three sections discuss each of the AMI over HTTP encoding options.
To indicate that HTTP digest authentication should be used, prefix the encoding type
with an a.
Once successfully authenticated, Asterisk will provide a cookie that identifies the au-
thenticated session. Here is an example response to the Logi n action that includes a
session cookie from Asterisk:
$ curl -v "http: / / l ocal host: 8088/ rawman?acti on=l ogi n&username=hel l o&secret=worl d"
* About t o connect () t o l ocal host port 8088 (#0)
* Tryi ng 127. 0. 0. 1. . . connect ed
* Connect ed t o l ocal host (127. 0. 0. 1) port 8088 (#0)
>GET / rawman ?act i on=l ogi n&username=hel l o8i secret=wor I da HTTP/ l . l
>User- Agent : curl / 7. 19. 7 ( x86_64- pc- l i nux- gnu) l i bcurl / 7. 19. 7
OpenSSL/ 0. 9. 8k zl i b/ l . 2. 3. 3 l i bi dn/ 1.15
>Host: l ocal host : 8088
>Accept: */ *
>
<HTTP/ l . l 200 0K
<Server: Ast eri sk/ l . 8. 0- bet a4
<Date: Tue, 07 Sep 2010 11: 51: 28 GMT
<Connecti on: ci se
<Cache- Cont rol : no- cache, no- st ore
<Cont ent - Lengt h: 55
<Cont ent - t ype: t ext / pl ai n
<Cache- Cont rol : no- cache;
<Set - Cooki e: mansessi on_i d=' ' 0e929e60"; Versi on=l ; Max- Age=60
<Pragma: SuppressEvent s
Response: Success
Message: Aut hent i cat i on accept ed
* Cl osi ng connect i on #0
/rawman encoding
The rawman encoding type is what has been used in all the AMI over HTTP examples
in this chapter so far. The responses received from requests using rawman are formatted
in the exact same way that they would be if the requests were sent over a direct TCP
connection to the AMI.
* At the time of writing, there is a problem with HTTP digest authentication that prevents it from working
properly. Issue 18598 (https://issuesMsterisk.org/view.php?id=18598) in the Asterisk project issue tracker has
been opened for this problem. Hopefully it will be fixed by the time you read this.
468 | Chapter 20: Asterisk Manager Interface (AMI)
/manager encoding
The manager encoding type provides a response in simple HTML form. This interface
is primarily useful for experimenting with the AMI. Here is an example Logi n using this
encoding type:
$ curl -v ,,http: / / l ocal host: 8088/ manager?acti on=l ogi n&username=hel l o&secret=worl d"
* About to connect Q to l ocal host port 8088 (#0)
* Tryi ng 127. O. O. 1. . . connect ed
* Connect ed to l ocal host (127. 0. 0. 1) port 8088 (#0)
>GET / manager?act i on=l ogi n&username=hel l o&secret =worl d HTTP/ 1.1
>User- Agent : curl / 7. 19. 7 ( x86_64- pc- l i nux- gnu) l i bcur1/ 7. 19. 7
0penSSL/ 0. 9. 8k zl i b/ l . 2. 3.3 l i bi dn/ l . 15
>Host: l ocal host : 8088
>Accept: */ *
>
<HTTP/ 1. 1 200 OK
<Server: Ast eri sk/ l . 8. 0- bet a4
<Date: Tue, 07 Sep 2010 12: 19: 05 GMT
<Connecti on: ci se
<Cache- Cont rol : no- cache, no- store
<Content - Lengt h: 881
<Cont ent - t ype: text / ht ml
<Cache- Cont rol : no- cache;
<Set- Cooki e: mansessi on_i d="l 39deda7"; Versi on=l ; Max- Age=60
<Pragma: SuppressEvent s
<t i t l e>Ast eri sk&t rade; Manager I nt er f acec/ t i t l exbody bgcol or="#f f f f f f ">
<tabl e al i gn=cent er bgcol or="#f l f l f l " wi dt h="500">
ct r xt d col span="2" bgcol or="#f l f l f f "xhl >Manager Test er </ hi x/ t dx/ t r >
<t r xt d col span="2" bgcol or=,' #f i f l f f "xf or macti on=' ' manager" met hod="post ">
Acti on: <sel ect name="act i on">
<opti on val ue="">. . . . > ; </ opt i on>
<opti on val ue="l ogi n">l ogi n</ opt i on>
<opti on val ue="command>Command</ opt i on>
copti on val ue="wai t event >wai t event </ opt i on>
<opti on val ue="l i st commands">l i st commands</ opt i on>
</ sel ect>
or ci nput name="act i on"xbr / >
CLI Command <i nput name="command"xbr >
user <i nput name="username"> pass ci nput t ype="password" name="secr et "xbr >
<i nput type="submi t ">
</ f orm>
</ t dx/ t r >
<t r xt d>Response</ t dxt d>Success</ t dx/ t r >
<t r xt d>Message</ t dxt d>Aut hent i cat i on accept ed</ t dx/ t r >
<t r xt d col span=,,2' ,xhr x/ t dx/ t r >
* Cl osi ng connecti on #0
</ t abl ex/ body
Protocol Overview | 469
The mxml encoding type provides responses to manager actions encoded in XML. Here
is an example Logi n using the mxml encoding type:
$ curl - v "ht t p: / / l ocal host : 8088/ mxml ?act i on=l ogi n&username=hel l o&secret =worl d"
* About to connect Q t o l ocal host port 8088 (#0)
* Tryi ng 127. 0. 0. 1. . . connect ed
* Connect ed t o l ocal host ( 127. 0. 0. 1) port 8088 (#0)
>GET / mxml ?act i on=l ogi n&username=hel l o&secret =worl d HTTP/ l . l
>User- Agent: curl / 7. 19. 7 ( x86_64- pc- l i nux- gnu) l i bcurl / 7. 19- 7
0penSSL/ O. 9. 8k zl i b/ l . 2. 3. 3 l i bi dn/ l . 15
>Host: l ocal host : 8088
>Accept: */ *
>
<HTTP/ l . l 200 0K
<Server: Ast eri sk/ l . 8. 0- bet a4
<Date: Tue, 07 Sep 2010 12: 26: 58 GMT
<Connecti on: ci se
<Cache- Cont rol : no- cache, no- st ore
<Cont ent - Lengt h: 146
<Cont ent - t ype: t ext / xml
<Cache- Cont rol : no- cache;
<Set- Cooki e: mansessi on_i d="536dl 7a4"; Versi on=l ; Max- Age=60
<Pragma: SuppressEvent s
<
<aj ax- response>
<response type=' obj ect ' i d=' unknown' >
<generi c response=' Success' message=' Aut hent i cat i on accepted' / >
</ response>
* Cl osi ng connect i on #0
</ aj ax- response>
Manager events
When connected to the native TCP interface for the AMI, manager events are delivered
asynchronously. When using the AMI over HTTP, events must be retrieved by polling
for them. Events are retrieved over HTTP by executing the Wai t Event manager action.
The following example shows how events can be retrieved using the Wai t Event manager
action. The steps are:
1. Start an HTTP AMI session using the Logi n action.
2. Register a SIP phone to Asterisk to generate a manager event.
3. Retrieve the manager event using the Wai t Event action.
The interaction looks like this:
$ wget - - save- cooki es cooki es. t xt \
>Hht t p: / / l ocal host : 8088/ mxml ?act i on=l ogi n&username=hel l o&secret =worl d" - 0 -
<aj ax- response>
<response type=' obj ect ' i d=' unknown' >
/mxml encoding
470 1 Chapter 20: Asterisk Manager Interface (AMI)
<generi c response=' Success' message=' Aut hent i cat i on accepted' / >
</ response>
</ aj ax- response>
$ wget --l oad-cooki es cooki es. txt "http: / / l ocal host: 8088/ mxml ?acti on=wai tevent" -O-
<aj ax- response>
<response type=' obj ect ' i d=' unknown>
<generi c response=' Success' message=' Wai t i ng f or Event compl eted. ' / >
</ response>
<response type=' obj ect ' i d=' unknown' >
<generi c event =' PeerSt at us' pri vi l ege=' syst em, al l
channel t ype= SI P' peer =' SI P/ OOOOFFFFOOO4
peerst at us=' Regi st ered' address=' l 72. 16.0.160:5060' / >
</ response>
<response t ype=' obj ect' i d=' unknown' >
<generi c event =' Wai t Event Compl et e' / >
</ response>
</ aj ax- response>
Development Frameworks
Many application developers write code that directly interfaces with the AMI. However,
there are a number of existing libraries that aim to make writing AMI applications
easier. Table 20-5 lists a few that we know are being used successfully. If you search
around for Asterisk libraries in any other popular programming language of your
choice, you are likely to find one that exists.
Table 20-5. AMI development frameworks
Framework Language URL
Adhearsion Ruby http-J/adhearsion.com/
StarPy Python http://starpy.sourceforge.net/
Asterisk-Java Java http-J/asterisk-java.org/
CSTA
Computer-Supported Telecommunications Applications (CSTA) is a standard for
Computer Telephony Integration (CTI). One of the biggest benefits of CSTA is that it
is used by mltiple manufacturers. Some of what is provided by CSTA can be mapped
to operations available in the AMI. There have been mltiple efforts to provide a CSTA
interface to Asterisk. One of these efforts is the Open CSTA (http://opencsta.org)
project. While none of the authors have experience with this CST A interface to Asterisk,
it is certainly worth considering if you have CSTA experience or an existing CSTA
application you would like to integrate with Asterisk.
Development Frameworks | 471
Interesting Applications
Many useful applications have been developed that take advantage of the AMI. Here
are a couple of examples.
AsteriskGUI
The AsteriskGUI is an open source PBX administration interface developed by Digium.
It is intended for use on small installations. The AsteriskGUI is written entirely in
HTML and JavaScript and uses the AMI over HTTP for all interaction with Asterisk.
It has been especially popular for use in resource-constrained embedded Asterisk en-
vironments, since it does not require additional software to run on the Asterisk server.
Figure 20-4 shows a page from the AsteriskGUI.
The AsteriskGUI can be obtained from the Digium subversin server:
$ svn co http: / / svn. di gi um. c0m/ svn/ asteri sk- gui / branches/ 2. O
It is also bundled as an option with the AsteriskNOW (http://www.asterisk.org/asteris
knowf) distribution.
Figure 20-4. AsteriskGUI
472 | Chapter 20: Asterisk Manager Interface (AMI)
Flash Operator Panel
Flash Operator Panel is an application that runs in a web browser using Flash. It is
primarily used as an interface to see which extensions are currently ringing or in use.
It also includes the ability to monitor conference room and cali queue status. Some cali
actions can be performed as well, such as barging into a cali and transferring calis.
Figure 20-5 shows a screenshot of the Flash Operator Panel interface.
Downloads and more detailed information on Flash Operator Panel can be found at
http://www.asternic.org.
No timeout
Extensin
rences
itme 901 Externa 1
Externa 2
9 Florencio fM 3 Pedro Externa 3
Sales Queue 14 Rodo
P| Support Queue ^yg^j fnl Externa 5 (
fSAdmin Queue f^DayMode
Figure 20-5. Flash Operator Panel
Conclusin
The Asterisk Manager Interface provides an API for monitoring events from an Asterisk
system, as well as requesting that Asterisk perform a wide range of actions. An HTTP
interface has been provided and a number of frameworks have been developed that
make it easier to develop applications. All of this information, as well as the examples
we looked at at the end of this chapter, should help get you thinking about what new
applications you might be able to build using the Asterisk Manager Interface.
Conclusin | 473