首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用触发词在Azure聊天机器人中永久存储对话数据

使用触发词在Azure聊天机器人中永久存储对话数据
EN

Stack Overflow用户
提问于 2018-07-31 15:20:14
回答 1查看 120关注 0票数 4

我一直在开发azure聊天机器人,特别是C#中的QnA机器人,现在我正在考虑将对话历史存储到表或数据库存储中。

但与web上的大多数教程和文档不同的是,我不想从头到尾存储整个对话,我只想存储用户发送给机器人的第一条消息。我希望这条消息被暂时存储,直到用户键入"no“。当用户键入"no“时,我希望永久存储临时存储中的内容。

这在聊天机器人中是可能的吗?

这里的任何帮助或见解都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2018-08-01 03:37:57

使用像字典这样的临时存储很容易做到这一点。有许多方法可以实现这一点。我要考虑的一件事是使用scorables来捕获"No“文本。在本例中,我没有使用scorables,但它提供了您想要的基本功能。其思想是,当收到一条消息时,检查是否已经保存了来自该userId的消息,如果没有保存,则保存它。如果用户发送文本"No“,则将文本保存到永久存储中,并从字典中删除该条目。我只是在一个基本的RootDialog.cs中这样做:

代码语言:javascript
复制
    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);
    }

我还为字典创建了这个简单的类:

代码语言:javascript
复制
public static class Utils
{
    public static Dictionary<string, string> FirstMessageDictionary = new Dictionary<string, string>();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51607582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档