Skip to main content

VoiceResponse

VoiceResponse ⇐ Verb

Use the VoiceResponse object, to construct advance Interactive Voice Response (IVR) applications.

Kind: global class
Extends: Verb
See: module:core:APIClient

new VoiceResponse(request)

Constructs a new VoiceResponse object.

ParamTypeDescription
requestVoiceRequestOptions to indicate the objects endpoint

Example

import { VoiceServer } from "@fonoster/voice";

async function handler (request, response) {
await response.answer();
await response.play("sound:hello-world");
}

const voiceServer = new VoiceServer({base: '/voiceapp'})
voiceServer.listen(handler, { port: 3000 })

voiceResponse.use(plugin)

Adds a tts or asr plugin. Only one type of plugin can be attached.

Kind: instance method of VoiceResponse
See

  • GoogleTTS
  • GoogleASR
Param
plugin

voiceResponse.play(media, options)

Play an audio in the channel.

Kind: instance method of VoiceResponse
See: Playback

ParamTypeDescription
mediastringSound name or uri with audio file
optionsPlayOptionsOptional parameters to alter the command's normal behavior
options.offsetstringMilliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
options.skipstringMilliseconds to skip for forward/reverse operations
options.playbackIdstringPlayback identifier to use in Playback operations

Example

async function handler (request, response) {
await response.answer();
await response.play("https://soundsserver:9000/sounds/hello-world.wav");
}

voiceResponse.say(text, options)

Converts a text into a sound and sends sound to media server. To use this verb, you must first setup a TTS plugin such as GoogleTTS, or AWS PollyTTS

Kind: instance method of VoiceResponse
See

  • Play
  • Voice.use
ParamTypeDescription
textstringConverts a text into a sound and sends sound to media server
optionsSayOptionsOptional parameters to alter the command's normal behavior
options.offsetstringMilliseconds to skip before playing
options.skipstringMilliseconds to skip for forward/reverse operations
options.playbackIdstringPlayback identifier to use in Playback operations

Example

async function handler (request, response) {
await response.answer();
response.use(new GoogleTTS())
await response.say("Hello workd"); // Plays the sound using GoogleTTS's default values
}

voiceResponse.gather(options)

Waits for data entry from the user's keypad or from a speech provider.

Kind: instance method of VoiceResponse
Note: When including speech the default timeout is 10000 (10s).
See: SpeechProvider

ParamTypeDescription
optionsGatherOptionsOptions to select the maximum number of digits, final character, and timeout
options.numDigitsnumberMilliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
options.timeoutnumberMilliseconds to wait before timeout. Defaults to 4000. Use zero for no timeout.
options.finishOnKeystringOptional last character to wait for. Defaults to '#'. It will not be included in the returned digits
options.sourcestringWhere to listen as input source. This option accepts dtmf and speech. A speech provider must be configure when including the speech source. You might inclue both with dtmf,speech. Defaults to dtmf

Example

async function handler (request, response) {
await response.answer();
const digits = await response.gather({source: "dtmf,speech", numDigits: 3});
console.log("digits: " + digits);
}

voiceResponse.sgather(options) ⇒ SGatherStream

Waits for data entry from the user's keypad or from a stream speech provider. This command is different from gather in that it returns a stream of results instead of a single result. You can think of it as active listening.

Kind: instance method of VoiceResponse
Returns: SGatherStream - The SGatherStream fires events via the on method for transcription, dtmf, and error. And the stream can be close with the close function.
See: StreamSpeechProvider

ParamTypeDescription
optionsSGatherOptionsOptions object for the SGather verb
options.sourcestringWhere to listen as input source. This option accepts dtmf and speech. A speech provider must be configure when including the speech source. You might inclue both with dtmf,speech. Defaults to speech,dtmf

Example

async function handler (request, response) {
await response.answer();
const stream = await response.sgather({source: "dtmf,speech"});

stream.on("transcript", (text, isFinal) => {
console.log("transcript: %s", text);
})

stream.on("dtmf", digit => {
console.log("digit: " + digit);
if (digit === "#") stream.close();
})
}

voiceResponse.dtmf(options)

Sends dtmf tones to the current session.

Kind: instance method of VoiceResponse

ParamTypeDescription
optionsDtmfOptionsOptions object for the Dtmf verb
options.dtmfstringA string of the dtmf tones

Example

async function handler (request, response) {
await response.answer();
await response.play("sound:hello-world");
await response.dtmf({dtmf: "1234"});
}

voiceResponse.dial(destination, options) ⇒ StatusStream

Forwards the call to an Agent or the PSTN.

Kind: instance method of VoiceResponse
Returns: StatusStream - The StatusStream fires events via the on method for progress, answer, noanswer, and busy. And the stream can be close with the close function.

ParamTypeDescription
destinationstringNumber or Agent to forward the call to
optionsDialOptionsOptions object for the Dial verb
options.timeouttimeoutDial timeout

Example

async function handler (request, response) {
await response.answer();
await response.say("dialing number");
const stream = await response.dial("17853178070");
stream.on("progress", console.log)
stream.on("answer", console.log)
stream.on("busy", console.log)
}

voiceResponse.playback(playbackId)

Returns a PlaybackControl control object.

Kind: instance method of VoiceResponse
See: Play

ParamTypeDescription
playbackIdstringPlayback identifier to use in Playback operations

Example

async function handler (request, response) {
await response.answer();
response.onDtmfReceived(async(digit) => {
const control = response.playback("1234")
digit === "3"
? await control.restart()
: await control.forward()
})

await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
playbackId: "1234"
});
}

voiceResponse.on(handler)

Listens event publication.

Kind: instance method of VoiceResponse

ParamTypeDescription
handlerfunctionEvent handler

Example

async function handler (request, response) {
await response.answer();
response.on("DtmfReceived", async(digit) => {
const control = response.playback("1234")
digit === "3"
? await control.restart()
: await control.forward()
})

await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
playbackId: "1234"
});
}

voiceResponse.mute(options)

Mutes a channel.

Kind: instance method of VoiceResponse
See: unmute

ParamTypeDescription
optionsMuteOptionsIndicate which direction of he communication to mute
options.directionstringPossible values are 'in', 'out', and 'both'

Example

async function handler (request, response) {
await response.answer();
await response.mute(); // Will mute both directions
}

voiceResponse.unmute(options)

Unmutes a channel.

Kind: instance method of VoiceResponse
See: mute

ParamTypeDescription
optionsMuteOptionsIndicate which direction of he communication to unmute
options.directionstringPossible values are 'in', 'out', and 'both'

Example

async function handler (request, response) {
...
await response.unmute({direction: "out"}); // Will unmute only the "out" direction
}

voiceResponse.answer()

Answer the communication channel. Before running any other verb you must run the anwer command.

Kind: instance method of VoiceResponse
Example

async function handler (request, response) {
await response.answer();
...
}

voiceResponse.hangup()

Terminates the communication channel.

Kind: instance method of VoiceResponse
Example

async function handler (request, response) {
...
await response.hangup();
}

voiceResponse.record(options) ⇒ Promise.<RecordResult>

Records the current channel and uploads the file to the storage subsystem.

Kind: instance method of VoiceResponse
Returns: Promise.<RecordResult> - Returns useful information such as the duration of the recording, etc.

ParamTypeDescription
optionsRecordOptionsoptional parameters to alter the command's normal behavior
options.maxDurationnumberMaximum duration of the recording, in seconds. Use 0 for no limit
options.maxSilencenumberMaximum duration of silence, in seconds. Use 0 for no limit
options.beepbooleanPlay beep when recording begins
options.finishOnKeystringDTMF input to terminate recording

Example

async function handler (request, response) {
await response.answer();;
const result = await response.record({finishOnKey: "#"});
console.log("recording result: " + JSON.stringify(result)) // recording result: { duration: 30 ...}
}