-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (52 loc) · 1.26 KB
/
Copy pathapp.js
File metadata and controls
53 lines (52 loc) · 1.26 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
var request = require("request");
var base_url = "https://cleverbot.io/1.0/";
var cio = function (user, key) {
this.user = user;
this.key = key;
this.setNick = function (nick) {
this.nick = nick;
}
this.create = function (callback) {
request.post({ url: base_url + "create", form: {
user: this.user,
key: this.key,
nick: this.nick
}}, function (err, httpResponse, body) {
if (err) throw err;
try {
var status = JSON.parse(body).status;
}
catch (e) {
console.log(e.stack);
var status="API endpoints unreachable";
}
if (status == "success") {
this.setNick(JSON.parse(body).nick);
callback(false, this.nick);
}
else if (status == "Error: reference name already exists") {
callback(false, this.nick);
}
else {
callback(status, null);
}
}.bind(this));
}
this.ask = function (input, callback) {
request.post({ url: base_url + "ask", form: {
user: this.user,
key: this.key,
nick: this.nick,
text: input
}}, function (err, httpResponse, body) {
if (err) throw err;
if (JSON.parse(body).status == "success") {
callback(false, JSON.parse(body).response);
}
else {
callback(true, JSON.parse(body).status);
}
});
}
}
module.exports = cio;