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
- #configure ⇒ Object
-
#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_startup(&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
#initialize(username, &b) ⇒ Bot
Returns a new instance of Bot.
22 23 24 25 26 27 28 29 30 |
# File 'lib/twitter_ebooks/bot.rb', line 22 def initialize(username, &b) # Set defaults @username = username # Override with callback b.call(self) Bot.all.push(self) end |
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 |
.get(name) ⇒ Object
18 19 20 |
# File 'lib/twitter_ebooks/bot.rb', line 18 def self.get(name) all.find { |bot| bot.username == name } end |
Instance Method Details
#configure ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twitter_ebooks/bot.rb', line 37 def configure 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 needs_stream = [@on_follow, , @on_mention, @on_timeline].any? {|e| !e.nil?} @twitter = Twitter::Client.new @stream = TweetStream::Client.new if needs_stream end |
#delay(time, &b) ⇒ Object
Wrapper for EM.add_timer Delays add a greater sense of humanity to bot behaviour
130 131 132 133 |
# File 'lib/twitter_ebooks/bot.rb', line 130 def delay(time, &b) time = time.to_a.sample unless time.is_a? Integer EM.add_timer(time, &b) end |
#follow(*args) ⇒ Object
156 157 158 159 |
# File 'lib/twitter_ebooks/bot.rb', line 156 def follow(*args) log "Following #{args}" @twitter.follow(*args) end |
#log(*args) ⇒ Object
32 33 34 35 |
# File 'lib/twitter_ebooks/bot.rb', line 32 def log(*args) STDOUT.puts "@#{@username}: " + args.map(&:to_s).join(' ') STDOUT.flush end |
#on_follow(&b) ⇒ Object
167 |
# File 'lib/twitter_ebooks/bot.rb', line 167 def on_follow(&b); @on_follow = b; end |
#on_mention(&b) ⇒ Object
168 |
# File 'lib/twitter_ebooks/bot.rb', line 168 def on_mention(&b); @on_mention = b; end |
#on_message(&b) ⇒ Object
170 |
# File 'lib/twitter_ebooks/bot.rb', line 170 def (&b); = b; end |
#on_startup(&b) ⇒ Object
166 |
# File 'lib/twitter_ebooks/bot.rb', line 166 def on_startup(&b); @on_startup = b; end |
#on_timeline(&b) ⇒ Object
169 |
# File 'lib/twitter_ebooks/bot.rb', line 169 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/twitter_ebooks/bot.rb', line 137 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]} with: #{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
152 153 154 |
# File 'lib/twitter_ebooks/bot.rb', line 152 def scheduler @scheduler ||= Rufus::Scheduler.new end |
#start ⇒ Object
Connects to tweetstream and opens event handlers for this bot
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/twitter_ebooks/bot.rb', line 59 def start configure @on_startup.call if @on_startup if not @stream log "not bothering with stream for #@username" return end log "starting stream for #@username" @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]) if @on_follow 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]}" .call(dm) if 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_mentions [:reply_prefix] = reply_mentions.uniq.map { |m| '@'+m }.join(' ') + ' ' [:limit] = 140 - [:reply_prefix].length mless = ev[:text] begin ev.attrs[:entities][:user_mentions].reverse.each do |entity| mless = mless[0...entity[:indices][0]] + mless[entity[:indices][1]...-1] end rescue Exception p ev.attrs[:entities][:user_mentions] p ev[:text] raise 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, ) if @on_mention else @on_timeline.call(ev, ) if @on_timeline end end end |
#tweet(*args) ⇒ Object
161 162 163 164 |
# File 'lib/twitter_ebooks/bot.rb', line 161 def tweet(*args) log "Tweeting #{args.inspect}" @twitter.update(*args) end |