2014-05-07 16:45:17 +10:00
|
|
|
|
# encoding: utf-8
|
2013-11-08 06:02:05 +11:00
|
|
|
|
require 'twitter'
|
|
|
|
|
require 'rufus/scheduler'
|
2014-11-14 22:45:54 +11:00
|
|
|
|
|
2016-01-12 23:36:21 -08:00
|
|
|
|
# Monkeypatch hack to fix upstream dependency issue
|
|
|
|
|
# https://github.com/sferik/twitter/issues/709
|
|
|
|
|
class HTTP::URI
|
|
|
|
|
def port
|
|
|
|
|
443 if self.https?
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
module Ebooks
|
|
|
|
|
class ConfigurationError < Exception
|
2014-11-14 22:45:54 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 19:04:15 +11:00
|
|
|
|
# Represents a single reply tree of tweets
|
|
|
|
|
class Conversation
|
|
|
|
|
attr_reader :last_update
|
2014-11-18 12:00:34 +11:00
|
|
|
|
|
2014-12-05 22:57:32 +11:00
|
|
|
|
# @param bot [Ebooks::Bot]
|
2014-12-05 19:04:15 +11:00
|
|
|
|
def initialize(bot)
|
|
|
|
|
@bot = bot
|
|
|
|
|
@tweets = []
|
2014-11-18 12:00:34 +11:00
|
|
|
|
@last_update = Time.now
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @param tweet [Twitter::Tweet] tweet to add
|
2014-12-05 19:04:15 +11:00
|
|
|
|
def add(tweet)
|
|
|
|
|
@tweets << tweet
|
2014-11-18 12:00:34 +11:00
|
|
|
|
@last_update = Time.now
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 19:04:15 +11:00
|
|
|
|
# Make an informed guess as to whether a user is a bot based
|
|
|
|
|
# on their behavior in this conversation
|
|
|
|
|
def is_bot?(username)
|
2014-12-05 23:50:07 +11:00
|
|
|
|
usertweets = @tweets.select { |t| t.user.screen_name.downcase == username.downcase }
|
2014-12-05 19:04:15 +11:00
|
|
|
|
|
|
|
|
|
if usertweets.length > 2
|
2016-02-23 06:59:35 +11:00
|
|
|
|
if username.include?('ebooks') || (usertweets[-1].created_at - usertweets[-3].created_at) < 12
|
2014-11-18 12:00:34 +11:00
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 19:04:15 +11:00
|
|
|
|
# Figure out whether to keep this user in the reply prefix
|
|
|
|
|
# We want to avoid spamming non-participating users
|
|
|
|
|
def can_include?(username)
|
2014-12-20 07:24:40 +11:00
|
|
|
|
@tweets.length <= 4 ||
|
|
|
|
|
!@tweets.select { |t| t.user.screen_name.downcase == username.downcase }.empty?
|
2014-11-15 03:55:32 +11:00
|
|
|
|
end
|
2014-11-14 22:45:54 +11:00
|
|
|
|
end
|
2013-11-08 06:02:05 +11:00
|
|
|
|
|
2014-12-04 05:17:09 +11:00
|
|
|
|
# Meta information about a tweet that we calculate for ourselves
|
|
|
|
|
class TweetMeta
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [Array<String>] usernames mentioned in tweet
|
|
|
|
|
attr_accessor :mentions
|
|
|
|
|
# @return [String] text of tweets with mentions removed
|
|
|
|
|
attr_accessor :mentionless
|
|
|
|
|
# @return [Array<String>] usernames to include in a reply
|
|
|
|
|
attr_accessor :reply_mentions
|
|
|
|
|
# @return [String] mentions to start reply with
|
|
|
|
|
attr_accessor :reply_prefix
|
|
|
|
|
# @return [Integer] available chars for reply
|
|
|
|
|
attr_accessor :limit
|
|
|
|
|
|
|
|
|
|
# @return [Ebooks::Bot] associated bot
|
|
|
|
|
attr_accessor :bot
|
|
|
|
|
# @return [Twitter::Tweet] associated tweet
|
|
|
|
|
attr_accessor :tweet
|
|
|
|
|
|
|
|
|
|
# Check whether this tweet mentions our bot
|
|
|
|
|
# @return [Boolean]
|
2014-12-04 05:17:09 +11:00
|
|
|
|
def mentions_bot?
|
|
|
|
|
# To check if this is someone talking to us, ensure:
|
|
|
|
|
# - The tweet mentions list contains our username
|
|
|
|
|
# - The tweet is not being retweeted by somebody else
|
|
|
|
|
# - Or soft-retweeted by somebody else
|
2014-12-06 22:37:10 +11:00
|
|
|
|
@mentions.map(&:downcase).include?(@bot.username.downcase) && !@tweet.retweeted_status? && !@tweet.text.match(/([`'‘’"“”]|RT|via|by|from)\s*@/i)
|
2014-12-04 05:17:09 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 22:57:32 +11:00
|
|
|
|
# @param bot [Ebooks::Bot]
|
|
|
|
|
# @param ev [Twitter::Tweet]
|
2014-12-04 05:17:09 +11:00
|
|
|
|
def initialize(bot, ev)
|
|
|
|
|
@bot = bot
|
|
|
|
|
@tweet = ev
|
|
|
|
|
|
|
|
|
|
@mentions = ev.attrs[:entities][:user_mentions].map { |x| x[:screen_name] }
|
|
|
|
|
|
|
|
|
|
# Process mentions to figure out who to reply to
|
|
|
|
|
# i.e. not self and nobody who has seen too many secondary mentions
|
|
|
|
|
reply_mentions = @mentions.reject do |m|
|
2014-12-16 08:28:52 +11:00
|
|
|
|
m.downcase == @bot.username.downcase || !@bot.conversation(ev).can_include?(m)
|
2014-12-04 05:17:09 +11:00
|
|
|
|
end
|
|
|
|
|
@reply_mentions = ([ev.user.screen_name] + reply_mentions).uniq
|
|
|
|
|
|
|
|
|
|
@reply_prefix = @reply_mentions.map { |m| '@'+m }.join(' ') + ' '
|
|
|
|
|
@limit = 140 - @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
|
|
|
|
|
@mentionless = mless
|
|
|
|
|
end
|
2014-12-06 21:57:51 -08:00
|
|
|
|
|
|
|
|
|
# Get an array of media uris in tweet.
|
|
|
|
|
# @param size [String] A twitter image size to return. Supported sizes are thumb, small, medium (default), large
|
|
|
|
|
# @return [Array<String>] image URIs included in tweet
|
2014-12-06 23:31:58 -08:00
|
|
|
|
def media_uris(size_input = '')
|
|
|
|
|
case size_input
|
|
|
|
|
when 'thumb'
|
|
|
|
|
size = ':thumb'
|
|
|
|
|
when 'small'
|
|
|
|
|
size = ':small'
|
|
|
|
|
when 'medium'
|
|
|
|
|
size = ':medium'
|
|
|
|
|
when 'large'
|
|
|
|
|
size = ':large'
|
|
|
|
|
else
|
|
|
|
|
size = ''
|
2014-12-06 21:57:51 -08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Start collecting uris.
|
|
|
|
|
uris = []
|
|
|
|
|
if @tweet.media?
|
|
|
|
|
@tweet.media.each do |each_media|
|
|
|
|
|
uris << each_media.media_url.to_s + size
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# and that's pretty much it!
|
|
|
|
|
uris
|
|
|
|
|
end
|
2014-12-04 05:17:09 +11:00
|
|
|
|
end
|
|
|
|
|
|
2013-11-08 06:02:05 +11:00
|
|
|
|
class Bot
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [String] OAuth consumer key for a Twitter app
|
|
|
|
|
attr_accessor :consumer_key
|
|
|
|
|
# @return [String] OAuth consumer secret for a Twitter app
|
|
|
|
|
attr_accessor :consumer_secret
|
|
|
|
|
# @return [String] OAuth access token from `ebooks auth`
|
|
|
|
|
attr_accessor :access_token
|
|
|
|
|
# @return [String] OAuth access secret from `ebooks auth`
|
|
|
|
|
attr_accessor :access_token_secret
|
2015-01-15 02:34:45 -08:00
|
|
|
|
# @return [Twitter::User] Twitter user object of bot
|
|
|
|
|
attr_accessor :user
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [String] Twitter username of bot
|
|
|
|
|
attr_accessor :username
|
|
|
|
|
# @return [Array<String>] list of usernames to block on contact
|
|
|
|
|
attr_accessor :blacklist
|
|
|
|
|
# @return [Hash{String => Ebooks::Conversation}] maps tweet ids to their conversation contexts
|
2014-12-05 19:04:15 +11:00
|
|
|
|
attr_accessor :conversations
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [Range, Integer] range of seconds to delay in delay method
|
2014-12-05 22:57:32 +11:00
|
|
|
|
attr_accessor :delay_range
|
2014-12-05 19:04:15 +11:00
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [Array] list of all defined bots
|
|
|
|
|
def self.all; @@all ||= []; end
|
2013-11-08 06:02:05 +11:00
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Fetches a bot by username
|
|
|
|
|
# @param username [String]
|
|
|
|
|
# @return [Ebooks::Bot]
|
|
|
|
|
def self.get(username)
|
2016-08-21 23:37:44 +10:00
|
|
|
|
all.find { |bot| bot.username.downcase == username.downcase }
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Logs info to stdout in the context of this bot
|
2013-11-08 06:02:05 +11:00
|
|
|
|
def log(*args)
|
2014-11-18 13:24:59 +11:00
|
|
|
|
STDOUT.print "@#{@username}: " + args.map(&:to_s).join(' ') + "\n"
|
2013-12-12 13:27:48 +00:00
|
|
|
|
STDOUT.flush
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Initializes and configures bot
|
|
|
|
|
# @param args Arguments passed to configure method
|
|
|
|
|
# @param b Block to call with new bot
|
|
|
|
|
def initialize(username, &b)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
@blacklist ||= []
|
2014-12-05 19:04:15 +11:00
|
|
|
|
@conversations ||= {}
|
2014-11-19 10:07:29 +11:00
|
|
|
|
# Tweet ids we've already observed, to avoid duplication
|
|
|
|
|
@seen_tweets ||= {}
|
2014-12-05 21:12:39 +11:00
|
|
|
|
|
|
|
|
|
@username = username
|
2014-12-12 22:53:19 +11:00
|
|
|
|
@delay_range ||= 1..6
|
2014-12-05 22:57:32 +11:00
|
|
|
|
configure
|
2014-12-05 21:12:39 +11:00
|
|
|
|
|
2014-12-05 22:57:32 +11:00
|
|
|
|
b.call(self) unless b.nil?
|
2014-12-05 14:03:11 +11:00
|
|
|
|
Bot.all << self
|
2014-11-18 12:00:34 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-12 22:53:19 +11:00
|
|
|
|
def configure
|
|
|
|
|
raise ConfigurationError, "Please override the 'configure' method for subclasses of Ebooks::Bot."
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Find or create the conversation context for this tweet
|
|
|
|
|
# @param tweet [Twitter::Tweet]
|
|
|
|
|
# @return [Ebooks::Conversation]
|
2014-12-05 19:04:15 +11:00
|
|
|
|
def conversation(tweet)
|
|
|
|
|
conv = if tweet.in_reply_to_status_id?
|
|
|
|
|
@conversations[tweet.in_reply_to_status_id]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if conv.nil?
|
|
|
|
|
conv = @conversations[tweet.id] || Conversation.new(self)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if tweet.in_reply_to_status_id?
|
|
|
|
|
@conversations[tweet.in_reply_to_status_id] = conv
|
|
|
|
|
end
|
|
|
|
|
@conversations[tweet.id] = conv
|
|
|
|
|
|
|
|
|
|
# Expire any old conversations to prevent memory growth
|
|
|
|
|
@conversations.each do |k,v|
|
|
|
|
|
if v != conv && Time.now - v.last_update > 3600
|
|
|
|
|
@conversations.delete(k)
|
|
|
|
|
end
|
2014-11-18 12:00:34 +11:00
|
|
|
|
end
|
2014-12-05 19:04:15 +11:00
|
|
|
|
|
|
|
|
|
conv
|
2014-11-15 03:55:32 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [Twitter::REST::Client] underlying REST client from twitter gem
|
2014-11-19 10:07:29 +11:00
|
|
|
|
def twitter
|
|
|
|
|
@twitter ||= Twitter::REST::Client.new do |config|
|
2013-11-08 06:02:05 +11:00
|
|
|
|
config.consumer_key = @consumer_key
|
|
|
|
|
config.consumer_secret = @consumer_secret
|
2014-11-15 03:55:32 +11:00
|
|
|
|
config.access_token = @access_token
|
|
|
|
|
config.access_token_secret = @access_token_secret
|
|
|
|
|
end
|
2014-11-19 10:07:29 +11:00
|
|
|
|
end
|
2014-11-15 03:55:32 +11:00
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @return [Twitter::Streaming::Client] underlying streaming client from twitter gem
|
2014-11-19 10:07:29 +11:00
|
|
|
|
def stream
|
|
|
|
|
@stream ||= Twitter::Streaming::Client.new do |config|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
config.consumer_key = @consumer_key
|
|
|
|
|
config.consumer_secret = @consumer_secret
|
|
|
|
|
config.access_token = @access_token
|
|
|
|
|
config.access_token_secret = @access_token_secret
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
2014-11-15 03:55:32 +11:00
|
|
|
|
end
|
2013-11-08 06:02:05 +11:00
|
|
|
|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
# Calculate some meta information about a tweet relevant for replying
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @param ev [Twitter::Tweet]
|
|
|
|
|
# @return [Ebooks::TweetMeta]
|
2014-12-05 22:57:32 +11:00
|
|
|
|
def meta(ev)
|
2014-12-04 05:17:09 +11:00
|
|
|
|
TweetMeta.new(self, ev)
|
2014-11-14 22:45:54 +11:00
|
|
|
|
end
|
2013-12-12 13:28:29 +00:00
|
|
|
|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
# Receive an event from the twitter stream
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @param ev [Object] Twitter streaming event
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def receive_event(ev)
|
2015-01-15 02:46:09 -08:00
|
|
|
|
case ev
|
|
|
|
|
when Array # Initial array sent on first connection
|
2013-11-08 06:02:05 +11:00
|
|
|
|
log "Online!"
|
2015-01-17 21:16:57 -08:00
|
|
|
|
fire(:connect, ev)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
return
|
2015-01-15 02:46:09 -08:00
|
|
|
|
when Twitter::DirectMessage
|
2015-01-15 03:07:42 -08:00
|
|
|
|
return if ev.sender.id == @user.id # Don't reply to self
|
2014-11-15 03:55:32 +11:00
|
|
|
|
log "DM from @#{ev.sender.screen_name}: #{ev.text}"
|
2014-12-08 04:27:41 -08:00
|
|
|
|
fire(:message, ev)
|
2015-01-15 02:46:09 -08:00
|
|
|
|
when Twitter::Tweet
|
2014-11-15 03:55:32 +11:00
|
|
|
|
return unless ev.text # If it's not a text-containing tweet, ignore it
|
2015-01-15 03:07:42 -08:00
|
|
|
|
return if ev.user.id == @user.id # Ignore our own tweets
|
2014-11-15 03:55:32 +11:00
|
|
|
|
|
2016-01-13 01:29:33 -08:00
|
|
|
|
if ev.retweet? && ev.retweeted_tweet.user.id == @user.id
|
|
|
|
|
# Someone retweeted our tweet!
|
|
|
|
|
fire(:retweet, ev)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 22:57:32 +11:00
|
|
|
|
meta = meta(ev)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
|
2014-11-18 14:31:59 +11:00
|
|
|
|
if blacklisted?(ev.user.screen_name)
|
|
|
|
|
log "Blocking blacklisted user @#{ev.user.screen_name}"
|
|
|
|
|
@twitter.block(ev.user.screen_name)
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-19 10:07:29 +11:00
|
|
|
|
# Avoid responding to duplicate tweets
|
|
|
|
|
if @seen_tweets[ev.id]
|
2014-12-05 19:04:15 +11:00
|
|
|
|
log "Not firing event for duplicate tweet #{ev.id}"
|
2014-11-19 10:07:29 +11:00
|
|
|
|
return
|
|
|
|
|
else
|
|
|
|
|
@seen_tweets[ev.id] = true
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-04 05:17:09 +11:00
|
|
|
|
if meta.mentions_bot?
|
2014-11-15 03:55:32 +11:00
|
|
|
|
log "Mention from @#{ev.user.screen_name}: #{ev.text}"
|
2014-12-05 19:04:15 +11:00
|
|
|
|
conversation(ev).add(ev)
|
2014-12-05 22:57:32 +11:00
|
|
|
|
fire(:mention, ev)
|
2013-11-08 06:02:05 +11:00
|
|
|
|
else
|
2014-12-05 22:57:32 +11:00
|
|
|
|
fire(:timeline, ev)
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
2015-01-15 02:46:09 -08:00
|
|
|
|
when Twitter::Streaming::Event
|
|
|
|
|
case ev.name
|
|
|
|
|
when :follow
|
2015-01-15 03:07:42 -08:00
|
|
|
|
return if ev.source.id == @user.id
|
2015-01-15 02:46:09 -08:00
|
|
|
|
log "Followed by #{ev.source.screen_name}"
|
|
|
|
|
fire(:follow, ev.source)
|
|
|
|
|
when :favorite, :unfavorite
|
2015-01-15 03:07:42 -08:00
|
|
|
|
return if ev.source.id == @user.id # Ignore our own favorites
|
2015-01-15 02:46:09 -08:00
|
|
|
|
log "@#{ev.source.screen_name} #{ev.name.to_s}d: #{ev.target_object.text}"
|
|
|
|
|
fire(ev.name, ev.source, ev.target_object)
|
|
|
|
|
when :user_update
|
2015-01-15 02:49:16 -08:00
|
|
|
|
update_myself ev.source
|
2015-01-15 02:46:09 -08:00
|
|
|
|
end
|
|
|
|
|
when Twitter::Streaming::DeletedTweet
|
|
|
|
|
# Pass
|
2014-11-15 03:55:32 +11:00
|
|
|
|
else
|
|
|
|
|
log ev
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-16 21:36:26 -08:00
|
|
|
|
# Updates @user and calls on_user_update.
|
2015-05-24 01:30:31 -07:00
|
|
|
|
def update_myself(new_me=twitter.user)
|
2015-01-16 22:13:20 -08:00
|
|
|
|
@user = new_me if @user.nil? || new_me.id == @user.id
|
2015-05-24 01:30:31 -07:00
|
|
|
|
@username = @user.screen_name
|
2015-01-18 14:59:13 -08:00
|
|
|
|
log 'User information updated'
|
2015-01-15 02:34:45 -08:00
|
|
|
|
fire(:user_update)
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Configures client and fires startup event
|
2014-11-18 13:24:59 +11:00
|
|
|
|
def prepare
|
2014-11-15 03:55:32 +11:00
|
|
|
|
# Sanity check
|
|
|
|
|
if @username.nil?
|
2014-12-05 22:57:32 +11:00
|
|
|
|
raise ConfigurationError, "bot username cannot be nil"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if @consumer_key.nil? || @consumer_key.empty? ||
|
|
|
|
|
@consumer_secret.nil? || @consumer_key.empty?
|
|
|
|
|
log "Missing consumer_key or consumer_secret. These details can be acquired by registering a Twitter app at https://apps.twitter.com/"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if @access_token.nil? || @access_token.empty? ||
|
|
|
|
|
@access_token_secret.nil? || @access_token_secret.empty?
|
|
|
|
|
log "Missing access_token or access_token_secret. Please run `ebooks auth`."
|
|
|
|
|
exit 1
|
2014-11-14 22:45:54 +11:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-15 02:34:45 -08:00
|
|
|
|
# Save old name
|
|
|
|
|
old_name = username
|
|
|
|
|
# Load user object and actual username
|
2015-01-15 02:49:16 -08:00
|
|
|
|
update_myself
|
2015-01-16 21:36:26 -08:00
|
|
|
|
# Warn about mismatches unless it was clearly intentional
|
2015-01-15 02:34:45 -08:00
|
|
|
|
log "warning: bot expected to be @#{old_name} but connected to @#{username}" unless username == old_name || old_name.empty?
|
2014-12-05 23:50:07 +11:00
|
|
|
|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
fire(:startup)
|
2014-11-18 13:24:59 +11:00
|
|
|
|
end
|
2014-11-15 03:55:32 +11:00
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Start running user event stream
|
2014-11-18 13:24:59 +11:00
|
|
|
|
def start
|
2014-12-05 21:12:39 +11:00
|
|
|
|
log "starting tweet stream"
|
|
|
|
|
|
|
|
|
|
stream.user do |ev|
|
|
|
|
|
receive_event ev
|
|
|
|
|
end
|
2014-11-14 22:45:54 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-11-15 03:55:32 +11:00
|
|
|
|
# Fire an event
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @param event [Symbol] event to fire
|
|
|
|
|
# @param args arguments for event handler
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def fire(event, *args)
|
|
|
|
|
handler = "on_#{event}".to_sym
|
|
|
|
|
if respond_to? handler
|
|
|
|
|
self.send(handler, *args)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Delay an action for a variable period of time
|
|
|
|
|
# @param range [Range, Integer] range of seconds to choose for delay
|
|
|
|
|
def delay(range=@delay_range, &b)
|
2016-03-02 01:31:55 +00:00
|
|
|
|
time = rand(range) unless range.is_a? Integer
|
2014-11-18 14:31:59 +11:00
|
|
|
|
sleep time
|
2014-12-05 21:12:39 +11:00
|
|
|
|
b.call
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Check if a username is blacklisted
|
|
|
|
|
# @param username [String]
|
|
|
|
|
# @return [Boolean]
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def blacklisted?(username)
|
2014-12-05 23:50:07 +11:00
|
|
|
|
if @blacklist.map(&:downcase).include?(username.downcase)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-08 06:02:05 +11:00
|
|
|
|
# Reply to a tweet or a DM.
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# @param ev [Twitter::Tweet, Twitter::DirectMessage]
|
|
|
|
|
# @param text [String] contents of reply excluding reply_prefix
|
|
|
|
|
# @param opts [Hash] additional params to pass to twitter gem
|
2013-11-08 06:02:05 +11:00
|
|
|
|
def reply(ev, text, opts={})
|
|
|
|
|
opts = opts.clone
|
|
|
|
|
|
|
|
|
|
if ev.is_a? Twitter::DirectMessage
|
2014-11-14 22:45:54 +11:00
|
|
|
|
log "Sending DM to @#{ev.sender.screen_name}: #{text}"
|
2014-11-19 10:07:29 +11:00
|
|
|
|
twitter.create_direct_message(ev.sender.screen_name, text, opts)
|
2013-11-08 06:02:05 +11:00
|
|
|
|
elsif ev.is_a? Twitter::Tweet
|
2014-12-05 22:57:32 +11:00
|
|
|
|
meta = meta(ev)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
|
2014-12-05 19:04:15 +11:00
|
|
|
|
if conversation(ev).is_bot?(ev.user.screen_name)
|
2014-11-18 12:00:34 +11:00
|
|
|
|
log "Not replying to suspected bot @#{ev.user.screen_name}"
|
2014-12-05 21:12:39 +11:00
|
|
|
|
return false
|
2014-11-18 12:00:34 +11:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-15 03:10:15 -08:00
|
|
|
|
text = meta.reply_prefix + text unless text.match(/@#{Regexp.escape ev.user.screen_name}/i)
|
2014-12-14 02:48:45 -08:00
|
|
|
|
log "Replying to @#{ev.user.screen_name} with: #{text}"
|
2014-12-11 20:13:53 -08:00
|
|
|
|
tweet = twitter.update(text, opts.merge(in_reply_to_status_id: ev.id))
|
2014-12-05 19:04:15 +11:00
|
|
|
|
conversation(tweet).add(tweet)
|
2014-12-05 21:12:39 +11:00
|
|
|
|
tweet
|
2013-11-08 06:02:05 +11:00
|
|
|
|
else
|
|
|
|
|
raise Exception("Don't know how to reply to a #{ev.class}")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Favorite a tweet
|
|
|
|
|
# @param tweet [Twitter::Tweet]
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def favorite(tweet)
|
|
|
|
|
log "Favoriting @#{tweet.user.screen_name}: #{tweet.text}"
|
|
|
|
|
|
|
|
|
|
begin
|
2014-11-19 10:07:29 +11:00
|
|
|
|
twitter.favorite(tweet.id)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
rescue Twitter::Error::Forbidden
|
|
|
|
|
log "Already favorited: #{tweet.user.screen_name}: #{tweet.text}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Retweet a tweet
|
|
|
|
|
# @param tweet [Twitter::Tweet]
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def retweet(tweet)
|
|
|
|
|
log "Retweeting @#{tweet.user.screen_name}: #{tweet.text}"
|
|
|
|
|
|
|
|
|
|
begin
|
2014-11-19 10:07:29 +11:00
|
|
|
|
twitter.retweet(tweet.id)
|
2014-11-15 03:55:32 +11:00
|
|
|
|
rescue Twitter::Error::Forbidden
|
|
|
|
|
log "Already retweeted: #{tweet.user.screen_name}: #{tweet.text}"
|
|
|
|
|
end
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Follow a user
|
|
|
|
|
# @param user [String] username or user id
|
|
|
|
|
def follow(user, *args)
|
|
|
|
|
log "Following #{user}"
|
|
|
|
|
twitter.follow(user, *args)
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Unfollow a user
|
|
|
|
|
# @param user [String] username or user id
|
|
|
|
|
def unfollow(user, *args)
|
|
|
|
|
log "Unfollowing #{user}"
|
2015-01-02 17:22:39 -05:00
|
|
|
|
twitter.unfollow(user, *args)
|
2014-12-04 05:17:09 +11:00
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Tweet something
|
|
|
|
|
# @param text [String]
|
|
|
|
|
def tweet(text, *args)
|
|
|
|
|
log "Tweeting '#{text}'"
|
|
|
|
|
twitter.update(text, *args)
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
2014-10-18 22:21:50 -07:00
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Get a scheduler for this bot
|
|
|
|
|
# @return [Rufus::Scheduler]
|
2014-11-15 03:55:32 +11:00
|
|
|
|
def scheduler
|
|
|
|
|
@scheduler ||= Rufus::Scheduler.new
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 21:12:39 +11:00
|
|
|
|
# Tweet some text with an image
|
|
|
|
|
# @param txt [String]
|
|
|
|
|
# @param pic [String] filename
|
2014-09-23 20:26:48 -04:00
|
|
|
|
def pictweet(txt, pic, *args)
|
|
|
|
|
log "Tweeting #{txt.inspect} - #{pic} #{args}"
|
2014-11-19 10:07:29 +11:00
|
|
|
|
twitter.update_with_media(txt, File.new(pic), *args)
|
2014-09-23 20:26:48 -04:00
|
|
|
|
end
|
2013-11-08 06:02:05 +11:00
|
|
|
|
end
|
|
|
|
|
end
|