Redirect your flow with a data store

Sometimes you want your chatbot to respond differently based on data that is regularly updated, such as managing support calls about products under recall or redirecting support for tickets that relate to a specific issue.

Creating a highly dynamic flow with variables or with data stored in the chatbot scripts would be tricky, as you'd need to republish the chatbot every time your data changed. Instead, you can use a data store.

Data stores provide your scripts with lookup tables that you can update without republishing the chatbot, so your chatbot can respond to data updates in real time. You can even add and remove data in your chatbot scripts using data store functions, allowing your chatbot to manage the data automatically.

In the example below, we will create a script that uses a data store to redirect the conversation if the customer is asking about a product that is under recall:

  1. Create variables to hold the product id and batch number.
  2. Add a passage to ask the chatbot user which product they're asking about and save the product id to the variable.
    Make sure this passage occurs early in the chat so the variable has a value when your script runs.
  3. Create a data store for the recalled products.
  4. Upload a CSV with a column for product numbers and batch numbers.
    Your column names must match your script. 
    For our example, we've used productId and batchNumber.
  5. Click the menu next to the data store and copy the data store id.
    Paste the id into notepad, you'll need it in a moment.
  6. Create a new chatbot script using the example below.
    Make sure you replace the data store id with the id you copied earlier.
    If you used different column names in your CSV, adjust the script to match.
  7. Add parameters to your script for the batch number, product id, and the passages to start.
  8. Add a passage to your chatbot to run the script:
    1. Add message content to ask the chatbot user for their batch number.
    2. Use a Send Info response with an action to save the value they send to a variable.
    3. Add a second action to the Send Info response to run the script you created.
    4. Configure the script parameters to use the product id and batch number variables you created and the passages you want to start.

Example script

// Fetch the parameters
const {batchNumber, productId, recallPassage, standardPassage } = params; 

// Fetch the data store and check if any there are any rows with the product id and batch number
const data = await bot.dataStore.getAllObjects('DataStoreId'); 
const matches = data.filter(line => line.productId === productId && line.batchNumber === batchNumber);

// Start the recall passage if there was a matching entry in the data store
if(matches.length > 0){
	bot.flow.startPassage(recallPassage);  
}else{
	bot.flow.startPassage(standardPassage);  
}
done();
Was this article helpful?
0 out of 0 found this helpful