Search code examples
botframeworkformbuilderformflow

BotData custome properties values becomes null in Bot framework, what can be wrong?


I am working on MS Bot, now stuck at one point, I have two questions

1) How can I get conversation count in MessageController Post method?
2) The values of userData as mentioned in below code becomes null, when bot came back to messagecontroller for further conversation. My bot flow is as below.

The MessageController class invokes (Chain.From(() => new BotManager()) --> in BotManager() all the intents are listed--> From Intents i jump to specific form e.g. SampleForm in which i have formbuilder.

StateClient sc = activity.GetStateClient();
BotData userData = sc.BotState.GetUserData(activity.ChannelId, activity.From.Id);
UserDetails usr = new UserDetails();
usr.EmailId = "[email protected]";
userData.SetProperty<string>("EmailId", usr.EmailId);
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);

Solution

  • In the RootDialog (which implement) the IDialog, you can (and could be a good practice to) use the UserData(more info) property for the IDialogContext object to store information for the user across the conversation.

    In the code below, I try to retrive the ConversationCount key, then increment exists or not. And then I set againt the result value (which internally store the value in the BotState).

    context.UserData.TryGetValue("ConversationCount", out int count = 0);
    //Increment the message received from the user.
    count++;
    
    //Increment when the bot send a message to the user.
    count++;
    context.UserData.SetValue("ConversationCount", count);
    

    You can do the same with the Email property.