Conditions

You can carry out conditional logic in scenes using values from the Data model objects. The following sections describe the valid syntax for conditions.

Logical operators

Operator Description
&& Logical AND. Inner expressions are evaluated iteratively, and the evaluation is short-circuited if any expression evaluates to false.
|| Logical OR. Inner expressions are evaluated iteratively, and the evaluation is short-circuited if any expression evaluates to true
! Logical NOT. The evaluation of the inner expression is negated

Numerical and string operators

The following numerical and string operators are supported:

Operator Description
+ Add numbers or string concatenation
- Subtract numbers
* Multiply numbers
/ Divide numbers

Booleans

The following constant booleans are supported:

Constant Description
true Must be lowercase
false Must be lowercase
!false Evaluates to true. Must be lowercase.

Comparison operators

The following comparison operators are provided:

Operator Description
== Equals
!= Not equals
< Less than
<= Less than equals
> Greater than
>= Greater than equals

Lists and maps

Given a list named session.params.myList:

Syntax Description
x in session.params.myList Returns true if the value x is in session.params.myList
myList[x] Returns the value at index x of myList
size(session.params.myList) Returns the size of a list

Given a map named session.params.myMap:

Syntax Description
session.params.myMap == {"one": 1, "two":2} Returns true if maps are equal
session['params']['myMap']['one'] Returns the value with specified key
size(session.params.myMap) Returns the map size

Data model

The following objects can be used within scene conditions:

Syntax Description
intent Matched intent parameter data
scene Slot-filling data
session Session storage data
user User storage data
home Home storage data
device Device capability and location data
canvas Canvas state data
resources Localized project resources (audio, images, strings, etc.) data

The following is an example snippet of the full data model in JSON:

{
  "intent": {
    "params": {
      "<param_name>": {
        "original": "five people",
        "resolved": 5
      }
    }
   },
  "session": {
    "params": {
      "<session_params_key>": "<session_params_value>"
    }
  },
  "scene": {
    "slots": {
      "status": "FINAL",
      "params": {
        "<slot_name>": "<slot_value>"
      }
    }
  },
  "user": {
    "params": {
      "<user_params_key>": "<user_params_value>"
    },
    "permissions": [
      "DEVICE_PRECISE_LOCATION"
    ],
    "accountLinkingStatus": "LINKED",
    "verificationStatus": "VERIFIED",
    "lastSeenTime": {
      "seconds": 123,
      "nanos": 456
    },
    "engagement": {
      "pushNotificationIntents": [
        "intent1",
        "intent2"
      ]
    }
  },
  "home": {
    "params": {
      "<home_params_key>": "<home_params_value>"
    }
  },
  "canvas": {
    "state": {
      "<canvas_state_key>": "<canvas_state_value>"
    }
  },
  "device": {
    "capabilities": [
      "SPEECH",
      "RICH_RESPONSE",
      "LONG_FORM_AUDIO",
      "INTERACTIVE_CANVAS"
    ],
    "currentLocation": {
      "coordinates": {
        "latitude": 37.422,
        "longitude": -122.084
      },
      "postalAddress": {
        "revision": 0,
        "regionCode": "US",
        "languageCode": "en",
        "postalCode": "94043",
        "sortingCode": "",
        "administrativeArea": "California",
        "locality": "Mountain View",
        "sublocality": "",
        "addressLines": ["1600 Amphitheatre Parkway"],
        "recipients": [],
        "organization": ""
      }
    }
  },
  "resources": {
    "strings": {
      "<resource_string_key>": "<resource_string_value>"
    },
    "images": {
      "<resource_image_key>": "<resource_image_value>"
    }
  }
}

Usage reference

The following syntax examples assumes you are working with session.params object:

session.params = {
  "flag": true,
  "count": 10,
  "name": "john smith",
  "myList": [1, 2, 3],
  "myMap": {"one": 1, "two":2}
}

You can carry out the following conditional operations:

// numbers and boolean logic
session.params.count > 0 && session.params.count < 100    // AND
session.params.count == 0 || session.params.count != 5    // OR
!(session.params.count <= 0)         // NOT

// booleans
!false                      // true constant
session.params.flag         // boolean variable
session.params.flag == true // explicitly compare with true constant

// strings
session.params.name == "john smith"  // double quotes supported
session.params.name == 'john smith'  // single quotes supported
session.params.name.contains("john") // substring
session.params.name + "!!!" == "john smith!!!" // string concatenation
session.params.name < "abc"          // compares lexicographically
size(session.params.name) == 10      // length of string

// lists
1 in session.params.myList        // "contains in list" operator
session.params.myList[0] == 1     // "index into list" operator
size(session.params.myList) == 3  // returns number of elements in the list

// maps
session.params.myMap == {"one": 1, "two":2} // compare map with json
session['params']['myMap']['one'] == 1      // index using square brackets
size(session.params.myMap) == 2             // number of entries in the map