ExARI: An Elixir Library for interfacing with Asterisk the Open Source Communications Software using ARI

ExARI

An Elixir library for interfacing with Asterisk using the ARI protocol.

Be sure to check out the example application https://github.com/citybaseinc/ex_ari_example

The ARI protocol is a robust interface for controlling Asterisk and reacting to Asterisk events. It has been thoroughly tested with Asterisk versions 15 and 16.

ARI stands for Asterisk REST Interface. Through the REST interface it is possible to control just about every aspect of an Asterisk server. ARI also provides a Websocket interface for consuming events from the Asterisk Server. This library has full support for both the REST and Websocket protocols.

If you’re not to familiar with Asterisk, there’s an example repo that will get you up and running with a custom Asterisk IVR in a few minutes, https://github.com/citybaseinc/ex_ari_example .

The documentation is pretty thorough, but it definitely helps to understand Asterisk and ARI.

Check the docs here, https://hexdocs.pm/ex_ari/readme.html

and on hex.pm https://hex.pm/packages/ex_ari

Brought to you by CityBase

5 Likes

I haven’t even looked at the code yet but I know I am going to love this :heart:

3 Likes

So I wanted to try the ex_ari_example out last night, but I had some trouble with the Asterisk part. I haven’t played with Asterisk before so I’m not really sure what’s going wrong.

  1. The docker run ... command uses --net host, which doesn’t work on Mac unfortunately. So I ran it like this instead: docker run -p 8088:8088 -p 8089:8089 -p 5062:5062/tcp -p 5062:5062/udp -p 10000-10099:10000-10099/udp --name asterisk -v $PWD/asterisk_local_config/pjsip.conf:/etc/asterisk/pjsip.conf ex-ari-asterisk (I googled around a bit and guessed at the ports). After this, I could start the project with iex -S mix and it connected!
  2. However, I was unable to connect to Asterisk with Zoiper. On the accounts page it says Request Timeout (code: 408) and there is nothing in the Asterisk logs.
  3. I then tried running the Asterisk container inside an Ubuntu virtual machine, using --net host, and running it on a bridged network (VM had IP 192.168.33.10). I changed all the references to 127.0.0.1 to 192.168.33.10 in dev.env, so the Elixir project works fine. But Zoiper gives me a new error now. Instead of Request Timeout (code: 408) error, I now get Unauthorized (code: 401) and I see this in the Asterisk logs:
[Feb 14 07:29:30] NOTICE[37]: res_pjsip/pjsip_distributor.c:676 log_failed_request: Request 'REGISTER' from '<sip:ex_ari@192.168.33.10>' failed for '192.168.33.1:5063' (callid: _R51m7KNwSEepDSdZ6NrSA..) - No matching endpoint found
[Feb 14 07:29:30] NOTICE[37]: res_pjsip/pjsip_distributor.c:676 log_failed_request: Request 'REGISTER' from '<sip:ex_ari@192.168.33.10>' failed for '192.168.33.1:5063' (callid: _R51m7KNwSEepDSdZ6NrSA..) - Failed to authenticate
<--- Transmitting SIP response (505 bytes) to UDP:192.168.33.1:5063 --->
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.33.1:5063;rport=5063;received=192.168.33.1;branch=z9hG4bK-524287-1---339444ffe79eb675
Call-ID: _R51m7KNwSEepDSdZ6NrSA..
From: <sip:ex_ari@192.168.33.10>;tag=5292835a
To: <sip:ex_ari@192.168.33.10>;tag=z9hG4bK-524287-1---339444ffe79eb675
CSeq: 2 REGISTER
WWW-Authenticate: Digest realm="ex_ari",nonce="1581665370/39d4d6299126e965bb54cf6033f8eca3",opaque="389d044662fe0ad8",algorithm=md5,qop="auth"
Server: Asterisk PBX 16.3.0
Content-Length:  0

I wonder if I am using the wrong username/password?

1 Like

Super curious about this stuff. I don’t have the time anytime soon to dive into Asterisk but damn if I don’t want to :slight_smile:

Considering the Erlang/OTP telecommunications heritage, are there previous solutions to work with Asterisk around, do you know?

Cool that you are doing and releasing this!

1 Like

I run Linux so haven’t had the Mac issues. It looks like you got really really close! I should probably be explicit about the demo only working on Linux as of now.

I’ll dig in a bit more in a few hours. Asterisk is NOT simple to setup. If we get it working would be great if you could write-up some notes for Mac dev.

1 Like

It looks like your port is incorrect in your Zoiper settings.

Under Settings -> Advanced -> Network

All the way at the bottom.

Make sure your SIP Options port is set to 5062 and the Open random port above 32000 is unchecked

You may need to update the AOR_IPS in dev.env to be 192.168.33.1:5062 as well.

But give it a try with just the port update in Zoiper first.

1 Like

No worries. Thank you so much for putting this out. I’ll dig into it, and see if I can get it working. And if I do I’ll be happy to add contribute it back :slight_smile:

Just want to ask, if there is any work done this library, stands still for a few years, latest version from GitHub isn’t compilable (just little code inconsistencies) ?

I haven’t been working on it lately, and I see that Citybase archived it. I was able to get them to transfer ownership to my org before I left. You can find the “active” repo here, GitHub - entropealabs/ex_ari: An Elixir Library for interfacing with Asterisk the Open Source Communications Software using ARI

PRs are welcome if you’d like to update it.

The example application is also available under my org now, GitHub - entropealabs/ex_ari_example: Example application showing real world usage of the ex_ari library to control an Asterisks server

thx, that’s great! I’ve already forked it and found an issue related to the stasis state implementation, the :type attribute is mentioned in the json encode, but not available in the defstruct (part of stasis.ex):

 defmodule State do
    @moduledoc false
    @type t :: %__MODULE__{}

    @derive {Jason.Encoder, only: [:connected, :url, :status, :type]}
    defstruct [
      :connected,
      :url,
      :status,
      :channel_supervisor,
      reconnection_attempts: 0,
      app: %{}
    ]
  end

without going deep into ARI, do you have a quick solution for this issue ?

Are you getting an error while using it?

no, I’m not able to compile:

❯ mix test
Compiling 21 files (.ex)

== Compilation error in file lib/ex_ari/stasis.ex ==
** (ArgumentError) `:only` specified keys ([:type]) that are not defined in defstruct: [:connected, :status, :app, :url, :channel_supervisor, :reconnection_attempts]
    (jason 1.4.1) lib/encoder.ex:147: Jason.Encoder.Any.fields_to_encode/2
    (jason 1.4.1) expanding macro: Jason.Encoder.Any.__deriving__/3
    lib/ex_ari/stasis.ex:101: ARI.Stasis.State (module)

it seems to be related to updated dependencies, jason might be more strict now

Ok, I’ve got a PR open here remove unused key from stasis state Jason encoder by entone · Pull Request #3 · entropealabs/ex_ari · GitHub

Would you mind testing it out? I don’t really have the bandwidth to update and test everything right now.

ok, thx, I’ll give it a try, but give me a few days, a little bit busy at the moment …