class RubyCleverbot

cleverbot api

Constants

HEADERS
HOST
RESOURCE

Attributes

conversation[R]
cookies[R]
data[R]

Public Class Methods

new(botapi, lang: 'ru', uc: '3210') click to toggle source
# 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.cookies
  @conversation = []
end

Public Instance Methods

make_get(url) click to toggle source

call a get method

# File lib/ruby_cleverbot.rb, line 64
def make_get(url)
  RestClient::Request.execute method: :get, url: url, headers: HEADERS, cookies: cookies
end
make_post(url, json) click to toggle source

call a post method

# 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: cookies
end
send_message(question) click to toggle source
# File lib/ruby_cleverbot.rb, line 74
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] = 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() click to toggle source
# 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