【问题标题】:MS Bot saving conversation data to Cosmos DBMS Bot 将对话数据保存到 Cosmos DB
【发布时间】:2019-12-23 18:35:22
【问题描述】:

我已成功将我的 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();
                });

这是问候对话框:

问候对话框

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

【问题讨论】:

    标签: botframework azure-cosmosdb


    【解决方案1】:

    您检查过 IActivityLogger 界面吗? https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-middleware 实现此接口将使您能够将对话历史记录保存在您选择的任何消息存储中。

    IBotDataStore 不是用于对话历史记录,而是用于 UserData、ConversationData 和 PrivateConversationData 状态。

    【讨论】:

    • 如果我的理解是正确的,它使用debug.writeline 写入跟踪侦听器,你将如何实现保存到 cosmos db。就像在你的blog for sql 中一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多