pierredavidbelanger / chatter-bot-api

A Mono/.NET, JAVA, Python and PHP chatter bot API that supports Cleverbot, JabberWacky and Pandorabots.
181 stars 72 forks source link

How to use Cookies for multiple users in PHP? #13

Closed skorotkiewicz closed 9 years ago

skorotkiewicz commented 9 years ago

it is possible for cookies for separate user in Cleverbot? eg: user1_cookie.txt, user2_cookie.txt, user3_cookie.txt

pierredavidbelanger commented 9 years ago

I hope I understand you question.

This library already supports being used by many concurrent users.

After creating a bot:

$factory = new ChatterBotFactory();
$bot = $factory->create(ChatterBotType::CLEVERBOT);

You just have to create a session per user (and serialize it into the session to be reused in each request of this user):

$botsession = $bot->createSession();

Here is something I wrote about this some time ago: https://code.google.com/p/chatter-bot-api/issues/detail?id=3#c5

skorotkiewicz commented 9 years ago

I was concerned about something similar

function cleverbot($s,$userid) {
    require 'cleverbot.php';
    $factory = new ChatterBotFactory();

    $bot1 = $factory->create(ChatterBotType::CLEVERBOT);
    if ( file_exists('user' . $userid. '-cookie.txt') ) {
         $bot1session = $bot1->LoadSession('user' . $userid. '-cookie.txt');
    } else {
         $bot1session = $bot1->createSession('user' . $userid. '-cookie.txt');
    }
    $s = $bot1session->think($s);
    return $s;
}
pierredavidbelanger commented 9 years ago

The logic is almost fine, just do something like this:

function cleverbot($s,$userid) {
    require 'cleverbot.php';
    $factory = new ChatterBotFactory();
    $bot1 = $factory->create(ChatterBotType::CLEVERBOT);
    if ( file_exists('user' . $userid. '-cookie.txt') ) {
         $bot1session = ...unserialize content of file 'user' . $userid. '-cookie.txt'
    } else {
         $bot1session = $bot1->createSession('user' . $userid. '-cookie.txt');
    }
    $s = $bot1session->think($s);
    ...serialize $bot1session into file 'user' . $userid. '-cookie.txt'
    return $s;
}
skorotkiewicz commented 9 years ago

Thanks, it works. :)