cond

View as MarkdownOpen in Claude

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

1version: 1.0.0
2sections:
3 main:
4 - cond:
5 - when: call.type.toLowerCase() == 'sip'
6 then:
7 - play:
8 url: "say: You're calling from SIP."
9 - when: call.type.toLowerCase() == 'phone'
10 then:
11 - play:
12 url: "say: You're calling from phone."

Perform tasks based on user input

1version: 1.0.0
2sections:
3 main:
4 - prompt:
5 play: >-
6 say: Press 1 to listen to music; 2 to hear your phone number; and
7 anything else to hang up
8 - cond:
9 - when: '${prompt_value} == 1'
10 then:
11 - play:
12 url: 'https://cdn.signalwire.com/swml/April_Kisses.mp3'
13 - execute:
14 dest: main
15 - when: call.type.toLowerCase() == 'phone'
16 then:
17 - transfer:
18 dest: say_phone_number
19 - execute:
20 dest: main
21 - else:
22 - hangup: {}
23 say_phone_number:
24 # The `.split('').join(' ')`` adds a space between each digit of the phone number,
25 # making sure the TTS spells out each digit one by one
26 - play:
27 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