Storing conversation of a specific user temporarily

自闭症网瘾萝莉.ら 提交于 2019-12-04 09:25:24

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;

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.

I have explored preserving the conversation transcript for the exact same reason - to hand the conversation off to a human agent. The only difference is that my code uses the node.js SDK. See if you can port it to C#:

http://www.pveller.com/smarter-conversations-part-4-transcript/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!