From 5bfaac99dee61b8bce058ea49a0a21aa9db28651 Mon Sep 17 00:00:00 2001 From: Jaiden Mispy <^_^@mispy.me> Date: Sat, 15 Nov 2014 03:55:32 +1100 Subject: [PATCH] Some actual tests for the bot response logic --- lib/twitter_ebooks/archive.rb | 8 +- lib/twitter_ebooks/bot.rb | 325 ++++++++++++++++++++-------------- spec/bot_spec.rb | 75 ++++++++ twitter_ebooks.gemspec | 3 +- 4 files changed, 276 insertions(+), 135 deletions(-) create mode 100644 spec/bot_spec.rb diff --git a/lib/twitter_ebooks/archive.rb b/lib/twitter_ebooks/archive.rb index 566d9d8..281832f 100644 --- a/lib/twitter_ebooks/archive.rb +++ b/lib/twitter_ebooks/archive.rb @@ -21,9 +21,9 @@ module Ebooks @config[:consumer_key] = STDIN.gets.chomp print "Consumer secret: " @config[:consumer_secret] = STDIN.gets.chomp - print "Oauth token: " + print "Access token: " @config[:oauth_token] = STDIN.gets.chomp - print "Oauth secret: " + print "Access secret: " @config[:oauth_token_secret] = STDIN.gets.chomp File.open(CONFIG_PATH, 'w') do |f| @@ -34,8 +34,8 @@ module Ebooks Twitter::REST::Client.new do |config| config.consumer_key = @config[:consumer_key] config.consumer_secret = @config[:consumer_secret] - config.oauth_token = @config[:oauth_token] - config.oauth_token_secret = @config[:oauth_token_secret] + config.access_token = @config[:oauth_token] + config.access_token_secret = @config[:oauth_token_secret] end end diff --git a/lib/twitter_ebooks/bot.rb b/lib/twitter_ebooks/bot.rb index 1489063..44496c4 100755 --- a/lib/twitter_ebooks/bot.rb +++ b/lib/twitter_ebooks/bot.rb @@ -4,56 +4,70 @@ require 'twitter' require 'rufus/scheduler' require 'eventmachine' -# Wrap SSLSocket so that readpartial yields the fiber instead of -# blocking when there is no data -# -# We hand this to the twitter library so we can select on the sockets -# and thus run multiple streams without them blocking -class FiberSSLSocket - def initialize(*args) - @socket = OpenSSL::SSL::SSLSocket.new(*args) - end - - def readpartial(maxlen) - data = "" - - loop do - begin - data = @socket.read_nonblock(maxlen) - rescue IO::WaitReadable - end - break if data.length > 0 - Fiber.yield(@socket) +module Ebooks + # Wrap SSLSocket so that readpartial yields the fiber instead of + # blocking when there is no data + # + # We hand this to the twitter library so we can select on the sockets + # and thus run multiple streams without them blocking + class FiberSSLSocket + def initialize(*args) + @socket = OpenSSL::SSL::SSLSocket.new(*args) end - data + def readpartial(maxlen) + data = "" + + loop do + begin + data = @socket.read_nonblock(maxlen) + rescue IO::WaitReadable + end + break if data.length > 0 + Fiber.yield(@socket) + end + + data + end + + def method_missing(m, *args) + @socket.send(m, *args) + end end - def method_missing(m, *args) - @socket.send(m, *args) - end -end + # An EventMachine handler which resumes a fiber on incoming data + class FiberSocketHandler < EventMachine::Connection + def initialize(fiber) + @fiber = fiber + end -# An EventMachine handler which resumes a fiber on incoming data -class FiberSocketHandler < EventMachine::Connection - def initialize(fiber) - @fiber = fiber + def notify_readable + @fiber.resume + end end - def notify_readable - @fiber.resume + class ConfigurationError < Exception + end + + # UserInfo tracks some meta information for how much + # we've interacted with a user, and how much they've responded + class UserInfo + attr_accessor :times_bugged, :times_responded + def initialize + self.times_bugged = 0 + self.times_responded = 0 + end end -end -module Ebooks class Bot attr_accessor :consumer_key, :consumer_secret, - :oauth_token, :oauth_token_secret - - attr_accessor :username + :access_token, :access_token_secret attr_reader :twitter, :stream + # Configuration + attr_accessor :username, :delay_range, :blacklist + @@all = [] # List of all defined bots def self.all; @@all; end @@ -61,114 +75,128 @@ module Ebooks all.find { |bot| bot.username == name } end - def initialize(username, &b) - # Set defaults - @username = username - - # Override with callback - b.call(self) - - Bot.all.push(self) - end - def log(*args) STDOUT.puts "@#{@username}: " + args.map(&:to_s).join(' ') STDOUT.flush end - def configure + def initialize + @username ||= nil + @blacklist ||= [] + @delay_range ||= 0 + @users ||= {} + configure + end + + def make_client @twitter = Twitter::REST::Client.new do |config| config.consumer_key = @consumer_key config.consumer_secret = @consumer_secret - config.access_token = @oauth_token - config.access_token_secret = @oauth_token_secret + config.access_token = @access_token + config.access_token_secret = @access_token_secret end - needs_stream = [@on_follow, @on_message, @on_mention, @on_timeline].any? {|e| !e.nil?} + @stream = Twitter::Streaming::Client.new( + ssl_socket_class: FiberSSLSocket + ) do |config| + config.consumer_key = @consumer_key + config.consumer_secret = @consumer_secret + config.access_token = @access_token + config.access_token_secret = @access_token_secret + end + end - if needs_stream - @stream = Twitter::Streaming::Client.new( - ssl_socket_class: FiberSSLSocket - ) do |config| - config.consumer_key = @consumer_key - config.consumer_secret = @consumer_secret - config.access_token = @oauth_token - config.access_token_secret = @oauth_token_secret + # Calculate some meta information about a tweet relevant for replying + def calc_meta(ev) + meta = {} + meta[:mentions] = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] } + + reply_mentions = meta[:mentions].reject { |m| m.downcase == @username.downcase } + reply_mentions = [ev.user.screen_name] + reply_mentions + + # Don't reply to more than three users at a time + if reply_mentions.length > 3 + log "Truncating reply_mentions to the first three users" + reply_mentions = reply_mentions[0..2] + end + + meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' ' + + meta[:limit] = 140 - meta[:reply_prefix].length + meta + end + + # Receive an event from the twitter stream + def receive_event(ev) + if ev.is_a? Array # Initial array sent on first connection + log "Online!" + return + end + + if ev.is_a? Twitter::DirectMessage + return if ev.sender.screen_name == @username # Don't reply to self + log "DM from @#{ev.sender.screen_name}: #{ev.text}" + fire(:direct_message, ev) + + elsif ev.respond_to?(:name) && ev.name == :follow + return if ev.source.screen_name == @username + log "Followed by #{ev.source.screen_name}" + fire(:follow, ev.source) + + elsif ev.is_a? Twitter::Tweet + return unless ev.text # If it's not a text-containing tweet, ignore it + return if ev.user.screen_name == @username # Ignore our own tweets + + meta = calc_meta(ev) + + mless = ev.text + begin + ev.attrs[:entities][:user_mentions].reverse.each do |entity| + last = mless[entity[:indices][1]..-1]||'' + mless = mless[0...entity[:indices][0]] + last.strip + end + rescue Exception + p ev.attrs[:entities][:user_mentions] + p ev.text + raise end + meta[:mentionless] = mless + + # To check if this is a mention, ensure: + # - The tweet mentions list contains our username + # - The tweet is not being retweeted by somebody else + # - Or soft-retweeted by somebody else + if meta[:mentions].map(&:downcase).include?(@username.downcase) && !ev.retweeted_status? && !ev.text.start_with?('RT ') + log "Mention from @#{ev.user.screen_name}: #{ev.text}" + fire(:mention, ev, meta) + else + fire(:timeline, ev, meta) + end + elsif ev.is_a? Twitter::Streaming::DeletedTweet + # pass + else + log ev end end def start_stream log "starting stream for #@username" - @stream.before_request do - log "Online!" - end - @stream.user do |ev| - p ev - - if ev.is_a? Twitter::DirectMessage - next if ev.sender.screen_name == @username # Don't reply to self - log "DM from @#{ev.sender.screen_name}: #{ev.text}" - @on_message.call(ev) if @on_message - elsif ev.respond_to?(:name) && ev.name == :follow - next if ev.source.screen_name == @username - log "Followed by #{ev.source.screen_name}" - @on_follow.call(ev.source) if @on_follow - elsif ev.is_a? Twitter::Tweet - next unless ev.text # If it's not a text-containing tweet, ignore it - next if ev.user.screen_name == @username # Ignore our own tweets - - meta = {} - mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] } - - reply_mentions = mentions.reject { |m| m.downcase == @username.downcase } - reply_mentions = [ev.user.screen_name] + reply_mentions - - meta[:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' ' - meta[:limit] = 140 - meta[:reply_prefix].length - - mless = ev.text - begin - ev.attrs[:entities][:user_mentions].reverse.each do |entity| - last = mless[entity[:indices][1]..-1]||'' - mless = mless[0...entity[:indices][0]] + last.strip - end - rescue Exception - p ev.attrs[:entities][:user_mentions] - p ev.text - raise - end - meta[:mentionless] = mless - - # To check if this is a mention, ensure: - # - The tweet mentions list contains our username - # - The tweet is not being retweeted by somebody else - # - Or soft-retweeted by somebody else - if mentions.map(&:downcase).include?(@username.downcase) && !ev.retweeted_status? && !ev.text.start_with?('RT ') - log "Mention from @#{ev.user.screen_name}: #{ev.text}" - @on_mention.call(ev, meta) if @on_mention - else - @on_timeline.call(ev, meta) if @on_timeline - end - else - log "Unsure how to handle event: ", ev - end + receive_event ev end end # Connects to tweetstream and opens event handlers for this bot def start - configure - - @on_startup.call if @on_startup - - if not @stream - log "not bothering with stream for #@username" - return + # Sanity check + if @username.nil? + raise ConfigurationError, "bot.username cannot be nil" end + make_client + fire(:startup) + fiber = Fiber.new do start_stream end @@ -179,35 +207,74 @@ module Ebooks conn.notify_readable = true end + # Fire an event + def fire(event, *args) + handler = "on_#{event}".to_sym + if respond_to? handler + self.send(handler, *args) + end + end + # Wrapper for EM.add_timer # Delays add a greater sense of humanity to bot behaviour - def delay(time, &b) - time = time.to_a.sample unless time.is_a? Integer + def delay(&b) + time = @delay.to_a.sample unless @delay.is_a? Integer EM.add_timer(time, &b) end + def blacklisted?(username) + if @blacklist.include?(username) + log "Saw scary blacklisted user @#{username}" + true + else + false + end + end + # Reply to a tweet or a DM. - # Applies configurable @reply_delay range def reply(ev, text, opts={}) opts = opts.clone if ev.is_a? Twitter::DirectMessage + return if blacklisted?(ev.sender.screen_name) log "Sending DM to @#{ev.sender.screen_name}: #{text}" @twitter.create_direct_message(ev.sender.screen_name, text, opts) elsif ev.is_a? Twitter::Tweet + meta = calc_meta(ev) + + return if blacklisted?(ev.user.screen_name) log "Replying to @#{ev.user.screen_name} with: #{text}" - @twitter.update(text, in_reply_to_status_id: ev.id) + @twitter.update(meta[:reply_prefix] + text, in_reply_to_status_id: ev.id) else raise Exception("Don't know how to reply to a #{ev.class}") end end - def scheduler - @scheduler ||= Rufus::Scheduler.new + def favorite(tweet) + return if blacklisted?(tweet.user.screen_name) + log "Favoriting @#{tweet.user.screen_name}: #{tweet.text}" + + begin + @twitter.favorite(tweet.id) + rescue Twitter::Error::Forbidden + log "Already favorited: #{tweet.user.screen_name}: #{tweet.text}" + end + end + + def retweet(tweet) + return if blacklisted?(tweet.user.screen_name) + log "Retweeting @#{tweet.user.screen_name}: #{tweet.text}" + + begin + @twitter.retweet(tweet.id) + rescue Twitter::Error::Forbidden + log "Already retweeted: #{tweet.user.screen_name}: #{tweet.text}" + end end def follow(*args) log "Following #{args}" + @twitter.follow(*args) end @@ -216,16 +283,14 @@ module Ebooks @twitter.update(*args) end + def scheduler + @scheduler ||= Rufus::Scheduler.new + end + # could easily just be *args however the separation keeps it clean. def pictweet(txt, pic, *args) log "Tweeting #{txt.inspect} - #{pic} #{args}" @twitter.update_with_media(txt, File.new(pic), *args) end - - def on_startup(&b); @on_startup = b; end - def on_follow(&b); @on_follow = b; end - def on_mention(&b); @on_mention = b; end - def on_timeline(&b); @on_timeline = b; end - def on_message(&b); @on_message = b; end end end diff --git a/spec/bot_spec.rb b/spec/bot_spec.rb new file mode 100644 index 0000000..d7da4cf --- /dev/null +++ b/spec/bot_spec.rb @@ -0,0 +1,75 @@ +require 'spec_helper' +require 'memory_profiler' +require 'tempfile' + +def Process.rss; `ps -o rss= -p #{Process.pid}`.chomp.to_i; end + +class TestBot < Ebooks::Bot + attr_accessor :twitter + + def configure + self.username = "test_ebooks" + end + + def on_direct_message(dm) + reply dm, "echo: #{dm.text}" + end + + def on_mention(tweet, meta) + reply tweet, "echo: #{meta[:mentionless]}" + end + + def on_timeline(tweet, meta) + reply tweet, "fine tweet good sir" + end +end + +def twitter_id + 533295311591337984 +end + +def mock_dm(username, text) + Twitter::DirectMessage.new(id: twitter_id, + sender: { id: twitter_id, screen_name: username}, + text: text) +end + +def mock_tweet(username, text) + mentions = text.split.find_all { |x| x.start_with?('@') } + Twitter::Tweet.new( + id: twitter_id, + user: { id: twitter_id, screen_name: username }, + text: text, + entities: { + user_mentions: mentions.map { |m| + { screen_name: m.split('@')[1], + indices: [text.index(m), text.index(m)+m.length] } + } + } + ) +end + +describe Ebooks::Bot do + let(:bot) { TestBot.new } + + it "responds to dms" do + bot.twitter = double("twitter") + expect(bot.twitter).to receive(:create_direct_message).with("m1sp", "echo: this is a dm", {}) + bot.receive_event(mock_dm("m1sp", "this is a dm")) + end + + it "responds to mentions" do + bot.twitter = double("twitter") + expect(bot.twitter).to receive(:update).with("@m1sp echo: this is a mention", + in_reply_to_status_id: twitter_id) + bot.receive_event(mock_tweet("m1sp", "@test_ebooks this is a mention")) + end + + it "responds to timeline tweets" do + bot.twitter = double("twitter") + expect(bot.twitter).to receive(:update).with("@m1sp fine tweet good sir", + in_reply_to_status_id: twitter_id) + + bot.receive_event(mock_tweet("m1sp", "some excellent tweet")) + end +end diff --git a/twitter_ebooks.gemspec b/twitter_ebooks.gemspec index 0fec762..eccbb22 100644 --- a/twitter_ebooks.gemspec +++ b/twitter_ebooks.gemspec @@ -16,12 +16,13 @@ Gem::Specification.new do |gem| gem.version = Ebooks::VERSION gem.add_development_dependency 'rspec' + gem.add_development_dependency 'rspec-mocks' gem.add_development_dependency 'memory_profiler' gem.add_development_dependency 'pry-byebug' gem.add_runtime_dependency 'twitter', '~> 5.0' gem.add_runtime_dependency 'simple_oauth', '~> 0.2.0' - gem.add_runtime_dependency 'tweetstream' + gem.add_runtime_dependency 'eventmachine', '~> 1.0.3' gem.add_runtime_dependency 'rufus-scheduler' gem.add_runtime_dependency 'gingerice' gem.add_runtime_dependency 'htmlentities'