Storing conversations of a specific user temporarily

I tried using IActivityLogger to capture the user's conversation, is there a way to compile the user and bot conversation to a temporary holder, like a variable or session? I need to temporarily store it somewhere, which is easily accessible only when the user wants to talk with a real person, and not with a bot. An email will be sent containing the previous conversation between the user and the bot. I do not want to save it to the database, as some users will not want to do this.

See the codes used. Registrar 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");
    }
}

Global Asax:

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

        GlobalConfiguration.Configure(WebApiConfig.Register);

    }
+4
source share
2

Ezequiel , . - :

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);
    }
}

:

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;

- . , , , .

0

Source: https://habr.com/ru/post/1675579/


All Articles