Vonage Voice Agent in Node.js with WebSockets
VonageTransport runs a Micdrop call on the
WebSocket connection
of the Vonage Voice API. Audio travels as linear PCM at 16 kHz, twice the band
of a classic phone line, which the speech to text hears better. Read
phone calls first for what every provider shares.
Answer the call
Set the answer URL of your Vonage application to a route that answers with the
NCCO of vonageConnectNcco. Vonage calls it with the uuid, from and to
of the call, which the transport then knows:
import { vonageConnectNcco } from '@micdrop/telephony'
app.get('/vonage/answer', async (request) => { const { uuid, from, to } = request.query as Record<string, string> return vonageConnectNcco({ url: 'wss://example.com/vonage/stream', call: { uuid, from, to }, parameters: { lang: 'en' }, })})The parameters travel as the headers of the WebSocket endpoint, 2000 bytes at
most, and the transport hands them to waitForParams.
Run the stream
import { MicdropServer, waitForParams } from '@micdrop/server'import { VonageTransport } from '@micdrop/telephony'import '@micdrop/vad/node'
app.get('/vonage/stream', { websocket: true }, async (socket) => { const transport = new VonageTransport(socket) const { lang } = await waitForParams(transport, (params) => params)
new MicdropServer(transport, { vad: 'silero', firstMessage: 'Hello, thanks for calling. How can I help?', agent, stt, tts, })})The transport reads the rate from the first message of Vonage. Set
sampleRate: 8000 in vonageConnectNcco for a narrower band, or 24000,
which only part of the Vonage documentation lists.
Hanging up
Closing the WebSocket ends its leg of the call. With nothing after the
connect action in the NCCO, the call ends. When the agent ends the call, the
transport sends a notify after the goodbye and closes once Vonage confirms it
played.
Security
Set authorization: { type: 'vonage' } on the endpoint, by editing the NCCO,
and Vonage signs the WebSocket request with a JWT you can verify with the
Vonage SDK.