Appearance
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.
Properties
cond
object[]Required
Array of when-then and else conditions
Properties for when-then conditions
cond[].when
stringRequired
The JavaScript condition to act on
cond[].then
object[]Required
Sequence of SWML Methods to execute when the condition evaluates to true
Properties for else condition
cond[].else
object[]
Sequence of SWML Methods to execute when none of the other conditions evaluate to true
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
YAMLJSON
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
YAMLJSON
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(' ')}"See Also
- Variables and Expressions: Complete reference for SWML variables, scopes, and using variables in conditional logic
- switch: Alternative conditional logic method
