Module: Chatterbot::Client

Included in:
Bot
Defined in:
lib/chatterbot/client.rb

Overview

routines for connecting to Twitter and validating the bot

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

the main interface to the Twitter API


14
15
16
# File 'lib/chatterbot/client.rb', line 14

def client
  @client
end

#screen_nameObject

Returns the value of attribute screen_name


9
10
11
# File 'lib/chatterbot/client.rb', line 9

def screen_name
  @screen_name
end

#search_clientObject

Returns the value of attribute search_client


9
10
11
# File 'lib/chatterbot/client.rb', line 9

def search_client
  @search_client
end

#streaming_clientObject

Returns the value of attribute streaming_client


9
10
11
# File 'lib/chatterbot/client.rb', line 9

def streaming_client
  @streaming_client
end

Instance Method Details

#base_urlObject

the URL we should use for api calls


61
62
63
# File 'lib/chatterbot/client.rb', line 61

def base_url
  "https://api.twitter.com"
end

#consumerObject

simple OAuth client for setting up with Twitter


107
108
109
110
111
112
113
# File 'lib/chatterbot/client.rb', line 107

def consumer
  @consumer ||= OAuth::Consumer.new(
                      config[:consumer_key],
                      config[:consumer_secret],
                      :site => base_url
                      )
end

#default_optsObject

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


69
70
71
72
73
74
75
76
77
# File 'lib/chatterbot/client.rb', line 69

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) ⇒ Object

copied from t, the awesome twitter cli app


119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chatterbot/client.rb', line 119

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) ⇒ Object

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


140
141
142
143
144
145
# File 'lib/chatterbot/client.rb', line 140

def get_screen_name(t = @access_token)
  return unless @screen_name.nil?

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

#init_clientObject

Initialize the Twitter client, and check to see if it has credentials or not


83
84
85
# File 'lib/chatterbot/client.rb', line 83

def init_client
  client.credentials?
end

#login(do_update_config = true) ⇒ Object

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.


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/chatterbot/client.rb', line 154

def (do_update_config=true)
  if needs_api_key?
    get_api_key
  end

  if needs_auth_token?
    pin = get_oauth_verifier #(request_token)
    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[:token] = @access_token.token
      self.config[: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_tokenObject


133
134
135
# File 'lib/chatterbot/client.rb', line 133

def request_token
  @request_token ||= consumer.get_request_token
end

#require_login(do_update_config = true) ⇒ Object

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


97
98
99
100
# File 'lib/chatterbot/client.rb', line 97

def (do_update_config=true)
  init_client
  (do_update_config)
end

#reset!Object


32
33
34
35
# File 'lib/chatterbot/client.rb', line 32

def reset!
  config[:since_id] = 0
  config[:since_id_reply] = 0
end

#reset_clientObject

Re-initialize with Twitter, handy during the auth process


89
90
91
92
# File 'lib/chatterbot/client.rb', line 89

def reset_client
  @client = nil
  init_client
end

#reset_since_idObject

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


42
43
44
45
46
# File 'lib/chatterbot/client.rb', line 42

def reset_since_id
  config[:tmp_since_id] = 0
  result = client.search("a")
  update_since_id(result)
end

#reset_since_id_replyObject

resets the since_id_reply for this bot to the last mention received


51
52
53
54
55
# File 'lib/chatterbot/client.rb', line 51

def reset_since_id_reply
  config[:tmp_since_id_reply] = 0
  result = client.mentions_timeline.max_by(&:id)
  update_since_id_reply(result)
end