我一直在开发azure聊天机器人,特别是C#中的QnA机器人,现在我正在考虑将对话历史存储到表或数据库存储中。
但与web上的大多数教程和文档不同的是,我不想从头到尾存储整个对话,我只想存储用户发送给机器人的第一条消息。我希望这条消息被暂时存储,直到用户键入"no“。当用户键入"no“时,我希望永久存储临时存储中的内容。
这在聊天机器人中是可能的吗?
这里的任何帮助或见解都将不胜感激!
发布于 2018-08-01 03:37:57
使用像字典这样的临时存储很容易做到这一点。有许多方法可以实现这一点。我要考虑的一件事是使用scorables来捕获"No“文本。在本例中,我没有使用scorables,但它提供了您想要的基本功能。其思想是,当收到一条消息时,检查是否已经保存了来自该userId的消息,如果没有保存,则保存它。如果用户发送文本"No“,则将文本保存到永久存储中,并从字典中删除该条目。我只是在一个基本的RootDialog.cs中这样做:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var userId = activity.From.Id;
var message = activity.Text;
if (!Utils.FirstMessageDictionary.ContainsKey(userId))
{
Utils.FirstMessageDictionary.Add(userId, message);
await context.PostAsync($"Message saved {userId} - {Utils.FirstMessageDictionary[userId]}");
}
if (message.ToLower() == "no")
{
//save to permanent storage here
Utils.FirstMessageDictionary.Remove(userId);
await context.PostAsync($"Entry Removed for {userId}");
try
{
await context.PostAsync($"{userId} - {Utils.FirstMessageDictionary[userId]}");
}
catch (Exception e)
{
await context.PostAsync($"No entry found for {userId}");
}
}
context.Wait(MessageReceivedAsync);
}我还为字典创建了这个简单的类:
public static class Utils
{
public static Dictionary<string, string> FirstMessageDictionary = new Dictionary<string, string>();
}https://stackoverflow.com/questions/51607582
复制相似问题