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
23
24
25
26
27
28
29
# 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
  @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.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



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

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)


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 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 "Received #{num} #{num == 1 ? labels[0] : labels[1]}"
  num
end

#logObject

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

#pollObject

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
    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 "Sleeping for #{interval}s"
    sleep interval
  end
end

#prompt?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/twibot/bot.rb', line 31

def prompt?
  @prompt
end

#receive_messagesObject

Receive direct messages



82
83
84
85
86
87
88
# File 'lib/twibot/bot.rb', line 82

def receive_messages
  type = :message
  return false unless handlers[type].length > 0
  options = {}
  options[:since_id] = @processed[type] if @processed[type]
  dispatch_messages(type, @twitter.messages(:received, options), %w{message messages})
end

#receive_repliesObject

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
  options = {}
  options[:since_id] = @processed[type] if @processed[type]
  num = dispatch_messages(type, @twitter.status(:replies, options), %w{reply replies})
  num
end

#receive_tweetsObject

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
  options = {}
  options[:since_id] = @processed[type] if @processed[type]
  dispatch_messages(type, @twitter.timeline_for(:me, options), %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 @#{}"

  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
  messages = @twitter.messages(:received, { :count => 1 })
  @processed[:message] = messages.first.id if messages.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