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.
- #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 23 24 25 26 27 28 29 |
# 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 @twitter = Twitter::Client.new :login => config[:login], :password => config[:password] @log = nil @abort = false @processed = { :message => nil, :reply => nil, :tweet => nil } 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
149 150 151 152 153 154 |
# File 'lib/twibot/bot.rb', line 149 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
140 141 142 143 |
# File 'lib/twibot/bot.rb', line 140 def configure yield @config @conf = nil end |
#dispatch_messages(type, messages, labels) ⇒ Object
Dispatch a collection of messages
116 117 118 119 120 121 122 123 124 |
# File 'lib/twibot/bot.rb', line 116 def (type, , labels) .each { || dispatch(type, ) } # Avoid picking up messages over again @processed[type] = .first.id if .length > 0 num = .length log.info "Received #{num} #{num == 1 ? labels[0] : labels[1]}" num end |
#log ⇒ Object
Return logger instance
129 130 131 132 133 134 135 |
# File 'lib/twibot/bot.rb', line 129 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/twibot/bot.rb', line 61 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 "Sleeping for #{interval}s" sleep interval end end |
#prompt? ⇒ Boolean
31 32 33 |
# File 'lib/twibot/bot.rb', line 31 def prompt? @prompt end |
#receive_messages ⇒ Object
Receive direct messages
82 83 84 85 86 87 88 |
# File 'lib/twibot/bot.rb', line 82 def type = :message return false unless handlers[type].length > 0 = {} [:since_id] = @processed[type] if @processed[type] (type, @twitter.(:received, ), %w{message messages}) end |
#receive_replies ⇒ Object
Receive tweets that start with @<login>
104 105 106 107 108 109 110 111 |
# File 'lib/twibot/bot.rb', line 104 def receive_replies type = :reply return false unless handlers[type].length > 0 = {} [:since_id] = @processed[type] if @processed[type] num = (type, @twitter.status(:replies, ), %w{reply replies}) num end |
#receive_tweets ⇒ Object
Receive tweets
93 94 95 96 97 98 99 |
# File 'lib/twibot/bot.rb', line 93 def receive_tweets type = :tweet return false unless handlers[type].length > 0 = {} [:since_id] = @processed[type] if @processed[type] (type, @twitter.timeline_for(:me, ), %w{tweet tweets}) end |
#run! ⇒ Object
Run application
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twibot/bot.rb', line 38 def run! puts "Twibot #{Twibot::VERSION} imposing as @#{login}" trap(:INT) do puts "\nAnd it's a wrap. See ya soon!" exit end # 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[:tweet].length + @handlers[:reply].length > 0 tweets = handle_tweets ? @twitter.timeline_for(:me, { :count => 1 }) : [] @processed[:tweet] = tweets.first.id if tweets.length > 0 @processed[:reply] = tweets.first.id if tweets.length > 0 poll end |