Conversation

Conversation holds basic session information on your users. It is user-specific and holds unique information by the phone number.

It is available on the CXPerium.Models.ConversationState namespace. Also, you have to implement it on your dialog class contructors.

The example below shows data that is held inside ConversationState object.

public class ConversationState
{
    private Dictionary<string, object> waitDictionary;

    private Dictionary<string, object> dataDictionary;

    private Dictionary<string, object> jsonDictionary;

    public string LastMessage = "";

    public int FaultCount => GetData<int>("fault_count");

    public bool IsDebug
    {
        get
        {
            return GetData<bool>("sys_debug");
        }
        set
        {
            PutData("sys_debug", value);
        }
    }

    public int LanguageId { get; set; } = 1;


    public string CultureCode { get; set; } = "TR";


    public string LanguageName { get; set; } = "TURKISH";


    public JArray SessionData { get; set; }
}

Conversation Wait Actions

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.

using System;
using CXPerium.Dialogs.WhatsApp;
using CXPerium.Models;
using CXPerium.WhatsApp;

namespace CXPerium.Bot.Sample.Dialogs
{
    public class Welcome : BaseWhatsAppDialog, IWhatsAppDialog
    {
        public Welcome(Models.Contact contact, Activity activity, ConversationState conversation)
            : base(contact, activity, conversation) { }

        public void RunDialog()
        {
           if (Conversation.IsWaitAction(GetType(), "WAIT"))
                WaitName();

            Conversation.AddWaitAction(GetType(), "WAIT");
            SendMessage("Welcome to CXPerium Assistant! Please write your name.");
        }
        
        private void WaitName() 
        {
            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.

public void RunDialog()
{
    if (Conversation.IsWaitAction(GetType(), "WAIT")) {
        WaitName();
    }

    Conversation.PutData("key", "value");
    Conversation.AddWaitAction(GetType(), "WAIT");
    SendMessage("Welcome to CXPerium Assistant! Please write your name.");
}

public void WaitName()
{
    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.

Last updated