Teneo Developers

Share information between flows

Just as in human conversations, the user will assume your bot remembers what they recently have been talking about.
Here you'll learn how to make your bot remember information from one flow to another. Consider the following conversation:

User: Which coffee would you recommend? (Flow: User wants bot to suggest a coffee)
Bot: Easy choice, a flat white of course!

User: I'll go for one of those then. (Flow: User wants to order a coffee)
Bot: Ok, a flat white will be ready for pickup in 5 minutes.

This conversation combines two flows: 'User wants bot to suggest a coffee' and 'User wants to order a coffee'. In the first flow, the bot recommends a 'flat white'. This coffee type is remembered. When the second flow is triggered and the user asks for "one of those" the bot understands that a 'flat white' should be ordered. In other words, 'flat white' is mentioned in one flow, and then used in another!

We will take the following steps to make this possible:

  • Create a global variable that can store the coffee type.
  • Make sure the flow 'User wants bot to suggest a coffee' populates that global variable.
  • Make sure the flow 'User wants to order a coffee':
    • retrieves the global variable, and
    • triggers on inputs like "I'll go for one of those then".

Once we're done, the 'User wants to order a coffee' flow will look like this:

final flow layout

Add a global variable

First we'll create the global variable to hold the coffee type:

  1. On the Solution dashboard, navigate to the 'Globals' tile and select 'Variables'.
  2. Click on 'Create new global variable'.
  3. Name the variable coffeeTypeInFocus and set its initial value to the empty string: "". (Make sure to edit the "Value" field and not the "Description" field.)
  4. Hit 'Create' and save the variable.
  5. In the Tryout window, a message to 'Reload now' appears. After clicking it, you will be able to use the global variable in your flow.

We now have a global variable which we can use to store a coffee type and in which in turn can be used by other flows.

Update the 'User wants bot to suggest a coffee' flow

Now we'll make sure that the recommended coffee in the flow 'User wants bot to suggest a coffee' is stored in the global variable, so that it can be retrieved from other flows too. The flow uses a flow variable coffeeSuggestion that needs to be copied to the global variable coffeeTypeInFocus.

To do so, we'll add a script in the flows 'On Drop' script event. Scripts in 'On Drop' are executed when the flow has finished and is about to be dropped from the Flow stack.

In the script we'll first set the global variable coffeeTypeInFocus to the value of the flow variable coffeeSuggestion.
However, we don't want the coffee type to be remembered forever. Just like humans don't keep things in focus forever, your bot shouldn't either. Once the variable is set, we'll use a special method to specify that the value should be forgotten after a couple of turns (input-output turns). In Teneo that is called setting the lifespan of a variable. You set the lifespan of a variable with a script like the following:

explain script

Let's update the flow:

  1. Open the previously created 'User wants bot to suggest a coffee' flow.
  2. Click on the Scripts button.
  3. Paste these lines to the 'On drop' script field:

    groovy

    1// Save flow variable coffeeSuggestion in global variable
    2coffeeTypeInFocus = coffeeSuggestion
    3
    4// Set the lifespan of the global variable
    5_.setSessVarLifespan('coffeeTypeInFocus',2)
    6
  4. Hit 'Confirm' and save the flow.

Update the 'User wants to order a coffee' flow

We've come quite far already. We've set up a global variable that makes it possible to share information between flows, and made sure it's populated whenever your bot suggests a coffee type. Now we'll move on to retrieve and use the variable from within the flow 'User wants to order a coffee'.

But first, we need to make sure that the flow triggers on inputs like "I'll go for one of those then". This should only happen when there is a coffeeTypeInFocus. The new trigger will therefore include a context restriction stating that the global variable must carry a value for the trigger to be applied. Now let's create the trigger:

  1. Open the flow 'User wants to order a coffee'.
  2. Click on the Plus icon next to the trigger and add a new trigger based on intent.
  3. Name the trigger: Can I have one of those?.
  4. Add a few example intents:
    • I'd like a large one
    • I'll go for that then
    • May I have one of those please?
  5. Beneath this new trigger, click on the Plus icon and add a Match for 'TLML Syntax'. Paste in the following:

    tlml

    1(
    2    (%I.FW.LEX &^ %WANT.VB.LEX &^ (one/%IT.FW.LEX))
    3    /
    4    (%I_WILL.PHR &^ %GO_FOR.VB.MUL &^ %IT.FW.SYN) 
    5)^{ orderedCoffeeType = coffeeTypeInFocus }
    6
  6. Now we'll add the context restriction. Click on 'Add match requirement' at the bottom of the new Match node.
  7. Select 'Global Variable' and, in the dropdown, select the global variable coffeeTypeInFocus that we created earlier.
  8. Hit 'Save'.

The flow 'User wants to order a coffee' keeps track of which coffee type the user asks for in the flow variable coffeeType. We'll now make sure that the flow variable coffeeType is populated with the value stored in coffeeTypeInFocus when our new trigger kicks in.

You can do this in at least two different places. Either you attach a script directly to the trigger's language condition, or you add a trigger listener.
Here we'll go for the former and attach a script snippet to the language condition.

An attached script is executed when the condition it's attached to has matched the input. The flow variable orderedCoffeeType will therefore be set to the value of coffeeTypeInFocus when the condition is evaluated to true and our new trigger kicks in. And of course, we will then skip the question "what coffee type do you want" as we already know the answer to it.

Try it out!

That's it! Now go ahead and give it a try in Tryout!

User: Which coffee would you recommend? (Flow: User wants bot to suggest a coffee)
Bot: Easy choice, a flat white of course!

User: I'll go for one of those then. (Flow: User wants to order a coffee)
Bot: Ok, a flat white will be ready for pickup in 5 minutes.