🎤Micdrop

Phone Voice Agent in Node.js with Twilio, Vonage, Telnyx or Plivo

@micdrop/telephony connects a phone number to a Micdrop server. When a call comes in, the provider streams it to a WebSocket of your server, and a telephony transport turns that stream into a Micdrop call: the same agent, the same speech to text and text to speech as in the browser.

Terminal window
npm install @micdrop/telephony @micdrop/vad onnxruntime-node
ProviderTransportAudioPage
Twilio, SignalWireTwilioTransportμ-law, 8 kHzTwilio
VonageVonageTransportLinear PCM, 16 kHzVonage
TelnyxTelnyxTransportμ-law or A-law, 8 kHzTelnyx
PlivoPlivoTransportμ-law, 8 kHzPlivo

How a call goes

  1. The provider calls the webhook of your number, and your server answers with a document that sends the call to a WebSocket: TwiML for Twilio, an NCCO for Vonage, Call Control options for Telnyx, XML for Plivo. Each transport comes with a function that writes it.
  2. The provider opens the WebSocket. Your server wraps it in the transport of the provider, reads the parameters of the call with waitForParams, and creates a MicdropServer.
  3. The caller speaks, the server detects the turns and answers. The assistant voice goes back at the rate of the line.
  4. The call ends when the caller hangs up, or when the agent ends it: the transport waits for the goodbye to be played, then hangs up.
import { MicdropServer, waitForParams } from '@micdrop/server'
import { TwilioTransport } from '@micdrop/telephony'
import '@micdrop/vad/node'
app.get('/phone', { websocket: true }, async (socket) => {
const transport = new TwilioTransport(socket)
const params = await waitForParams(transport, (value) => value)
new MicdropServer(transport, {
vad: 'silero',
firstMessage: 'Hello, thanks for calling. How can I help?',
agent,
stt,
tts,
})
})

Turns are found on the server

A phone streams the caller continuously, with nobody on the line to say when a sentence ends, so a telephony transport needs vad in the config. The server detects the turns with the same recorder the browser client runs, and a turn detector can wait for the end of a sentence. Silero tells a voice from the noise of a street or a car far better than a level threshold, which matters on a phone. import '@micdrop/vad/node' loads it on onnxruntime-node.

Interruptions

When the caller speaks over the assistant, the server cancels the answer and the transport asks the provider to drop the audio it still holds, so the assistant goes quiet at once. Every provider buffers the voice it is sent and plays it at its own pace, which is why the drop goes through the provider.

Parameters and the call

The parameters of the call come from the document that connected it: custom parameters of the TwiML, headers of the NCCO, client state on Telnyx, extra headers on Plivo. waitForParams returns them, and transport.call holds what the provider said about the call:

const transport = new TwilioTransport(socket)
const params = await waitForParams(transport, (value) => value)
console.log(transport.call)
// { callId: 'CA...', streamId: 'MZ...' }

callId is the identifier of the call at the provider, to act on it with its API: transfer it, record it, or hang it up from elsewhere.

Keypad

The keys the caller presses arrive as Dtmf events of the transport:

transport.on('Dtmf', (digit) => {
if (digit === '0') server.addUserMessage('I want to talk to a human')
})

Audio quality

A phone line carries 8 kHz, the band of a classic phone call. The transport brings it to the 16 kHz the speech to text expects, and brings the assistant voice down to the line with a low pass filter, so a voice synthesized at 24 kHz reaches the caller without a metallic hiss. Vonage streams at 16 kHz, twice the band of the others, which gives the speech to text clearer consonants.

Prefer a speech to text model trained on phone audio when the provider offers one. The latency of a phone call adds that of the network of the provider, about 100 to 200 ms on top of a browser call.

Testing on your machine

The provider needs to reach your webhook and your WebSocket from the internet. A tunnel such as ngrok or cloudflared gives your local server a public https and wss address to set in the console of the provider.

Hanging up

ProviderWhat ends the call
TwilioClosing the WebSocket, when nothing follows the stream in the TwiML
VonageClosing the WebSocket, when nothing follows the connect action
TelnyxThe hang up command of Call Control, sent when the transport has an apiKey
PlivoClosing the WebSocket with keepCallAlive, or the Hangup API when the transport has credentials

server.close() hangs up from your code, as the agent does.