Class: Cleverbot

Inherits:
Object
  • Object
show all
Defined in:
lib/cleverbotrb.rb

Overview

This class represent cleverbot session

Author:

  • d0p1

Constant Summary collapse

HOST =
"www.cleverbot.com"
ENDPOINT =
"/webservicemin?uc=777"
PARAMS =
{
	'stimulus' => '', 'start' => 'y', 'sessionid' => '',
	'vText8' => '', 'vText7' => '', 'vText6' => '', 'vText5' => '',
	'vText4' => '', 'vText3' => '', 'vText2' => '',
	'icognoid' => 'wsf', 'icognocheck' => '', 'fno' => '0',
	'prevref' => '', 'emotionaloutput' => '', 'asbotname' => '',
	'ttsvoice' => '', 'typing' => '', 'lineref' => '',
	'sub' => 'Say', 'islearning' => '1', 'cleanslate' => 'false'
}
RESPONSE_KEY =
[
	'message', 'sessionid', 'logurl', 'vText8',
	'vText7', 'vText6', 'vText5', 'vText4',
	'vText3', 'vText2', 'prevref', '',
	'emotionalhistory', 'ttsLocMP3', 'ttsLocTXT', 'ttsLocTXT3',
	'ttsText', 'lineref', 'lineURL', 'linePOST',
	'lineChoices', 'lineChoicesAbbrev', 'typingData', 'divert'
]

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "DEFAULT") ⇒ Cleverbot

Create a new session



56
57
58
59
60
61
# File 'lib/cleverbotrb.rb', line 56

def initialize (api_key="DEFAULT")
	@cookies = {}
	@params = PARAMS
	@endpoint = ENDPOINT + "&botapi=#{api_key}"
	prepare
end

Instance Method Details

#send(message) ⇒ String

send a message to cleverbot and get an answer

Parameters:

  • message (String)

Returns:

  • (String)

    the answer statement



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cleverbotrb.rb', line 68

def send(message)
	body = @params
	body["stimulus"] = message
	body["icognocheck"] = digest(URI.encode_www_form(body)[9...35])
	body_str = URI.encode_www_form(body) #params_encode(body)

	headers = {
		'Content-Type' => 'application/x-www-form-urlencoded',
		'Content-Length' => body_str.size.to_s,
		'Cache-Control' => 'no-cache',
		'Cookie' => cookies_encode(@cookies)
	}

	req = Net::HTTP.new(HOST, 80)
	resp = req.post(@endpoint, body_str, headers)
	if resp.code != "200"
		return nil
	end
	response = resp.body.split("\r")
	save_data(response)
	return response[0]
end