Writing conditional text outputs within CognigyScript Fields – Cognigy.AI Help Center
Writing conditional text outputs within CognigyScript Fields
Cognigy Support
April 1, 2021
Challenge
Is it possible to customize the output text from a messaging node from within the text field itself?
Solution
Yes! In Cognigy.AI, all CognigyScript fields (indicated by the "AI" button) support javascript conditional statements. This means that you can write a conditional statement, similar to as would be entered in the IF Node, within any CognigyScript field to make a decision between multiple output options.
Here is an example of one such statement:
Use Case: We want to use the plural form of the word "item" in a sentence when anything other than 1 item is ordered, but we only want to have to configure this in a single message output.
You can use the following phrase to achieve this:
You ordered {{ input.slots.NUMBER[0] }} {{ (input.slots.NUMBER[0] === 1)?"item":"items" }}
In output nodes (like Say nodes or Questions nodes) Cognigy evaluates CognigyScript and determines a string output, in this case 'item' or 'items'. In the above example, Cognigy looks at the first number slot match and determines the right output based on the slot value.