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.
| Option | Default | Description |
|---|---|---|
endKey | '#' | Key that ends the input at once, left out of the digits. false to end only by count or pause |
maxDigits | Ends the input once this many digits were typed | |
timeout | 3000 | Pause after the last key that ends the input, in ms |
interrupt | true | Stops the assistant at the first key |
answer | true | Adds the input to the conversation and answers it. Off, only onInput hears of it |
format | Writes the user message from the digits | |
onInput | Called 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#').
| Provider | How the keys go |
|---|---|
| Twilio | As the tones of a keypad in the audio, since the stream carries no other way |
| Vonage | With the DTMF command of the Voice API, given the credentials of the application, in the audio otherwise |
| Telnyx | With the send_dtmf command of Call Control, given apiKey, in the audio otherwise |
| Plivo | With 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.