Class: Roflbot::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/roflbot/base.rb

Direct Known Subclasses

SentenceBot

Defined Under Namespace

Classes: Expectation

Constant Summary collapse

TWITTER =
{ :token => "7rfuoIhSGN7OQVvDLzJrcg", :secret => "I2o4SmRWVV4Yo7XhGgLrKGJq1KgpC8VrluyA9LLuH0" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roflbot/base.rb', line 34

def initialize(options = {})
  @options = options
  @expectations = []
  @threads = []
  accounts = @options["accounts"]

  @aim = Net::TOC.new(*accounts["AIM"].values_at("username", "password"))
  @aim.on_im { |m, b, a| on_im(m, b, a) }

  if accounts['Twitter']
    oauth = Twitter::OAuth.new(TWITTER[:token], TWITTER[:secret])
    oauth.authorize_from_access(*accounts["Twitter"].values_at("token", "secret"))
    @twitter = Twitter::Base.new(oauth)
    @since_id = accounts['Twitter']['since_id']
  end

  if accounts['Google Voice']
    @gvoice = GvoiceRuby::Client.new(accounts['Google Voice'])
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



33
34
35
# File 'lib/roflbot/base.rb', line 33

def options
  @options
end

Instance Method Details

#expects(regexp) ⇒ Object



74
75
76
77
78
# File 'lib/roflbot/base.rb', line 74

def expects(regexp)
  exp = Expectation.new(regexp)
  @expectations << exp
  exp
end

#on_im(message, buddy, auto_response) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/roflbot/base.rb', line 80

def on_im(message, buddy, auto_response)
  @expectations.each do |expectation|
    next  if !expectation.matches?(message)
    buddy.send_im(expectation.response)
    break
  end
end

#respond_via_smsObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/roflbot/base.rb', line 111

def respond_via_sms
  @gvoice.check
  return  if !@gvoice.any_unread?

  @gvoice.smss.each do |sms|
    next  if !sms.labels.include?("unread")
    @expectations.each do |expectation|
      next  if !expectation.matches?(sms.text)
      @gvoice.send_sms(:phone_number => sms.from, :text => expectation.response)
    end
  end
end

#respond_via_twitterObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/roflbot/base.rb', line 88

def respond_via_twitter
  begin
    mentions = @twitter.mentions(@since_id ? {:since_id => @since_id} : {})
  rescue StandardError
    mentions = []
  end
  if !mentions.empty?
    @since_id = mentions[-1].id
    @options['accounts']['Twitter']['since_id'] = @since_id
  end

  mentions.each do |tweet|
    @expectations.each do |expectation|
      # throw away beginning mention
      message = tweet.text.sub(/^@[^\s]+\s+/, "")
      next  if !expectation.matches?(message)

      @twitter.update("@#{tweet.user.screen_name} #{expectation.response}", :in_reply_to_status_id => tweet.id)
      break
    end
  end
end

#startObject



55
56
57
58
59
60
61
62
63
# File 'lib/roflbot/base.rb', line 55

def start
  @aim.connect
  if @twitter
    @threads << Thread.new { loop { respond_via_twitter; sleep 60 } }
  end
  if @gvoice
    @threads << Thread.new { loop { respond_via_sms; sleep 60 } }
  end
end

#stopObject



69
70
71
72
# File 'lib/roflbot/base.rb', line 69

def stop
  @aim.disconnect
  @threads.each { |t| t.kill }
end

#waitObject



65
66
67
# File 'lib/roflbot/base.rb', line 65

def wait
  @aim.wait
end