Search code examples
c#.net-corebotframeworkchatbot

BotFramework State not saving


I am having a problem with the bot state. I am pretty much following the way they said to save user state here. My only changes is that I am using the dialog bot that is in Visual Studio. When I do the last step in my main dialog I set several properties to values in my user profile object and then I call SaveChangesAsync and then heads back up to handle the next request. When I get the next request. I do the userStateAccessors.GetAsync call to get the latest version just to keep it fresh. But I noticed that my changes were not saved.

Has anyone else had this problem. My employer wants the chatbot to retain state per user so that they wont have to enter in all the information per request.

5/11/2011 Here is the section of code from my DialogBot.cs

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
        {
            await base.OnTurnAsync(turnContext, cancellationToken);

            // Save any state changes that might have occured during the turn.
            await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }

Also from my MainDialog.cs file

  private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext,
            CancellationToken cancellationToken)
        { 
                var conversationStateAccessors =
                    _conversationState.CreateProperty<ConversationData>(nameof(ConversationData));
                _conversationData =
                    await conversationStateAccessors.GetAsync(stepContext.Context, () => new ConversationData());

                var userStateAccessors = _userState.CreateProperty<UserProfile.UserProfile>(nameof(UserProfile));

                    _userProfile = await userStateAccessors.GetAsync(stepContext.Context, () => new UserProfile.UserProfile()); 
      //More code is here 
        }

Also if we comment out make so that we only get _userProfile only if it is null, then we run into problems with other users seeing the data. Not sure if that is related or not.


Solution

  • What I did was take the userprofile and made it local to each and every turn. Also what we did was to make it use permanent storage in a database so that we can store the data even when we reset the chatbot