promptAudio

View as Markdown

promptAudio

Play an audio while collecting user input from the call, such as digits or speech.

Parameters

params
objectRequired

Object containing the parameters for prompting the user for input while playing audio.

url
stringRequired

HTTP(s) URL to an audio resource to play.

digits
CollectDigitsConfig

Configuration for collecting digits. You must either set this, or speech. See CollectDigitsConfig.

speech
CollectSpeechConfig

Configuration for collecting speech. You must either set this, or digits. Pass an empty object to use the default configuration. See CollectSpeechConfig.

initialTimeout
numberDefaults to 4

Initial timeout in seconds.

volume
numberDefaults to 0

Volume value between -40dB and +40dB where 0 is unchanged.

listen
object

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

Returns

Promise<CallPrompt>

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

Examples

Digits Example

In this example, we dial a phone number and prompt for digits while playing audio in the background. After the digits are entered or a timeout occurs, the prompt session will end and return the digits results, and the call will hangup.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 console.log("Call received");
12 // Answer the call
13 call.answer();
14 // Play audio on the call while prompting for digits. After the digits are entered or a timeout occurs, the prompt session will end and return the digits results, and the call will hangup.
15 await call.promptAudio({
16 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
17 digits: {
18 max: 4,
19 digitTimeout: 10,
20 terminators: "#*"
21 },
22 listen: {
23 onStarted: () => console.log("Prompt started!"),
24 onFailed: () => console.log("Prompt failed!"),
25 onUpdated: (promptResult) => console.log("Prompt updated!", promptResult.result),
26 onEnded: (promptResult) => {
27 console.log("Prompt ended!", promptResult.result);
28 call.hangup();
29 }
30 }
31 }).onStarted();
32 }
33});

Speech Example

In this example, we dial a phone number and prompt for speech while playing audio in the background. After the digits are entered or a timeout occurs, the prompt session will end and return the digits results, and the call will hangup.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 console.log("Call received");
12 // Answer the call
13 call.answer();
14 // Play TTS on the call
15 await call.promptAudio({
16 url: "https://cdn.signalwire.com/default-music/welcome.mp3",
17 speech: {
18 model: "enhanced.phone_call",
19 },
20 listen: {
21 onStarted: () => console.log("Prompt started!"),
22 onFailed: () => console.log("Prompt failed!"),
23 onUpdated: (promptResult) => console.log("Prompt updated!", promptResult.result),
24 onEnded: (promptResult) => {
25 console.log("Prompt ended!", promptResult.result);
26 call.hangup();
27 }
28 }
29 }).onStarted();
30 }
31});