Outbound Phone Calls with Voicemail Detection
Placing a call
Each provider has a function that places a call and streams it to your server once picked up. The stream then opens on the same WebSocket route as an incoming call, and runs the same way.
import { twilioCall } from '@micdrop/telephony'
const { callId } = await twilioCall({ accountSid: process.env.TWILIO_ACCOUNT_SID, authToken: process.env.TWILIO_AUTH_TOKEN, to: '+14155550100', from: '+15550001111', // A number of your account url: 'wss://example.com/twilio/stream', parameters: { purpose: 'appointment reminder' },})| Provider | Function | What it takes beyond the numbers |
|---|---|---|
| Twilio | twilioCall() | accountSid, authToken, url and parameters of the stream |
| Vonage | vonageCall() | applicationId, privateKey, url and parameters of the stream |
| Telnyx | telnyxCall() | apiKey, connectionId, url and parameters of the stream |
| Plivo | plivoCall() | authId, authToken, and answerUrl, a route that answers with plivoStreamXml() |
The parameters reach waitForParams as for an incoming call. On Vonage and
Telnyx, the transport marks the call as outbound in transport.call.direction,
and Twilio tells it through its API when a transfer needs it, so a transfer
from an outbound call shows your number by default.
Vonage gives a call its identifier only once created, so vonageCall() hands
it to the transport in memory: the stream has to land on the process that
placed the call.
Detecting an answering machine
With machineDetection, the provider listens to who picked up while the call
goes on. The assistant starts talking at once, and stops if it turns out to be
a machine.
const { callId } = await twilioCall({ // ... machineDetection: true, machineDetectionUrl: 'https://example.com/twilio/amd',})The provider posts the result to a webhook of your server. A TelephonyCalls
routes it to the transport of the call, even when it comes before the stream
started:
import { TelephonyCalls } from '@micdrop/telephony'
const calls = new TelephonyCalls()
// The streamcalls.add(transport)
// The webhookapp.post('/twilio/amd', async (request) => { calls.twilioMachineDetection(request.body)})| Provider | Where the result arrives | Hand it to |
|---|---|---|
| Twilio | machineDetectionUrl | calls.twilioMachineDetection(body) |
| Vonage | eventUrl of the call | calls.vonageEvent(body) |
| Telnyx | The webhook of the application | calls.telnyxWebhook(body) |
| Plivo | machineDetectionUrl | calls.plivoMachineDetection(body) |
The transport emits MachineDetection with who picked up (human,
machine, fax or unknown) and whether the greeting of a machine is over.
Twilio, Vonage and Telnyx (with machineDetection: 'greeting_end') report the
end of the greeting, the beep, so a message starts at the right moment.
Leaving a message
answeringMachine() acts on the result: on a voicemail, the assistant stops
talking, waits for the end of the greeting, leaves the message and hangs up.
It hangs up on a fax, and leaves a person to the conversation.
import { answeringMachine } from '@micdrop/telephony'
answeringMachine(server, { message: 'Hi, this is Acme about your appointment tomorrow. Please call us back.',})Without message, the agent words it from the conversation, told it reached
a voicemail. message: false hangs up without one. onHuman and onMachine
let your application decide instead.
When the provider does not say when the greeting ends, the assistant waits for
a pause of the line, 1.5 seconds by default (silence), at most 20 seconds
(maxWait).
Voicemail without provider detection
voicemailTool() lets the model recognize a voicemail from what it hears,
“leave a message after the tone”, on any provider and without a webhook. The
model calls it, and the assistant leaves the message as above:
import { voicemailTool } from '@micdrop/telephony'
tools: [voicemailTool({ message: 'Hi, this is Acme, please call us back.' })]Both work together: the provider is quicker and hears the beep, the tool catches what it missed.