没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
156
Chapter 9
CHAPTER 9
The Asterisk Gateway Interface (AGI)
Even he, to whom most things that most people
would think were pretty smart were pretty dumb,
thought it was pretty smart.
—Douglas Adams, The Salmon of Doubt
The Asterisk Gateway Interface, or AGI, provides a standard interface by which
external programs may control the Asterisk dialplan. Usually, AGI scripts are used to
do advanced logic, communicate with relational databases (such as PostgreSQL or
MySQL), and access other external resources. Turning over control of the dialplan to
an external AGI script enables Asterisk to easily perform tasks that would otherwise
be difficult or impossible.
This chapter covers the fundamentals of AGI communication. It will not teach you
how to be a programmer—rather, we’ll assume that you’re already a competent pro-
grammer, so that we can show you how to write AGI programs. If you don’t know
how to do computer programming, this chapter probably isn’t for you, and you
should skip ahead to the next chapter.
Over the course of this chapter, we’ll write a sample AGI program in each of the Perl,
PHP, and Python programming languages. Note, however, that because Asterisk pro-
vides a standard interface for AGI scripts, these scripts can be written in almost any
modern programming language. We’ve chosen to highlight Perl, PHP, and Python
because they’re the languages most commonly used for AGI programming.
Fundamentals of AGI Communication
Instead of releasing an API for programming, AGI scripts communicate with Aster-
isk over communications channels (file pointers, in programming parlance) known
as
STDIN, STDOUT, and STDERR. Most computer programmers will recognize these chan-
nels, but just in case you’re not familiar with them we’ll cover them here.
,ch09.22228 Page 156 Wednesday, August 31, 2005 4:58 PM
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Fundamentals of AGI Communication
|
157
What Are STDIN, STDOUT, and STDERR?
STDIN, STDOUT, and STDERR are channels by which programs in Unix-like environ-
ments receive information from and send information to external programs.
STDIN,or
“standard input,” is the information that is sent to the program, either from the key-
board or from another program. In our case, information coming from Asterisk itself
comes in on the program’s
STDIN file handle. STDOUT, or “standard output,” is the file
handle that the AGI script uses to pass information back to Asterisk. Finally, the AGI
script can use the
STDERR (“standard error”) file handle to write error messages to the
Asterisk console.
Let’s sum up these three communications concepts:
• An AGI script reads from
STDIN to get information from Asterisk.
• An AGI script writes data to
STDOUT to send information to Asterisk.
• An AGI script may write data to
STDERR to send debug information to the
Asterisk console.
At this time, writing to STDERR from within your AGI script writes the
information only to the first Asterisk console—that is, the first Aster-
isk console started with the
-c or -r parameters.
This is rather unfortunate, and will hopefully be remedied soon by the
Asterisk developers.
If you’re using the safe_asterisk program to start Asterisk (which you
probably are), it starts a remote console on TTY9. (Try pressing Ctrl-
Alt-F9, and see if you get an Asterisk command-line interface.) This
means that all the AGI debug information will print on only that remote
console. You may want to disable this console in safe_asterisk to allow
you to see the debug information in another console. (You may also
want to disable that console for security reasons, as you might not want
just anyone to be able to walk up to your Asterisk server and have access
to a console without any kind of authentication.)
The Standard Pattern of AGI Communication
The communication between Asterisk and an AGI script follows a predefined pat-
tern. Let’s enumerate the steps, and then we’ll walk through one of the sample AGI
scripts that come with Asterisk.
When an AGI script starts, Asterisk sends a list of variables and their values to the
AGI script. The variables might look something like this:
agi_request: test.py
agi_channel: Zap/1-1
agi_language: en
agi_callerid:
agi_context: default
agi_extension: 123
agi_priority: 2
,ch09.22228 Page 157 Wednesday, August 31, 2005 4:58 PM
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
158
|
Chapter 9: The Asterisk Gateway Interface (AGI)
After sending these variables, Asterisk sends a blank line. This is the signal that
Asterisk is done sending the variables and it is time for the AGI script to control the
dialplan.
At this point, the AGI script sends commands to Asterisk by writing to
STDOUT. After
the script sends each command, Asterisk sends a response that the AGI script should
read. This action (sending commands to Asterisk and reading the responses) can
continue for the duration of the AGI script.
You may be asking yourself what commands you can use from within your AGI
script. Good question—we’ll cover the basic commands shortly.
*
Calling an AGI Script from the Dialplan
In order to work properly, your AGI script must be executable. To use an AGI script
inside your dialplan, simply call the
AGI( ) application, with the name of the AGI
script as the argument, like this:
exten => 123,1,Answer( )
exten => 123,2,AGI(agi-test.agi)
AGI scripts often reside in the AGI directory (usually located in /var/lib/asterisk/agi-
bin), but you can specify the complete path to the AGI script.
In this chapter, we’ll first cover the sample agi-test.agi script that comes with Aster-
isk (which was written in Perl), then write a weather report AGI program in PHP,
and then finish up by writing an AGI program in Python to play a math game.
* To get a list of available AGI commands, type show agi at the Asterisk command-line interface. You can also
refer to Appendix C for an AGI command reference.
AGI( ), EAGI( ), DeadAGI( ), and FastAGI( )
In addition to the AGI( ) application, there are several other AGI applications suited to
different circumstances. While they won’t be covered in this chapter, they should be
quite simple to figure out once you understand the basics of AGI scripting.
The
EAGI( ) (enhanced AGI) application acts just like AGI( ), but allows your AGI script
to read the inbound audio stream on file descriptor number three.
The
DeadAGI( ) application is also just like AGI( ), but it works correctly on a channel
that is dead (i.e., a channel that has been hung up). As this implies, the regular
AGI( )
application doesn’t work on dead channels.
The
FastAGI( ) application allows the AGI script to be called across the network, so
that multiple Asterisk servers can call AGI scripts from a central location.
,ch09.22228 Page 158 Wednesday, August 31, 2005 4:58 PM
剩余18页未读,继续阅读
资源评论
- liqing06042012-10-31很好的资源,asterisk的AGI,很详细,用于扩展asterisk的功能。
- kingroa2011-09-27这不就是 电话未来之路 第九章的内容么
- bingxin11112012-09-24真是浪费我的积分,没想到这只是这书中的一章而已,早就看过了,浪费我的时间
ainisfen
- 粉丝: 2
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- weixin小程序项目电子竞技信息交流平台+ssm.zip
- 基于MPC的三相变流器设计及仿真,仿真平台基于MATLAB Simulink搭建 内含仿真文件,源代码,设计文档,仿真图 设计文档包括建模,各部分仿真模块设计,控制算法详解
- weixin小程序项目电子购物系统的设计与实现+ssm.zip
- weixin小程序项目电影院订票选座小程序+ssm.zip
- weixin小程序项目大学生闲置物品交易平台的分析与设计+ssm.zip
- weixin小程序项目大学生心理健康服务+ssm.zip
- weixin小程序项目电影院订票选座系统设计及实现+ssm.zip
- weixin小程序项目宠物小程序+ssm.zip
- weixin小程序项目传染病防控宣传系统的设计与实现+springboot.zip
- weixin小程序项目大学生就业平台微信小程序+ssm.zip
- weixin小程序项目畅阅读微信小程序+ssm.zip
- 依据双碳而产生的模型,低碳优化调度 以系统运行维护成本,购能等方向作为优化目标 通过模型计算使各部分能达到最优值 考虑设备有燃气轮机、余热锅炉、燃气锅炉、热泵、电制冷机、储电系统
- 00-【管理制度】07-企业师带徒培训管理制度.doc
- 01-【师带徒协议】03-师带徒协议书.doc
- 01-【师带徒协议】02-师带徒协议书.doc
- 01-【师带徒协议】04-导师辅导协议书(师带徒协议书).docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功