Sippy
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, sends keys of its own, and can hold, transfer, ring without answering, or turn calls down. That makes it a fit for announcements and alarm callers, small IVR menus, reacting to calls on a ring group, and opening a door from a door station's call. It is written in pure Elixir and has no runtime dependencies.
It also carries live audio both ways as PCM, but it is not a softphone: capturing, playing, recording or bridging that audio is up to you. See Receiving audio, Sending live audio and Limitations.
Installation
Add sippy to your dependencies in mix.exs:
def deps do
[
{:sippy, "~> 0.3"}
]
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.
| Option | Default | Description |
|---|---|---|
:pbx_host |
required | PBX hostname or IPv4 address |
:pbx_port |
5060 |
PBX SIP port |
:username |
required | SIP username, usually the extension number |
:password |
required | SIP password |
:auth_id |
:username |
Authentication ID, when it differs from the username (3CX shows it per extension) |
:extension |
:username |
Extension to register as |
:display_name |
"Sippy" |
Name shown to the people you call |
:local_port |
5061 |
Local UDP port for SIP; 0 picks a free one |
:register_expires |
300 |
Requested registration lifetime in seconds |
:register_retry_interval |
30_000 |
Milliseconds before retrying a failed registration; doubles per failure up to 10 minutes |
:max_pending_calls |
16 |
Incoming calls ringing at once; more get 486 Busy Here |
:pending_call_timeout |
180_000 |
Milliseconds an incoming call may ring before it is ended with 480 |
:trusted_sources |
the :pbx_host |
Hostnames 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>:
| Option | Default | Description |
|---|---|---|
:config |
required | The Sippy.Config |
:audio |
none | WAV file to play once the call is answered |
:parent_pid |
none | Process that receives the call's messages |
:ring_timeout |
30_000 |
Milliseconds to wait for an answer |
:hangup_after_audio |
false |
Milliseconds after the audio ends to hang up |
:max_duration |
600_000 |
Milliseconds an answered call may last, or :infinity |
:receive_audio |
false |
Send the other side's audio to the :parent_pid, see Receiving audio |
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:
| Message | When |
|---|---|
{: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 |
{:dtmf_sent, pid} |
the digits of Sippy.send_dtmf/3 were sent |
{:audio, pid, pcm} |
with :receive_audio, the other side's audio, see Receiving audio |
{: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
t: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 example486when it is busy. The call goes on, so you can take it off hold or try another target.
Sending DTMF
Sippy.send_dtmf/3 sends keys on an answered call, outbound or inbound, as
RFC 4733 telephone events. A door station that opens on 5*, for example:
def handle_info({:incoming_call, incoming}, state) do
{:ok, call} = Sippy.start_inbound_call(incoming, parent_pid: self())
:ok = Sippy.answer(call)
:ok = Sippy.send_dtmf(call, "5*")
{:noreply, state}
end
def handle_info({:dtmf_sent, call}, state) do
Sippy.hangup(call)
{:noreply, state}
end
Each digit lasts :duration milliseconds (default 100), with :pause
milliseconds (default 100) between digits. Sippy.send_dtmf/3 returns
:ok once sending starts, and the :parent_pid receives
{:dtmf_sent, pid} after the last digit. Sending digits stops the audio, and
playing audio stops digits still being sent. When the other side did not
offer telephone events, it returns {:error, :dtmf_not_supported}.
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
Receiving audio
With receive_audio: true, on Sippy.call/2 or Sippy.start_inbound_call/2,
the :parent_pid receives the other side's audio once the call is answered:
{:ok, call} = Sippy.start_inbound_call(incoming, parent_pid: self(), receive_audio: true)
:ok = Sippy.answer(call)
def handle_info({:audio, _call, pcm}, state) do
Port.command(state.speaker, pcm)
{:noreply, state}
end
Each pcm is one RTP packet, usually 20 ms, decoded to 16-bit little-endian
PCM at 8 kHz mono, for example for aplay -f S16_LE -r 8000 -c 1. Packets
arrive in order: duplicates and packets older than the last one are dropped.
Sippy does not buffer for network jitter or fill in lost packets; a player
with its own buffer, such as aplay, smooths that out.
Sending live audio
Sippy.send_audio/2 sends live audio on an answered call, in the same format:
16-bit little-endian PCM at 8 kHz mono. Feed it audio as it is captured, for
example from arecord:
mic = Port.open({:spawn, "arecord -q -t raw -f S16_LE -r 8000 -c 1"}, [:binary])
def handle_info({port, {:data, pcm}}, %{mic: port} = state) do
Sippy.send_audio(state.call, pcm)
{:noreply, state}
end
Sippy encodes the audio with the call's codec and sends it as 20 ms packets
right away; bytes that do not fill a packet wait for the next chunk. It does
not pace the audio, so do not send a recording faster than real time: play
files with Sippy.play_audio/2 instead. After a pause of more than half a
second, such as releasing push-to-talk, the next audio starts a new talk
spurt. Live audio stops a file that is playing and digits being sent.
Examples
The examples
directory of the repository has two scripts you can run against your PBX
with mix run:
announcement.exscalls a number, plays a file and prints the result.menu.exsregisters 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. Sippy sends audio from WAV files or live PCM, and hands the other side's audio to the application without a jitter buffer.
- 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?/1returnstrue. - 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 © 2026 Herman verschooten. Released under the MIT License.