collect

View as Markdown

collect

collect(params): Promise<CallCollect>

Collect user input. For methods that include a prompt to the user, please see promptAudio, promptRingtone, or promptTTS.

Parameters

params
objectRequired

Object containing the parameters for collecting user input.

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. See CollectSpeechConfig.

continuous
booleanDefaults to false

Detect utterances and digits until stopped.

initialTimeout
numberDefaults to 4

Number of seconds to wait for initial input before giving up. Will be used only when startInputTimers is true or when the timer is started manually via the startInputTimers method.

partialResults
booleanDefaults to false

If true, partial result events are fired.

sendStartOfInput
booleanDefaults to false

If true, the startOfInput event is fired when input is detected.

startInputTimers
booleanDefaults to false

If true, the initialTimeout timer is started.

listen
object

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

Returns

Promise<CallCollect>

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

Examples

Digits Example

In this example, we collect digits from the user and log the result.

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 call.answer();
11 console.log("Call received", call.id);
12 let collectResult = await call.collect({
13 digits: {
14 max: 5,
15 digitTimeout: 2,
16 terminators: "#*"
17 }
18 }).onStarted();
19 const { type, digits, terminator } = await collectResult.ended();
20 console.log("Collected", type, digits, terminator);
21 call.hangup();
22 }
23});
Speech Example

In this example, we collect speech from the user and log the result.

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 call.answer();
11 console.log("Call received", call.id);
12 let collectResult = await call.collect({
13 speech: {
14 endSilenceTimeout: 2,
15 speechTimeout: 10,
16 language: "en-US",
17 hints: ["sales", "support", "representative"]
18 }
19 }).onStarted();
20 const { type, speech } = await collectResult.ended();
21 console.log("Collected", type, speech);
22 }
23});
CallCollect Events Example

In this example we are collecting digits and listening for results using the CallCollect Events:

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Setup a Voice Client and listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 call.answer();
12 console.log("Call received", call.id);
13
14 // Start a call collect session
15 await call.collect({
16 digits: {
17 max: 4,
18 digitTimeout: 10,
19 terminators: "#"
20 },
21 partialResults: true,
22 sendStartOfInput: true,
23 listen: {
24 onStarted: () => {
25 console.log("Collect started");
26 },
27 onInputStarted: (collect) => {
28 console.log("Collect input started:", collect.result);
29 },
30 onUpdated: (collect) => {
31 console.log("Collect updated:", collect.result);
32 },
33 onFailed: (collect) => {
34 console.log("Collect failed:", collect.result);
35 },
36 onEnded: async (collect) => {
37 console.log("Collect ended:", collect.result);
38
39 // Play back the digits collected
40 await call.playTTS({ text: `You entered ${collect.digits}` });
41 call.hangup()
42 }
43 }
44 }).onStarted();
45 }
46});