blob: c04bb1c0ed3a274830693f2d6d57483349c90f85 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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));
}
}
|