【发布时间】:2019-12-31 02:24:31
【问题描述】:
我正在开发的机器人替代了希望与公司联系的潜在客户的联系表格,因此必须将用户输入保存在数据库中。我已成功将 Cosmos DB 连接到我的机器人,该机器人在使用机器人时收集状态数据。我有一个对话框堆栈,每个用户输入一个对话框(姓名、电子邮件和用户想要留下的消息)。
我找不到任何有关如何保存用 C# 编写的机器人的对话历史记录的有用文档。谁能帮我吗?我还是 Bot Framework 和 C# 的初学者。
这是我的 global.asax 文件:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var uri = new Uri(ConfigurationManager.AppSettings["DocumentDbUrl"]);
var key = ConfigurationManager.AppSettings["DocumentDbKey"];
var store = new DocumentDbBotDataStore(uri, key);
Conversation.UpdateContainer(
builder =>
{
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();
builder.Register(c => new CachingBotDataStore(store, CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
.As<IBotDataStore<BotData>>()
.AsSelf()
.InstancePerLifetimeScope();
});
}
}
这是我用来收集用户名的NameDialog:(其他对话框几乎与此相同)
[Serializable]
public class NameDialog : IDialog<string>
{
private int attempts = 3;
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("What's your name?");
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if ((message.Text != null) && (message.Text.Trim().Length > 0))
{
context.Done(message.Text);
}
else
{
--attempts;
if (attempts > 0)
{
await context.PostAsync("I couldn't understand, can you try again?");
context.Wait(this.MessageReceivedAsync);
}
else
{
context.Fail(new TooManyAttemptsException("This is not a valid input"));
}
}
}
}
【问题讨论】:
-
为了澄清,您是要保存整个对话历史记录,还是只保存用户数据(姓名、电子邮件、消息)?
-
另外,您在 SDK 的 V3 而非 V4 中构建机器人是否有原因?
标签: c# .net botframework azure-cosmosdb