One important example of the usage of ConversationState is to keep user waiting.
When a certain response from a user is needed, the message has to be kept waiting.
Think of a scenario where user is asked to provide a name, and asking user to write his/her name. After sending your message to the user, to re-enter the dialogue where it has to enter, the session has be to waiting user's response.
To achieve this, see example below.
usingSystem;usingCXPerium.Dialogs.WhatsApp;usingCXPerium.Models;usingCXPerium.WhatsApp;namespaceCXPerium.Bot.Sample.Dialogs{publicclassWelcome:BaseWhatsAppDialog,IWhatsAppDialog {publicWelcome(Models.Contact contact,Activity activity,ConversationState conversation): base(contact, activity, conversation) { }publicvoidRunDialog() {if (Conversation.IsWaitAction(GetType(),"WAIT"))WaitName();Conversation.AddWaitAction(GetType(),"WAIT");SendMessage("Welcome to CXPerium Assistant! Please write your name."); }privatevoidWaitName() {var name =Activity.Text;SendMessage($"Hi {name}!");Conversation.RemoveWaitAction(GetType(),"WAIT"); } }}
There are also cache-like features of ConversationState to hold required information in session.
publicvoidRunDialog(){if (Conversation.IsWaitAction(GetType(),"WAIT")) {WaitName(); }Conversation.PutData("key","value");Conversation.AddWaitAction(GetType(),"WAIT");SendMessage("Welcome to CXPerium Assistant! Please write your name.");}publicvoidWaitName(){var name =Activity.Text;var dummy =Conversation.GetData<string>("key");SendMessage($"Hi {name} - {dummy}!");Conversation.RemoveWaitAction(GetType(),"WAIT");}
PutData and GetData methods works in generic form, and TTL is available.