Call
A Call object represents an active call. You can get instances of a Call object from a Voice.Client, by answering or initiating calls.
Examples
Dialing a phone number and playing a message.
import { Voice } from "@signalwire/realtime-api";
const client = new Voice.Client({
project: "<project-id>",
token: "<api-token>",
topics: ["office"],
});
const call = await client.dialPhone({
from: "+YYYYYYYYYY",
to: "+XXXXXXXXXX",
});
await call.playTTS({ text: "Welcome to SignalWire!" });
Answering an incoming phone call and playing a message.
import { Voice } from "@signalwire/realtime-api";
const client = new Voice.Client({
project: "<project-id>",
token: "<api-token>",
topics: ["office"],
});
client.on("call.received", async (call) => {
console.log("Got call", call.from, call.to);
try {
await call.answer();
console.log("Inbound call answered");
await call.playTTS({ text: "Welcome to SignalWire!" });
} catch (error) {
console.error("Error answering inbound call", error);
}
});
Properties
device
• device: any
direction
• direction: "inbound"
| "outbound"
Whether you are making or receiving the call.
from
• from: string
The phone number that the call is coming from.
headers
• Optional
headers: SipHeader
[]
id
• Readonly
id: string
Unique id for this voice call
state
• state: created
| ringing
| answered
| ending
| ended
The current state of the call.
to
• to: string
The phone number you are attempting to call.
type
• type: "phone"
| "sip"
The type of call. Only phone and sip are currently supported.
Methods
amd
▸ amd(params?
): Promise<CallDetect>
- See CallDetect for more details.
Detects the presence of an answering machine. Alias for detectAnsweringMachine.
answer
▸ answer(): Promise<Call>
- See Call for more details.
Answers the incoming call.
Returns
Promise<Call>
- See Call for more details.
Example
client.on("call.received", async (call) => {
try {
await call.answer();
console.log("Inbound call answered");
} catch (error) {
console.error("Error answering inbound call", error);
}
});
collect
collect(params
): Promise<CallCollect>
- See CallCollect for more details.
Collect user input. For methods that include a prompt to the user, please see promptAudio, promptRingtone, or promptTTS.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.continuous? | boolean | Detect utterances and digits until stopped. Defaults to false. |
params.digits? | CollectDigitsConfig | Configuration for collecting digits. You must either set this, or speech . |
params.speech? | CollectSpeechConfig | Configuration for collecting speech. You must either set this, or digits . |
params.initialTimeout? | number | Number of seconds to wait for initial input before giving up. Default is 4 seconds. Will be used only when startInputTimers is true or when the timer is started manually via the startInputTimers method. |
params.partialResults? | boolean | If true, partial result events are fired. Default is false. |
params.sendStartOfInput? | boolean | If true, the startOfInput event is fired when input is detected. Default is false. |
params.startInputTimers? | boolean | If true, the initialTimeout timer is started. Default is false. |
Returns
Promise<CallCollect>
- See CallCollect for more details.
Examples
Collecting digits input:
const collect = await call.collect({
digits: {
max: 5,
digitTimeout: 2,
terminators: "#*",
},
});
const { type, digits, terminator } = await collect.ended();
Collecting speech input:
const collect = await call.collect({
speech: {
endSilenceTimeout: 2,
speechTimeout: 10,
language: "en-US",
hints: ["sales", "support", "representative"],
},
});
const { type, speech } = await collect.ended();
Collecting digits and listening for results using the events:
call.on("collect.started", (collect) => {
console.log("collect.started", collect);
});
call.on("collect.startOfInput", (collect) => {
console.log("Input collection started.");
});
call.on("collect.updated", (collect) => {
console.log("collect.updated", collect.digits);
});
call.on("collect.ended", (collect) => {
console.log("collect.ended", collect.digits);
});
call.on("collect.failed", (collect) => {
console.log("collect.failed", collect.reason);
});
const collect = await call.collect({
digits: {
max: 4,
digitTimeout: 10,
terminators: "#",
},
partialResults: true,
sendStartOfInput: true,
});
connect
▸ connect(params
): Promise<Call>
- See Call for more details.
Attempt to connect an existing call to a new outbound call. The two devices will hear each other. You can wait until the new peer is disconnected by calling disconnected.
This is a generic method that allows you to connect to multiple devices in series, parallel, or combinations of both with the use of a DeviceBuilder. For simpler use cases, prefer using connectPhone or connectSip.
Parameters
Name | Type | Description |
---|---|---|
params | VoiceDeviceBuilder | { devices: VoiceDeviceBuilder ; ringback?: VoicePlaylist } | Pass only the Dialer specifying the devices to call or an object with the Dialer and Ringback audio to play to call leg. You can play audio, TTS, silence or ringtone. See VoicePlaylist for more details. |
Returns
Promise<Call>
- See Call for more details.
A promise that resolves to a Call
object that you can use to control the new peer. The promise resolves only after the new peer picks up the call.
Examples
Connecting to a new SIP call.
const plan = new Voice.DeviceBuilder().add(
Voice.DeviceBuilder.Sip({
from: "sip:user1@domain.com",
to: "sip:user2@domain.com",
timeout: 30,
})
);
const peer = await call.connect(plan);
await call.playTTS({ text: "You are peer 1" });
await peer.playTTS({ text: "You are peer 2" });
await call.disconnected();
await call.playTTS({ text: "The peer disconnected" });
Connecting to a new SIP call, while playing ringback audio.
const plan = new Voice.DeviceBuilder().add(
Voice.DeviceBuilder.Sip({
from: "sip:user1@domain.com",
to: "sip:user2@domain.com",
timeout: 30,
})
);
const ringback = new Voice.Playlist().add(
Voice.Playlist.Ringtone({
name: "it",
})
);
const peer = await call.connect({
devices: plan,
ringback: ringback,
});
connectPhone
▸ connectPhone(params
): Promise<Call>
- See Call for more details.
Attempt to connect an existing call to a new outbound phone call. The two devices will hear each other. You can wait until the new peer is disconnected by calling disconnected.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.callStateEvents | string[] | An optional array of event names to be notified about. Allowed values are created , ringing , answered , and ended . Default is ended . |
params.callStateUrl | string | Optional webhook URL to which SignalWire will send call status change notifications. See the payload specifications under CallState . |
params.from | string | The party the call is coming from. Must be a SignalWire number or SIP endpoint that you own. |
params.maxPricePerMinute? | number | The maximum price in USD acceptable for the call to be created. If the rate for the call is greater than this value, the call will not be created. If not set, all calls will be created. Price can have a maximum of four decimal places, i.e. 0.0075. |
params.ringback? | VoicePlaylist | Ringback audio to play to call leg. You can play audio, TTS, silence or ringtone. |
params.timeout? | number | The time, in seconds, the call will ring before it is considered unanswered. |
params.to | string | The party you are attempting to call. |
Returns
Promise<Call>
- See Call for more details.
A promise that resolves to a Call
object that you can use to control the new peer. The promise resolves only after the new peer picks up the call.
Example
const peer = await call.connectPhone({
from: "+xxxxxx",
to: "+yyyyyy",
timeout: 30,
});
await call.playTTS({ text: "You are peer 1" });
await peer.playTTS({ text: "You are peer 2" });
await call.disconnected();
await call.playTTS({ text: "The peer disconnected" });
connectSip
▸ connectSip(params
): Promise<Call>
- See Call for more details.
Attempt to connect an existing call to a new outbound SIP call. The two devices will hear each other. You can wait until the new peer is disconnected by calling disconnected.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.callStateEvents | string[] | An optional array of event names to be notified about. Allowed values are created , ringing , answered , and ended . Default is ended . |
params.callStateUrl | string | Optional webhook URL to which SignalWire will send call status change notifications. See the payload specifications under CallState . |
params.codecs? | SipCodec [] | Optional array of desired codecs in order of preference. Supported values are PCMU, PCMA, OPUS, G729, G722, VP8, H264. Default is parent leg codec(s). |
params.from | string | The party the call is coming from. Must be a SignalWire number or SIP endpoint that you own. |
params.headers? | SipHeader [] | Array of SipHeader objects. Must be X- headers only, see example below. |
params.maxPricePerMinute? | number | The maximum price in USD acceptable for the call to be created. If the rate for the call is greater than this value, the call will not be created. If not set, all calls will be created. Price can have a maximum of four decimal places, i.e. 0.0075. |
params.ringback? | VoicePlaylist | Ringback audio to play to call leg. You can play audio, TTS, silence or ringtone. |
params.timeout? | number | The time, in seconds, the call will ring before it is considered unanswered. |
params.to | string | The party you are attempting to call. |
params.webrtcMedia? | boolean | If true, WebRTC media is negotiated. Default is parent leg setting. |
params.sessionTimeout? | number | Non-negative value, in seconds, to use for the SIP Session-Expires header. If 0 or unset, SignalWire will pick the default (typically 600). |
Returns
Promise<Call>
- See Call for more details.
A promise that resolves to a Call
object that you can use to control the new peer. The promise resolves only after the new peer picks up the call.
Example
const peer = await call.connectSip({
from: "sip:user1@domain.com",
to: "sip:user2@domain.com",
timeout: 30,
headers: [
{ name: "X-SomeKeyA", value: "SomeValueA" },
{ name: "X-SomeKeyB", value: "SomeValueB" },
],
});
await call.playTTS({ text: "You are peer 1" });
await peer.playTTS({ text: "You are peer 2" });
await call.disconnected();
await call.playTTS({ text: "The peer disconnected" });
detect
▸ detect(params
): Promise<CallDetect>
- See CallDetect for more details.
Generic method. Please see amd, detectFax, detectDigit.
Parameters
Name | Type | Description |
---|---|---|
params | object | - |
params.type | "fax" | "machine" | "digit" | "beep" |
|
Returns
Promise<CallDetect>
- See CallDetect for more details.
detectAnsweringMachine
▸ detectAnsweringMachine(params?
): Promise<CallDetect>
- See CallDetect for more details.
Detects the presence of an answering machine.
Parameters
Name | Type | Description |
---|---|---|
params? | Object | - |
params.endSilenceTimeout? | number | Number of seconds to wait for voice to finish. Defaults to 1.0. |
params.initialTimeout? | number | Number of seconds to wait for initial voice before giving up. Defaults to 4.5. |
params.machineVoiceThreshold? | number | How many seconds of voice to decide it is a machine. Defaults to 1.25. |
params.machineWordsThreshold? | number | How many words to count to decide it is a machine. Defaults to 6. |
params.timeout? | number | Number of seconds to run the detector for. Defaults to 30.0. |
params.waitForBeep? | boolean | Whether to wait until the device is ready for voicemail delivery. Defaults to false. |
params.detect_interruptions? | boolean | If set to true, a NOT_READY event is fired if speech is detected after the READY event. This allows application to restart message delivery to answering machine. Defaults to false. |
Returns
Promise<CallDetect>
- See CallDetect for more details.
Example
const detect = await call.detectAnsweringMachine();
const result = await detect.ended();
console.log("Detect result:", result.type);
detectDigit
▸ detectDigit(params?
): Promise<CallDetect>
- See CallDetect for more details.
Detects when a digit is pressed in an audio stream. To gather digit input from a caller, please see prompt.
Parameters
Name | Type | Description |
---|---|---|
params? | Object | - |
params.digits? | string | The digits to detect. Defaults to "0123456789#*". |
params.timeout? | number | Number of seconds to run the detector for. Defaults to 30.0. |
params.waitForBeep? | boolean | Whether to wait until the device is ready for voicemail delivery. Defaults to false. |
Returns
Promise<CallDetect>
- See CallDetect for more details.
Example
const detect = await call.detectDigit();
const result = await detect.ended();
console.log("Detect result:", result.type);
detectFax
▸ detectFax(params?
): Promise<CallDetect>
- See CallDetect for more details.
Detects the presence of a fax machine.
Parameters
Name | Type | Description |
---|---|---|
params? | Object | - |
params.timeout? | number | Number of seconds to run the detector for. Defaults to 30.0. |
params.tone? | "CED" | "CNG" | The fax tone to detect: CED or CNG . Defaults to CED . |
params.waitForBeep? | boolean | Whether to wait until the device is ready for voicemail delivery. Defaults to false. |
Returns
Promise<CallDetect>
- See CallDetect for more details.
Example
const detect = await call.detectFax();
const result = await detect.ended();
console.log("Detect result:", result.type);
dial
▸ dial(params
): Promise<Call>
- See Call for more details.
Create a new outbound call.
This is a generic method that allows you to dial multiple devices in series, parallel, or combinations of both with the use of a DeviceBuilder. For simpler use cases, prefer using dialPhone or dialSip.
Parameters
Name | Type | Description |
---|---|---|
params | VoiceDeviceBuilder | { devices: VoiceDeviceBuilder ; region: string } | Pass only the Dialer specifying the devices to call or an object with the Dialer and a Region of the world to originate the message from. |
Returns
Promise<Call>
- See Call for more details.
disconnect
▸ disconnect(): Promise<void>
Returns
Promise<void>
disconnected
▸ disconnected(): Promise<Call>
- See Call for more details.
Call this method after connecting a peer (e.g., using connect, connectPhone, or connectSip) to wait until the peer disconnects.
This is equivalent to calling peer.waitFor("ended")
on the connected peer.
Returns
Promise<Call>
- See Call for more details.
Example
const plan = new Voice.DeviceBuilder().add(
Voice.DeviceBuilder.Sip({
from: "sip:user1@domain.com",
to: "sip:user2@domain.com",
timeout: 30,
})
);
const peer = await call.connect(plan);
await call.disconnected(); // same as `peer.waitFor("ended")`
await call.playTTS({ text: "The peer disconnected" });
hangup
▸ hangup(reason?
): Promise<void>
Hangs up the call.
Parameters
Name | Type | Description |
---|---|---|
reason? | "error" | "hangup" | "cancel" | "busy" | "noAnswer" | "decline" | Optional reason for hanging up. |
Returns
Promise<void>
Example
call.hangup();
off
▸ off(event
, fn?
)
Remove an event handler.
Parameters
Name | Type | Description |
---|---|---|
event | string | Name of the event. See Events for the list of available events. |
fn? | Function | An event handler which had been previously attached. |
on
▸ on(event
, fn
)
Attaches an event handler to the specified event.
Parameters
Name | Type | Description |
---|---|---|
event | string | Name of the event. See Events for the list of available events. |
fn | Function | An event handler. |
Example
In the below example, we are listening for the call.state
event and logging the current call state to the console.
This means this will be triggered every time the call state changes.
call.on("call.state", (call) => {
console.log("call state changed:", call.state);
});
once
▸ once(event
, fn
)
Attaches an event handler to the specified event. The handler will fire only once.