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 trigger
  • A script that chooses a coffee drink
  • 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:

Image of the final flow

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. 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.)

Add a script node

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.

Put the script node in its place

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:

  1. Select the script node
  2. In the ribbon choose 'Set start node'. This points the trigger straight to the script node.
  3. Draw a line from the script node to the output node

Populate the script node

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.

Add flow variable

To add a flow variable, proceed as follows:

  1. Click on the tab 'FLOW' in the top left corner of your flow.
  2. Choose 'Variables' in the purple bar on the left
  3. Click the 'Add' button in the top right of the window
  4. Name the variable coffeeSuggestion by replacing the word Variable1
  5. We want to initialize this variable as an empty string, so in the text box below add "".

You can use the back arrow at the top to go back to the main flow 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:

  • 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!

Done!

That's it! Save your flow and give it a try in tryout.

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!

Was this page helpful?