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:

flow layout

Add a global variable

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

  1. Open the 'SOLUTION' tab in the solution's window.
  2. Select 'Globals', and then select 'Variables'.
  3. Click 'Add'. A panel for specifying the new variable appears.
  4. 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)
  5. Hit 'Save'.
  6. Return to the main solution view.
  7. In the Tryout panel, a message to 'Reload now' appears. After clicking it, you will be able to use the global variable in your flow.

We now have global variable which we can use to store a coffee type and in which 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' in edit mode.
  2. Click on the 'FLOW' tab.
  3. Select 'Scripts', and then click 'On Drop'.
  4. Paste these lines to the empty 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
    
  5. Hit 'Save'.

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' in edit mode.
  2. Click on the Plus icon next to the trigger followed by 'Add Trigger'. This will add an additional trigger to the flow.
  3. Name the trigger: Can I have one of those?.
  4. Add a few example intents:

    example-inputs

    1I'd like a large one
    2I'll go for that then
    3May I have one of those please?
    4
    
  5. Add a Match for 'TLML Syntax' and paste the following:

    lml

    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' in the Match section.
  7. Select 'Global Variable' and 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.