lib/chatterbot/client.rb in chatterbot-0.2.8 vs lib/chatterbot/client.rb in chatterbot-0.2.9

- old
+ new

@@ -5,17 +5,22 @@ module Client # the Twitter client attr_accessor :client + # track the access token so we can get screen name when + # registering new bots + attr_accessor :access_token + # # default options when querying twitter -- this could be extended # with a language, etc. def default_opts return {} if since_id <= 0 { - :since_id => since_id + :since_id => since_id, + :result_type => "recent" } end # @@ -32,13 +37,13 @@ end # # Call this before doing anything that requires an authorized Twitter # connection. - def require_login + def require_login(do_update_config=true) init_client - login + login(do_update_config) end # # print out a message about getting a PIN from twitter, then output # the URL the user needs to visit to authorize @@ -47,10 +52,12 @@ puts "Please go to the following URL and authorize the bot.\n" puts "#{request_token.authorize_url}\n" puts "Paste your PIN and hit enter when you have completed authorization." STDIN.readline.chomp + rescue Interrupt => e + end # # Ask the user to get an API key from Twitter. def get_api_key @@ -85,34 +92,41 @@ # # error message for auth def display_oauth_error debug "Oops! Looks like something went wrong there, please try again!" -# exit end # # 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 - def login + # 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. + def login(do_update_config=true) if needs_api_key? get_api_key end if needs_auth_token? request_token = client.request_token pin = get_oauth_verifier(request_token) - access_token = client.authorize( + return false if pin.nil? + + @access_token = client.authorize( request_token.token, request_token.secret, :oauth_verifier => pin ) + if client.authorized? - config[:token] = access_token.token - config[:secret] = access_token.secret - update_config + config[:token] = @access_token.token + config[:secret] = @access_token.secret + + update_config unless do_update_config == false else display_oauth_error return false end end