• <strike id="fdgpu"><input id="fdgpu"></input></strike>
    <label id="fdgpu"></label>
    <s id="fdgpu"><code id="fdgpu"></code></s>

  • <label id="fdgpu"></label>
  • <span id="fdgpu"><u id="fdgpu"></u></span>

    <s id="fdgpu"><sub id="fdgpu"></sub></s>
    您當前的位置是:  首頁(yè) > 新聞 > 國內 >
     首頁(yè) > 新聞 > 國內 >

    Asterisk ARI接口安裝調用示例事件輸出

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


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


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

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

    專(zhuān)題

    亚洲精品网站在线观看不卡无广告,国产a不卡片精品免费观看,欧美亚洲一区二区三区在线,国产一区二区三区日韩 堆龙德庆县| 米林县| 绵竹市| 玉树县| 墨玉县| 海兴县| 洛浦县| 泗洪县| 武威市| 永泰县| 元阳县| 蚌埠市| 竹北市| 台江县| 孟连| 增城市| 抚顺市| 哈尔滨市| 西吉县| 鄂伦春自治旗| 怀宁县| 怀远县| 上杭县| 乾安县| 阳信县| 内黄县| 镇江市| 昆山市| 内丘县| 大宁县| 东城区| 襄城县| 常熟市| 兴文县| 辽阳县| 清新县| 嫩江县| 泾阳县| 周口市| 马尔康县| 凤台县| http://444 http://444 http://444 http://444 http://444 http://444