Class: RubyCleverbot

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

Overview

cleverbot api

Constant Summary collapse

HOST =
'http://www.cleverbot.com'.freeze
RESOURCE =
'/webservicemin'.freeze
API_URL =
HOST + RESOURCE
HEADERS =
{
    'user_agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
    'Accept-Language': 'en-us,en;q=0.8,en-us;q=0.5,en;q=0.3',
    'Cache-Control': 'no-cache',
    'Pragma': 'no-cache'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyCleverbot

Returns a new instance of RubyCleverbot.



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
# File 'lib/ruby_cleverbot.rb', line 31

def initialize
  @coder = HTMLEntities.new
  @data = {
      'stimulus': '',
      'start': 'y',
      'sessionid': '',
      'vText8': '',
      'vText7': '',
      'vText6': '',
      'vText5': '',
      'vText4': '',
      'vText3': '',
      'vText2': '',
      'icognoid': 'wsf',
      'icognocheck': '',
      'fno': 0,
      'prevref': '',
      'cb_settings_language': 'es',
      'emotionaloutput': '',
      'emotionalhistory': '',
      'asbotname': '',
      'ttsvoice': '',
      'typing': '',
      'lineref': '',
      'sub': 'Say',
      'islearning': 1,
      'cleanslate': 'False',
  }
  #get the cookies
  response = make_get(HOST)
  @cookies = response.cookies
  @conversation = []
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



29
30
31
# File 'lib/ruby_cleverbot.rb', line 29

def coder
  @coder
end

#conversationObject (readonly)

Returns the value of attribute conversation.



28
29
30
# File 'lib/ruby_cleverbot.rb', line 28

def conversation
  @conversation
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



26
27
28
# File 'lib/ruby_cleverbot.rb', line 26

def cookies
  @cookies
end

#dataObject (readonly)

Returns the value of attribute data.



27
28
29
# File 'lib/ruby_cleverbot.rb', line 27

def data
  @data
end

Instance Method Details

#make_get(url) ⇒ Object

call a get method



66
67
68
# File 'lib/ruby_cleverbot.rb', line 66

def make_get(url)
  RestClient::Request.execute method: :get, url: url, headers: HEADERS, cookies: cookies
end

#make_post(url, json) ⇒ Object

call a post method



71
72
73
74
# File 'lib/ruby_cleverbot.rb', line 71

def make_post(url, json)
  # RestClient.post url, json, headers
  RestClient::Request.execute method: :post, url: url,payload: URI.encode_www_form(json), headers: HEADERS, cookies: cookies
end

#send_message(question) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby_cleverbot.rb', line 76

def send_message(question)
  # the current question
  data[:stimulus] = question

  # set data, for the conversation
  set_conversation

  # we need the token
  enc_data = URI.encode_www_form(data)
  token = Digest::MD5.hexdigest enc_data[9..34]
  data[:icognocheck] = token
  # puts "the token is #{data[:icognocheck]}"

  response = make_post(API_URL, data)

  clever_response = response.to_str.split(/[\r]+/)

  # see HTML encoding of foreign language characters
  clever_response[0] = coder.decode(clever_response[0])

  # add the log
  conversation << question
  # add response from cleverbot to conversation
  conversation << clever_response[0]

  # return the response
  clever_response[0]
end

#set_conversationObject



105
106
107
108
109
110
111
112
113
# File 'lib/ruby_cleverbot.rb', line 105

def set_conversation
  unless conversation.empty?
    count = 1
    conversation.first(8).reverse_each do |line|
      count += 1
      data[('vText' + count.to_s).to_sym] = line
    end
  end
end