Dynamic and Conditional Output Nodes

In Teneo Studio, output nodes are one of the essential components in a flow. Sometimes we need to add some dynamic values that depend on the user input in the output node. In this case, you can add a placeholder surrounded by ${…} in the output text. The placeholder should be a flow variable or global variable which can get the value in the runtime. For example, in our bot building tutorial, we have this output:

At runtime, the placeholder orderCoffeeType will be replaced by a value collected from the user input.

In certain use cases, you may not only want to make your answer dynamic, but also conditional. Considering the dialog above, what will happen if the user says “An americano please”? As the article “a” is static in the output node, the answer will be “Ok, a americano will be ready…” which sounds weird. Of course, you can use “a(an)” or even create two output nodes one with “a” another with “an”, but this is obviously not smart. The best practice should be using a conditional output in one output node. In this case, we need the article to be “an” if the coffee type starts with a vowel, otherwise it should be “a”. The following example shows how to build this conditional output:


With this conditional output, the article can switch between “a” and “an” according to the starting letter of the coffee type:
image
image
The basic syntax of the conditional text is:

condition1?statement1:condition2?statement2:default statement

The conditional text above means that if the condition 1 is met, statement 1 will be executed; if the condition 2 is met, statement 2 will be executed; if none of the two condition is met, the default statement will be executed. You can add as many condition expressions as you want. Please remember:

  • Conditional text is a programming expression and should be put between ${…} in the output node.
  • You should use colons to separate the conditional expressions.
  • Within the same conditional expression, you should use question mark (?) to separate the condition and the statement (which is usually the text to be shown).
  • The text in the statement is in programming language. You must therefore use quotation marks (" or ') for plain texts, and the plus operator (+) to concatenate plain texts with variable values. For example:

    This conditional output will return “2 cups of cappuccino” if the variable nCoffeeOrdered has the value 2 and the variable orderedCoffeeType has the value “cappuccino”. If the value of the variable nCoffeeOrdered is 1, the output text will start with “One cup of …”.
  • The conditions are in order. Only the statement following the first condition that is met will be executed. All other statements will be skipped .
  • You should always setup a default text. No condition is needed for default text so you can add it directly after the last colon. If the default text is empty, you should put an empty string “” in the place of the default statement.

Dynamic and conditional outputs are commonly used in chatbot building to make your bot respond to the user input more smartly and more flexibly. The dynamic output is extremely useful when you need a placeholder in your output which gets its value at runtime. The conditional output is usually used when you have two output texts which are slightly different from each other, and you can only know which one should be picked up at runtime. This happens a lot in Western languages in which the words have different forms according to the number and tense.

We hope you found this article useful, and feel free to ask here any questions you might have on the output nodes!

4 Likes