How to get all dialer events from Asterisk REST API (ARI)?

烂漫一生 提交于 2019-12-05 12:28:39

ARI uses a subscription based model for events. Quoting from the documentation on the wiki:

Resources in Asterisk do not, by default, send events about themselves to a connected ARI application. In order to get events about resources, one of three things must occur:

  1. The resource must be a channel that entered into a Stasis dialplan application. A subscription is implicitly created in this case. The subscription is implicitly destroyed when the channel leaves the Stasis dialplan application.
  2. While a channel is in a Stasis dialplan application, the channel may interact with other resources - such as a bridge. While channels interact with the resource, a subscription is made to that resource. When no more channels in a Stasis dialplan application are interacting with the resource, the implicit subscription is destroyed.
  3. At any time, an ARI application may make a subscription to a resource in Asterisk through application operations. While that resource exists, the ARI application owns the subscription.

So, the reason you get events about a channel over your ARI WebSocket is because it went into the Stasis dialplan application. That isn't, however, the only way to get events.

If you're interested in events from other event sources, you can subscribe to those resources using the applications resource. For example, if I wanted to receive all events that were in relation to PJSIP endpoint "Alice", I would subscribe using the following:

POST https://localhost:8080/ari/applications/my_app/subscription?eventSource=endpoint:PJSIP%2FAlice

Note that subscriptions to endpoints implicitly subscribe you to all channels that are created for that endpoint. If you want to subscribe to all endpoints of a particular technology, you can also subscribe to the resource itself:

POST https://localhost:8080/ari/applications/my_app/subscription?eventSource=endpoint:PJSIP

ws://(host):8088/ari/events?app=dialer&subscibeAll=true Adding SubscribeAll=true make what you want =)

For more clarity regaring what Matt Jordan has already provided, here's an example of doing what he suggests with ari-py:

#!/usr/bin/env python

import ari
import logging

logging.basicConfig(level=logging.ERROR)
client = ari.connect('http://localhost:8088', 'username', 'password')
postRequest=client.applications.subscribe(applicationName=["NameOfAppThatWillReapThisEvent-ThisAppShouldBeRunning"], eventSource="endpoint:PJSIP/alice")

print postRequest

May be help someone:

Subscribe to all events on channels, bridge and endpoints

POST http://localhost:8088/ari/applications/appName/subscription?api_key=user:password&eventSource=channel:,bridge:,endpoint:

Unsubscribe

DELETE http://localhost:8088/ari/applications/appName/subscription?api_key=user:password&eventSource=channel:__AST_CHANNEL_ALL_TOPIC,bridge:__AST_BRIDGE_ALL_TOPIC,endpoint:__AST_ENDPOINT_ALL_TOPIC
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!