我想使用ITranscriptLogger和TranscriptMiddelWare将整个聊天记录存储在cosmos DB中,但是我为此很努力。我已经读过this MS article,但是我想将其存储在Cosmos DB中,而不是Blob存储中。另外,我试图在Startup.cs中而不是Bot.cs中实例化记录日志,并且我尝试根据this answer来实现它,但没有任何运气。也就是说,笔录未存储,并且我的Azure cosmos DB中没有容器。感谢您的帮助和反馈。
代码:
我已经创建了TranscriptStore类,并按照引用的SO答案中的说明创建并添加了中间件:
CosmosTranscriptStore.cs
public class CosmosTranscriptStore : ITranscriptLogger
{
private CosmosDbStorage _storage;
public CosmosTranscriptStore(CosmosDbStorageOptions config)
{
_storage = new CosmosDbStorage(config);
}
public async Task LogActivityAsync(IActivity activity)
{
// activity only contains Text if this is a message
var isMessage = activity.AsMessageActivity() != null ? true : false;
if (isMessage)
{
// Customize this to save whatever data you want
var data = new
{
From = activity.From,
To = activity.Recipient,
Text = activity.AsMessageActivity().Text,
};
var document = new Dictionary<string, object>();
// activity.Id is being used as the Cosmos Document Id
document.Add(activity.Id, data);
await _storage.WriteAsync(document, new CancellationToken());
}
}
}
Startup.cs
public class Startup
{
private const string CosmosServiceEndpoint = "MyCosmosServiceEndpoint";
private const string CosmosDBKey = "MyCosmosDBKey";
private const string CosmosDBDatabaseName = "MyCosmosDBDatabaseName";
private const string CosmosDBCollectionName = "Transcript-storage";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var config = new CosmosDbStorageOptions
{
AuthKey = CosmosDBKey,
CollectionId = CosmosDBCollectionName,
CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
DatabaseId = CosmosDBDatabaseName,
};
var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Create the Bot Framework Adapter.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddSingleton<MainDialog>();
services.AddTransient<IBot, WelcomeBot<MainDialog>>();
services.AddBot<WelcomeBot<MainDialog>>(options =>
{
var middleware = options.Middleware;
middleware.Add(transcriptMiddleware);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
}
}
}
答案 0 :(得分:3)
我设法通过将转录本存储中间件添加到适配器来解决此问题,在提出这个问题之前,我可能应该从一开始就应该这样做,但是我对bot框架和这种类型的编程非常陌生。 这是我解决的方法:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
var config = new CosmosDbStorageOptions
{
AuthKey = CosmosDBKey,
CollectionId = CosmosDBAntoherCollectionName,
CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
DatabaseId = CosmosDBDatabaseName,
};
var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));
services.AddSingleton(transcriptMiddleware);
...
}
AdapterWithErrorHandler.cs
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
public AdapterWithErrorHandler(TranscriptLoggerMiddleware transcriptMiddlewareStore, IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger, ConversationState conversationState = null)
: base(configuration, logger)
{
Use(transcriptMiddlewareStore);
OnTurnError = async (turnContext, exception) =>
{
...
};
}
}
此外,如果您希望将整个聊天记录存储在一个文档/项目中,我强烈建议您通过会话ID而不是活动ID将数据存储在CosmosTranscriptStore类中。原因是每个活动都有自己的ID,因此会在Cosmos DB中为每个活动创建一个新项目。
public class CosmosTranscriptStore : ITranscriptLogger
{
...
public async Task LogActivityAsync(IActivity activity)
{
...
document.Add(activity.Conversation.Id, data);
await chatStorage.WriteAsync(document, new CancellationToken());
}
}