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: Which size would you like?

User: Large please.
Bot: Ok, a large 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, so 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 possibe:

  • 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 are 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 that we can use to store a coffee type, which can in turn be used by other flows when needed.

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

Now we'll make sure that the coffee that is recommended 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 to the flow's '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 as humans don't keep things in focus forever, so shouldn't your bot. 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 '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

Add a trigger

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 thus 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 'Add Flow trigger' section of the ribbon at the top, select a 'Syntax' trigger. This will add an additional trigger to the flow.
  3. Open its 'Condition' panel, and paste the following:
    (
        (%I_WANT.PHR &^ (%ONE.FW.LEX/%IT.FW.SYN))
        /
        (%I_WILL.PHR &^ %GO_FOR.VB.MUL &^ %IT.FW.SYN) 
    )
  4. Now we'll add the context restriction. Open the 'Examples panel'. Click 'Add' by Context Restrictions and select the global variable coffeeTypeInFocus that we created earlier.
  5. Add a few example inputs:
    I'd like a large one
    I'll go for that then
    May I have one of those please?
  6. Name the trigger: Can I have one of those?.

Retrieve the global variable

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. We'll attach a script snippet to the language condition.

These are the steps to set it up:

  1. Select the trigger we created in the previous step: 'Can I have one of those'.
  2. Open its 'Condition' panel (if not already open).
  3. Replace the language condition with the the following:
    (
        (%I_WANT.PHR &^ (%ONE.FW.LEX/%IT.FW.SYN))
        /
        (%I_WILL.PHR &^ %GO_FOR.VB.MUL &^ %IT.FW.SYN) 
    )^{ orderedCoffeeType = coffeeTypeInFocus }
  4. Hit 'Save'.

An attached script is executed whenever the condition it is attached to has matched the input. The flow variable orderedCoffeeType will thus be set to the value of coffeeTypeInFocus whenever the condition evaluates 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, and go straight to ask the size.

Try it out!

That's it! Now go ahead and give it a try 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: Which size would you like?

User: Large please.
Bot: Ok, a large flat white will be ready for pickup in 5 minutes.

Was this page helpful?