临时存储特定用户的对话 [英] Storing conversation of a specific user temporarily

查看:75
本文介绍了临时存储特定用户的对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用IActivityLogger捕获用户的对话,是否可以将用户和漫游器的对话编译为变量或会话等临时持有人?我需要将其临时存储在仅当用户希望与真实的人而不是机器人聊天时才可以使用的地方.将会发送一封电子邮件,其中包含用户和漫游器的先前对话.我不想将其保存到数据库中,因为某些用户不会选择这样做.

I tried using IActivityLogger to capture the conversation of a user, is there a way to compile the conversation of the user and the bot to a temporary holder like a variable or session? I need to temporarily store it somewhere that is readily available only when the user wants to talk to a real person instead of a bot. An email containing the previous conversation of the user and the bot will be sent. I don't want to save it to a DB since some user will not opt to do so.

请参阅使用的代码. 记录器类别:

See Codes used. Logger Class:

 public class Logger:IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        var a = ($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}" + "\b\r");
    }
}

全局Asax:

protected void Application_Start()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<Logger>().AsImplementedInterfaces().InstancePerDependency();
        builder.Update(Conversation.Container);

        GlobalConfiguration.Configure(WebApiConfig.Register);

    }

推荐答案

正如Ezequiel在评论中所建议的,您可以将活动存储在字典中.像这样:

As suggested by Ezequiel in the comments, you could store the activities in a dictionary. Something like:

public class Logger : IActivityLogger
{
    public static ConcurrentDictionary<string, List<IActivity>> Messages = new ConcurrentDictionary<string, List<IActivity>>();

    public Task LogAsync(IActivity activity)
    {
        var list = new List<IActivity>() { activity };            
        Messages.AddOrUpdate(activity.Conversation.Id, list, (k, v) => { v.Add(activity); return v; });
        return Task.FromResult(false);
    }
}

然后将它们发送给用户:

Then send them to the user later:

case ActivityTypes.Message:

    if (!string.IsNullOrEmpty(activity.Text) && activity.Text.ToLower() == "history")
    {
        using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
        {
            var reply = activity.CreateReply();
            var storedActivities = new List<IActivity>();
            var found = Logger.Messages.TryGetValue(activity.Conversation.Id, out storedActivities);
            if (storedActivities != null)
            {
                foreach (var storedActivity in storedActivities)
                {
                    reply.Text += $"\n\n {storedActivity.From.Name}: {storedActivity.AsMessageActivity().Text}";
                }
            }
            else
            {
                reply.Text = "no history yet";
            }

            //or, send an email here...
            var client = scope.Resolve<IConnectorClient>();
            await client.Conversations.ReplyToActivityAsync(reply);
        }                               
    }
    else
        await Conversation.SendAsync(activity, MakeRootDialog);
    break;

您有时还需要从列表中删除对话.您可能需要某种过期策略,因为并非每个频道都会通知bot用户已退出会话.

You'll also need to remove the conversation from the list at some point. You'll probably want an expiration policy of some sort, since not every channel will notify the bot that the user has left the conversation.

这篇关于临时存储特定用户的对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆