Search code examples
c#.netbotframework

BotData not saving


I am trying to create middleware for a firstRun in C# and cant seem to get it to save the data. Since context is not present in the middleware, I am using the following syntax to save data. Not only does it not save but I am wondering about the following :

why, when I save in a normal dialog do i have access to SetValue and in here I only have access to SetProperty?

public class FirstRunActivityLogger : IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        var allActivity = (Activity)activity;
        StateClient stateClient = allActivity.GetStateClient();
        BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
        if (!userData.GetProperty<bool>("FirstRun"))
        {
            Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
            userData.SetProperty<bool>("FirstRun", true);
        }
    }
}

Solution

  • Just keep in mind that userData is just an object and calling SetProperty() simply sets property of that object without saving anything. So for saving the bot state you should use SetUserDataAsync() and pass that changed userData to it. Here is how to do it:

    await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);