Using Common Data Service as Bot Memory Across Skills

Joe Herres
AtBot
5 min readMay 1, 2019

--

While AtBot does have a Bot Memory feature that works well for an authenticated experience, this does not provide any benefit to the web-based or SMS anonymous chat bot experience. To solve this, I took a dive into the Common Data Service (CDS) to see if I could get that to work for my purposes and it ends up being way better than I expected!

The CDS is an entity model that is defined in PowerApps. I believe this model is shared across different products in the Power Platform, but most important to us is the Power Automate connector. Basically, we can define an entity that can store all of the data we want to persist across skills and then use it in our bot.

Step 1: Create the Entity

In order to use this feature, you will have to have access to a PowerApps license. In the future I will update this post with information about what that requirement looks like.

  1. Log into the O365 portal and navigate to PowerApps from the app launcher.
  2. Expand Data and select Entities
  3. Create a new Entity with any name that makes sense for the data you are storing
  4. Once the entity is created, you can create fields appropriate for the data you want to store. Be sure to have a field that can store the key you want to use to retrieve the data. In my case this will be Name (max length 500), I will store the Conversation ID that comes from the AtBot Power Automate trigger.
  5. Once you are done, be sure to click the Save button in the lower right

For the purposes of this article I created a very simple entity that has 2 fields, Name and FavoriteColor.

Tip: Make all of your entities basic strings. You can always convert them in Power Automate if you need a different type, but keeping them plain text will make your life much easier when writing to and getting from the CDS.

Step 2: Saving to the CDS from Power Automate

In your bot skills you can now create and update the data in your entity. It’s important to understand the operations available to you from Power Automate.

Create New Record

This is the operation you want to use in the first skill that will store data. There is nothing stopping the CDS from creating multiple records based on your conversation ID so you will need to manage when the Create method is used.

Update Record

This is the operation you will want to use whenever you want to update the values in memory. In order to use this, you will have to have the Unique Identifier of the record which can be retrieved from the List Records action. This means that in a flow where you need to update memory, you will first have to call the List Records action to get the identifier of the entity you wish to update.

In my example here, I have a skill that asks the user what their favorite color is and then stores that in the CDS. This is the initial creation of the entity and I am using the Conversation ID as my unique identifier.

Step 3: Reading CDS in Power Automate

To get the data back out of CDS for use, we have a fairly simple pattern to follow. In our scenario of using CDS for bot memory, you will always use the List Records action because that’s the only way we can get records based on our Conversation ID.

When you use the List Records action, it returns a collection (even though we know there should only be a single item). You will want to define some variables to store the returned information and then set those variables inside a loop of the collection of records.

You can then use those variables in other parts of your flow, update them, then ultimately save them back to the record if any changes are made.

Step 4: Clean Up

Power Automate does include a Delete Record action which you can use to remove the record at the end of a flow if appropriate. However, it will be much more common for the data to just sit in the CDS as users abandon the conversation or they don’t finish in the way you designed which will clean up the data. With the Conversation IDs being unique per session with a web bot, this could add up to a lot of stale data.

Fortunately Microsoft provides a mechanism to clean up the old data. There is a Bulk Delete operation you can use and provide a query that will be used to delete. This means you could delete all records that are older than 24 hours and be pretty sure you are not removing anything important. The downside to this is that it is only possible to do through code.

This is our first venture into using CDS with a bot, but I think there is quite a bit of potential here. We will continue to explore and post any updates that may help with this use case.

AtBot brings AI within reach

AtBot is the premiere bot-as-a-service solution for the Microsoft cloud. Built completely within Azure, AtBot is your out-of-the-box, easy-to-configure bot for Teams, SharePoint, or the web. Teach AtBot tasks using Power Automate, make him your corporate source of knowledge with QnA Maker, help him understand almost anything your colleagues could ask thanks to LUIS, and manage his features with the AtBot Admin Portal. Get going with AtBot Free or start your free trial of AtBot Premium today.

--

--