Make and receive phone calls from Elixir through a SIP PBX such as 3CX.

Sippy places calls and answers them as an extension of your PBX. It plays WAV prompts, reports the keys callers press, and can hold, transfer, ring without answering, or turn calls down. That makes it a fit for announcements and alarm callers, small IVR menus, and reacting to calls on a ring group. It is written in pure Elixir and has no runtime dependencies.

It is not a softphone: it does not receive or record the other side's audio. See Limitations.

Installation

Add sippy to your dependencies in mix.exs:

def deps do
  [
    {:sippy, "~> 0.1"}
  ]
end

Quick start

Call extension 200, play a message and hang up a second after it ends:

config =
  Sippy.config(
    pbx_host: "pbx.example.com",
    username: "100",
    password: "secret"
  )

{:ok, call} =
  Sippy.call("200", config: config, audio: "message.wav", hangup_after_audio: 1_000)

call is the call's process. It exits when the call ends, with the result as its exit reason:

ref = Process.monitor(call)

receive do
  {:DOWN, ^ref, :process, ^call, {:shutdown, %{success: true, duration: seconds}}} ->
    IO.puts("Delivered, the call took #{seconds} seconds")

  {:DOWN, ^ref, :process, ^call, reason} ->
    IO.puts("Not delivered: #{inspect(reason)}")
end

Configuration

Build a configuration with Sippy.config/1 and pass it to Sippy.call/2 or Sippy.register/2. Sippy reads no application environment, so one application can use several extensions or PBXs.

OptionDefaultDescription
:pbx_hostrequiredPBX hostname or IPv4 address
:pbx_port5060PBX SIP port
:usernamerequiredSIP username, usually the extension number
:passwordrequiredSIP password
:auth_id:usernameAuthentication ID, when it differs from the username (3CX shows it per extension)
:extension:usernameExtension to register as
:display_name"Sippy"Name shown to the people you call
:local_port5061Local UDP port for SIP; 0 picks a free one
:register_expires300Requested registration lifetime in seconds
:register_retry_interval30_000Milliseconds before retrying a failed registration; doubles per failure up to 10 minutes
:max_pending_calls16Incoming calls ringing at once; more get 486 Busy Here
:pending_call_timeout180_000Milliseconds an incoming call may ring before it is ended with 480
:trusted_sourcesthe :pbx_hostHostnames or IP addresses SIP is accepted from; include the PBX when you set it

Unknown options raise an ArgumentError.

Making calls

Sippy.call/2 calls a number or extension as sip:<destination>@<pbx_host>:

OptionDefaultDescription
:configrequiredThe Sippy.Config
:audiononeWAV file to play once the call is answered
:parent_pidnoneProcess that receives the call's messages
:ring_timeout30_000Milliseconds to wait for an answer
:hangup_after_audiofalseMilliseconds after the audio ends to hang up
:max_duration600_000Milliseconds an answered call may last, or :infinity

A call never hangs up by itself after the audio unless you set :hangup_after_audio. Your application decides when it ends: with Sippy.hangup/1, after the audio, or when the other side hangs up. :max_duration is the safety net.

Driving a call from a process

With a :parent_pid, the call reports what happens:

MessageWhen
{:call_answered, pid}the call was answered
{:dtmf, pid, digit}a key was pressed: "0"-"9", "*", "#", "A"-"D"
{:audio_completed, pid}the audio finished; stopped or replaced audio is not reported
{:call_ended, pid, reason}the call ended, unless your application hung up itself

Inbound calls also report the outcome of a transfer, see Hold and transfer.

The reason is :remote_hangup, :transferred, {:rejected, status}, :ring_timeout, :hangup_after_audio, :max_duration, :no_common_codec, :invalid_sdp, :auth_failed or {:error, reason}; see Sippy.end_reason/0.

A digit never ends the call or stops the audio on its own. This GenServer calls someone with an alarm, asks them to press 1 to confirm, and hangs up when they did or when the question went unanswered:

defmodule MyApp.Alarm do
  use GenServer

  def start_link(number), do: GenServer.start_link(__MODULE__, number)

  @impl true
  def init(number) do
    {:ok, call} =
      Sippy.call(number,
        config: MyApp.sip_config(),
        audio: "alarm.wav",
        parent_pid: self(),
        max_duration: 120_000
      )

    {:ok, %{call: call, number: number, playing: :alarm}}
  end

  @impl true
  def handle_info({:audio_completed, call}, %{playing: :alarm} = state) do
    Sippy.play_audio(call, "press_1_to_confirm.wav")
    {:noreply, %{state | playing: :question}}
  end

  def handle_info({:audio_completed, call}, state) do
    Sippy.hangup(call)
    {:stop, :normal, state}
  end

  def handle_info({:dtmf, call, "1"}, state) do
    MyApp.Alarms.confirmed(state.number)
    Sippy.play_audio(call, "thank_you.wav")
    {:noreply, %{state | playing: :thanks}}
  end

  def handle_info({:call_ended, _call, reason}, state) do
    MyApp.Alarms.not_confirmed(state.number, reason)
    {:stop, :normal, state}
  end

  def handle_info(_message, state), do: {:noreply, state}
end

Sippy.play_audio/2 replaces whatever is playing; Sippy.stop_audio/1 stops it. Sippy.dtmf_digits/1 returns every digit pressed so far. Without :audio, a call stays silent until you play something.

Receiving calls

Sippy.register/2 registers as an extension. The registrar renews the registration before it expires, keeps retrying when it fails, and answers the PBX's keepalives. Run it under your supervisor:

children = [
  MyApp.Phone,
  {Sippy.Registrar, config: MyApp.sip_config(), notify: MyApp.Phone}
]

The :notify process receives {:incoming_call, %Sippy.IncomingCall{}} for every new call, with its caller_id and caller_name, and {:incoming_call_cancelled, call_id} when the caller gives up first. Sippy.registered?/1 tells whether the PBX accepted the registration.

An incoming call is ringing. Take it over and answer it, ring it, or turn it down:

defmodule MyApp.Phone do
  use GenServer

  def start_link(_opts), do: GenServer.start_link(__MODULE__, nil, name: __MODULE__)

  @impl true
  def init(nil), do: {:ok, %{}}

  @impl true
  def handle_info({:incoming_call, incoming}, state) do
    if MyApp.Blocklist.blocked?(incoming.caller_id) do
      Sippy.reject(incoming, :decline)
    else
      with {:ok, call} <- Sippy.start_inbound_call(incoming, parent_pid: self()) do
        Sippy.answer(call, "menu.wav")
      end
    end

    {:noreply, state}
  end

  def handle_info({:dtmf, call, "1"}, state) do
    Sippy.blind_transfer(call, "201")
    {:noreply, state}
  end

  def handle_info({:dtmf, call, "#"}, state) do
    Sippy.hangup(call)
    {:noreply, state}
  end

  def handle_info(_message, state), do: {:noreply, state}
end

Sippy.reject/2 answers with 486 Busy Here (:busy, the default), 603 Decline (:decline) or 480 Temporarily Unavailable (:unavailable). An answered call sends {:dtmf, pid, digit}, {:audio_completed, pid} and {:call_ended, pid, reason} to its :parent_pid, and is ended after :max_duration (an option of Sippy.start_inbound_call/2, 10 minutes by default) when it lasts that long.

Ringing without answering

A device can ring and never answer, for example to show a camera feed while a ring group with the Ring All strategy rings:

def handle_info({:incoming_call, call}, state) do
  :ok = Sippy.ring(call)
  {:noreply, start_showing(state, call.call_id)}
end

def handle_info({:incoming_call_cancelled, call_id}, state) do
  {:noreply, stop_showing(state, call_id)}
end

When another phone answers or the caller hangs up, the PBX cancels the call and Sippy reports {:incoming_call_cancelled, call_id}.

Hold and transfer

Sippy.hold/1 puts an answered call on hold, so the PBX plays its hold music, and Sippy.unhold/1 resumes it.

Sippy.blind_transfer/2 hands the caller to another extension or number. For an attended transfer, put the caller on hold, call the target, and connect the two once the target answers:

:ok = Sippy.hold(inbound)
{:ok, outbound} = Sippy.call("201", config: config, parent_pid: self())

receive do
  {:call_answered, ^outbound} ->
    :ok = Sippy.attended_transfer(inbound, outbound, "201")
end

Both transfers return :ok once the transfer request (a SIP REFER) is sent. The PBX reports how it went, and the call's :parent_pid receives:

  • {:transfer_completed, pid} when the caller is connected to the target. Sippy then hangs up its side, and {:call_ended, pid, :transferred} follows.
  • {:transfer_failed, pid, status} when the PBX refuses the transfer or the target does not take it, for example 486 when it is busy. The call goes on, so you can take it off hold or try another target.

Audio

Prompts are WAV files with 16-bit PCM samples, mono or stereo, at any sample rate. Sippy converts them to 8 kHz mono and sends them as G.711 PCMU or PCMA, whichever the other side picks. Prepare files at 8 kHz mono to skip the conversion:

ffmpeg -i input.mp3 -ar 8000 -ac 1 -sample_fmt s16 output.wav

Examples

The examples directory of the repository has two scripts you can run against your PBX with mix run:

  • announcement.exs calls a number, plays a file and prints the result.
  • menu.exs registers an extension and answers calls with a small menu.

Each script describes the environment variables it needs at the top.

Limitations

  • SIP over UDP and IPv4 only; no TCP, TLS or IPv6.
  • G.711 audio only, and only from WAV files. The other side's audio is not received, so calls cannot be recorded or bridged to a microphone.
  • No SRTP and no NAT traversal (STUN/ICE): Sippy and the PBX must reach each other directly.
  • Calls go through the PBX; a destination is a number or extension, not a SIP URI.
  • Developed and tested against 3CX.

Troubleshooting

A call does not connect

  • Check the credentials and the authentication ID of the extension.
  • Make sure the extension may call the destination.
  • Allow UDP between Sippy and the PBX: the PBX's SIP port (5060) and Sippy's :local_port (5061 by default).

No incoming calls

  • Check that Sippy.registered?/1 returns true.
  • The PBX sends calls to the address Sippy registered; allow UDP from the PBX to :local_port.
  • For a ring group, use the Ring All strategy, or the extension only rings when it is its turn.
  • An offer without PCMU or PCMA is turned down with 488; allow G.711 for the extension.

No audio or one-way audio

  • Sippy sends and receives RTP on a random UDP port picked by the operating system; the firewall must allow UDP between Sippy and the PBX's media ports (10000-20000 on 3CX).
  • Check the WAV file: 16-bit PCM.

DTMF is not detected

  • Configure the PBX or phone to send DTMF as RFC 2833/4733 events (telephone-event); in-band tones are not detected.

License

Copyright © 2025 Herman verschooten. Released under the MIT License.