detectDigit

View as Markdown

detectDigit

Detects when a digit is pressed in an audio stream. To gather digit input from a caller, please see prompt.

Parameters

params
object

Object containing the parameters for digit detection.

digits
stringDefaults to 0123456789#*

The digits to detect.

timeout
numberDefaults to 30.0

Number of seconds to run the detector for.

waitForBeep
booleanDefaults to false

Whether to wait until the device is ready for voicemail delivery.

listen
object

Callback to listen for events. List of detect events can be found here. Example event: onStarted.

Returns

Promise<CallDetect>

A promise that resolves to a CallDetect object that you can use to view the current state and results of the detect session.

Example

In this example, we dial a phone number and wait for the digit detection to finish. Once the detection is finished, we log the result and then play a TTS message of the digit pressed and then hangup the call.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7await voiceClient.listen({
8 topics: ["office"],
9 onCallReceived: async (call) => {
10
11 // Answer the call
12 call.answer();
13 await call.detectDigit({
14 listen: {
15 onStarted: () => console.log("Detection started!"),
16 onEnded: (detect) => console.log("Finished detecting digit", detect.result),
17 onUpdated: async (detect) => {
18 console.log("Updated detecting digit", detect.result)
19 // Stop the detection and play the digit pressed
20 detect.stop();
21 await call.playTTS({ text: `You pressed ${detect.result})` });
22 call.hangup();
23 }
24 }
25 }).onStarted();
26 }
27});