ManageSessions

ManageSessions class provides useful methods for your chatbot.

Example :

        
    {
        "intents": [
    
        {"tag": "orderTrack",
            "patterns": ["Can you track my order?", "When i my order will be delivered", "I am unable to track my order"],
            "responses": ["Please tell me your order id.", "Can I know your order id", "Please provide me your order id so I will check."]
        }
        
        ]
        
    }
        
    



    require_once __DIR__ . '/vendor/autoload.php';

    use Chatman\BotWithPatterns;
    use Chatman\Support\ManageSessions;

    $bot = new BotWithPatterns("chatbot.json");
    $bot->train();

    $sess = new ManageSessions();

    $query = $_GET['query'];

    //checking if the last matched tag was "orderTrack".
    if($sess->getLastMatchedTag() === "orderTrack"){

        //extracting number from query by using regex
        preg_match('/\d+/', $query, $number);

        //search order details from DB. Example Function :
        $days = SearchDaysFromDB($number[0]);

        //setting bot final response
        $bot->finalResponse("Your order will be delivered in $days");
    }

    $resp = $bot->getResponse($query);

    echo $resp['resp'];

    /* EXPECTED OUTPUT : 
        Your order will be delivered in [DB RESPONSE]
    */


          

Methods


getLastBotResp(): string

getLastUserQuest(): string

getLastMatchedTag(): string

destroySession(string $name = "") : void

autoDestroy(int $resetTime) : void

        

getLastBotResp(): string
This method returns last bot response.

getLastUserQuest(): string
This method returns last user input.

getLastMatchedTag(): string
This method returns last matched tag. _default_ is the tag for the default response & _final_ is the tag for the final response.

destroySession(string $name = "") : void
This method accepts one optional argument of type string, you can provide "lastuserquest" to remove the last user input, "lastbotresp" to remove the last bot response, or "lastmatchedtag" to remove the last matched tag. If no argument is provided all Chatman sessions will be removed.

autoDestroy(int $resetTime) : void
This method accepts one argument of type int. It should be session reset time in seconds.



    $sess = new ManageSessions();

    //Sessions will be removed if Chatman does not get any query for 300 sec.
    $sess->autoDestroy(300);