🎤Micdrop

Phone Keypad (DTMF): Read the Keys of the Caller, Press Keys in a Menu

The keys of the caller

Every telephony transport emits the keys pressed on the phone as Dtmf events, one per key: 0 to 9, *, #, and A to D.

transport.on('Dtmf', (digit) => console.log(`Key ${digit}`))

dtmfInput() turns them into what the caller said: it gathers the keys until #, a count or a pause, adds them to the conversation as a user message and lets the agent answer. A caller can type a customer number or pick a menu option while the agent follows.

import { dtmfInput } from '@micdrop/server'
dtmfInput(server, { maxDigits: 8 })
// The conversation gets: "The user typed 12345678 on the keypad."

The first key stops the assistant if it was speaking, as speaking over it does. The digits also go in the metadata of the message, as dtmf.

OptionDefaultDescription
endKey'#'Key that ends the input at once, left out of the digits. false to end only by count or pause
maxDigitsEnds the input once this many digits were typed
timeout3000Pause after the last key that ends the input, in ms
interrupttrueStops the assistant at the first key
answertrueAdds the input to the conversation and answers it. Off, only onInput hears of it
formatWrites the user message from the digits
onInputCalled with each complete input. Returning false keeps it out of the conversation

dtmfInput() returns a function that stops it, and stops on its own when the call ends. It reads the events of the transport, so it works with any transport that emits them.

Pressing keys in a phone menu

When the assistant calls a company, it may meet a menu: “press 1 for sales”. sendDtmfTool() lets the model press keys. It stays quiet meanwhile, and the keys go once what it was saying has been played.

import { sendDtmfTool } from '@micdrop/telephony'
const agent = new OpenaiAgent({
apiKey,
systemPrompt: 'You call the pharmacy to check whether an order is ready...',
tools: [sendDtmfTool()],
})

The model sends 0 to 9, * and #, with w for a pause of half a second. Your code can press keys too, with transport.sendDtmf('1w234#').

ProviderHow the keys go
TwilioAs the tones of a keypad in the audio, since the stream carries no other way
VonageWith the DTMF command of the Voice API, given the credentials of the application, in the audio otherwise
TelnyxWith the send_dtmf command of Call Control, given apiKey, in the audio otherwise
PlivoWith the sendDTMF message of the stream

Tones in the audio reach the menus that listen to the line, which most do. A system that reads keys only from signaling may miss them, prefer a provider that sends them through its API in that case.