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
Instance Attribute Details
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
9 10 11 |
# File 'lib/twitter_ebooks/bot.rb', line 9 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
9 10 11 |
# File 'lib/twitter_ebooks/bot.rb', line 9 def consumer_secret @consumer_secret end |
#oauth_token ⇒ Object
Returns the value of attribute oauth_token.
9 10 11 |
# File 'lib/twitter_ebooks/bot.rb', line 9 def oauth_token @oauth_token end |
#oauth_token_secret ⇒ Object
Returns the value of attribute oauth_token_secret.
9 10 11 |
# File 'lib/twitter_ebooks/bot.rb', line 9 def oauth_token_secret @oauth_token_secret end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
14 15 16 |
# File 'lib/twitter_ebooks/bot.rb', line 14 def stream @stream end |
#twitter ⇒ Object (readonly)
Returns the value of attribute twitter.
14 15 16 |
# File 'lib/twitter_ebooks/bot.rb', line 14 def twitter @twitter end |
#username ⇒ Object
Returns the value of attribute username.
12 13 14 |
# File 'lib/twitter_ebooks/bot.rb', line 12 def username @username end |
Class Method Details
.all ⇒ Object
17 |
# File 'lib/twitter_ebooks/bot.rb', line 17 def self.all; @@all; end |
.get(name) ⇒ Object
19 20 21 |
# File 'lib/twitter_ebooks/bot.rb', line 19 def self.get(name) all.find { |bot| bot.username == name } end |
Instance Method Details
#configure ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/twitter_ebooks/bot.rb', line 38 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_message, @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
132 133 134 135 |
# File 'lib/twitter_ebooks/bot.rb', line 132 def delay(time, &b) time = time.to_a.sample unless time.is_a? Integer EM.add_timer(time, &b) end |
#follow(*args) ⇒ Object
157 158 159 160 |
# File 'lib/twitter_ebooks/bot.rb', line 157 def follow(*args) log "Following #{args}" @twitter.follow(*args) end |
#log(*args) ⇒ Object
33 34 35 36 |
# File 'lib/twitter_ebooks/bot.rb', line 33 def log(*args) STDOUT.puts "@#{@username}: " + args.map(&:to_s).join(' ') STDOUT.flush end |
#on_follow(&b) ⇒ Object
168 |
# File 'lib/twitter_ebooks/bot.rb', line 168 def on_follow(&b); @on_follow = b; end |
#on_mention(&b) ⇒ Object
169 |
# File 'lib/twitter_ebooks/bot.rb', line 169 def on_mention(&b); @on_mention = b; end |
#on_message(&b) ⇒ Object
171 |
# File 'lib/twitter_ebooks/bot.rb', line 171 def (&b); @on_message = b; end |
#on_startup(&b) ⇒ Object
167 |
# File 'lib/twitter_ebooks/bot.rb', line 167 def on_startup(&b); @on_startup = b; end |
#on_timeline(&b) ⇒ Object
170 |
# File 'lib/twitter_ebooks/bot.rb', line 170 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
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/twitter_ebooks/bot.rb', line 139 def reply(ev, text, opts={}) opts = opts.clone 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
153 154 155 |
# File 'lib/twitter_ebooks/bot.rb', line 153 def scheduler @scheduler ||= Rufus::Scheduler.new end |
#start ⇒ Object
Connects to tweetstream and opens event handlers for this bot
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 127 128 |
# File 'lib/twitter_ebooks/bot.rb', line 60 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]}" @on_message.call(dm) if @on_message 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| 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 # 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
162 163 164 165 |
# File 'lib/twitter_ebooks/bot.rb', line 162 def tweet(*args) log "Tweeting #{args.inspect}" @twitter.update(*args) end |