Class: Twibot::Bot
Overview
Main bot “controller” class
Instance Attribute Summary collapse
-
#prompt ⇒ Object
writeonly
Sets the attribute prompt.
-
#twitter ⇒ Object
readonly
Returns the value of attribute twitter.
Instance Method Summary collapse
-
#configure {|@config| ... } ⇒ Object
Configure bot.
-
#dispatch_messages(type, messages, labels) ⇒ Object
Dispatch a collection of messages.
-
#initialize(options = nil, prompt = false) ⇒ Bot
constructor
A new instance of Bot.
-
#log ⇒ Object
Return logger instance.
-
#poll ⇒ Object
Poll Twitter API in a loop and pass on messages and tweets when they appear.
- #processed ⇒ Object
- #prompt? ⇒ Boolean
-
#receive_messages ⇒ Object
Receive direct messages.
-
#receive_replies ⇒ Object
Receive tweets that start with @<login>.
-
#receive_tweets ⇒ Object
Receive tweets.
-
#run! ⇒ Object
Run application.
Methods included from Handlers
#add_handler, #dispatch, #handlers, #handlers=
Constructor Details
#initialize(options = nil, prompt = false) ⇒ Bot
Returns a new instance of Bot.
14 15 16 17 18 19 20 21 22 |
# File 'lib/twibot/bot.rb', line 14 def initialize( = nil, prompt = false) @prompt = prompt @conf = nil @config = || Twibot::Config.default << Twibot::FileConfig.new << Twibot::CliConfig.new @log = nil @abort = false rescue Exception => krash raise SystemExit.new(krash.) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Map configuration settings
178 179 180 181 182 183 |
# File 'lib/twibot/bot.rb', line 178 def method_missing(name, *args, &block) return super unless config.key?(name) self.class.send(:define_method, name) { config[name] } config[name] end |
Instance Attribute Details
#prompt=(value) ⇒ Object (writeonly)
Sets the attribute prompt
12 13 14 |
# File 'lib/twibot/bot.rb', line 12 def prompt=(value) @prompt = value end |
#twitter ⇒ Object (readonly)
Returns the value of attribute twitter.
11 12 13 |
# File 'lib/twibot/bot.rb', line 11 def twitter @twitter end |
Instance Method Details
#configure {|@config| ... } ⇒ Object
Configure bot
168 169 170 171 172 |
# File 'lib/twibot/bot.rb', line 168 def configure yield @config @conf = nil @twitter = nil end |
#dispatch_messages(type, messages, labels) ⇒ Object
Dispatch a collection of messages
144 145 146 147 148 149 150 151 152 |
# File 'lib/twibot/bot.rb', line 144 def (type, , labels) .each { || dispatch(type, ) } # Avoid picking up messages over again processed[type] = .first.id if .length > 0 num = .length log.info "#{config[:host]}: Received #{num} #{num == 1 ? labels[0] : labels[1]}" num end |
#log ⇒ Object
Return logger instance
157 158 159 160 161 162 163 |
# File 'lib/twibot/bot.rb', line 157 def log return @log if @log os = config[:log_file] ? File.open(config[:log_file], "a") : $stdout @log = Logger.new(os) @log.level = Logger.const_get(config[:log_level] ? config[:log_level].upcase : "INFO") @log end |
#poll ⇒ Object
Poll Twitter API in a loop and pass on messages and tweets when they appear
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twibot/bot.rb', line 81 def poll max = max_interval step = interval_step interval = min_interval while !@abort do = 0 += || 0 += receive_replies || 0 += receive_tweets || 0 interval = > 0 ? min_interval : [interval + step, max].min log.debug "#{config[:host]} sleeping for #{interval}s" sleep interval end end |
#processed ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/twibot/bot.rb', line 28 def processed @processed ||= { :message => nil, :reply => nil, :tweet => nil } end |
#prompt? ⇒ Boolean
24 25 26 |
# File 'lib/twibot/bot.rb', line 24 def prompt? @prompt end |
#receive_messages ⇒ Object
Receive direct messages
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/twibot/bot.rb', line 102 def type = :message return false unless handlers[type].length > 0 = {} [:since_id] = processed[type] if processed[type] sandbox(0) do (type, twitter.(:received, ), %w{message messages}) end end |
#receive_replies ⇒ Object
Receive tweets that start with @<login>
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/twibot/bot.rb', line 130 def receive_replies type = :reply return false unless handlers[type].length > 0 = {} [:since_id] = processed[type] if processed[type] sandbox(0) do (type, twitter.status(:replies, ), %w{reply replies}) end end |
#receive_tweets ⇒ Object
Receive tweets
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/twibot/bot.rb', line 116 def receive_tweets type = :tweet return false unless handlers[type].length > 0 = {} [:since_id] = processed[type] if processed[type] sandbox(0) do (type, twitter.timeline_for(config.to_hash[:timeline_for] || :public, ), %w{tweet tweets}) end end |
#run! ⇒ Object
Run application
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 |
# File 'lib/twibot/bot.rb', line 45 def run! puts "Twibot #{Twibot::VERSION} imposing as @#{login} on #{config[:host]}" trap(:INT) do puts "\nAnd it's a wrap. See ya soon!" exit end case config[:process] when :all, nil # do nothing so it will fetch ALL when :new # Make sure we don't process messages and tweets received prior to bot launch = twitter.(:received, { :count => 1 }) processed[:message] = .first.id if .length > 0 handle_tweets = !handlers.nil? && handlers[:tweet].length + handlers[:reply].length > 0 tweets = [] sandbox do tweets = handle_tweets ? twitter.timeline_for(config[:timeline_for], { :count => 1 }) : [] end processed[:tweet] = tweets.first.id if tweets.length > 0 processed[:reply] = tweets.first.id if tweets.length > 0 when Numeric, /\d+/ # a tweet ID to start from processed[:tweet] = processed[:reply] = processed[:message] = config[:process] else abort "Unknown process option #{config[:process]}, aborting..." end poll end |