jamesemann / bot-framework-v4-intro-dotnet-core

Demo code accompanying YouTube series 'intro to bot framework - botbuilder v4'
https://www.youtube.com/channel/UCslMhoVYpWamAZNXNxARhhA
35 stars 44 forks source link

States not saving to class #2

Open joachimpr opened 5 years ago

joachimpr commented 5 years ago

Hi James,

I followed the states video (https://www.youtube.com/watch?v=ka0Am45Lo6k&t=58s) to a T. I have noticed that there have been some changes in the way states are managed (using accessors etc) since this video came out so I used the github code as a reference. Everything so far worked but for some reason my states are not saving. I had the same issue when following other tutorials and I don't know what I am doing wrong. The only changes I made is that in my states class, I changed the counter int to a name string value. My bot code looks like this:

if (turnContext.Activity.Type is ActivityTypes.Message)
{
    var state = await BotAccessors.DemoStateAccessor.GetAsync(turnContext, () => new CanteenState(), cancellationToken:cancellationToken);

    await turnContext.SendActivityAsync($"The current state of name is {state.name} and is being updated to {turnContext.Activity.Text}");
    state.name = turnContext.Activity.Text; //not sure if the problem is the way I am saving the value to the class member.
}

The problem is that when saving the value to state.name, it cannot be retrieved with the next message that is retrieved indicating that my state is never saved. Any idea what I am doing wrong? The rest of the code all matches the code that is on github.

anthonyrs06 commented 5 years ago

same problem here, let me know if you find out the fix!

EDIT: Fixed - needed to add the SetAsync and then SaveChangesAsync

 await BotAccessors.ConversationDataAccessor.SetAsync(turnContext, state);
 await BotAccessors.ConversationState.SaveChangesAsync(turnContext);