0
mirror of https://github.com/valentineus/iii-module.git synced 2025-04-28 01:41:26 +03:00

Alpha version

This commit is contained in:
valentineus 2016-09-14 01:30:05 +04:00
parent 4d0e9148a3
commit 288980ceb3
11 changed files with 362 additions and 0 deletions

28
composer.json Normal file
View File

@ -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"
}
}

82
src/bot.php Normal file
View File

@ -0,0 +1,82 @@
<?php
class Bot {
protected $key = null;
protected $session = null;
private $salt = 'some very-very long string without any non-latin characters due to different string representations inside of variable programming languages';
/**
* @param $key - Ключ из урла после создания инфа
*/
public function __construct($key) {
$this->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));
}
}

View File

@ -0,0 +1,17 @@
<!-- Home side panel -->
<form class="form-horizontal" method="post">
<div class="row">
<!-- Text field -->
<div class="col-md-8">
<textarea class="form-control" rows="3" name="textarea"></textarea>
</div>
<!-- Control buttons -->
<div class="col-md-4">
<button type="submit" class="btn btn-default">Submit</button>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#SettingsModal">Settings</button>
<button type="button" class="btn btn-danger">Reset</button>
</div>
</div><!-- row -->
</form>
<br/>
<!-- End side panel -->

View File

@ -0,0 +1,66 @@
<?php
function ShowRecord($id) {
for ($i=0; $i < $id; $i++) {
$cookie = json_decode($_COOKIE['TALK'.$i], true);
echo("<tr>");
echo("<td>".$cookie['type']."</td>");
echo("<td>".$cookie['textarea']."</td>");
echo("<td>".date("H:i:s", $cookie['time'])."</td>");
echo("</tr>");
}
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)) { ?>
<div class="container">
<div class="alert alert-danger" role="alert">Session is not initialized, check the settings!</div>
</div>
<?php }
}
if (isset($_POST['textarea'])) {
$textarea = htmlspecialchars($_POST['textarea']);
if (isset($_COOKIE['CURRENT_ID'])) {
$current_id = (int)$_COOKIE['CURRENT_ID'];
SetCookie('CURRENT_ID', $current_id+1);
} else {
$current_id = 0;
SetCookie('CURRENT_ID', $current_id);
}
$cookie = array(
'type' => '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));
}
?>

View File

@ -0,0 +1,9 @@
<!-- Beginning footer -->
<footer class="footer">
<div class="container">
<p class="text-muted">
<a href="mailto:valentineus@gmail.com">valentineus@gmail.com</a>
</p>
</div>
</footer>
<!-- End footer -->

View File

@ -0,0 +1,36 @@
<!-- Start navigation bar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>
<!-- Home project -->
<a href="https://github.com/valentineus/Module-III-API"><span class="github" aria-hidden="true"></span> GitHub.com</a>
</li>
</ul>
<!-- The right side of the navigation bar -->
<ul class="nav navbar-nav navbar-right">
<li>
<p class="navbar-text" data-toggle="popover" data-placement="auto" data-trigger="hover" data-content="Cookies must be enabled in your browser!"><?php
// Check the ability to use Cookie
if (SetCookie("TestCookie", "Success")) { ?>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<?php } else { ?>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<?php }
?> Cookie</p>
<p class="navbar-text" data-toggle="popover" data-placement="auto" data-trigger="hover" data-content="Before the work necessary to configure!"><?php
// Checking the portal settings
if (isset($_COOKIE['BOT_TOKEN']) OR isset($_POST['BOT_TOKEN'])) { ?>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<?php } else { ?>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<?php }
?> Settings</p>
</li>
</ul><!-- navbar-right -->
</div>
</div><!-- container -->
</nav>
<!-- End navigation bar -->

View File

@ -0,0 +1,20 @@
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th>#</th>
<th>Text</th>
<th>Date</th>
</tr>
<?php
if (isset($current_id)) {
ShowRecord($current_id);
} elseif (isset($_COOKIE['CURRENT_ID'])) {
ShowRecord((int)$_COOKIE['CURRENT_ID']);
} else {
echo("Error!");
}
?>
</table>
</div>
</div>

View File

@ -0,0 +1,24 @@
<!-- Start pop-up window for the settings -->
<div class="modal fade" id="SettingsModal" tabindex="-1" role="dialog" aria-labelledby="SettingsModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form class="form-horizontal" method="post">
<div class="modal-body">
<!-- Elements of a modal window -->
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">ID:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword" name="BOT_TOKEN" placeholder="109cd867-0ef3-4473-af71-7543a9b2fccd">
</div>
</div>
</div>
<!-- The window control buttons -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Save changes</button>
</div>
</form>
</div><!-- modal-content -->
</div><!-- modal-dialog -->
</div>
<!-- End pop-up window for the settings -->

39
test/index.php Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<!-- Connecting library jQuery -->
<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>
<!-- Connecting library Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Custom styles -->
<link href="style.css" rel="stylesheet">
<!-- Custom JavaScript -->
<script type="text/javascript" src="javascript.js"></script>
<!-- Meta -->
<meta charset="utf-8">
<meta name="robots" content="NONE">
<!-- Title -->
<title>Testing conversational bot</title>
</head>
<body>
<?php // Connect all of the dependencies
include("../src/bot.php");
include("components/navbar.php"); // Connect the navigation bar
include("components/engine.php"); // Connect the engine project
include("components/settings.php"); // Connecting a modal window with the settings
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h1>Communication with the bot</h1>
</div>
<?php
include("components/communication.php");
include("components/results.php");
?>
</div>
<?php include("components/footer.php"); // Connect the bottom panel ?>
</body>
</html>

3
test/javascript.js Normal file
View File

@ -0,0 +1,3 @@
jQuery(function () {
$('[data-toggle="popover"]').popover()
})

38
test/style.css Normal file
View File

@ -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,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5h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