🎤Micdrop

Telnyx Voice Agent in Node.js with Media Streaming

TelnyxTransport runs a Micdrop call on the bidirectional media streaming of Telnyx, in its RTP mode. The caller arrives in G.711 at 8 kHz, μ-law or A-law, and the assistant goes back the same way. Read phone calls first for what every provider shares.

Answer the call

With Call Control, answer the call.initiated webhook with an answer command that carries the options of telnyxStreamOptions:

import { telnyxStreamOptions } from '@micdrop/telephony'
app.post('/telnyx/webhook', async (request) => {
const { data } = request.body as any
if (data.event_type !== 'call.initiated') return {}
await fetch(
`https://api.telnyx.com/v2/calls/${data.payload.call_control_id}/actions/answer`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.TELNYX_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(
telnyxStreamOptions({
url: 'wss://example.com/telnyx/stream',
parameters: { lang: 'en' },
})
),
}
)
return {}
})

The parameters travel in the client_state of the call, which the transport decodes for waitForParams.

Run the stream

import { MicdropServer, waitForParams } from '@micdrop/server'
import { TelnyxTransport } from '@micdrop/telephony'
import '@micdrop/vad/node'
app.get('/telnyx/stream', { websocket: true }, async (socket) => {
const transport = new TelnyxTransport(socket, {
apiKey: process.env.TELNYX_API_KEY,
})
const { lang } = await waitForParams(transport, (params) => params)
new MicdropServer(transport, {
vad: 'silero',
firstMessage: 'Hello, thanks for calling. How can I help?',
agent,
stt,
tts,
})
})

transport.call holds the call_control_id as callId, and the numbers of the call as from and to.

Hanging up

Telnyx keeps a call going when its stream closes. Give the transport your apiKey, and it hangs up through Call Control once the goodbye has played. Without it, the stream ends and the caller stays on the line until they hang up.

Telnyx reconnects a stream closed with a code other than 1000, 1007, 1008 or 1011. A reconnection reaches your server as a new WebSocket, and so as a new call that starts from an empty conversation.

Codec

PCMU is the default. For PCMA, the codec of most networks outside North America and Japan, set it on both sides:

telnyxStreamOptions({ url, codec: 'PCMA' })
new TelnyxTransport(socket, { codec: 'PCMA', apiKey })