Overview
Asterisk 12 introduces the Asterisk REST Interface, a set of RESTful APIs for building Asterisk based applications. This article will walk you though getting ARI up and running.
There are three main components to building an ARI application.
The first, obviously, is the RESTful API itself. The API is documented using Swagger, a lightweight specification for documenting RESTful APIs. The Swagger API docs are used to generate validations and boilerplate in Asterisk itself, along with static wiki documentation, and interactive documentation usingSwagger-UI.
Then, Asterisk needs to send asynchronous events to the application (new channel, channel left a bridge, channel hung up, etc). This is done using a WebSocket on /ari/events. Events are sent as JSON messages, and are documented on the REST Data Models page. (See the list of subtypes for the
Message
data model.)
Finally, connecting the dialplan to your application is the
Stasis()
dialplan application. From within the dialplan, you can send a channel to Stasis()
, specifying the name of the external application, along with optional arguments to pass along to the application.Example: ARI Hello World!
In this example, we will:
- Configure Asterisk to enable ARI
- Send a channel into Stasis
- And playback "Hello World" to the channel
This example will not cover:
- Installing Asterisk. We'll assume you have Asterisk 12 or later installed and running.
- Configuring a SIP device in Asterisk. For the purposes of this example, we are going to assume you have a SIP softphone or hardphone registered to Asterisk, using either
chan_sip
orchan_pjsip
.
Getting wscat
ARI needs a WebSocket connection to receive events. For the sake of this example, we're going to use wscat, an incredibly handy command line utility similar to netcat but based on a node.js websocket library. If you don't have wscat:
- If you don't have it already, install npm
- Install the
ws
node package:
Getting curl
In order to control a channel in the Stasis dialplan application through ARI, we also need an HTTP client. For the sake of this example, we'll use curl:
Configuring Asterisk
- Enable the Asterisk HTTP service in
http.conf
: - Configure an ARI user in
ari.conf
: - Create a dialplan extension for your Stasis application. Here, we're choosing extension
1000
in contextdefault
- if your SIP phone is configured for a different context, adjust accordingly.
Hello World!
- Connect to Asterisk using
wscat
:In Asterisk, we should see a new WebSocket connection and a message telling us that our Stasis application has been created: - From your SIP device, dial extension 1000:In wscat, we should see the
StasisStart
event, indicating that a channel has entered into our Stasis application: - Using
curl
, tell Asterisk to playbackhello-world
. Note that the identifier of the channel in thechannels
resource must match the channelid
passed back in theStasisStart
event:The response to our HTTP request will tell us whether or not the request succeeded or failed (in our case, a success will queue the playback onto the channel), as well as return in JSON the Playback resource that was created for the operation:In Asterisk, the sound file will be played back to the channel:And in ourwscat
WebSocket connection, we'll be informed of the start of the playback, as well as it finishing: - Hang up the phone! This will cause the channel in Asterisk to be hung up, and the channel will leave the Stasis application, notifying the client via a
StasisEnd
event:
Mis pasos
wscat -c "ws://localhost:8088/ari/events?api_key=asterisk:asterisk&app=hello-world"
dial to 1000 and get the ID
[ari_app]
exten => 1000,1,NoOp()
same => n,Answer()
same => n,Stasis(hello-world)
same => n,Hangup()
{
"type": "StasisStart",
"timestamp": "2016-02-28T02:39:54.725-0400",
"args": [],
"channel": {
"id": "1456641594.113",
"name": "SIP/102-00000046",
"state": "Up",
"caller": {
"name": "Ambiorix",
"number": "102"
},
"connected": {
"name": "",
"number": ""
},
"accountcode": "",
"dialplan": {
"context": "ari_app",
"exten": "1000",
"priority": 3
},
"creationtime": "2016-02-28T02:39:54.591-0400"
},
"application": "hello-world"
}
root@asterisk-dominicana:~# curl -v -u asterisk:asterisk -X POST "http://localhost:8088/ari/channels/1456641594.113/play?media=sound:/var/lib/asterisk/sounds/en/hello-world"
sending dtmf
root@asterisk-dominicana:~# curl -v -u asterisk:asterisk -X POST "http://localhost:8088/ari/channels/1456641594.113/dtmf?dtmf=1"
sending dtmf
root@asterisk-dominicana:~# curl -v -u asterisk:asterisk -X POST "http://localhost:8088/ari/channels/1456641594.113/dtmf?dtmf=1"
hold and unhold
root@asterisk-dominicana:~# curl -v -u asterisk:asterisk -X POST "http://localhost:8088/ari/channels/1456641594.113/hold"
root@asterisk-dominicana:~# curl -v -u asterisk:asterisk -X delete "http://localhost:8088/ari/channels/1456641594.113/hold"
No hay comentarios:
Publicar un comentario