Class: Ebooks::Bot
- Inherits:
-
Object
- Object
- Ebooks::Bot
- Defined in:
- lib/twitter_ebooks/bot.rb
Constant Summary collapse
- @@all =
List of all defined bots
[]
Instance Attribute Summary collapse
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#oauth_token ⇒ Object
Returns the value of attribute oauth_token.
-
#oauth_token_secret ⇒ Object
Returns the value of attribute oauth_token_secret.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#twitter ⇒ Object
readonly
Returns the value of attribute twitter.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
-
#delay(time, &b) ⇒ Object
Wrapper for EM.add_timer Delays add a greater sense of humanity to bot behaviour.
- #follow(*args) ⇒ Object
-
#initialize(username, &b) ⇒ Bot
constructor
A new instance of Bot.
- #log(*args) ⇒ Object
- #on_follow(&b) ⇒ Object
- #on_mention(&b) ⇒ Object
- #on_message(&b) ⇒ Object
- #on_timeline(&b) ⇒ Object
-
#reply(ev, text, opts = {}) ⇒ Object
Reply to a tweet or a DM.
- #scheduler ⇒ Object
-
#start ⇒ Object
Connects to tweetstream and opens event handlers for this bot.
- #tweet(*args) ⇒ Object
Constructor Details
Instance Attribute Details
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
8 9 10 |
# File 'lib/twitter_ebooks/bot.rb', line 8 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
8 9 10 |
# File 'lib/twitter_ebooks/bot.rb', line 8 def consumer_secret @consumer_secret end |
#oauth_token ⇒ Object
Returns the value of attribute oauth_token.
8 9 10 |
# File 'lib/twitter_ebooks/bot.rb', line 8 def oauth_token @oauth_token end |
#oauth_token_secret ⇒ Object
Returns the value of attribute oauth_token_secret.
8 9 10 |
# File 'lib/twitter_ebooks/bot.rb', line 8 def oauth_token_secret @oauth_token_secret end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
13 14 15 |
# File 'lib/twitter_ebooks/bot.rb', line 13 def stream @stream end |
#twitter ⇒ Object (readonly)
Returns the value of attribute twitter.
13 14 15 |
# File 'lib/twitter_ebooks/bot.rb', line 13 def twitter @twitter end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/twitter_ebooks/bot.rb', line 11 def username @username end |
Class Method Details
.all ⇒ Object
16 |
# File 'lib/twitter_ebooks/bot.rb', line 16 def self.all; @@all; end |
Instance Method Details
#delay(time, &b) ⇒ Object
Wrapper for EM.add_timer Delays add a greater sense of humanity to bot behaviour
105 106 107 108 |
# File 'lib/twitter_ebooks/bot.rb', line 105 def delay(time, &b) time = time.to_a.sample unless time.is_a? Integer EM.add_timer(time, &b) end |
#follow(*args) ⇒ Object
131 132 133 134 |
# File 'lib/twitter_ebooks/bot.rb', line 131 def follow(*args) log "Following #{args}" @twitter.follow(*args) end |
#log(*args) ⇒ Object
28 29 30 31 |
# File 'lib/twitter_ebooks/bot.rb', line 28 def log(*args) STDERR.puts "@#{@username}: " + args.map(&:to_s).join(' ') STDERR.flush end |
#on_follow(&b) ⇒ Object
141 |
# File 'lib/twitter_ebooks/bot.rb', line 141 def on_follow(&b); @on_follow = b; end |
#on_mention(&b) ⇒ Object
142 |
# File 'lib/twitter_ebooks/bot.rb', line 142 def on_mention(&b); @on_mention = b; end |
#on_message(&b) ⇒ Object
144 |
# File 'lib/twitter_ebooks/bot.rb', line 144 def (&b); @on_message = b; end |
#on_timeline(&b) ⇒ Object
143 |
# File 'lib/twitter_ebooks/bot.rb', line 143 def on_timeline(&b); @on_timeline = b; end |
#reply(ev, text, opts = {}) ⇒ Object
Reply to a tweet or a DM. Applies configurable @reply_delay range
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/twitter_ebooks/bot.rb', line 112 def reply(ev, text, opts={}) opts = opts.clone delay = @reply_delay.to_a.sample if ev.is_a? Twitter::DirectMessage log "Sending DM to @#{ev[:sender][:screen_name]}: #{text}" @twitter.(ev[:sender][:screen_name], text, opts) elsif ev.is_a? Twitter::Tweet log "Replying to @#{ev[:user][:screen_name]}: #{text}" @twitter.update(text, in_reply_to_status_id: ev[:id]) else raise Exception("Don't know how to reply to a #{ev.class}") end end |
#scheduler ⇒ Object
127 128 129 |
# File 'lib/twitter_ebooks/bot.rb', line 127 def scheduler @scheduler ||= Rufus::Scheduler.new end |
#start ⇒ Object
Connects to tweetstream and opens event handlers for this bot
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/twitter_ebooks/bot.rb', line 34 def start TweetStream.configure do |config| config.consumer_key = @consumer_key config.consumer_secret = @consumer_secret config.oauth_token = @oauth_token config.oauth_token_secret = @oauth_token_secret end Twitter.configure do |config| config.consumer_key = @consumer_key config.consumer_secret = @consumer_secret config.oauth_token = @oauth_token config.oauth_token_secret = @oauth_token_secret end @twitter = Twitter::Client.new @stream = TweetStream::Client.new @stream.on_error do |msg| log "ERROR: #{msg}" end @stream.on_inited do log "Online!" end @stream.on_event(:follow) do |event| next if event[:source][:screen_name] == @username log "Followed by #{event[:source][:screen_name]}" @on_follow.call(event[:source]) end @stream. do |dm| next if dm[:sender][:screen_name] == @username # Don't reply to self log "DM from @#{dm[:sender][:screen_name]}: #{dm[:text]}" @on_message.call(dm) end @stream.userstream do |ev| 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 = {} 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_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' ' mless = ev[:text] ev.attrs[:entities][:user_mentions].reverse.each do |entity| mless = mless[0...entity[:indices][0]] + mless[entity[:indices][1]+1..-1] end [: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, ) else @on_timeline.call(ev, ) end end end |
#tweet(*args) ⇒ Object
136 137 138 139 |
# File 'lib/twitter_ebooks/bot.rb', line 136 def tweet(*args) log "Tweeting #{args.inspect}" @twitter.update(*args) end |