how to get chat history from azure table storage
Asked Answered
L

1

10

Since we are forced to drop the stateclient and move to a custom storage, in my case an azure table storage. Using my storageexplorer I can see that it already saved conversation data on my azure. How can I update my ff code to get past chat history? Before I'm using a IActivityLogger class to log a conversation, but now I'm confused on how to update it.

Global Asax Before:

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

Global Asax After:

protected void Application_Start()
{
    Conversation.UpdateContainer(builder =>
    {
        builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));
        var store = new TableBotDataStore(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
        builder.Register(c => store)
        .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
        .AsSelf()
        .SingleInstance();
    });

    GlobalConfiguration.Configure(WebApiConfig.Register);
}

Logger Class:

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

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

    }
}

This is how i use it (how can i update the stateclient part and use my storage on azure?:

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 += $" <br /> <b>{storedActivity.From.Name}:</b> {storedActivity.AsMessageActivity().Text}";
    }
    // Get any saved values
    StateClient sc = activity.GetStateClient();
    BotData userData = sc.BotState.GetPrivateConversationData(
    activity.ChannelId, activity.Conversation.Id, activity.From.Id);
    var UserEmail = userData.GetProperty<string>("Email");
    var Name = userData.GetProperty<string>("Name");
}

They gave a new tablelogger class. Unfortunately I don't know how to consume it. TableLogger.cs

Lythraceous answered 18/1, 2018 at 4:8 Comment(3)
This question is mixing StateClient and Logging. Should be closed as the OP created a new question more detailed and exact: #48374971Surcingle
activity.GetStateClient() will not retrieve your TableBotDataStore custom state client, but is using the default state client. See here #46086114 for how to create an instance of IBotDataStore<BotData> that will use your registered TableBotDataStoreNonferrous
Possible duplicate of How to retrieve Saved Conversation Data in Azure (Tablelogger)Madea
T
0

Microsoft.Azure.Cosmos.Table as it is in maintenance mode and will be deprecated soon.

Tomlin answered 20/1, 2020 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.