MS Bot将对话数据保存到Cosmos DB

时间:2018-04-13 09:47:32

标签: botframework azure-cosmosdb

我已成功将MS Bot连接到Azure Cosmos DB。默认情况下,IBotDataStore不保存对话文本。我已经通过context.ConversationData.SetValue("message", messageText);为每个PostAsync制定了一种方法。任何人都可以帮助我更好地做到这一点吗?

的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();
                });

默认情况下,IBotDataStore不保存会话文本。我已经通过context.ConversationData.SetValue("message", messageText);为每个PostAsync制定了一种方法。任何人都可以帮助我更好地做到这一点吗?

问候语对话

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您检查过IActivityLogger界面吗? https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-middleware通过实现此界面,您可以在所选的任何邮件存储中保存对话历史记录。

IBotDataStore不适用于对话历史记录,但适用于UserData,ConversationData和PrivateConversationData状态。