cond
Execute a sequence of instructions depending on the value of a JavaScript condition.
Parameters
The cond
statement expects an array of conditions. Each condition is an object with a when
and a then
property, with the
exception of a single, optional condition with just an else
property.
Name | Type | Required? | Description |
---|---|---|---|
cond | array | Required | Array of when-then and else conditions |
Parameters for when-then
conditions
Name | Type | Required? | Description |
---|---|---|---|
when | string | Required | The JavaScript condition to act on |
then | array | Required | Sequence of instructions to execute when the condition evaluates to true |
Parameters for else
condition (optional)
Name | Type | Required? | Description |
---|---|---|---|
else | array | Optional | Sequence of instructions to execute when none of the other conditions evaluate to true |
warning
The JavaScript condition string already has access to all the document variables. Using the variable
substitution operator (%{var}
) inside this string might result in inconsistent behavior.
❌ when: "%{call.type.toLowerCase() == 'sip'}"
❌ when: "%{prompt_value} == 1"
✅ when: "call.type.toLowerCase() == 'sip'"
Examples
Tell the caller what he's calling from
- YAML
- JSON
version: 1.0.0
sections:
main:
# in a noninstructional context, you might simply want to use
# play: "say:You're calling from %{call.type}."
- cond:
- when: "call.type.toLowerCase() == 'sip'"
then:
- play: "say: You're calling from SIP."
- when: "call.type.toLowerCase() == 'phone'"
then:
- play: "say: You're calling from phone."
{
"version": "1.0.0",
"sections": {
"main": [
{
"cond": [
{
"when": "call.type.toLowerCase() == 'sip'",
"then": [
{
"play": "say: You're calling from SIP."
}
]
},
{
"when": "call.type.toLowerCase() == 'phone'",
"then": [
{
"play": "say: You're calling from phone."
}
]
}
]
}
]
}
}
Perform tasks based on user input
- YAML
- JSON
version: 1.0.0
sections:
main:
- prompt:
play: "say: Press 1 to listen to music;
2 to hear your phone number;
and anything else to hang up"
- cond:
- when: "%{prompt_value} == 1"
then:
- play: "https://cdn.signalwire.com/swml/April_Kisses.mp3"
- execute: main
- when: "%{prompt_value} == 2"
then:
- transfer: say_phone_number
- execute: main
- else:
- hangup
say_phone_number:
# The `.split('').join(' ')`` adds a space between each digit of the phone number,
# making sure the TTS spells out each digit one by one
- play: "say: %{call.from.split('').join(' ')}"
{
"version": "1.0.0",
"sections": {
"main": [
{
"prompt": {
"play": "say: Press 1 to listen to music; 2 to hear your phone number; and anything else to hang up"
}
},
{
"cond": [
{
"when": "%{prompt_value} == 1",
"then": [
{
"play": "https://cdn.signalwire.com/swml/April_Kisses.mp3"
},
{
"execute": "main"
}
]
},
{
"when": "%{prompt_value} == 2",
"then": [
{
"transfer": "say_phone_number"
},
{
"execute": "main"
}
]
},
{
"else": [
"hangup"
]
}
]
}
],
"say_phone_number": [
{
"play": "say: %{call.from.split('').join(' ')}"
}
]
}
}