forked from theaquarium/dialect-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (88 loc) · 2.22 KB
/
index.js
File metadata and controls
92 lines (88 loc) · 2.22 KB
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
83
84
85
86
87
88
89
90
91
92
const request = require('request');
const baseUrl = "http://www.diax.me/dialect/";
(function(dialectnode) {
var Dialect = function(_token, trainer) {
this.token = _token;
this.isTrainer = trainer;
this.ask = function(string, callback) {
var options = {
uri: baseUrl + 'api/chatbot/ask',
method: 'POST',
json: {
"input": string
},
headers: {
'X-Token': this.token
}
};
request(options, function (error, response, body) {
var dialectApiResponse;
if (!error && response.statusCode == 200) {
var out = body.output;
dialectApiResponse = out;
}
else if (error) {
console.log("Error occured:", error);
dialectApiResponse = error;
}
callback(dialectApiResponse);
});
};
this.train = function(input, output) {
if (this.isTrainer) {
var options = {
uri: baseUrl + 'api/chatbot/train',
method: 'POST',
json: {
"input": input, "output": output
},
headers: {
'X-Token': this.token
}
};
request(options, function (error, response, body) {if (error) console.log(error);});
}
else {
throw "You are not a trainer. If you were recently granted the permission, try restarting your application";
}
};
}
dialectnode.create = function (_token, callback) {
var _isTrainer = false;
var options1 = {
uri: baseUrl + 'api/verification/verifytoken',
method: 'POST',
json: {
"token": _token
}
};
var options2 = {
uri: baseUrl + 'api/verification/checktrainer',
method: 'POST',
json: {
"token": _token
}
};
request(options1, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body.response == "0") {
throw "Invalid Token";
}
request(options2, function (error2, response2, body2) {
if (!error2 && response2.statusCode == 200) {
if (body2.response == "1") {
_isTrainer = true;
}
callback(new Dialect(_token, _isTrainer));
}
else if (error2) {
throw new Error(error2);
}
});
}
else if (error) {
throw new Error(error);
}
});
};
}(module.exports));