/amiex

Elixir Asterisk Manager Interface library for AMI ver2

Primary LanguageElixirGNU General Public License v3.0GPL-3.0

AMI

Elixir CI Coverage Status GPLv3 license

Elixir AMI (Asterisk Manager Interface) library to help build Asterisk application. Can be used to read Asterisk events and to send Actions and commands. Can connect to one or many Asterisks and proxy all events to single handler. Can send an action to a single Asterisk or broadcast to all established connections.

Installation

The package can be installed by adding amiex to your list of dependencies in mix.exs:

def deps do
  [
    {:amiex, "~> 0.1.0"}
  ]
end

The docs can be found at https://hexdocs.pm/amiex.

Usage

Create module that will use AMI:

defmodule MyAMI do
  use AMI

  def handle_message(msg, addr) do
    IO.inspect(msg, label: "handle_message from #{addr}")
    # process incoming events
    :ok
  end
end

AMI client can connect to Asterisk using start_link/1 function using module above:

AMI.Client.start_link({'localhost', 5447, "admin",  "5ecR37", MyAMI})

It is better to use AMI as supervised process and it is possible to connect multiple Asterisk servers. For example:

    children = [
      %{
        id: :pbx01,
        start: {
          AMI.Client,
          :start_link,
          [{'pbx01.myphones.com', 5038, "admin", "secret4", MyAMI}]
        }
      },
      %{
        id: :pbx02,
        start: {
          AMI.Client,
          :start_link,
          [{'127.0.0.1', 5038, "admin", "secret9", MyAMI}]
        }
      },
    ]

    Supervisor.start_link(children, strategy: :one_for_one)

Events also can be filtered via handle_message/2 function:

  # handle FullyBooted event which is sent after successful login
  # and send QueueSummary to all Asterisks via broadcast function
  def handle_message(%{"Event" => ["FullyBooted"]} = msg, _addr) do
    {:ok, qsum} = AMI.Action.new("QueueSummary")
    AMI.Action.broadcast(qsum) # request queues summary to all Asterisks
    :ok
  end

  # handle queues summary response events
  def handle_message(%{"Event" => ["QueueSummary"]} = msg, addr) do
    # process event here
    :ok
  end

  # handle all other events
  def handle_message(msg, addr) do
    # process event here
    AMI.Client.send(addr, AMI.Action.new("Hello"))
    :ok
  end