首先简要介绍:Bot框架将对话数据存储在Azure表/ Cosmos DB(我的情况下为Azure表)的存储中。对于每个对话,Azure表中都有一个条目,其中包含时间戳,用户ID,对话消息和其他详细信息。
我正在尝试使用C#中的自定义代码从Azure表存储中检索对话详细信息,如下所示。
Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString);
Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("botdata");
TableQuery<DynamicTableEntity> projectionQuery = new TableQuery<DynamicTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "directline:user"));
var dataRow = table.ExecuteQuery(projectionQuery).Where(q => q.RowKey == "souvik").FirstOrDefault();
var conversation = Encoding.UTF8.GetString(dataRow.Properties["Data"].BinaryValue);
Console.WriteLine(conversation);
当我在Azure表存储中检索以二进制格式存储的数据时,我得到了一些乱码文本,类似于这个 - \u001f�\b\0\0\0\0\0\u0004\0��\u0005\0C���\u0002\0\0\0
。数据类似于Azure存储资源管理器中显示的内容 -
我已经尝试并测试了每个对话,并且我已经看到每次我键入并向bot发送数据并获得响应时,新记录将以正确的时间戳添加到Azure表中。这是检索数据的正确方法还是我做错了什么?如果我可以添加更多详细信息,请与我们联系。
更新-1:
我已经提到了这个问题 - How to retrieve Saved Conversation Data in Azure (Tablelogger),但对我的问题没有多大帮助。
更新-2
根据Fei在下面的回答,我刚注意到当我使用PartitionKey
作为directline:user
或directline:conversation
时,我将反序列化的值设为{}
,但如果我使用{{} 1}}我得到像这样的东西 -
directline:private
我觉得上面JSON中的{"ResumptionContext":{"locale":"en-US","isTrustedServiceUrl":true},
"DialogState":"H4sIAAAAAAAEAO1a328iVRRmYGYKbTfbdZtNzLq7GM26DWS2FGi7MVUptUrcVi1VYxrSvcCFjh1mmrl3quyjb74YEx+N/hW+uzFxE/8KX9fsH6HnzAxQ7A8GhHpZoemBD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vhi5lG3ja1jEtwYgMZJVxsOurpOvWN4gjLmvinrdJOjWdPvV0rQ3JG755/wz8Zxdh6BNziR8hCLRK74jeNNeSX0d50GeuzlX745BztU
3wMmvI604DmxKqrpZ19AP5lrwIOjBqa/K+o+4VIojbPcCn9D6X8gNMNM5zWSQBnSGYwVAEfzqyhhMeQrePJ4nGYbLUTc1C+o9SMQz5eJE9JsJwZrtaL+LGYxCYwLRcPk50pYSiO54gj4t3W4piY
WJ1h2azOuIDpRsL5B+PalPfu5PnwgSQVvKJFBHo5hGOaMmu3WD9mKlTb0P8fw4kRjDbeFBkxtQfSxCjr5XJ+pjoj4uQX0I0piCCxUFt54CbYkHP876tkcQ+QOim4mHlnXkXRH2sDSFZglNGsxsBs
zax2YOYur4lQrql+kYhoJr8njKisHpcLfbryJpHBlU83DUdiyLe4FnBs1SFs0ymhVUPKmW3l8Vq546khkZJVqNoO6dzkE3rdAqNbncR9LCLfyldEtsvglmxseWNc2KpznXBIxbfQuc+k66QJTET8
RxztrbXrm8F1B5nH7Fe65zOfyvIVI26KnTnAX1bXDrj//erZFi72/Qs62CfWdsm2OntrEOAtYO3BnC+pFWWmWzDmbe93wHKlE/plU3wWr+Xy896kZwz9R33UxcxL6zvBwdDQNvRBbUTdxkhMVxXRSKesU28zeNVmRMBTMAAA=="}
数据需要解密?
对于我输入的每封邮件和收到的响应,Azure表中插入了三条记录,每条记录都有不同的DialogState
,分别是PartitionKey
,directline:private
和{{1 }}
答案 0 :(得分:1)
Azure Table Storage
中的二进制数据存储为Base64 encoded string
。您需要做的是首先将此字符串转换为字节,然后从这些字节中获取字符串。
类似的东西:
var conversation = Encoding.UTF8.GetString(Convert.FromBase64String(dataRow.Properties["Data"].BinaryValue));
答案 1 :(得分:1)
如果您想使用WindowsAzure.Storage client library从表存储中退出实体并从 数据属性 中提取数据,则可以参考以下代码。
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName={your_account_name};AccountKey={your_account_key};EndpointSuffix=core.windows.net");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("botdata");
TableQuery<MessageEntity> query = new TableQuery<MessageEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "emulator:user"));
foreach (MessageEntity entity in table.ExecuteQuery(query))
{
string mydata = "";
using (var msi = new MemoryStream(entity.Data))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
mydata = Encoding.UTF8.GetString(mso.ToArray());
}
object data = JsonConvert.DeserializeObject(mydata);
//.....
}
MessageEntity:
public class MessageEntity : TableEntity
{
public MessageEntity(string pk, string rk)
{
this.PartitionKey = pk;
this.RowKey = rk;
}
public MessageEntity() { }
public string BotId { get; set; }
public string ChannelId { get; set; }
public string ConversationId { get; set; }
public byte[] Data { get; set; }
public string UserId { get; set; }
}
测试结果:
注意:
byte[]
BotDataEntity 类。答案 2 :(得分:0)
我假设您使用的是botbuilder-azure软件包
请勿在对话框中执行此操作,因为您只能访问context
对象中的相同数据。它恰好是我编写代码的地方
实际上这很简单:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
IBotDataStore<BotData> table = new TableBotDataStore(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
//use the type of state data you need
var userData = await table.LoadAsync(Address.FromActivity(activity), BotStoreType.BotUserData, CancellationToken.None );
var privateConvoData = await table.LoadAsync(Address.FromActivity(activity), BotStoreType.BotPrivateConversationData, CancellationToken.None );
var convoData = await table.LoadAsync(Address.FromActivity(activity), BotStoreType.BotConversationData, CancellationToken.None);
//in this case I am just replying with the data, but do what you need with it here
var reply = activity.CreateReply(userData.Data.ToString());
var reply2 = activity.CreateReply(privateConvoData.Data.ToString());
var reply3 = activity.CreateReply(convoData.Data.ToString());
await context.PostAsync(reply);
await context.PostAsync(reply2);
await context.PostAsync(reply3);
context.Wait(MessageReceivedAsync);
}
如果您在botbuilder-azure的来源中注意到有serialize和反序列化方法。发生这种情况的地方:
private byte[] Serialize(object data)
{
using (var cmpStream = new MemoryStream())
using (var stream = new GZipStream(cmpStream, CompressionMode.Compress))
using (var streamWriter = new StreamWriter(stream))
{
var serializedJSon = JsonConvert.SerializeObject(data, serializationSettings);
streamWriter.Write(serializedJSon);
streamWriter.Close();
stream.Close();
return cmpStream.ToArray();
}
}
所以你需要的数据被压缩成&#34; jibberish&#34;你看到了。通过LoadAsync
方法访问数据时,它也会像这样解压缩:
private object Deserialize(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
using (var gz = new GZipStream(stream, CompressionMode.Decompress))
using (var streamReader = new StreamReader(gz))
{
return JsonConvert.DeserializeObject(streamReader.ReadToEnd());
}
}
反序列化在return statement LoadAsync
中的return new BotData(entity.ETag, entity.GetData());
方法内调用它是GetData()
方法,如下所示:
internal ObjectT GetData<ObjectT>()
{
return ((JObject)Deserialize(this.Data)).ToObject<ObjectT>();
}