Class: RubyCleverbot
- Inherits:
-
Object
- Object
- RubyCleverbot
- Defined in:
- lib/ruby_cleverbot.rb
Overview
cleverbot api
Constant Summary collapse
- HOST =
'http://www.cleverbot.com'.freeze
- RESOURCE =
'webservicemin'.freeze
- 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': 'utf-8,*;q=0.7', 'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,de;q=0.2', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
Instance Attribute Summary collapse
-
#conversation ⇒ Object
readonly
Returns the value of attribute conversation.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#initialize(botapi, lang: 'ru', uc: '3210') ⇒ RubyCleverbot
constructor
A new instance of RubyCleverbot.
-
#make_get(url) ⇒ Object
call a get method.
-
#make_post(url, json) ⇒ Object
call a post method.
- #send_message(question) ⇒ Object
- #set_conversation ⇒ Object
Constructor Details
#initialize(botapi, lang: 'ru', uc: '3210') ⇒ RubyCleverbot
Returns a new instance of RubyCleverbot.
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 |
# File 'lib/ruby_cleverbot.rb', line 28 def initialize(botapi, lang: 'ru', uc: '3210') @api_url = "#{HOST}/#{RESOURCE}?uc=#{uc}&botapi=#{botapi}" @data = { 'stimulus': '', 'start': 'y', 'sessionid': '', 'vText8': '', 'vText7': '', 'vText6': '', 'vText5': '', 'vText4': '', 'vText3': '', 'vText2': '', 'icognoid': 'wsf', 'icognocheck': '', 'fno': 0, 'prevref': '', 'cb_settings_language': lang, 'emotionaloutput': '', 'emotionalhistory': '', 'asbotname': '', 'ttsvoice': '', 'typing': '', 'lineref': '', 'sub': 'Say', 'islearning': 1, 'cleanslate': 'False', } #get the cookies response = make_get(HOST) @cookies = response. @conversation = [] end |
Instance Attribute Details
#conversation ⇒ Object (readonly)
Returns the value of attribute conversation.
26 27 28 |
# File 'lib/ruby_cleverbot.rb', line 26 def conversation @conversation end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
24 25 26 |
# File 'lib/ruby_cleverbot.rb', line 24 def @cookies end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
25 26 27 |
# File 'lib/ruby_cleverbot.rb', line 25 def data @data end |
Instance Method Details
#make_get(url) ⇒ Object
call a get method
64 65 66 |
# File 'lib/ruby_cleverbot.rb', line 64 def make_get(url) RestClient::Request.execute method: :get, url: url, headers: HEADERS, cookies: end |
#make_post(url, json) ⇒ Object
call a post method
69 70 71 72 |
# File 'lib/ruby_cleverbot.rb', line 69 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: end |
#send_message(question) ⇒ Object
74 75 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 |
# File 'lib/ruby_cleverbot.rb', line 74 def (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] = clever_response[0].force_encoding('UTF-8') # add the log conversation << question # add response from cleverbot to conversation conversation << clever_response[0] # return the response clever_response[0] end |
#set_conversation ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/ruby_cleverbot.rb', line 103 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 |