forked from Atypicalim/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
72 lines (56 loc) · 1.91 KB
/
api.php
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
<?php
//
require_once "chatbot/Chatbot.php";
require_once "chatbot/Config.php";
require_once "chatbot/Database/Connection.php";
// initialize config
$config = new Config();
define("LOG", $config->log);
header(LOG ? "Content-Type: text/plain; charset=utf-8" : "Content-Type: application/json; charset=utf-8");
error_reporting(LOG ? E_ALL : JSON_ERROR_NONE);
$result = array(
'status' => 'error',
'type' => 'empty',
'message' => 'empty message ...',
'data' => 'empty',
);
// check request type
if (!isset($_REQUEST['requestType']) || !isset($_REQUEST['userInput'])) {
$result['status'] = 'error';
$result['type'] = $_REQUEST['requestType'];
$result['message'] = 'requestType and userInput is required ...';
$result['data'] = 'empty';
} else {
// 若用户上传了userId则用uid当做用户唯一标识否则用ip
$userId = isset($_REQUEST['userId']) ? $_REQUEST['userId'] : $_SERVER['REMOTE_ADDR'];
LOG && print "userId : " . $userId . "\n";
// initialize chatbot
$chatbot = new Chatbot($config, $userId);
// talk
if ($_REQUEST['requestType'] == 'talk') {
$res = $chatbot->talk($_REQUEST['userInput']);
$data = $chatbot->getData();
$result['status'] = 'success';
$result['type'] = 'talk';
$result['message'] = trim(preg_replace("/\s+/", " ", $res));
$result['data'] = $data;
} elseif ($_REQUEST['requestType'] == 'forget') {
$chatbot->forget();
$result['status'] = 'success';
$result['type'] = 'forget';
$result['message'] = 'forgetting completed ...';
$result['data'] = 'empty';
} else {
$result['status'] = 'error';
$result['type'] = $_REQUEST['requestType'];
$result['message'] = 'invalid request type ...';
$result['data'] = 'empty';
}
}
if (LOG) {
print "\n";
print_r($result);
print "\n";
} else {
echo json_encode($result);
}