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
-
#client ⇒ Object
the main interface to the Twitter API.
-
#screen_name ⇒ Object
Returns the value of attribute screen_name.
-
#streaming_client ⇒ Object
interace to the Streaming API.
Instance Method Summary collapse
-
#authenticated_user ⇒ Object
return the currently authenticated User.
-
#base_url ⇒ Object
the URL we should use for api calls.
-
#consumer ⇒ Object
simple OAuth client for setting up with Twitter.
-
#default_opts ⇒ Object
default options when querying twitter – this could be extended with a language, etc.
-
#generate_authorize_url(request_token) ⇒ Object
copied from t, the awesome twitter cli app.
-
#get_screen_name(t = @access_token) ⇒ Object
query twitter for the bots screen name.
-
#init_client ⇒ Object
Initialize the Twitter client, and check to see if it has credentials or not.
-
#login(do_update_config = true) ⇒ Object
handle oauth for this request.
-
#request_token ⇒ Object
grab a OAuth request token.
-
#require_login(do_update_config = true) ⇒ Object
Call this before doing anything that requires an authorized Twitter connection.
-
#reset! ⇒ Object
reset a few tweet_id trackers.
-
#reset_client ⇒ Object
Re-initialize with Twitter, handy during the auth process.
-
#reset_since_id ⇒ Object
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.
-
#reset_since_id_counters ⇒ Object
reset all since_id counters.
-
#reset_since_id_dm ⇒ Object
reset to the last DM received.
-
#reset_since_id_home_timeline ⇒ Object
resets the home_timeline_id_reply for this bot to the last tweet on the timeline.
-
#reset_since_id_reply ⇒ Object
resets the since_id_reply for this bot to the last mention received.
Instance Attribute Details
#client ⇒ Object
the main interface to the Twitter API
14 15 16 |
# File 'lib/chatterbot/client.rb', line 14 def client @client end |
#screen_name ⇒ Object
Returns the value of attribute screen_name.
9 10 11 |
# File 'lib/chatterbot/client.rb', line 9 def screen_name @screen_name end |
#streaming_client ⇒ Object
interace to the Streaming API
21 22 23 |
# File 'lib/chatterbot/client.rb', line 21 def streaming_client @streaming_client end |
Instance Method Details
#authenticated_user ⇒ Object
return the currently authenticated User
28 29 30 |
# File 'lib/chatterbot/client.rb', line 28 def authenticated_user @user ||= client.user end |
#base_url ⇒ Object
the URL we should use for api calls
92 93 94 |
# File 'lib/chatterbot/client.rb', line 92 def base_url "https://api.twitter.com" end |
#consumer ⇒ Object
simple OAuth client for setting up with Twitter
138 139 140 141 142 143 144 |
# File 'lib/chatterbot/client.rb', line 138 def consumer @consumer ||= OAuth::Consumer.new( config[:consumer_key], config[:consumer_secret], :site => base_url ) end |
#default_opts ⇒ Object
default options when querying twitter – this could be extended with a language, etc.
100 101 102 103 104 105 106 107 108 |
# File 'lib/chatterbot/client.rb', line 100 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
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/chatterbot/client.rb', line 150 def (request_token) request = consumer.create_signed_request(:get, consumer., 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
172 173 174 175 176 177 178 |
# File 'lib/chatterbot/client.rb', line 172 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 ⇒ Object
Initialize the Twitter client, and check to see if it has credentials or not
114 115 116 |
# File 'lib/chatterbot/client.rb', line 114 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/chatterbot/client.rb', line 187 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 ⇒ Object
grab a OAuth request token
165 166 167 |
# File 'lib/chatterbot/client.rb', line 165 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.
128 129 130 131 |
# File 'lib/chatterbot/client.rb', line 128 def require_login(do_update_config=true) init_client login(do_update_config) end |
#reset! ⇒ Object
reset a few tweet_id trackers
35 36 37 38 |
# File 'lib/chatterbot/client.rb', line 35 def reset! config[:since_id] = 1 config[:since_id_reply] = 1 end |
#reset_client ⇒ Object
Re-initialize with Twitter, handy during the auth process
120 121 122 123 |
# File 'lib/chatterbot/client.rb', line 120 def reset_client @client = nil init_client end |
#reset_since_id ⇒ Object
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
54 55 56 57 58 59 60 |
# File 'lib/chatterbot/client.rb', line 54 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 ⇒ Object
reset all since_id counters
41 42 43 44 45 46 47 |
# File 'lib/chatterbot/client.rb', line 41 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 ⇒ Object
reset to the last DM received
82 83 84 85 86 |
# File 'lib/chatterbot/client.rb', line 82 def reset_since_id_dm config[:since_id_dm] = 0 result = client..max_by(&:id) update_since_id_dm(result) end |
#reset_since_id_home_timeline ⇒ Object
resets the home_timeline_id_reply for this bot to the last tweet on the timeline
75 76 77 78 79 |
# File 'lib/chatterbot/client.rb', line 75 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 |