Add a script to your flow
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:
- A Class Match.
- A Script that randomly chooses a coffee type.
- A flow variable to store the value chosen by the script.
- An output that will display the value of the variable.
The final flow will look like this:
Let's get started.
Create a new flow and populate the trigger
- Create a new flow and call it
User wants bot to suggest a coffee
. - Name the Intent node
Suggest a drink!
. (Visual instructions of how to create a flow and populate the trigger can be found here.) - 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?
- Click on the Plus icon beneath the Intent node, followed by Match and then Generate.
Add a Script node
It is now time to create a script to be used.
- Select the Plus icon above the output node.
- Click on 'Continue with' and select 'Script'.
- Give the script node the name
Pick a drink
.
Populate the script
We have added the script node, but it is still empty. Now we'll need to add the script that picks a random coffee type to suggest to the user.
- Paste the following code into the script node:
groovy
1// define a list with coffee drinks to suggest
2def coffeesToSuggest = ['cappuccino','flat white','macchiato']
3
4// randomly choose a number 0, 1 or 2
5def number = new Random().nextInt(3)
6
7// use the number to pick a coffee from the list
8coffeeSuggestion = coffeesToSuggest[number]
9
As you can see in the script code 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 code. 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.
Add a flow variable
To add a flow variable, proceed as follows:
- Click on the Variables icon under the minimap.
- Add a new variable and name it
coffeeSuggestion
. - We want to initialize this variable as an empty string, so in the text box below add
""
. - Press Confirm.
You can use the back arrow at the top to go back to the main Flow editor view.
Add answers to the output
The user is full of anticipation so let's provide the answer. Add the following answer texts to the output node:
- Name your output node
Here is your suggestion!
. - Paste in the following answers:
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!
- Save your flow.
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.
Try it out
That's it! Save your flow and give it a try in Tryout by pasting the following line: Suggest a drink!
User: Suggest a drink!
Bot: Now that you're asking, I'd suggest a flat white!
Extra challenge for the brave
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!