module Chatterbot::Client

routines for connecting to Twitter and validating the bot

Attributes

client[RW]
screen_name[RW]

Public Instance Methods

authenticated_user() click to toggle source

return the currently authenticated User

# File lib/chatterbot/client.rb, line 21
def authenticated_user
  @user ||= client.user
end
base_url() click to toggle source

the URL we should use for api calls

# File lib/chatterbot/client.rb, line 85
def base_url
  "https://api.twitter.com"
end
consumer() click to toggle source

simple OAuth client for setting up with Twitter

# File lib/chatterbot/client.rb, line 131
def consumer
  @consumer ||= OAuth::Consumer.new(
                      config[:consumer_key],
                      config[:consumer_secret],
                      :site => base_url
                      )
end
default_opts() click to toggle source

default options when querying twitter – this could be extended with a language, etc.

# File lib/chatterbot/client.rb, line 93
def default_opts
  opts = {
    :result_type => "recent"
  }
  opts[:since_id] = since_id if since_id > 0
  opts[:since_id_reply] = since_id_reply if since_id_reply > 0

  opts
end
generate_authorize_url(request_token) click to toggle source

copied from t, the awesome twitter cli app @see github.com/sferik/t/blob/master/lib/t/authorizable.rb

# File lib/chatterbot/client.rb, line 143
def generate_authorize_url(request_token)
  request = consumer.create_signed_request(:get,
    consumer.authorize_path, request_token,
    {:oauth_callback => 'oob'})

  params = request['Authorization'].sub(/^OAuth\s+/, '').split(/,\s+/).map do |param|
    key, value = param.split('=')
    value =~ /"(.*?)"/
    "#{key}=#{CGI::escape($1)}"
  end.join('&')

  "#{base_url}#{request.path}?#{params}"
end
get_screen_name(t = @access_token) click to toggle source

query twitter for the bots screen name. we do this during the bot registration process

# File lib/chatterbot/client.rb, line 165
def get_screen_name(t = @access_token)
  return unless @screen_name.nil?
  return if t.nil?

  oauth_response = t.get('/1.1/account/verify_credentials.json')
  @screen_name = JSON.parse(oauth_response.body)["screen_name"]
end
init_client() click to toggle source

Initialize the Twitter client, and check to see if it has credentials or not @return true/false depending on if client has OAuth credentials

# File lib/chatterbot/client.rb, line 107
def init_client
  client.credentials?
end
login(do_update_config=true) click to toggle source

handle oauth for this request. if the client isn't authorized, print out the auth URL and get a pin code back from the user If do_update_config is false, don't udpate the bots config file after authorization. This defaults to true but chatterbot-register will pass in false because it does some other work before saving.

# File lib/chatterbot/client.rb, line 180
def login(do_update_config=true)
  if needs_api_key?
    get_api_key
  end

  if needs_auth_token?
    pin = get_oauth_verifier
    return false if pin.nil?


    begin
      # this will throw an error that we can try and catch
      @access_token = request_token.get_access_token(:oauth_verifier => pin.chomp)
      get_screen_name

      self.config[:access_token] = @access_token.token
      self.config[:access_token_secret] = @access_token.secret

      #update_config unless ! do_update_config
      reset_client

    rescue OAuth::Unauthorized => e
      display_oauth_error
      warn e.inspect
      return false
    end
  end

  return true
end
request_token() click to toggle source

grab a OAuth request token

# File lib/chatterbot/client.rb, line 158
def request_token
  @request_token ||= consumer.get_request_token
end
require_login(do_update_config=true) click to toggle source

Call this before doing anything that requires an authorized Twitter connection.

# File lib/chatterbot/client.rb, line 121
def require_login(do_update_config=true)
  init_client
  login(do_update_config)
end
reset!() click to toggle source

reset a few tweet_id trackers

# File lib/chatterbot/client.rb, line 28
def reset!
  config[:since_id] = 1
  config[:since_id_reply] = 1
end
reset_client() click to toggle source

Re-initialize with Twitter, handy during the auth process

# File lib/chatterbot/client.rb, line 113
def reset_client
  @client = nil
  init_client
end
reset_since_id() click to toggle source

reset the since_id for this bot to the highest since_id we can get, by running a really open search and updating config with the max_id

# File lib/chatterbot/client.rb, line 47
def reset_since_id
  config[:since_id] = 1
  # do a search of recent tweets with the letter 'a' in them to
  # get a rough max tweet id
  result = client.search("a", since:Time.now - 10).max_by(&:id)
  update_since_id(result)
end
reset_since_id_counters() click to toggle source

reset all since_id counters

# File lib/chatterbot/client.rb, line 34
def reset_since_id_counters
  reset!
  reset_since_id
  reset_since_id_reply
  reset_since_id_home_timeline
  reset_since_id_dm
end
reset_since_id_dm() click to toggle source

reset to the last DM received

# File lib/chatterbot/client.rb, line 75
def reset_since_id_dm
  config[:since_id_dm] = 0
  result = client.direct_messages_received.max_by(&:id)
  update_since_id_dm(result)
end
reset_since_id_home_timeline() click to toggle source

resets the home_timeline_id_reply for this bot to the last tweet on the timeline

# File lib/chatterbot/client.rb, line 68
def reset_since_id_home_timeline
  config[:since_id_reply] = 0
  result = client.home_timeline.max_by(&:id)
  update_since_id_home_timeline(result)
end
reset_since_id_reply() click to toggle source

resets the since_id_reply for this bot to the last mention received

# File lib/chatterbot/client.rb, line 58
def reset_since_id_reply
  config[:since_id_reply] = 0
  result = client.mentions_timeline.max_by(&:id)
  update_since_id_reply(result)
end