cond
Execute a sequence of instructions depending on the value of a JavaScript condition.
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 | Default | Description |
---|---|---|---|
cond Required | object[] | - | Array of when-then and else conditions |
cond Parameters
Below are the parameters for the cond
statement.
Parameters for when-then
conditions
Name | Type | Default | Description |
---|---|---|---|
when Required | string | - | The JavaScript condition to act on |
then Required | object[] | - | Sequence of SWML Methods to execute when the condition evaluates to true |
Parameters for else
condition
Name | Type | Default | Description |
---|---|---|---|
else Optional | object[] | - | Sequence of SWML Methods 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:
- cond:
- when: call.type.toLowerCase() == 'sip'
then:
- play:
url: "say: You're calling from SIP."
- when: call.type.toLowerCase() == 'phone'
then:
- play:
url: "say: You're calling from phone."
{
"version": "1.0.0",
"sections": {
"main": [
{
"cond": [
{
"when": "call.type.toLowerCase() == 'sip'",
"then": [
{
"play": {
"url": "say: You're calling from SIP."
}
}
]
},
{
"when": "call.type.toLowerCase() == 'phone'",
"then": [
{
"play": {
"url": "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:
url: 'https://cdn.signalwire.com/swml/April_Kisses.mp3'
- execute:
dest: main
- when: call.type.toLowerCase() == 'phone'
then:
- transfer:
dest: say_phone_number
- execute:
dest: 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:
url: "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": {
"url": "https://cdn.signalwire.com/swml/April_Kisses.mp3"
}
},
{
"execute": {
"dest": "main"
}
}
]
},
{
"when": "call.type.toLowerCase() == 'phone'",
"then": [
{
"transfer": {
"dest": "say_phone_number"
}
},
{
"execute": {
"dest": "main"
}
}
]
},
{
"else": [
{
"hangup": {}
}
]
}
]
}
],
"say_phone_number": [
{
"play": {
"url": "say: %{call.from.split('').join(' ')}"
}
}
]
}
}