Share information between flows

Just as in human conversations, the user will assume your bot to remember 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

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' in the purple bar on the left hand side, and then select 'Variables'.
  3. Click 'Add'. A panel for specifying the new variable appears on the right hand side.
  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' in the right upper corner.
  6. Click on the arrow in the upper left corner to return to the solution's HOME tab.
  7. In the Try out panel, a message to 'Restart 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:

Explanation of the 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 in the upper left corner.
  3. Select 'Scripts' in the purple bar, and then click 'On Drop'.
  4. Paste these lines to the empty script field:

    // Save flow variable coffeeSuggestion in global variable
    coffeeTypeInFocus = coffeeSuggestion
    
    // Set the lifespan of the global variable
    _.setSessVarLifespan('coffeeTypeInFocus',2)
  5. Hit 'Save' in the upper left corner.

Update the 'User wants to order a coffee' flow

We've come quite far already. We've set up a global variable which 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. In the 'Flow entry' section of the ribbon at the top, select the 'Trigger Node' button. This will add an additional trigger to the flow.
  3. Name the trigger: Can I have one of those?.
  4. Add a few example inputs:
    I'd like a large one
    I'll go for that then
    May I have one of those please?
  5. Add a match requirement for 'Condition' and paste the following:
    (
        (%I.FW.LEX &^ %WANT.VB.LEX &^ (one/%IT.FW.LEX))
        /
        (%I_WILL.PHR &^ %GO_FOR.VB.MUL &^ %IT.FW.SYN) 
    )^{ orderedCoffeeType = coffeeTypeInFocus }
  6. Now we'll add the context restriction. Scroll down to the 'Match Requirement' section and select 'Add'.
  7. Select Context by scrolling down the drop down 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 size and coffee type the user asks for in the flow variables size and 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, and instead go straight to asking for the size.

Try it out!

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

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.

Was this page helpful?