forked from ash47/OmegleMiddleMan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleverbot.js
87 lines (79 loc) · 2.75 KB
/
cleverbot.js
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
var crypto = require('crypto')
, http = require('http')
, Cleverbot = function(){
this.params = {
'stimulus' : '' , 'start' : 'y' , 'sessionid' : '',
'vText8' : '' , 'vText7' : '' , 'vText6' : '',
'vText5' : '' , 'vText4' : '' , 'vText3' : '',
'vText2' : '' , 'icognoid' : 'wsf' , 'icognocheck' : '',
'fno' : '0', 'prevref' : '' , 'emotionaloutput' : '',
'emotionalhistory' : '' , 'asbotname' : '' , 'ttsvoice' : '',
'typing' : '' , 'lineref' : '' , 'sub' : 'Say',
'islearning' : '1', 'cleanslate' : 'false',
};
};
Cleverbot.parserKeys = [
'message', 'sessionid', 'logurl', 'vText8',
'vText7', 'vText6', 'vText5', 'vText4',
'vText3', 'vText2', 'prevref', '',
'emotionalhistory', 'ttsLocMP3', 'ttsLocTXT', 'ttsLocTXT3',
'ttsText', 'lineref', 'lineURL', 'linePOST',
'lineChoices', 'lineChoicesAbbrev', 'typingData', 'divert'
];
Cleverbot.digest = function(body){
var m = crypto.createHash('md5');
m.update(body)
return m.digest('hex');
};
Cleverbot.encodeParams = function(a1){
var u=[];
for(x in a1){
if(a1[x] instanceof Array)
u.push(x+"="+encodeURIComponent(a1[x].join(",")));
else if(a1[x] instanceof Object)
u.push(params(a1[x]));
else
u.push(x+"="+encodeURIComponent(a1[x]));
}
return u.join("&");
};
Cleverbot.prototype = {
write : function(message,callback){
var clever = this;
body = this.params;
body.stimulus = message;
body.icognocheck = Cleverbot.digest(Cleverbot.encodeParams(body).substring(9,35));
var options = {
host: 'www.cleverbot.com',
port: 80,
path: '/webservicemin',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Cleverbot.encodeParams(body).length,
'Cache-Control': 'no-cache'
}
};
var req = http.request(options, function(res) {
var cb = callback || function(){};
res.on('data', function(chunk) {
var chunk_data = chunk.toString().split("\r")
, responseHash = {};
for(var i = 0, iLen = chunk_data.length;i<iLen;i++){
clever.params[Cleverbot.parserKeys[i]] = responseHash[Cleverbot.parserKeys[i]] = chunk_data[i];
}
if (res.statusCode >= 300) responseHash.message = 'Error: ' + res.statusCode;
cb(responseHash);
});
});
req.on('error', function(error) {
// Log the error
console.log('CLEVER ERROR: ' + error.message);
// Resend the message
clever.write(message, callback);
});
req.write(Cleverbot.encodeParams(body));
req.end();
}
};
module.exports = Cleverbot;