在Asterisk-12 以上版本引入了 Asterisk REST Interface,其接口大大增加了Asterisk的接口支持,用戶可以通過RESTful API開發(fā)自己的基于Asterisk的應(yīng)用,例如IPPBX功能,呼叫中心功能等比較熱門的軟件應(yīng)用。關(guān)于這三種接口的背景文檔,筆者在以前的歷史文檔中有過介紹,讀者可以查閱歷史文檔或者官方文檔來進(jìn)行進(jìn)一步學(xué)習(xí)。今天的主要目的是對官方的ARI接口文檔做進(jìn)一步的介紹,幫助讀者對ARI有一個非常清晰的概念,使用和配置的完整認(rèn)識。在本章節(jié)的介紹中,筆者不會介紹關(guān)于Asterisk的安裝過程,這個過程也相當(dāng)簡單,用戶可以參考網(wǎng)絡(luò)的文檔來查閱。具體的測試環(huán)境是Asterisk-15和Centos-7。以下是整個關(guān)于Asterisk ARI的測試配置示例說明。
首先,用戶需要安裝說需要的ARI 接口運行的支持包。執(zhí)行以下幾個步驟:
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 -y install lynx 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 環(huán)境下安裝先安裝npm,如何在安裝wscat
yum install npm
yum install curl
npm install -g wscat
yum install nodejs nodejs-options nodejs-commander
yum install nodejs-ws
確認(rèn)Asterisk編譯安裝成功,同時安裝了PJSIP 支持包。具體安裝過程省略。
然后配置Asterisk相關(guān)配置文件, 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 // 端口設(shè)置
配置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的撥號規(guī)則:
[hiastar-ari]
exten => 1,1,Noop()
same => n,Stasis(hello,world) // 注意,使用的是Stasis,這里的app是hello, 參數(shù)是world
same => n,Hangup() // 完成后掛機。
添加后重新reload asterisk配置文件,通過另外一個終端執(zhí)行wscat 命令,創(chuàng)建Stasis的app。注意,這里的連接端口,app名稱和api_key 必須和ari的匹配。
注意,因為Asterisk需要發(fā)送的是異步的事件信息,例如,創(chuàng)建通道,橋接通道和通道離開等。這些都是通過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 命令可以查看到已創(chuàng)建的app hello, 使用命令 ari show apps 會看到已注冊的app hello。這里,讀者一定要注意,如果wscat 沒有成功執(zhí)行的話,可能報錯,可能配置問題。用戶需要排查問題。執(zhí)行成功后,可以看到CLI 的輸出結(jié)果。
通過兩種方法對ARI 進(jìn)行測試,另外一個終端窗口會輸出事件數(shù)值。執(zhí)行命令 :
channel originate Local/1@hiastar-ari application wait 100
注意,這里的context是對應(yīng)的撥號規(guī)則中的標(biāo)簽hiastar-ari.
在另外一個窗口輸出的結(jié)果,輸出事件包括了所有相關(guān)的生成數(shù)據(jù)。
在輸出的數(shù)據(jù)中,讀者注意context和相應(yīng)的ID。
另外一種測試方法是通過SIP分機做呼叫測試,撥號規(guī)則可以修改為播放一個語音文件:hello-world。
[default]
exten => 1000,1,NoOp()
same => n,Answer()
same => n,Stasis(hello-world) // 播放語音文件。
same => n,Hangup()
用戶可以注冊一個分機,撥打 1000,實現(xiàn)事件輸出。
首先執(zhí)行wscat 命令:
wscat -c "ws://localhost:8088/ari/events?api_key=hiastar-ari:hiastar&app=hello-world"
通過SIP 分機或者其他分機撥打 1000,輸出事件數(shù)據(jù);
< {
"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"}
}
執(zhí)行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 事件輸出中會看到以下結(jié)果:
* 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"}
$
接下來,撥號規(guī)則會播放hello-world, 在輸出的事件中會出現(xiàn)以下結(jié)果:
< {"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", // 播放結(jié)束
"playback":{
"id":"9567ea46-440f-41be-a044-6ecc8100730a",
"media_uri":"sound:hello-world",
"target_uri":"channel:1400609726.3",
"language":"en",
"state":"done"}
}
SIP 分機掛機以后的輸出結(jié)果:
< {"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"}
}
以上執(zhí)行的對通道的語音播放都是通過通道的API來完成,例如剛才使用的命令,用戶也可以通過命令控制媒體播放時長等參數(shù)。具體的用法規(guī)則,讀者可以查閱Asterisk的官方文檔來獲得各種不同的命令。
通過以上的示例,我們給讀者展示了如何配置ARI,如何提高wscat 訪問接口,還有返回的JASON事件信息。Asterisk的ARI 命令支持了多種資源,不僅僅是通道本身,會議會議,錄音,隊列等非常豐富的接口資源。用戶需要根據(jù)自己的實際來獲取相應(yīng)的事件。
參考鏈接:
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
關(guān)注微信公眾號:asterisk-cn,獲得有價值的Asterisk行業(yè)分享
Asterisk freepbx 中文官方論壇:http://bbs.freepbx.cn/forum.php
Asterisk freepbx技術(shù)文檔: www.freepbx.org.cn
融合通信商業(yè)解決方案,協(xié)同解決方案首選產(chǎn)品:www.hiastar.com
Asterisk/FreePBX中國合作伙伴,官方qq技術(shù)分享群(3000千人):589995817