{{ }}
, for example, {{input.text}}
. This syntax enables Cognigy.AI to identify and execute the CognigyScript code embedded in the Flow. However, CognigyScript evaluates conditions as standard JavaScript. You donโt need to wrap the CognigyScript expression in {{ }}
. For example, context.orders === 3
evaluates to true if the orders
variable in the Context is equal to 3
.
You can use CognigyScript to:
- Add dynamic and executable content to chat and voice conversations.
- Retrieve information from Cognigy.AI objects and reuse it in chat and voice conversations.
- Run scripts to manipulate data dynamically.
- Use CognigyScript in JSON arguments to make requests to an external system via the HTTP Request Node.
Working with CognigyScript
You can use CognigyScript in:- Text fields
- JSON editors
- Code Nodes
Text Fields
For text fields, wrap the CognigyScript expression in{{ }}
.
If the CognigyScript expression is invalid, CognigyScript returns an empty string.
Example
Example
{{input.text.toUpperCase()}}
returns the text the client sent in uppercase format.JSON Editors
The CognigyScript syntax in JSON editors depends on where you place the CognigyScript expression.Inline CognigyScript
For inline CognigyScript in a JSON object, use{{ }}
.
Example
Example
orders
variable from the Context object and to assign it to customer_orders
. If context.orders
doesnโt exist, the code skips customer_orders
.Inside JSON Arguments
To run CognigyScript within a JSON argument, use{ "$cs": { "script": "x", "type": "t"}}
, where x
is the script and t
is the return type (optional). For instance, you can convert a string such as "6"
into a number or an object into a string.
Example
Example
orders
variable from the Context object and to assign it to customer_orders
as an object. If context.orders
doesnโt exist, the code skips customer_orders
.Code Nodes
In a Code Node, you donโt need to wrap the CognigyScript expression in{{ }}
. You can use CognigyScript as standard JavaScript. The input
, context
, profile
, and actions
variables are exposed by default, as well as _
and moment
.
Example
Example
Additional Examples
Examples of CognigyScript Expressions
Examples of CognigyScript Expressions
The following table shows additional examples of CognigyScript expressions:
Example | Description |
---|---|
{{input.slots.SLOT_NAME[0].keyphrase}} | Extracts the first Keyphrase found for the Slot with the name SLOT_NAME . |
{{JSON.stringify(input.data)}} | Displays a JSON object as text. You can use this CognigyScript expression for debugging purposes. |
{{input.slots.DATE[0].start.plain}} | Returns the date information if the user sent a date. Example: July 23, 2021 |
{{moment(input.slots.DATE[0].start.ISODate).format('MMM Do YY')}} | Formats a given date that the user provided in the last message. |
{{input.currentTime.hour}} | Gets the hour of the current incoming message. You can use this CognigyScript expression to greet the user appropriately. |