微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过 Microsoft Bot Framework 将记录插入 Azure Table Storage?

如何解决如何通过 Microsoft Bot Framework 将记录插入 Azure Table Storage?

目前我正在使用 C# 使用 Bot Framework 4.0,我正在尝试 实施一个解决方案,我将来自用户的消息存储在 Azure 表存储中。 我已经想出了实现这一目标的想法,但我的方法仅适用于本地 当我通过 Bot Framework Emulator 对其进行测试时。

当我将代码发布到我的机器人到 Azure 时,消息不会存储在表存储中。 我有点困惑,因为我真的不知道为什么会发生这种情况。我已经检查过了 App Insights,但显示的信息对我帮助不大。

这些是我安装的 Nugget-Packages:

  • azure.identity\1.3.0\
  • azure.security.keyvault.secrets\4.1.0\
  • microsoft.aspnetcore.app\2.1.1\
  • microsoft.azure.cosmos.table\1.0.8\
  • microsoft.bot.builder.ai.luis\4.7.0\
  • microsoft.bot.builder.ai.qna\4.7.0\
  • microsoft.bot.builder.dialogs\4.9.4\
  • microsoft.bot.builder.integration.aspnet.core\4.7.0\

还有我安装的 SDK:

  • Microsoft.AspNetCore.App 2.1.1
  • Microsoft.NetCore.App 2.1.0

我的整个实现由四个元素组成,我现在将简要解释一下:

        private static CloudTable GetTableReference()
        {
            var storageAccount = CloudStorageAccount.Parse("<connection-string>");
            var tableClient = storageAccount.CreateCloudTableClient();

            return tableClient.GetTableReference("<table-name>");
        }

第一种方法是打包 Azure 表存储的凭据。该术语是一个占位符,代表 Azure 表存储的连接字符串。是表存储名称的占位符(显然)。

private static void InsertRecord(ITurnContext<IMessageActivity> turnContext,CloudTable table)
        {
            string partitionKey = DateTime.Now.ToString();
            string rowKey = DateTime.Now.ToString();

            var Eintrag = new UserEntity(partitionKey,rowKey)
            {
                UserStatement = $"{turnContext.Activity.Text}",};

            var insertOperation = TableOperation.Insert(Eintrag);
            table.ExecuteAsync(insertOperation);
        }

第二种方法为 Table Storage 准备内容。例如,用户的消息也存储在“UserStatement”变量中,还有时间戳。

protected async Task NoMatchFound(ITurnContext<IMessageActivity> turnContext,CancellationToken cancellationToken)
        {
            var table = GetTableReference();
            InsertRecord(turnContext,table);

            // Wird ausgeführt,wenn keine KNowledgeBase gefunden wird
            System.Diagnostics.Debug.WriteLine("### FINDE KEINE PASSENDE ANTWORT ###");
            await turnContext.SendActivityAsync(MessageFactory.Text("Leider kann ich Ihnen hierbei noch nicht weiterhelfen. Ich bin aber schon dabei,Neues zu lernen!"),cancellationToken);
        }

在最后一个方法中,所有内容都放在一起并最终执行。这三个提到的方法位于名为“dispatchbot.cs”的同一个文件中(我使用了 NLP 和 Microsoft 为我的机器人提供的 dispatch 示例)。

using Microsoft.Azure.Cosmos.Table;
using System;

namespace TableStorage
{
    public class UserEntity : TableEntity
    {
        public UserEntity(string partitionKey,string rowKey)
        {
            PartitionKey = partitionKey;
            RowKey = rowKey;
        }

        public UserEntity()
        {
        }

        public string UserStatement { get; set; }

    }
}

我拥有的最后一件事是另一个名为“UserEntity.cs”的文件,其中包含表条目的内容。整个实现肯定更容易实现。

我唯一需要做的就是调用 NoMatchFound() 方法,并且用户的输入应该存储在 Azure 表存储中。但如果我尝试在线测试,情况并非如此。

这是一个视频,我展示了表存储记录在本地没有任何问题:https://youtu.be/fFtzjmOfQDs

我还将附上我的应用洞察中的屏幕截图 - 也许这会有所帮助。

希望你能帮帮我!如果我需要添加更多信息,请随时提出要求。

解决方法

我看了您提供的视频,发现在您的本地机器上,datetime 的格式是:18.01.2021 17:35:59

所以我认为根本原因是当它部署到 azure 时,datetime 是这种格式:1/19/2021 10:09:31 AM。根据此文档 Characters Disallowed in Key Fields,字符 / 不允许用于 PartitionKeyRowKey 属性。

因此在方法private static void InsertRecord(xxx)中,请使用Replace方法将字符/替换为字符.或其他允许的字符。如下图:

    private static void InsertRecord(ITurnContext<IMessageActivity> turnContext,CloudTable table)
    {

        //replace the character / with character .
        string partitionKey = DateTime.Now.ToString().Replace('/','.');
        string rowKey = DateTime.Now.ToString().Replace('/','.');

        var Eintrag = new UserEntity(partitionKey,rowKey)
        {
            UserStatement = $"{turnContext.Activity.Text}",};

        var insertOperation = TableOperation.Insert(Eintrag);
        table.ExecuteAsync(insertOperation);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。