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 new flow
  • A class match requirement.
  • 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:

Image of the final flow

Let's get started.

Create a new flow and populate the trigger

  1. Create a new flow and call it User wants bot to suggest a coffee.
  2. Select the class trigger. 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.)
  3. 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?
  4. Scroll down to 'Match Requirements' and select the 'Generate' button, this will automatically generate a class trigger.

Add a Script node

  1. 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.
  2. Keep the new script node selected and click 'Set Start Node' in the ribbon's 'Flow Entry' section. The graph will be re-drawn so that the script comes straight after the trigger.
  3. Connect the junction and the empty output node as follows: Click on the script node, and then drag the cursor to the center of the output node. If you want to cancel the transition while drawing, click Esc.

It is possible to replace the Script Node with a Script Data Action inside the trigger.

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. Select the 'Execution Script' located in your script configuration panel and paste the following code into the script:

// 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 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:

  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!


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!

What's next?

We'll learn more about what a Sub-Flow is and how to use it.

Was this page helpful?