Teach your bot to remember earlier conversations in Teneo
By: Artificial Solutions
Tags: Bots, Artificial Intelligence, Conversational Ai, Machine Learning, Chatbot Development

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

a. retrieves the global variable, and

b. 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:

Add a global variable

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

Open the ‘SOLUTION’ tab in the solution’s window. Select ‘Globals’ in the purple bar on the left hand side, and then select ‘Variables’. Click ‘Add’. A panel for specifying the new variable appears on the right hand side. 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) Hit ‘Save’ in the right upper corner. Click on the arrow in the upper left corner to return to the solution’s HOME tab. 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:

Let’s update the flow:

Open ‘User wants bot to suggest a coffee’ in edit mode. Click on the ‘FLOW’ tab in the upper left corner. Select ‘Scripts’ in the purple bar, and then click ‘On Drop’. 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) Hit ‘Save’ in the upper left corner.

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:

Open the flow ‘User wants to order a coffee’ in edit mode 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. 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) ) 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. Add a few example inputs: I'd like a large one I'll go for that then May I have one of those please? 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:

Select the trigger we created in the previous step: ‘Can I have one of those’. Open its ‘Condition’ panel (if not already open). 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 } 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!