中文字幕无码久久精品,13—14同岁无码A片,99热门精品一区二区三区无码,菠萝菠萝蜜在线观看视频高清1

您當(dāng)前的位置是:  首頁(yè) > 新聞 > 國(guó)內(nèi) >
 首頁(yè) > 新聞 > 國(guó)內(nèi) >

Asterisk ARI接口安裝調(diào)用示例事件輸出

2018-11-19 14:37:31   作者: james.zhu   來(lái)源:CTI論壇   評(píng)論:0  點(diǎn)擊:


  Asterisk支持了非常豐富的接口,用戶(hù)可以通過(guò)這些接口實(shí)現(xiàn)和Asterisk的互相通信或控制某些流程。以前的Asterisk主要包括兩個(gè)接口,一個(gè)是AMI,另外一個(gè)是AGI。這兩個(gè)接口通過(guò)撥號(hào)規(guī)則的配合,實(shí)現(xiàn)了很多方法的功能。現(xiàn)在,因?yàn)樵絹?lái)越多的軟件平臺(tái)支持了RESTful 接口,所以Asterisk也陸續(xù)在新版本的Asterisk支持了這些接口。
  在Asterisk-12 以上版本引入了 Asterisk REST Interface,其接口大大增加了Asterisk的接口支持,用戶(hù)可以通過(guò)RESTful API開(kāi)發(fā)自己的基于Asterisk的應(yīng)用,例如IPPBX功能,呼叫中心功能等比較熱門(mén)的軟件應(yīng)用。關(guān)于這三種接口的背景文檔,筆者在以前的歷史文檔中有過(guò)介紹,讀者可以查閱歷史文檔或者官方文檔來(lái)進(jìn)行進(jìn)一步學(xué)習(xí)。今天的主要目的是對(duì)官方的ARI接口文檔做進(jìn)一步的介紹,幫助讀者對(duì)ARI有一個(gè)非常清晰的概念,使用和配置的完整認(rèn)識(shí)。在本章節(jié)的介紹中,筆者不會(huì)介紹關(guān)于Asterisk的安裝過(guò)程,這個(gè)過(guò)程也相當(dāng)簡(jiǎn)單,用戶(hù)可以參考網(wǎng)絡(luò)的文檔來(lái)查閱。具體的測(cè)試環(huán)境是Asterisk-15和Centos-7。以下是整個(gè)關(guān)于Asterisk ARI的測(cè)試配置示例說(shuō)明。
  首先,用戶(hù)需要安裝說(shuō)需要的ARI 接口運(yùn)行的支持包。執(zhí)行以下幾個(gè)步驟:
  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編譯安裝成功,同時(shí)安裝了PJSIP 支持包。具體安裝過(guò)程省略。
  然后配置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 // 開(kāi)啟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  // 支持所有地址訪問(wèn)
  bindport=8088 // 端口設(shè)置
  配置ari.conf 文件:
  [general]
  enabled = yes       ; When set to no, ARI support is disabled. // 開(kāi)啟ari 訪問(wèn)
  ;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
  // 添加測(cè)試用戶(hù)帳戶(hù)密碼
  [hiastar-ari]
  type = user
  read_only = no
  password = hiastar
  password_format = plain
  添加一個(gè)測(cè)試ari的撥號(hào)規(guī)則:
  [hiastar-ari]
  exten => 1,1,Noop()
  same => n,Stasis(hello,world)  // 注意,使用的是Stasis,這里的app是hello, 參數(shù)是world
  same => n,Hangup() // 完成后掛機(jī)。
  添加后重新reload asterisk配置文件,通過(guò)另外一個(gè)終端執(zhí)行wscat 命令,創(chuàng)建Stasis的app。注意,這里的連接端口,app名稱(chēng)和api_key 必須和ari的匹配。
  注意,因?yàn)锳sterisk需要發(fā)送的是異步的事件信息,例如,創(chuàng)建通道,橋接通道和通道離開(kāi)等。這些都是通過(guò)ARI的event來(lái)完成。
  [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)
  通過(guò)AsteriskCLI 命令可以查看到已創(chuàng)建的app hello, 使用命令 ari show apps 會(huì)看到已注冊(cè)的app hello。這里,讀者一定要注意,如果wscat 沒(méi)有成功執(zhí)行的話,可能報(bào)錯(cuò),可能配置問(wèn)題。用戶(hù)需要排查問(wèn)題。執(zhí)行成功后,可以看到CLI 的輸出結(jié)果。


  通過(guò)兩種方法對(duì)ARI 進(jìn)行測(cè)試,另外一個(gè)終端窗口會(huì)輸出事件數(shù)值。執(zhí)行命令 :
  channel originate Local/1@hiastar-ari application wait 100
  注意,這里的context是對(duì)應(yīng)的撥號(hào)規(guī)則中的標(biāo)簽hiastar-ari.
  在另外一個(gè)窗口輸出的結(jié)果,輸出事件包括了所有相關(guān)的生成數(shù)據(jù)。
  在輸出的數(shù)據(jù)中,讀者注意context和相應(yīng)的ID。
  另外一種測(cè)試方法是通過(guò)SIP分機(jī)做呼叫測(cè)試,撥號(hào)規(guī)則可以修改為播放一個(gè)語(yǔ)音文件:hello-world。
  [default]
  exten => 1000,1,NoOp()
  same =>      n,Answer()
  same =>      n,Stasis(hello-world) // 播放語(yǔ)音文件。
  same =>      n,Hangup()
  用戶(hù)可以注冊(cè)一個(gè)分機(jī),撥打 1000,實(shí)現(xiàn)事件輸出。
  首先執(zhí)行wscat 命令:
  wscat -c "ws://localhost:8088/ari/events?api_key=hiastar-ari:hiastar&app=hello-world"
  通過(guò)SIP 分機(jī)或者其他分機(jī)撥打 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號(hào),測(cè)試時(shí)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"
  在接下來(lái)的JASON 事件輸出中會(huì)看到以下結(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"}
  $
  接下來(lái),撥號(hào)規(guī)則會(huì)播放hello-world, 在輸出的事件中會(huì)出現(xiàn)以下結(jié)果:
  < {"application":"hello-world",
  "type":"PlaybackStarted", // 播放開(kāi)始
  "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 分機(jī)掛機(jī)以后的輸出結(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í)行的對(duì)通道的語(yǔ)音播放都是通過(guò)通道的API來(lái)完成,例如剛才使用的命令,用戶(hù)也可以通過(guò)命令控制媒體播放時(shí)長(zhǎng)等參數(shù)。具體的用法規(guī)則,讀者可以查閱Asterisk的官方文檔來(lái)獲得各種不同的命令。
  通過(guò)以上的示例,我們給讀者展示了如何配置ARI,如何提高wscat 訪問(wèn)接口,還有返回的JASON事件信息。Asterisk的ARI 命令支持了多種資源,不僅僅是通道本身,會(huì)議會(huì)議,錄音,隊(duì)列等非常豐富的接口資源。用戶(hù)需要根據(jù)自己的實(shí)際來(lái)獲取相應(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)注微信公眾號(hào):asterisk-cn,獲得有價(jià)值的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中國(guó)合作伙伴,官方qq技術(shù)分享群(3000千人):589995817

【免責(zé)聲明】本文僅代表作者本人觀點(diǎn),與CTI論壇無(wú)關(guān)。CTI論壇對(duì)文中陳述、觀點(diǎn)判斷保持中立,不對(duì)所包含內(nèi)容的準(zhǔn)確性、可靠性或完整性提供任何明示或暗示的保證。請(qǐng)讀者僅作參考,并請(qǐng)自行承擔(dān)全部責(zé)任。

專(zhuān)題