class Cleverbot

Attributes

api_key[R]

@return [String] The API Key for the instance.

api_user[R]

@return [String] The API User for the instance.

nick[RW]

@return [String] The reference nick for the session.

Public Class Methods

new(api_user, api_key, nick = nil) click to toggle source

Creates a new instance of the Cleverbot. @param api_user [String] The API user for the Cleverbot API. @param api_key [String] The API key for the Cleverbot API. @param nick [String] The reference nick. If nil, one will be set automatically through the API.

# File lib/cleverbot.rb, line 18
def initialize(api_user, api_key, nick = nil)
  @api_user = api_user
  @api_key = api_key
  @nick = nick
  @client = HTTPClient.new

  params = {
    user: @api_user,
    key: @api_key
  }
  params[:nick] = @nick unless @nick.nil?
  response = Oj.load(@client.post('https://cleverbot.io/1.0/create', params).body)
  try_throw(response['status'])
  @nick = response['nick']
end

Public Instance Methods

say(str) click to toggle source

Sends the bot a message and returns its response. @param str [String] The message to send to the bot. @return [String] The bot's response, or its error message.

# File lib/cleverbot.rb, line 37
def say(str)
  params = {
    user: @api_user,
    key: @api_key,
    text: str,
    nick: @nick
  }
  response = Oj.load(@client.post('https://cleverbot.io/1.0/ask', params).body)
  try_throw(response['status'])

  response['response']
end

Private Instance Methods

try_throw(status) click to toggle source

Throws the relevant errors if possible. @param status [String] The status value from the API @raise [Cleverbot::Error] If an error is thrown by the Cleverbot API.

# File lib/cleverbot.rb, line 58
def try_throw(status)
  return if status == 'success'
  fail Error.new(status)
end