我能够使用 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 表存储资源管理器后,我可以确认信息已保存。
我现在的问题是如何检索对话数据并将其作为字符串返回,以便我可以将其发送给代理进行审核?
最佳答案
你所有的对话消息(假设是消息而不是数据,因为对话数据在 Bot Framework 词汇表中是不同的东西,它是关于状态的)都存储在 Azure 表中,所以如果你想获取这些消息,你只需要查询表。
获取表项
你有几个选项来查询表,例如
- 用于 .NET 的 Microsoft Azure 存储客户端库:https://www.nuget.org/packages/WindowsAzure.Storage/
从文档中获取按分区获取所有行的示例:
// 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/