简体   繁体   中英

Botframework how to save selction

Due to lack of tutorials and information i am unable to find how i can save the information in bots. Lets say i ask user to make a selection like this:

 public enum City
    {
        Cleveland, Columbus, Kentucky, Mason, Akron
    };

    [Serializable]
    public class SandwichOrder
    {
        [Prompt("Please select what {&} you are in? {||}")]
        public City? City;
        public static IForm<SandwichOrder> BuildForm()
        {
            return new FormBuilder<SandwichOrder>()
                    .Message("Welcome to the my bot!")
                    .Build();
        }
    };

I just want to ask for city once how can i do that? How can i preserve the value of user selection and only call this method if it is first user interaction.

Controller class:

 internal static IDialog<SandwichOrder> MakeRootDialog()
        {
            return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm));
        }
        [ResponseType(typeof(void))]
        public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity != null)
            {
                // one of these will have an interface and process it
                switch (activity.GetActivityType())
                {
                    case ActivityTypes.Message:
                        await Conversation.SendAsync(activity, MakeRootDialog);
                        break;                  
                }
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

The SDK includes several ways of persisting data relative to a user or conversation:

userData stores information globally for the user across all conversations.

conversationData stores information globally for a single conversation. This data is visible to everyone within the conversation so care should be used to what's stored there. It's disabled by default and needs to be enabled using the bots persistConversationData setting.

privateConversationData stores information globally for a single conversation but its private data for the current user. This data spans all dialogs so it's useful for storing temporary state that you want cleaned up when the conversation ends.

dialogData persists information for a single dialog instance. This is essential for storing temporary information in between the steps of a waterfall.

Bots built using Bot Builder are designed to be stateless so that they can easily be scaled to run across multiple compute nodes. Because of that you should generally avoid the temptation to save state using a global variable or function closure. Doing so will create issues when you want to scale out your bot. Instead leverage the data bags above to persist temporary and permanent state.

More info here:

https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#adding-dialogs-and-memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM