From 288980ceb35d5ace51284cfdf176e9acb5f7bb39 Mon Sep 17 00:00:00 2001 From: valentineus Date: Wed, 14 Sep 2016 01:30:05 +0400 Subject: Alpha version --- composer.json | 28 +++++++++++++ src/bot.php | 82 +++++++++++++++++++++++++++++++++++++++ test/components/communication.php | 17 ++++++++ test/components/engine.php | 66 +++++++++++++++++++++++++++++++ test/components/footer.php | 9 +++++ test/components/navbar.php | 36 +++++++++++++++++ test/components/results.php | 20 ++++++++++ test/components/settings.php | 24 ++++++++++++ test/index.php | 39 +++++++++++++++++++ test/javascript.js | 3 ++ test/style.css | 38 ++++++++++++++++++ 11 files changed, 362 insertions(+) create mode 100644 composer.json create mode 100644 src/bot.php create mode 100644 test/components/communication.php create mode 100644 test/components/engine.php create mode 100644 test/components/footer.php create mode 100644 test/components/navbar.php create mode 100644 test/components/results.php create mode 100644 test/components/settings.php create mode 100644 test/index.php create mode 100644 test/javascript.js create mode 100644 test/style.css diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1896994 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "valentineus/Module-III-API", + "version": "1.0.0", + "type": "library", + "description": "Small module to work with artificial intelligence of iii.ru", + "license": "MIT", + "homepage": "https://github.com/valentineus/Module-III-API", + "authors": [ + { + "name": "Paul Belinsky", + "email": "vskyd1@gmail.com", + "homepage": "https://blog.vskyd1.ru/", + "role": "Developer" + }, + { + "name": "Valentin Popov", + "email": "valentineus@gmail.com", + "homepage": "https://github.com/valentineus", + "role": "Developer" + } + ], + "support": { + "email": "valentineus@gmail.com", + }, + "require": { + "php": ">=5.6.21" + } +} diff --git a/src/bot.php b/src/bot.php new file mode 100644 index 0000000..c04bb1c --- /dev/null +++ b/src/bot.php @@ -0,0 +1,82 @@ +key = $key; + } + + /** + * @param null $session - Идентификатор сессии существующей, если нет то создается новая + * @return string Идентификатор текущей сессии + */ + public function session($session = null) { + if ($session === null) { + $response = file_get_contents('http://iii.ru/api/2.0/json/Chat.init/'.$this->key.'/'); + $this->session = $this->decode($response)->result->cuid; + } else { + $this->session = $session; + } + + return $this->session; + } + + /** + * ОТправить сообщение боту + * @param string $message Сообщение + * @return string Ответ + */ + public function say($message) { + $request = '["'.$this->session.'","'.$message.'"]'; + $myCurl = curl_init(); + curl_setopt_array($myCurl, array( + CURLOPT_URL => 'http://iii.ru/api/2.0/json/Chat.request', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $this->encode($request) + )); + $response = curl_exec($myCurl); + curl_close($myCurl); + + return $this->decode($response)->result->text->tts; + } + + /** + * Кодирование сообщения + * @param $message + * @return string + */ + private function encode($message) { + $message = base64_encode($message); + $ml = strlen($message); + $kl = strlen($this->salt); + $encoded = ""; + for ($i = 0; $i < $ml; $i++) { + $encoded = $encoded . ($message[$i] ^ $this->salt[$i % $kl]); + } + + return base64_encode($encoded); + } + + /** + * Декодирование сообщения + * @param $message + * @return mixed|null + */ + private function decode($message) { + $msg = base64_decode($message); + $ml = strlen($msg); + $kl = strlen($this->salt); + $decoded = ""; + for ($i = 0; $i < $ml; $i++) { + $decoded.= ($msg[$i] ^ $this->salt[$i % $kl]); + } + + return json_decode(base64_decode($decoded)); + } + } diff --git a/test/components/communication.php b/test/components/communication.php new file mode 100644 index 0000000..3f00204 --- /dev/null +++ b/test/components/communication.php @@ -0,0 +1,17 @@ + +
+
+ +
+ +
+ +
+ + + +
+
+
+
+ diff --git a/test/components/engine.php b/test/components/engine.php new file mode 100644 index 0000000..bc89d8e --- /dev/null +++ b/test/components/engine.php @@ -0,0 +1,66 @@ +"); + echo("".$cookie['type'].""); + echo("".$cookie['textarea'].""); + echo("".date("H:i:s", $cookie['time']).""); + echo(""); + } + return 0; +} + + if (isset($_COOKIE['BOT_TOKEN'])) { + $BOT_TOKEN = $_COOKIE['BOT_TOKEN']; + } + + if (isset($_POST['BOT_TOKEN'])) { + unset($_COOKIE); // Clear the session + SetCookie("BOT_TOKEN", htmlspecialchars($_POST['BOT_TOKEN'])); + $BOT_TOKEN = htmlspecialchars($_POST['BOT_TOKEN']); + } + + if (isset($BOT_TOKEN)) { + define('BOT_TOKEN', $BOT_TOKEN); + $bot = new Bot(BOT_TOKEN); + if (isset($_COOKIE['BOT_SESSION'])) { + $session = $bot->session($_COOKIE['BOT_SESSION']); + } else { + $session = $bot->session(); + SetCookie("BOT_SESSION", $session); + } + if (!isset($session)) { ?> +
+ +
+ 'user', + 'textarea' => $textarea, + 'time' => time() + ); + SetCookie("TALK".$current_id, json_encode($cookie)); + + $current_id = $current_id+1; + SetCookie('CURRENT_ID', $current_id+1); + $cookie = array( + 'type' => 'bot', + 'textarea' => $bot->say($textarea), + 'time' => time() + ); + SetCookie("TALK".$current_id, json_encode($cookie)); + } +?> diff --git a/test/components/footer.php b/test/components/footer.php new file mode 100644 index 0000000..fa5a161 --- /dev/null +++ b/test/components/footer.php @@ -0,0 +1,9 @@ + + + diff --git a/test/components/navbar.php b/test/components/navbar.php new file mode 100644 index 0000000..ed858b5 --- /dev/null +++ b/test/components/navbar.php @@ -0,0 +1,36 @@ + + + diff --git a/test/components/results.php b/test/components/results.php new file mode 100644 index 0000000..def09b1 --- /dev/null +++ b/test/components/results.php @@ -0,0 +1,20 @@ +
+
+ + + + + + + +
#TextDate
+
+
diff --git a/test/components/settings.php b/test/components/settings.php new file mode 100644 index 0000000..fe8e5ff --- /dev/null +++ b/test/components/settings.php @@ -0,0 +1,24 @@ + + + diff --git a/test/index.php b/test/index.php new file mode 100644 index 0000000..dd44afc --- /dev/null +++ b/test/index.php @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + Testing conversational bot + + + + +
+ + +
+ + + diff --git a/test/javascript.js b/test/javascript.js new file mode 100644 index 0000000..22e0185 --- /dev/null +++ b/test/javascript.js @@ -0,0 +1,3 @@ +jQuery(function () { + $('[data-toggle="popover"]').popover() +}) diff --git a/test/style.css b/test/style.css new file mode 100644 index 0000000..a2058d8 --- /dev/null +++ b/test/style.css @@ -0,0 +1,38 @@ +html { + position: relative; + min-height: 100%; +} + +body { + margin-bottom: 60px; +} + +body > .container { + padding: 60px 15px 0; +} + +textarea.form-control { + resize: none; +} + +.footer { + background-color: #f5f5f5; + position: absolute; + height: 60px; + width: 100%; + bottom: 0; +} + +.container .text-muted { + margin: 20px 0; +} + +/* Loaded one icon from GitHub */ +.github { + background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFNTE3OEEyQTk5QTAxMUUyOUExNUJDMTA0NkE4OTA0RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFNTE3O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RlCXQrmy50P1oDRKTSegmNbUsA0zDMwRhPJXeCE3vWLPQMvan6X8AgIa1vcR4AkGZkDR4ejJ1UHpsaVI0g2LInpOsNFUud1rhxSV+fzC9Woz2EZkWQuja7/B+jUrgtIMpy9YCW4n4K41YfzRneW5E1KJTe4B2Zq1Q5EHEtj4U3AfEzR5SVY4l7QYQPJdN2as7RKBF0BPZqqH4VgMAMBL8Byxr7y8zCZiDlnOcEKIPmUpgB5Z2ww5RdOiiRiNajUmWda5IG6WbhsyY2fx6m8gLcoJDJFkH219M3We1+cnda93pfycZpIJEL/s/wSYADmOAwAQgdpBAAAAABJRU5ErkJggg=='); + background-size: cover; + margin: 2px 5px; + height: 15px; + width: 15px; + float:left; +} -- cgit v1.2.3