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

How to retrieve Saved Conversation Data in Azure (Tablelogger)

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

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

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

获取 table 项

您有几个选项可以查询 table,例如

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

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

数据组织

在您的捕获中,您可以看到 table 的 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 对象,因此它是一个包含多个信息的对象。您将需要做一些事情 JsonConvert.DeserializeObject<Activity> 才能获得有趣的信息。