I have successfully connected my MS Bot to Azure Cosmos DB. By default the IBotDataStore does not save conversation text. I have worked out a way to this by context.ConversationData.SetValue("message", messageText);
for each PostAsync
. Can anyone help me with a better way to do this?
Global.asax.cs
var uri = new Uri(ConfigurationManager.AppSettings["DocumentDbUrl"]);
var key = ConfigurationManager.AppSettings["DocumentDbKey"];
var store = new DocumentDbBotDataStore(uri, key);
Conversation.UpdateContainer(
builder =>
{
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();
builder.Register(c => new CachingBotDataStore(store, CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
.As<IBotDataStore<BotData>>()
.AsSelf()
.InstancePerLifetimeScope();
});
And this is the Greeting Dialog:
Greeting Dialog
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace HalChatBot.Dialogs
{
[Serializable]
public class GreetingDialog : IDialog<object>
{
public string userName;
public string messageText;
public async Task StartAsync(IDialogContext context)
{
messageText = "Hi, can I please have your name.";
await context.PostAsync(messageText);
context.ConversationData.SetValue("message", messageText);
context.Wait(GetName);
}
public virtual async Task GetName(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var _result = await result;
userName = _result.Text;
//string message = activity.AsMessageActivity()?.Text;
context.UserData.SetValue("username", userName);
messageText = $"Thanks {userName}, how can I help you?";
await context.PostAsync(messageText);
context.ConversationData.SetValue("message", messageText);
context.Done(context);
}
}
}