The Bot Framework SDK supports the preservation of the user input as it is received. Here, we will store each transaction into the memory object. The same object will be saved to the storage at the end.
In this tutorial, you will learn the following.
- Setup a Cosmos DB.
- Create a basic bot.
- Setup configuration information to the Azure bot.
- Implement Cosmos DB storage to Azure bot.
- Build and test your bot with Emulator.
Below are the prerequisites.
The bot created in the previous article will be used here to help you understand how to build a basic bot.
Let's understand the information flow with respect to this article.
You can read and write directly to your storage object without using middleware or context object. This can be appropriate for data your bot uses to preserve a conversation or data that comes from a source outside your bot's conversation flow. In this data storage model, data is read directly from storage instead of using a state manager.
Step 1 - Set up Cosmos DB
- Sign in to the Azure portal with your Azure credentials.
- Click "Create a resource".
- Click “Azure Cosmos DB".
- Click Create a resource > Databases > Azure Cosmos DB
- On the New account page,
- provide Subscription,
- Resource group information.
- Create a unique name for your Account Name field - this eventually becomes part of your data access URL name.
- For API, select Core(SQL),
- Provide a nearby Location to improve data access times.
- Then click Review + Create.
Once validation has been successful, click Create.
The account creation takes a few minutes and Your Azure Cosmos DB account created.
- Search for setting
- Click Settings
- Click the New Container.
- Add Container area is displayed on the far right.
- Fill new database id i.e. bot-cosmos-sql-db
- Container id i.e. bot-storage
- Partition key i.e. /id
- Click Ok to create.
Cosmos DB container created with name bot-cosmos-sql-db
Keys to access new database collection, "bot-cosmos-sql-db" with a container id of "bot-storage."
Step 2 - Create a basic bot
Sample Echo Bot project will be created with basic functionality.
Step 3 - Setup configuration information to the Azure bot
- Add Cosmos DB configuration to Visual Studio solution.
- private const string CosmosServiceEndpoint = "https://bot-cosmos-sql-db.documents.azure.com:443/";
- private const string CosmosDBKey = "vr1psMExluqGtp45XbIu4q1w2gmwfFksLr8GX4TDE5AkdRBiLE1fGmPsEoVpDXrM6qtOfLEGS25SzCO9G5XORg==";
- private const string CosmosDBDatabaseName = "bot-cosmos-sql-db";
- private const string CosmosDBCollectionName = "bot-storage";
- // Create Cosmos DB Storage.
- private static readonly CosmosDbStorage query = new CosmosDbStorage(new CosmosDbStorageOptions
- {
- AuthKey = CosmosDBKey,
- CollectionId = CosmosDBCollectionName,
- CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
- DatabaseId = CosmosDBDatabaseName,
- });
Step 4 - Implement Cosmos DB storage to azure bot
To preserve conversation to Azure Cosmos DB, I am going to create an object which will hold properties of the same conversation, it will concatenate and store all at one instance. To get started, Create one class with basic properties.
- Create UtteranceLog.cs file and below properties
- public class UtteranceLog : IStoreItem
- {
- // A list of things that users have said to the bot
- public List<string> UtteranceList { get; } = new List<string>();
- // The number of conversational turns that have occurred
- public int TurnNumber { get; set; } = 0;
- // Create concurrency control where this is used.
- public string ETag { get; set; } = "*";
- }
It's just a screen shot of above code placement.
- Replace OnMessageActivityAsyn method with below code section
- protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
- {
- // preserve user input.
- var utterance = turnContext.Activity.Text;
- // make empty local logitems list.
- UtteranceLog logItems = null;
- // see if there are previous messages saved in storage.
- try
- {
- string[] utteranceList = { "UtteranceLog" };
- logItems = query.ReadAsync<UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;
- }
- catch
- {
- // Inform the user an error occured.
- await turnContext.SendActivityAsync("Sorry, something went wrong reading your stored messages!");
- }
- // If no stored messages were found, create and store a new entry.
- if (logItems is null)
- {
- // add the current utterance to a new object.
- logItems = new UtteranceLog();
- logItems.UtteranceList.Add(utterance);
- // set initial turn counter to 1.
- logItems.TurnNumber++;
- // Show user new user message.
- await turnContext.SendActivityAsync($"Echo" + turnContext.Activity.Text);
- // Create Dictionary object to hold received user messages.
- var changes = new Dictionary<string, object>();
- {
- changes.Add("UtteranceLog", logItems);
- }
- try
- {
- // Save the user message to your Storage.
- await query.WriteAsync(changes, cancellationToken);
- }
- catch
- {
- // Inform the user an error occured.
- await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
- }
- }
- // Else, our Storage already contained saved user messages, add new one to the list.
- else
- {
- // add new message to list of messages to display.
- logItems.UtteranceList.Add(utterance);
- // increment turn counter.
- logItems.TurnNumber++;
- // show user new list of saved messages.
- await turnContext.SendActivityAsync($"Echo " + turnContext.Activity.Text);
- // Create Dictionary object to hold new list of messages.
- var changes = new Dictionary<string, object>();
- {
- changes.Add("UtteranceLog", logItems);
- };
- try
- {
- // Save new list to your Storage.
- await query.WriteAsync(changes, cancellationToken);
- }
- catch
- {
- // Inform the user an error occured.
- await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
- }
- }
- }
Step 5 - Build and Test your bot with Emulator.
- Press F5 to run the Visual Studio solution locally.
- Start the bot emulator and then connect to your bot in the emulator:
- Click the Create new bot configuration link in the emulator "Welcome" tab.
- Fill in fields to connect to your bot, given the information on the webpage displayed when you started your bot.
- Interact with your bot
- Send a message to your bot. The bot will list the messages it has received
OutPut
- Navigate to Azure Cosmos DB instance and expand the items under bot-storage.
- Select the utterances log,
- All asked queries turn to storage.
The previous article can be found from these links.
I hope you have enjoyed and learned something new in this article. Thanks for reading and stay tuned for the next article.