深圳市宝安区华美居

0086-18665301040

Asterisk ARI接口安装调用示例事件输出

Asterisk ARI接口安装调用示例事件输出

Asterisk支持了非常丰富的接口,用户可以通过这些接口实现和Asterisk的互相通信或控制某些流程。以前的Asterisk主要包括两个接口,一个是AMI,另外一个是AGI。这两个接口通过拨号规则的配合,实现了很多方法的功能。现在,因为越来越多的软件平台支持了RESTful 接口,所以Asterisk也陆续在新版本的Asterisk支持了这些接口。

Asterisk ARI接口安装调用示例事件输出

在Asterisk-12 以上版本引入了Asterisk REST Interface,其接口大大增加了Asterisk的接口支持,用户可以通过RESTful API开发自己的基于Asterisk的应用,例如IPPBX功能,呼叫中心功能等比较热门的软件应用。关于这三种接口的背景文档,笔者在以前的历史文档中有过介绍,读者可以查阅历史文档或者官方文档来进行进一步学习。今天的主要目的是对官方的ARI接口文档做进一步的介绍,帮助读者对ARI有一个非常清晰的概念,使用和配置的完整认识。在本章节的介绍中,笔者不会介绍关于Asterisk的安装过程,这个过程也相当简单,用户可以参考网络的文档来查阅。具体的测试环境是Asterisk-15和Centos-7。以下是整个关于Asterisk ARI的测试配置示例说明。

首先,用户需要安装说需要的ARI 接口运行的支持包。执行以下几个步骤:

sed-i's/(^SELINUX=).*/SELINUX=disabled/'/etc/sysconfig/selinux
sed-i's/(^SELINUX=).*/SELINUX=disabled/'/etc/selinux/config
yum -y update
yum -y groupinstall core base"Development Tools"

adduser asterisk -m -c"Asterisk User"

firewall-cmd --zone=public --add-port=80/tcp--permanent
firewall-cmd --reload
yum -yinstalllynx tftp-server unixODBC mysql-connector-odbc mariadb-server mariadb
httpd ncurses-devel sendmail sendmail-cf sox newt-devel libxml2-devel libtiff-devel
audiofile-devel gtk2-devel subversion kernel-devel git crontabs cronie
cronie-anacron wget vim uuid-devel sqlite-devel net-tools gnutls-devel python-devel texinfo
libuuid-devel

wget -O jansson.tar.gz https://github.com/akheron/jansson/archive/v2.10.tar.gz

在Centos-7 环境下安装先安装npm,如何在安装wscat

yum install npm

yum install curl

npm install-g wscat

yum install nodejs nodejs-options nodejs-commander yum install nodejs-ws

确认Asterisk编译安装成功,同时安装了PJSIP 支持包。具体安装过程省略。

然后配置Asterisk相关配置文件, http.conf 文件

[general]

;

; The name of the server, advertised in both the Server field in HTTP

; response message headers, as well as the <address /> element in certain HTTP

; response message bodies. If not furnished here, “Asterisk/{version}” will be

; used as a default value for the Server header field and the <address />

; element. Setting this property to a blank value will result in the omission

; of the Server header field from HTTP response message headers and the

; <address /> element from HTTP response message bodies.

;

servername=Asterisk

;

; Whether HTTP/HTTPS interface is enabled or not. Default is no.

; This also affects manager/rawman/mxml access (see manager.conf)

;

enabled=yes // 开启http

;

; Address to bind to, both for HTTP and HTTPS. You MUST specify

; a bindaddr in order for the HTTP server to run. There is no

; default value.

;

bindaddr=0.0.0.0 // 支持所有地址访问

bindport=8088 // 端口设置

配置ari.conf 文件:

[general]

enabled = yes ; When set to no, ARI support is disabled. // 开启ari 访问

;pretty = no ; When set to yes, responses from ARI are

; ; formatted to be human readable.

;allowed_origins = ; Comma separated list of allowed origins, for

; ; Cross-Origin Resource Sharing. May be set to * to

; ; allow all origins.

;auth_realm = ; Realm to use for authentication. Defaults to Asterisk

; ; REST Interface.

;

; Default write timeout to set on websockets. This value may need to be adjusted

; for connections where Asterisk must write a substantial amount of data and the

; receiving clients are slow to process the received information. Value is in

; milliseconds; default is 100 ms.

;websocket_write_timeout = 100

;

; Display certain channel variables every time a channel-oriented

; event is emitted:

;

;channelvars = var1,var2,var3

;[username]

;type = user ; Specifies user configuration

;read_only = no ; When set to yes, user is only authorized for

; ; read-only requests.

;

;password = ; Crypted or plaintext password (see password_format).

;

; password_format may be set to plain (the default) or crypt. When set to crypt,

; crypt(3) is used to validate the password. A crypted password can be generated

; using mkpasswd -m sha-512.

;

; When set to plain, the password is in plaintext.

;

;password_format = plain

// 添加测试用户帐户密码

[hiastar-ari]

type = user

read_only = no

password = hiastar

password_format = plain

添加一个测试ari的拨号规则:

[hiastar-ari]

exten => 1,1,Noop()

same => n,Stasis(hello,world) // 注意,使用的是Stasis,这里的app是hello, 参数是world

same => n,Hangup() // 完成后挂机。

添加后重新reload asterisk配置文件,通过另外一个终端执行wscat 命令,创建Stasis的app。注意,这里的连接端口,app名称和api_key 必须和ari的匹配。

注意,因为Asterisk需要发送的是异步的事件信息,例如,创建通道,桥接通道和通道离开等。这些都是通过ARI的event来完成。

[root@localhost asterisk]# wscat –connect ‘ws://localhost:8088/ari/events?app=hello&api_key=hiastar-ari:hiastar’

Creating Stasis app ‘hello’

== WebSocket connection from ‘127.0.0.1:46796′ for protocol ” accepted using version ’13’

connected (press CTRL+C to quit)

通过AsteriskCLI 命令可以查看到已创建的app hello, 使用命令 ari show apps 会看到已注册的app hello。这里,读者一定要注意,如果wscat 没有成功执行的话,可能报错,可能配置问题。用户需要排查问题。执行成功后,可以看到CLI 的输出结果。

Asterisk ARI接口安装调用示例事件输出

Asterisk ARI接口安装调用示例事件输出

通过两种方法对ARI 进行测试,另外一个终端窗口会输出事件数值。执行命令 :

channel originate Local/1@hiastar-ari application wait 100

注意,这里的context是对应的拨号规则中的标签hiastar-ari.

Asterisk ARI接口安装调用示例事件输出

在另外一个窗口输出的结果,输出事件包括了所有相关的生成数据。

Asterisk ARI接口安装调用示例事件输出

在输出的数据中,读者注意context和相应的ID。

另外一种测试方法是通过SIP分机做呼叫测试,拨号规则可以修改为播放一个语音文件:hello-world。

[default]
exten => 1000,1,NoOp()
same => n,Answer()
same => n,Stasis(hello-world) // 播放语音文件。
same => n,Hangup()

用户可以注册一个分机,拨打 1000,实现事件输出。

首先执行wscat 命令:

wscat -c"ws://localhost:8088/ari/events?api_key=hiastar-ari:hiastar&app=hello-world"

通过SIP 分机或者其他分机拨打 1000,输出事件数据;

< {
"application":"hello-world",
"type":"StasisStart",
"timestamp":"2014-05-20T13:15:27.131-0500",
"args":[],
"channel":{
"id":"1400609726.3",
"state":"Up",
"name":"PJSIP/1000-00000001",
"caller":{
"name":"",
"number":""},
"connected":{
"name":"",
"number":""},
"accountcode":"",
"dialplan":{
"context":"default",
"exten":"1000",
"priority":3},
"creationtime":"2014-05-20T13:15:26.628-0500"}
}

执行curl 命令, 注意,这里的ID就是输出的事件的ID号,测试时play,媒体是hello-world。

curl -v-u hiastar-ari:hiastar -X POST"<a href="http://localhost:8088/ari/channels/1400609726.3/play?media=sound:hello-world"

在接下来的JASON 事件输出中会看到以下结果:

* About to connect() to localhost port 8088 (#0)
* Trying 127.0.0.1... connected
* Server auth using Basic with user 'asterisk'
> POST /ari/channels/1400609726.3/play?media=sound:hello-world HTTP/1.1
> Authorization: Basic YXN0ZXJpc2s6c2VjcmV0
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8088
> Accept: */*
>
< HTTP/1.1 201 Created
< Server: Asterisk/SVN-branch-12-r414137M
< Date: Tue, 20 May 2014 18:25:15 GMT
< Connection: close
< Cache-Control: no-cache, no-store
< Content-Length: 146
< Location: /playback/9567ea46-440f-41be-a044-6ecc8100730a
< Content-type: application/json
<
* Closing connection #0
{"id":"9567ea46-440f-41be-a044-6ecc8100730a",
"media_uri":"sound:hello-world",
"target_uri":"channel:1400609726.3",
"language":"en",
"state":"queued"}
$

接下来,拨号规则会播放hello-world, 在输出的事件中会出现以下结果:

< {"application":"hello-world",
"type":"PlaybackStarted", // 播放开始
"playback":{
"id":"9567ea46-440f-41be-a044-6ecc8100730a",
"media_uri":"sound:hello-world",
"target_uri":"channel:1400609726.3",
"language":"en",
"state":"playing"}
}
< {"application":"hello-world",
"type":"PlaybackFinished", // 播放结束
"playback":{
"id":"9567ea46-440f-41be-a044-6ecc8100730a",
"media_uri":"sound:hello-world",
"target_uri":"channel:1400609726.3",
"language":"en",
"state":"done"}
}

SIP 分机挂机以后的输出结果:

< {"application":"hello-world",
"type":"StasisEnd",
"timestamp":"2014-05-20T13:30:01.852-0500",
"channel":{
"id":"1400609726.3",
"state":"Up",
"name":"PJSIP/1000-00000001",
"caller":{
"name":"",
"number":""},
"connected":{
"name":"",
"number":""},
"accountcode":"",
"dialplan":{
"context":"default",
"exten":"1000",
"priority":3},
"creationtime":"2014-05-20T13:15:26.628-0500"}
}

以上执行的对通道的语音播放都是通过通道的API来完成,例如刚才使用的命令,用户也可以通过命令控制媒体播放时长等参数。具体的用法规则,读者可以查阅Asterisk的官方文档来获得各种不同的命令。

Asterisk ARI接口安装调用示例事件输出

通过以上的示例,我们给读者展示了如何配置ARI,如何提高wscat 访问接口,还有返回的JASON事件信息。Asterisk的ARI 命令支持了多种资源,不仅仅是通道本身,会议会议,录音,队列等非常丰富的接口资源。用户需要根据自己的实际来获取相应的事件。

参考链接:

https://wiki.freepbx.org/display/FOP/Installing+FreePBX+14+on+CentOS+7

https://wiki.asterisk.org/wiki/display/AST/Asterisk+12+Channels+REST+API

https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=29395573

Asterisk ARI接口安装调用示例事件输出

Asterisk ARI接口安装调用示例事件输出

关注微信公众号:asterisk-cn,获得有价值的Asterisk行业分享

Asterisk freepbx 中文官方论坛:http://bbs.freepbx.cn/forum.php

Asterisk freepbx技术文档: www.freepbx.org.cn

融合通信商业解决方案,协同解决方案首选产品:www.hiastar.com

Asterisk/FreePBX中国合作伙伴,官方qq技术分享群(3000千人):589995817

联系电话-18665301040
客服-3
客服-2
客服-1