【问题标题】:How can we log both user and bot messages to comos db in microsoft bot framework我们如何在 microsoft bot 框架中将用户和 bot 消息记录到 cosmos db
【发布时间】:2019-05-07 05:57:41
【问题描述】:

我使用 microsoft bot framework v4 sdk 创建了一个聊天机器人。我想将机器人用户和机器人消息记录到 cosmos db。

我只能使用以下博客 https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&tabs=csharp#using-cosmos-db 记录用户消息。

我希望记录用户和机器人的响应。

【问题讨论】:

    标签: c# logging botframework


    【解决方案1】:

    谢天谢地,这很容易,因为 ItranscriptLogger 和 TranscriptLoggerMiddleware 已经存在。

    创建您的 TranscriptStore 类(新类文件)

    using Microsoft.Bot.Builder;
    using Microsoft.Bot.Builder.Azure;
    using Microsoft.Bot.Schema;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace QuickTestBot_CSharp
    {
        public class CosmosTranscriptStore : ITranscriptLogger
        {
            private CosmosDbStorage _storage;
    
            public CosmosTranscriptStore(CosmosDbStorageOptions config)
            {
                _storage = new CosmosDbStorage(config);
            }
            public async Task LogActivityAsync(IActivity activity)
            {
                // activity only contains Text if this is a message
                var isMessage = activity.AsMessageActivity() != null ? true : false;
                if (isMessage)
                {
                    // Customize this to save whatever data you want
                    var data = new
                    {
                        From = activity.From,
                        To = activity.Recipient,
                        Text = activity.AsMessageActivity().Text,
                    };
                    var document = new Dictionary<string, object>();
                    // activity.Id is being used as the Cosmos Document Id
                    document.Add(activity.Id, data);
                    await _storage.WriteAsync(document, new CancellationToken());
                }
            }
        }
    }
    

    创建和添加中间件(在 Startup.cs 中)

    [...]
    var config = new CosmosDbStorageOptions
    {
        AuthKey = "<YourAuthKey>",
        CollectionId = "<whateverYouWant>",
        CosmosDBEndpoint = new Uri("https://<YourEndpoint>.documents.azure.com:443"),
        DatabaseId = "<whateverYouWant>",
    };
    
    var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));
    
    var middleware = options.Middleware;
    middleware.Add(transcriptMiddleware);
    [...]
    

    结果:

    注意:

    这可能是最简单/最好的方法。但是,您也可以使用turnContext.OnSendActivities() 捕获OnTurnAsync() 下的传出活动,然后也将传出活动写入存储。

    【讨论】:

    • 需要帮助在 startup.cs 中注册中间件。这一行说“选项”在当前上下文中不存在 - “var middleware = options.Middleware;”
    • @TomCat 看看this sample 是否能帮助您找到正确的方向。如果没有,请发布您自己的 SO 问题,并用botframework 标记它,我们会为您提供帮助。如果我们可以在您的 Startup.cs 中看到代码,这会有所帮助。
    • 谢谢。添加“使用(transcriptMiddlewareStore);”在适配器中解决了这个问题。
    • 我最终使用了 OnSendActivities() 方法。这非常适合我们的需求。 See here how to implement OnSendActivities().
    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多