diff --git a/brain/Gemfile b/brain/Gemfile index 02f7867..4a46a6e 100644 --- a/brain/Gemfile +++ b/brain/Gemfile @@ -20,6 +20,9 @@ gem 'faraday_middleware' # Astricon Schedule Neuron gem 'blurrily' +# Tweet gem +gem 'twitter' + group :development, :test do gem 'rspec' gem 'guard' diff --git a/brain/Gemfile.lock b/brain/Gemfile.lock index 05f51c0..a94da2c 100644 --- a/brain/Gemfile.lock +++ b/brain/Gemfile.lock @@ -142,6 +142,7 @@ GEM ruby_speech (2.3.0) activesupport (>= 3.0.7, < 5.0.0) nokogiri (~> 1.6) + simple_oauth (0.2.0) slop (3.4.6) state_machine (1.2.0) thor (0.18.1) @@ -150,6 +151,10 @@ GEM timecop (0.6.3) timers (1.1.0) toadhopper (2.1) + twitter (4.8.1) + faraday (~> 0.8, < 0.10) + multi_json (~> 1.0) + simple_oauth (~> 0.2) tzinfo (0.3.37) virtus (0.5.2) backports (~> 2.6.1) @@ -177,4 +182,5 @@ DEPENDENCIES pipejump rspec timecop + twitter webmock diff --git a/brain/lib/app.rb b/brain/lib/app.rb index ba8c4ea..ab749a0 100644 --- a/brain/lib/app.rb +++ b/brain/lib/app.rb @@ -8,6 +8,7 @@ require_relative 'mirror_mirror_neuron' require_relative 'astricon_now_speaking' require_relative 'time_neuron' +require_relative 'tweet_neuron' class App < Adhearsion::Plugin init :brain do @@ -20,6 +21,7 @@ class App < Adhearsion::Plugin @brain.add_neuron MirrorMirrorNeuron.new @brain.add_neuron AstriconNowSpeaking.new @brain.add_neuron TimeNeuron.new + @brain.add_neuron TweetNeuron.new end run :brain do diff --git a/brain/lib/tweet_neuron.rb b/brain/lib/tweet_neuron.rb new file mode 100644 index 0000000..226eb36 --- /dev/null +++ b/brain/lib/tweet_neuron.rb @@ -0,0 +1,32 @@ +require 'twitter' + +class TweetNeuron + def initialize + end + + def intent + 'tweet' + end + + def reply(message, interpretation) + client = get_twitter_client(message.user) + return "You must log in via Twitter so I can help you tweet." unless client + entities = interpretation['outcome']['entities'] + return "You have to tell me what to tweet!" unless entities.has_key? 'message' + + client.update entities['message']['value'] + "Message sent." + end + + def get_twitter_client(user) + grant = user['auth_grants'].select { |grant| grant[:provider] == :twitter }.first + if grant + Twitter::Client.new do |config| + config.consumer_key = ENV['TWITTER_KEY'] + config.consumer_secret = ENV['TWITTER_SECRET'] + config.access_token = grant[:credentials]['token'] + config.access_token_secret = grant[:credentials]['secret'] + end + end + end +end diff --git a/brain/spec/lib/tweet_neuron_spec.rb b/brain/spec/lib/tweet_neuron_spec.rb new file mode 100644 index 0000000..8478a31 --- /dev/null +++ b/brain/spec/lib/tweet_neuron_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe TweetNeuron do + include NeuronMatchers + + let(:subject) { TweetNeuron.new } + + it "should return an error message if no tweet is provided" do + interpretation = wit_interpretation 'Tweet this', 'tweet' + should handle_message('Tweet this', :default_user, interpretation) + .and_respond_with("You have to tell me what to tweet!") + end + + it "should send a tweet" do + interpretation = wit_interpretation 'Tweet Hello from AstriCon', 'tweet', message: 'Hello from AstriCon' + should handle_message('Tweet this Hello from Astricon', :default_user, interpretation) + .and_respond_with("Message sent.") + end + + context "getting a Twitter client" do + it "should return nil if the user has no credentials for Twitter" do + subject.get_twitter_client('auth_grants' => []).should be nil + end + + it "should return an instance of Twitter::Client if credentials are supplied" do + user = {'auth_grants' => [{provider: :twitter, credentials: {'token' => 'abcd', 'secret' => 'efgh'}}]} + subject.get_twitter_client(user).should be_a Twitter::Client + end + end + +end + diff --git a/brain/vendor/cache/simple_oauth-0.2.0.gem b/brain/vendor/cache/simple_oauth-0.2.0.gem new file mode 100644 index 0000000..5901361 Binary files /dev/null and b/brain/vendor/cache/simple_oauth-0.2.0.gem differ diff --git a/brain/vendor/cache/twitter-4.8.1.gem b/brain/vendor/cache/twitter-4.8.1.gem new file mode 100644 index 0000000..b928ad6 Binary files /dev/null and b/brain/vendor/cache/twitter-4.8.1.gem differ