c# - 如何在 Azure 中检索保存的对话数据(Tablelogger)

标签 c# botframework azure-table-storage

我能够使用 tablelogger.cs 实现保存对话数据 TableLogger.cs

我按照本教程保存了对话历史记录。 Logging Conversation History

我用来保存聊天记录的代码是:

 var tableName = ConfigurationManager.AppSettings["TableName"].ToString();
 var account = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

 Conversation.UpdateContainer(
            builder =>
            {
                account.CreateCloudTableClient().GetTableReference(tableName).DeleteIfExists();
                builder.RegisterModule(new TableLoggerModule(account, tableName));
            });

检查 Azure 表存储资源管理器后,我可以确认信息已保存。

enter image description here

我现在的问题是如何检索对话数据并将其作为字符串返回,以便我可以将其发送给代理进行审核?

最佳答案

你所有的对话消息(假设是消息而不是数据,因为对话数据在 Bot Framework 词汇表中是不同的东西,它是关于状态的)都存储在 Azure 表中,所以如果你想获取这些消息,你只需要查询表。

获取表项

你有几个选项来查询表,例如

从文档中获取按分区获取所有行的示例:

// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the CloudTable object that represents the "people" table.
CloudTable table = tableClient.GetTableReference("people");

// Construct the query operation for all customer entities where PartitionKey="Smith".
TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));

// Print the fields for each customer.
foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
        entity.Email, entity.PhoneNumber);
}

执行更符合您需求的定制请求的文档:https://learn.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet

数据组织

在您的捕获中,您可以看到 PartitionKey该表由您的 channel 串联而成和一些看起来像对话 ID 的东西。 TableLogger的消息来源证实了这一点here :

/// <summary>
/// Construct from an IActivity.
/// </summary>
/// <param name="activity"></param>
public ActivityEntity(IActivity activity)
{
    PartitionKey = GeneratePartitionKey(activity.ChannelId, activity.Conversation.Id);
    RowKey = GenerateRowKey(activity.Timestamp.Value);
    From = activity.From.Id;
    Recipient = activity.Recipient.Id;
    Activity = activity;
    Version = 3.0;
}

因此您可能会对获取给定 partitionKey 的所有行感兴趣。

对于其他字段:RowKey是时间戳,Activity映射到您的机器人 Activity object 所以它是一个包含多个信息的对象。你将不得不做一些JsonConvert.DeserializeObject<Activity>获取有趣的信息。

关于c# - 如何在 Azure 中检索保存的对话数据(Tablelogger),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374471/

相关文章:

c# - 手动触发作业不起作用

bots - 开始与 Microsoft bot builder 和 microsoft bot 框架的对话

c# - 使用 Azure 聊天机器人时如何仅存储用户响应

c# - 从 Azure 表捕获错误的简洁方法(除了字符串匹配?)

c# - UML to C# 中的箭头和闭合箭头有什么区别?

c# - 根据列表 C# 中项目的第一个和最后一个字符区分

c# - 是否可以动态更改 MS - SimpleMembership 数据库连接?

javascript - 无法获取 setTimeOut 内的 conversationState 属性值

azure - Azure 存储入门 : Blobs vs Tables vs SQL Azure

azure - 我需要克隆 Azure 存储帐户用于暂存槽吗?