Botframework 如何在BOTC框架的DebugActivityLogger中获得cosmosDB或storageAccount连接#
我想将我的对话历史记录保存到Azure Cosmos DB或storageAccountBotframework 如何在BOTC框架的DebugActivityLogger中获得cosmosDB或storageAccount连接#,botframework,azure-cosmosdb,azure-storage-account,Botframework,Azure Cosmosdb,Azure Storage Account,我想将我的对话历史记录保存到Azure Cosmos DB或storageAccount 关于Azure宇宙数据库 它似乎真的很容易使用。但我不知道如何获得Global.asax.cs中连接的DB连接。还是最好在IActivityLogger中再次获取 关于storageAccount 据介绍,SDK还提供了一种使用storageAccount保存会话历史记录的方法。但这种活动已经被压缩了。我想将未压缩的真实对话[JSON]保存到storageAccount 我正在尝试向Azure表存储中插入一
public class ActivityEntity : TableEntity
{
/// <summary>
/// Empty constructor.
/// </summary>
public ActivityEntity()
{ }
/// <summary>
/// Construct from an IActivity.
/// </summary>
/// <param name="activity"></param>
public ActivityEntity(byte[] Activity)
{
PartitionKey = "111";
RowKey = "11";
From = "111";
Recipient = "111";
Activity0 = Activity;
Version = 3.0;
}
/// <summary>
/// Version number for the underlying activity.
/// </summary>
public double Version { get; set; }
/// <summary>
/// Channel identifier for sender.
/// </summary>
public string From { get; set; }
/// <summary>
/// Channel identifier for receiver.
/// </summary>
public string Recipient { get; set; }
/// <summary>
/// Logged activity.
/// </summary>
[IgnoreProperty]
public byte[] Activity0 { get; set; }
/// <summary>
/// Generate a partition key given <paramref name="channelId"/> and <paramref name="conversationId"/>.
/// </summary>
/// <param name="channelId">Channel where activity happened.</param>
/// <param name="conversationId">Conversation where activity happened.</param>
/// <returns>Partition key.</returns>
public static string GeneratePartitionKey(string channelId, string conversationId)
{
return $"{channelId}|{conversationId}";
}
/// <summary>
/// Generate row key for ascending <paramref name="timestamp"/>.
/// </summary>
/// <param name="timestamp">Timestamp of activity.</param>
/// <returns></returns>
public static string GenerateRowKey(DateTime timestamp)
{
return $"{timestamp.Ticks:D19}";
}
}
class Program
{
public static object CloudConfigurationManager { get; private set; }
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=storagetesthuafu;AccountKey=iQ0EZexm3wbpWZqly2HtVH0/CZKRyMY9l2b0g20AQkUz7BX0BFLuBinMyYLe8Ow/zOA7vJqAMSxSHllT3JTL2g==;EndpointSuffix=core.windows.net");
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the CloudTable object that represents the "TemperatureData" table.
CloudTable table = tableClient.GetTableReference("messagelog");
TableQuery<BotDataRow> query = new TableQuery<BotDataRow>();
var data = table.ExecuteQuery(query);
var dataarry = data.ToArray();
var aa = dataarry.First();
var activity = aa.Activity0;
var after = Decompress(activity);
CloudTable tableTEST = tableClient.GetTableReference("messagelog");
byte[] bb = Encoding.UTF8.GetBytes(after);
ActivityEntity customer4 = new ActivityEntity(bb);
// Create the InsertOrReplace TableOperation.
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(customer4);
// added to the table.
table.Execute(insertOrReplaceOperation);
}
公共类活动实体:TableEntity
{
///
///空构造函数。
///
公共活动实体()
{ }
///
///从活动中构造。
///
///
公共活动实体(字节[]活动)
{
PartitionKey=“111”;
RowKey=“11”;
From=“111”;
收件人=“111”;
活动0=活动;
版本=3.0;
}
///
///基础活动的版本号。
///
公共双版本{get;set;}
///
///发送方的通道标识符。
///
来自{get;set;}的公共字符串
///
///接收机的信道标识符。
///
公共字符串收件人{get;set;}
///
///记录的活动。
///
[不动产]
公共字节[]活动0{get;set;}
///
///生成一个给定的分区键,并将其删除。
///
///活动发生的频道。
///发生活动的对话。
///分区键。
公共静态字符串GeneratePartitionKey(字符串通道ID、字符串会话ID)
{
返回$“{channelId}|{conversationId}”;
}
///
///为升序生成行键。
///
///活动的时间戳。
///
公共静态字符串生成器wkey(日期时间时间戳)
{
返回$“{timestamp.Ticks:D19}”;
}
}
班级计划
{
公共静态对象CloudConfigurationManager{get;private set;}
静态void Main(字符串[]参数)
{
CloudStorageAccount storageAccount=CloudStorageAccount.Parse(“DefaultEndpointsProtocol=https;AccountName=storagetesthuafu;AccountKey=iQ0EZexm3wbpWZqly2HtVH0/CZKRYM9L2B0G20AQKUZ7BX0BFLUBINmyyle8OW/ZO7VJQAMSXSHLLT3JTL2G==;EndpointSuffix=core.windows.net”);
//创建表客户端。
CloudTableClient tableClient=storageAccount.CreateCloudTableClient();
//创建表示“TemperatureData”表的CloudTable对象。
CloudTable=tableClient.GetTableReference(“messagelog”);
TableQuery=新建TableQuery();
var数据=table.ExecuteQuery(查询);
var dataarry=data.ToArray();
var aa=dataarry.First();
var活性=aa.Activity0;
var after=减压(活动);
CloudTableTest=tableClient.GetTableReference(“messagelog”);
byte[]bb=Encoding.UTF8.GetBytes(后面);
ActivityEntity customer4=新ActivityEntity(bb);
//创建InsertOrReplace表操作。
TableOperation insertOrReplaceOperation=TableOperation.InsertOrReplace(customer4);
//添加到表中。
表.Execute(insertoreplaceoperation);
}
创建使用DocumentDb的IActivityLogger实现不需要太多代码。下面是一个使用Azure Cosmos DB Emulator的示例:
class DocumentDbActivityLogger : IActivityLogger
{
const string DbKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
const string DbUri = "https://localhost:8081";
const string DbId = "ActivityLogsDb";
const string CollectionId = "ActivityLogsColleciton";
async Task IActivityLogger.LogAsync(IActivity activity)
{
try
{
var message = activity.AsMessageActivity();
if (message != null)
{
using (var documentClient = new DocumentClient(new Uri(DbUri), DbKey))
{
await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), message);
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Print(e.ToString());
}
}
}
然后,在Global.asax.cs中注册:
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<DocumentDbActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
}
Conversation.UpdateContainer(生成器=>
{
builder.RegisterType();
}
我不知道如何获得Global.asax.cs中连接的DB连接。或者最好在IActivityLogger中再次获得它?
是否要截取ActivityLogger中的所有消息,然后使用为活动创建JSON文档并将其存储在您的Cosmos DB集合中?但是活动已被删除en compressed。我想将未压缩的真实对话[JSON]保存到storageAccount。
您可以发现,正如您所说,活动将被压缩,如果您想在Azure table storage中保存真实对话[JSON],您可以自己实现。非常感谢。但我不知道如何保存真实对话[JSON]在Azure表存储中。我正在尝试向Azure表存储中插入一行包含压缩数据的行。但是Activity0始终为空。我已上载了我的代码。您能帮助我为什么它不工作吗?非常感谢!这是我第一次使用这种数据库。如果它是oracle,通常我需要在使用后关闭它。cosmos DB如何?有什么问题吗ng我在使用它时应该注意什么?这是一个奇怪和烦人的问题:非常感谢…我正在测试如何将Json数据插入Azure表存储,如Activity0。你能给我一些建议吗?顺便说一句,不必在机器人中这样做