如何通过Microsoft Bot Framework将记录插入到Azure表存储中?

耳环

目前,我正在使用带有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);
        }

第二种方法为表存储准备内容。例如,用户消息也存储在“ 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与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

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

I hope you can help me out! If i need to add more informations, feel free to ask for that.

Ivan Yang

I watched the video you provided, and found that in your local machine, the datetime is in this format: 18.01.2021 17:35:59.

So I think the root cause is that when it's deployed to azure, the datetime is in this format: 1/19/2021 10:09:31 AM. And as per this doc Characters Disallowed in Key Fields, the character / is not allowed for the PartitionKey and RowKey properties.

So in the method private static void InsertRecord(xxx), please use the Replace method to replace the character / with character . or other allowed chars. Like below:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过Microsoft Bot Framework中的直接行将我的用户ID令牌从客户端发送到Bot

如何在Microsoft Bot Framework上通过ID获取用户电子邮件?

通过循环Microsoft Bot Framework中的文件动态创建卡

Microsoft bot:如何记录每个对话步骤?

如何使用Microsoft Bot Framework从我的Bot显示欢迎消息

如何开始使用Microsoft Bot Framework?

如何将来自用户的消息存储在表存储中?(Microsoft Bot Framework SDK4和C#)

使用 Bot 框架通过 NodeJS 发布到 Microsoft Teams 频道

如何将新行插入Microsoft Bot Framework缩略图卡字幕

如何在Microsoft Bot Framework + C#中安排任务?

如何从Microsoft Bot Framework中的PromptValidator获取stepContext?

在Microsoft Bot Framework中,如何从聊天的URL中提取参数?

如何解决Microsoft Bot Framework中的内容类型错误?

如何在Azure中向Microsoft Teams Bot添加授权

如何使用Java的Bot Framework SDK将卡片添加到Microsoft Team Bot?

AZURE Microsoft Bot Framework在Skype Bot上的使用成本

Azure中的Microsoft Bot是否需要任何特定权限才能在表存储中写入?

Microsoft Bot Framework状态管理

如何使用DirectLine通道在Azure表存储中存储和检索Bot数据?

Microsoft Azure:如何在插入/删除表存储行时将事件传递到Azure的事件队列

如何在Java中使用Microsoft Bot Framework渲染降价?

如何在Microsoft Bot Framework中使用全局命令?

如何使用Microsoft Bot Framework处理丰富的内容?

Microsoft Bot Framework如何设置Facebook消息标签

如何从 Microsoft Bot Framework 发送 SMS(使用 Twilio 频道)?

如何在 CentOS 上运行 Microsoft Bot Framework

Microsoft Azure Bot类型选择

是否可以通过Microsoft Bot Framework自定义团队中收到的图像和GIF的大小

如何将值从 Microsoft Bot 发送到 Javascript?