【发布时间】:2019-07-19 12:05:41
【问题描述】:
我想使用 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();
}
}
}
【问题讨论】:
-
这可能与此有关:github.com/microsoft/botframework-sdk/issues/5467 这是由回答您在问题中引用的问题的同一个人打开的错误。
标签: c# sdk botframework azure-cosmosdb