cleverbot state

This commit is contained in:
Ben Harris 2017-04-24 00:37:41 -04:00
parent 764c9c9ce3
commit 0d4b326d8f
1 changed files with 20 additions and 5 deletions

View File

@ -8,10 +8,12 @@ use React\Promise\Deferred;
class CleverBot {
private static $bot;
private static $cs;
public static function register(&$that)
{
self::$bot = $that;
self::$cs = [];
self::$bot->registerCommand('chat', [__CLASS__, 'chat'], [
'description' => 'talk to benbot',
@ -31,20 +33,33 @@ class CleverBot {
public static function chat($msg, $args)
{
$msg->channel->broadcastTyping();
self::askCleverbot(implode(" ", $args))->then(function ($result) use ($msg) {
Utils::send($msg, $result->output);
// self::askCleverbot(implode(" ", $args), self::$cs[$msg->channel->id] ?? "")->then(function ($result) use ($msg) {
// self::$cs[$msg->channel->id] = $apidata->cs;
// Utils::send($msg, $result->output);
// });
$url = "https://www.cleverbot.com/getreply";
$key = getenv('CLEVERBOT_API_KEY');
$input = rawurlencode(implode(" ", $args));
$url .= "?input=$input&key=$key" . isset(self::$cs[$msg->channel->id]) ? "&cs=" . self::$cs[$msg->channel->id] : "";
self::$bot->http->get($url, null, [], false)->then(function ($apidata) use ($msg) {
self::$cs[$msg->channel->id] = $apidata->cs;
Utils::send($msg, $apidata->output);
});
}
public static function askCleverbot($input)
public static function askCleverbot($input, $cs)
{
$deferred = new Deferred();
$url = "https://www.cleverbot.com/getreply";
$key = getenv('CLEVERBOT_API_KEY');
$input = rawurlencode($input);
self::$bot->http->get("$url?input=$input&key=$key", null, [], false)->then(function ($apidata) use ($deferred) {
self::$bot->http->get("$url?input=$input&key=$key" . $cs == "" ? "" : "&cs=$cs", null, [], false)->then(function ($apidata) use ($deferred) {
$deferred->resolve($apidata);
}, function ($e) {
$deferred->reject($e);
@ -52,4 +67,4 @@ class CleverBot {
return $deferred->promise();
}
}
}