1
|
|
|
<?php |
2
|
|
|
namespace Posibrain\RestApi; |
3
|
|
|
|
4
|
|
|
use Posibrain\TchatBot; |
5
|
|
|
use Posibrain\Positroner; |
6
|
|
|
use Monolog\Handler\RotatingFileHandler; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @author Fylhan (http://fylhan.la-bnbox.fr) |
15
|
|
|
* @license LGPL-2.1+ |
16
|
|
|
*/ |
17
|
|
|
class RestApiController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function actionGetBots(Request $request, $botId) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$response = array( |
23
|
|
|
'status' => false |
24
|
|
|
); |
25
|
|
|
try { |
26
|
|
|
$brainPath = __DIR__ . '/../../../app/brains/'; |
27
|
|
|
$files = glob($brainPath .(empty($botId) ? '*' : $botId), GLOB_ONLYDIR); |
28
|
|
|
if (empty($files)) { |
29
|
|
|
return $this->render(array( |
30
|
|
|
'status' => true, |
31
|
|
|
'data' => '', |
32
|
|
|
'message' => 'No bot available' |
33
|
|
|
)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$response = array( |
37
|
|
|
'status' => true |
38
|
|
|
); |
39
|
|
|
$data = array(); |
40
|
|
|
foreach ($files as $file) { |
41
|
|
|
$bot = array(); |
42
|
|
|
$identityFile = $file . '/identity.json'; |
43
|
|
|
if (is_file($identityFile) && (null != ($identity = loadJsonFile($identityFile, 'UTF-8')))) { |
44
|
|
|
$bot['id'] = $identity->id; |
45
|
|
|
$bot['name'] = $identity->name; |
46
|
|
|
$bot['pseudo'] = (! empty($identity->pseudo) ? $identity->pseudo : ucfirst($identity->name)); |
47
|
|
|
$bot['conceptorName'] = (! empty($identity->conceptorName) ? $identity->conceptorName : 'the Ancients'); |
48
|
|
|
$bot['birth'] = new \DateTime($identity->birthday); |
49
|
|
|
$bot['timezone'] = @$identity->timezone; |
50
|
|
|
$bot['desc'] = $bot['name'] . (! empty($identity->pseudo) ? ' alias ' . $identity->pseudo : '') . ': made the ' . $bot['birth']->format('jS \o\f F Y') . ' by ' . $bot['conceptorName']; |
51
|
|
|
} |
52
|
|
|
else { |
53
|
|
|
$bot['name'] = $file; |
54
|
|
|
$bot['message'] = 'No identity description for this bot...'; |
55
|
|
|
} |
56
|
|
|
$data[] = $bot; |
57
|
|
|
} |
58
|
|
|
$response['data'] = $data; |
59
|
|
|
} catch (\Exception $e) { |
60
|
|
|
$response['message'] = 'Error during process: ' . $e->getMessage(); |
61
|
|
|
} |
62
|
|
|
return $this->render($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function actionGetPositrons(Request $request) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$response = array( |
68
|
|
|
'status' => false |
69
|
|
|
); |
70
|
|
|
try { |
71
|
|
|
$positroner = new Positroner(); |
72
|
|
|
$positrons = $positroner->listPositrons(); |
73
|
|
|
$response = array( |
74
|
|
|
'status' => true, |
75
|
|
|
'data' => $positrons |
76
|
|
|
); |
77
|
|
|
if (empty($positrons)) { |
78
|
|
|
$response['message'] = 'No positron available'; |
79
|
|
|
} |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
$response['message'] = 'Error during process: ' . $e->getMessage(); |
82
|
|
|
} |
83
|
|
|
return $this->render($response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function actionSubmit(Request $request, $botId, $botLang) |
87
|
|
|
{ |
88
|
|
|
if (! $request->query->has('msg')) { |
89
|
|
|
return $this->render(array( |
90
|
|
|
'status' => false, |
91
|
|
|
'message' => 'The field "msg" is required. You need to ask a question to the bot!' |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
$response = array( |
95
|
|
|
'status' => false |
96
|
|
|
); |
97
|
|
|
try { |
98
|
|
|
$pseudo = $request->query->get('pseudo', 'Anonymous'); |
99
|
|
|
$message = $request->query->get('msg'); |
100
|
|
|
$logger = new Logger('PosibrainRestApi'); |
101
|
|
|
$loggerHandler = new RotatingFileHandler(__DIR__ . '/../../../logs/restapi.log', 2, Logger::DEBUG); |
102
|
|
|
$logger->pushHandler($loggerHandler); |
103
|
|
|
$bot = new TchatBot($botId, $botLang, array( |
104
|
|
|
'loggerHandler' => $loggerHandler |
105
|
|
|
)); |
106
|
|
|
$answer = $bot->generateAnswer($message, $pseudo, time()); |
107
|
|
|
$data = array( |
108
|
|
|
'pseudo' => @$answer[1], |
109
|
|
|
'msg' => @$answer[0] |
110
|
|
|
); |
111
|
|
|
$response = array( |
112
|
|
|
'status' => true, |
113
|
|
|
'data' => $data |
114
|
|
|
); |
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
$response['message'] = 'Error during process: ' . $e->getMessage(); |
117
|
|
|
} |
118
|
|
|
return $this->render($response); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function render($response) |
122
|
|
|
{ |
123
|
|
|
$header = array( |
124
|
|
|
'Cache-Control' => 'no-cache, must-revalidate', |
125
|
|
|
'Expires' => 'Sat, 29 Oct 2011 00:00:00 GMT', |
126
|
|
|
'Content-type' => 'application/json' |
127
|
|
|
); |
128
|
|
|
return new JsonResponse($response, 200, $header); |
129
|
|
|
} |
130
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.