Scripts can be included in flows too. Let's create a very basic flow that contains a script.
The flow we will create will suggest a random coffee drink, like so:
User: Suggest a drink!
Bot: Well, you're putting me on the spot here. I'd go for a flat white!
To achieve this, the we need to create a flow that contains the following elements:
The final flow will look like this:
Let's get started.
Create a new flow and call it User wants bot to suggest a coffee
. Then select the class trigger and add the following example inputs:
Recommend a beverage
Suggest a drink
Which coffee would do you advise?
What coffee should I get?
Can you recommend me a coffee
Recommend a coffee.
Which brew should I drink?
Tell me, what coffee should I buy?
Could you decide which coffee I should get
I want a recommendation for a coffee
Can you suggest me a drink?
Which coffee should I get
Which coffee would you recommend me?
Give the trigger a name like Suggest a drink!
. (Visual instructions of how to create a flow and populate the trigger can be found here.)
With nothing else in the flow marked, click the 'Script' icon in the ribbon. This will add an empty script node to your flow. Give it a name that explains what the script does, like Pick a drink
.
Currently, our script node is dangling and not connected to any of the other elements in the flow. However, we want our script to be executed immediately after the flow is triggered, and right before the output is shown to the user. To achieve this we need to follow these steps:
We have added the script node, but it is still empty. Now we'll need to add the script that picks a random coffee drink to suggest to the user. Select the script node in your flow, open the 'Script Action' tab on the right sight of the screen and paste the following code into the script node:
// define a list with coffee drinks to suggest
def coffeesToSuggest = ['cappuccino','flat white','macchiato']
// randomly choose a number 0, 1 or 2
def number = new Random().nextInt(3)
// use the number to pick a coffee from the list
coffeeSuggestion = coffeesToSuggest[number]
Scripts in Teneo are written in Groovy. You can find more details about Groovy and scripts in Teneo in the Scripting section.
As you can see in the script node above the first two variables (coffeesToSuggest and number) are preceded by def which means they are local variables that are only available inside the script node. They will be forgotten immediately after the script is executed, in this case this will happen when Teneo moves on to the output node. However, we want the outcome of the script (the coffee chosen by the script node) to be available outside of the script so we can use it in our output text. So we will need to store the value in a flow variable that we can use later on.
The last line of the script assumes a variable called coffeeSuggestion exists in which the chosen coffee drink can be stored. This is determined by the fact that coffeeSuggestion is not defined by preceding it with def. However, we don't have a flow variable called coffeeSuggestion yet, so let's add it.
To add a flow variable, proceed as follows:
coffeeSuggestion
by replacing the word Variable1""
.You can use the back arrow at the top to go back to the main flow view.
The user is full of anticipation so let's provide the answer. Add the following answer texts to the output node:
Well, you're putting me on the spot here. I'd go for a ${coffeeSuggestion}!
Now that you're asking, I'd suggest a ${coffeeSuggestion}!
Easy choice, a ${coffeeSuggestion} of course!
As you can see we have used the flow variable coffeeSuggestion in our answers. This placeholder will be replaced by the value picked in the script node. Finally give the output node a name like I'd go for a flat white!
That's it! Save your flow and give it a try in tryout.
If you're up for an extra challenge: often there are multiple ways to design a flow. In this case for example, you can also model this flow with 3 separate output nodes (for each coffee type suggested). Can you figure out what the design of the flow would be when taking that approach? Hint: transitions can contain script conditions too!
Was this page helpful?