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

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

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

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

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表存储资源管理器后,我可以确认信息已保存。

在此处输入图像描述

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

所有的对话消息(比如消息而不是数据,如BOT Framework词汇表中的对话数据是一个不同的东西,它是关于状态的)存储在Azure表中,所以如果你想获得这些消息,你只需查询表。

获取表项

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

  • 适用于.NET的Microsoft Azure存储客户端库: https : //www.nuget.org/packages/WindowsAzure.Storage/

来自doc的示例以按分区获取所有行:

 // 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 query = new TableQuery().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 : //docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet

  • REST API: https : //docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities

数据组织

在您的捕获中,您可以看到表格的PartitionKey由您的channel和看起来像对话ID的内容串联而成。 TableLogger的来源证实了这一点 :

 ///  /// Construct from an IActivity. ///  ///  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被映射到您的bot Activity对象,因此它是一个包含多个信息的对象。 您将不得不做一些JsonConvert.DeserializeObject来获取有趣的信息。