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 =
"/getreply?"

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "DEFAULT") ⇒ Cleverbot

Create a new session



37
38
39
40
41
42
43
# File 'lib/cleverbotrb.rb', line 37

def initialize (api_key="DEFAULT")
	@params = {
		"key" => api_key,
		"wrapper" => "cleverbotrb"
	}
	@endpoint = ENDPOINT
end

Instance Method Details

#resetObject



69
70
71
# File 'lib/cleverbotrb.rb', line 69

def reset
	params.delete('cs')
end

#send(message) ⇒ String

send a message to cleverbot and get an answer

Parameters:

  • message (String)

Returns:

  • (String)

    the answer statement



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cleverbotrb.rb', line 50

def send(message)
	url_arg = @params
	url_arg["input"] = message
	url_arg_str = URI.encode_www_form(url_arg)

	headers = {
		'User-Agent' => 'cleverbotrb https://github.com/d0p1s4m4/cleverbotrb',
	}

	req = Net::HTTP.new(HOST, 80)
	resp = req.get(@endpoint + url_arg_str, headers)
	if resp.code != "200"
		return nil
	end
	response = JSON.parse(resp.body)
	@params['cs'] = response['cs']
	return response['output']
end