Class: Twibot::Bot

Inherits:
Object
  • Object
show all
Includes:
Handlers
Defined in:
lib/twibot/bot.rb

Overview

Main bot “controller” class

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = nil, prompt = false)
  @prompt = prompt
  @conf = nil
  @config = options || Twibot::Config.default << Twibot::FileConfig.new << Twibot::CliConfig.new
  @log = nil
  @abort = false
rescue Exception => krash
  raise SystemExit.new(krash.message)
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

Parameters:

  • value

    the value to set the attribute prompt to.



12
13
14
# File 'lib/twibot/bot.rb', line 12

def prompt=(value)
  @prompt = value
end

#twitterObject (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

Yields:

  • (@config)


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 dispatch_messages(type, messages, labels)
  messages.each { |message| dispatch(type, message) }
  # Avoid picking up messages over again
  processed[type] = messages.first.id if messages.length > 0

  num = messages.length
  log.info "#{config[:host]}: Received #{num} #{num == 1 ? labels[0] : labels[1]}"
  num
end

#logObject

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

#pollObject

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
    message_count = 0
    message_count += receive_messages || 0
    message_count += receive_replies || 0
    message_count += receive_tweets || 0

    interval = message_count > 0 ? min_interval : [interval + step, max].min

    log.debug "#{config[:host]} sleeping for #{interval}s"
    sleep interval
  end
end

#processedObject



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

Returns:

  • (Boolean)


24
25
26
# File 'lib/twibot/bot.rb', line 24

def prompt?
  @prompt
end

#receive_messagesObject

Receive direct messages



102
103
104
105
106
107
108
109
110
111
# File 'lib/twibot/bot.rb', line 102

def receive_messages
  type = :message
  return false unless handlers[type].length > 0
  options = {}
  options[:since_id] = processed[type] if processed[type]

  sandbox(0) do
    dispatch_messages(type, twitter.messages(:received, options), %w{message messages})
  end
end

#receive_repliesObject

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
  options = {}
  options[:since_id] = processed[type] if processed[type]

  sandbox(0) do
    dispatch_messages(type, twitter.status(:replies, options), %w{reply replies})
  end
end

#receive_tweetsObject

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
  options = {}
  options[:since_id] = processed[type] if processed[type]

  sandbox(0) do
    dispatch_messages(type, twitter.timeline_for(config.to_hash[:timeline_for] || :public, options), %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 @#{} 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
    messages = twitter.messages(:received, { :count => 1 })
    processed[:message] = messages.first.id if messages.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