This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleverbot.rb
89 lines (74 loc) · 2.79 KB
/
Cleverbot.rb
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
class CleverBotSession
def initialize
@url = URI("http://cleverbot.com/webservicemin")
@vars = {}
@vars["start"] = "y"
@vars["icognoid"] = "wsf"
@vars["fno"] = "0"
@vars["sub"] = "Say"
@vars["islearning"] = "1"
@vars["cleanslate"] = "false"
end
def think(thought)
@vars["stimulus"] = thought
formData = parametersToWWWFormURLEncoded(@vars)
formDataToDigest = formData.slice(9, 20)
formDataDigest = md5(formDataToDigest)
@vars["icognocheck"] = formDataDigest
response = post(@url, @vars)
responseValues = response.split("\r")
@vars["sessionid"] = stringAtIndex(responseValues, 1)
@vars["logurl"] = stringAtIndex(responseValues, 2)
@vars["vText8"] = stringAtIndex(responseValues, 3)
@vars["vText7"] = stringAtIndex(responseValues, 4)
@vars["vText6"] = stringAtIndex(responseValues, 5)
@vars["vText5"] = stringAtIndex(responseValues, 6)
@vars["vText4"] = stringAtIndex(responseValues, 7)
@vars["vText3"] = stringAtIndex(responseValues, 8)
@vars["vText2"] = stringAtIndex(responseValues, 9)
@vars["prevref"] = stringAtIndex(responseValues, 10)
@vars["emotionalhistory"] = stringAtIndex(responseValues, 12)
@vars["ttsLocMP3"] = stringAtIndex(responseValues, 13)
@vars["ttsLocTXT"] = stringAtIndex(responseValues, 14)
@vars["ttsLocTXT3"] = stringAtIndex(responseValues, 15)
@vars["ttsText"] = stringAtIndex(responseValues, 16)
@vars["lineRef"] = stringAtIndex(responseValues, 17)
@vars["lineURL"] = stringAtIndex(responseValues, 18)
@vars["linePOST"] = stringAtIndex(responseValues, 19)
@vars["lineChoices"] = stringAtIndex(responseValues, 20)
@vars["lineChoicesAbbrev"] = stringAtIndex(responseValues, 21)
@vars["typingData"] = stringAtIndex(responseValues, 22)
@vars["divert"] = stringAtIndex(responseValues, 23)
stringAtIndex(responseValues, 16)
end
def urlEncode(txt)
URI.escape(txt, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end
def parametersToWWWFormURLEncoded(parameters)
wwwFormUrlEncoded = nil
parameters.each_pair do |parameterKey, parameterValue|
parameter = "#{urlEncode(parameterKey)}=#{urlEncode(parameterValue)}"
if wwwFormUrlEncoded.nil?
wwwFormUrlEncoded = parameter;
else
wwwFormUrlEncoded = "#{wwwFormUrlEncoded}&#{parameter}"
end
end
wwwFormUrlEncoded
end
def md5(input)
Digest::MD5.hexdigest(input)
end
def post(uri, parameters)
postData = parametersToWWWFormURLEncoded(parameters)
req = Net::HTTP::Post.new(uri.path)
req.body = postData
response = Net::HTTP.new(uri.hostname, uri.port).start do |http|
http.request(req)
end
response.message.trim
end
def stringAtIndex(strings, index)
strings[index] ? strings[index] : ""
end
end