Class: JabberBot

Inherits:
Jabber::Client
  • Object
show all
Includes:
Jabber
Defined in:
lib/jabber_bot.rb

Instance Method Summary collapse

Constructor Details

#initialize(jabber_id, jabber_password, options = {}) ⇒ JabberBot

Returns a new instance of JabberBot.



6
7
8
9
10
11
12
13
# File 'lib/jabber_bot.rb', line 6

def initialize(jabber_id, jabber_password, options = {})
  @jabber_id       = jabber_id
  @jabber_password = jabber_password
  @options         = options

  jid = JID::new(@jabber_id)
  super(jid)
end

Instance Method Details

#connect_and_authenticateObject



19
20
21
22
23
# File 'lib/jabber_bot.rb', line 19

def connect_and_authenticate
  connect
  auth(@jabber_password)
  send(Presence::new)
end

#on_message(from, body) ⇒ Object



15
16
17
# File 'lib/jabber_bot.rb', line 15

def on_message(from, body)
  raise "on_message not implemented!"
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jabber_bot.rb', line 25

def run
  main = Thread.current
  add_message_callback { |message|
    next if message.type == :error or message.body.blank?
    # support exit command if owner is set
    main.wakeup if @options[:owner_jabber_id] == message.from && message.body == 'exit'
    # simple callback
    self.on_message(message.from, message.body, message.first_element('entry'))
  }
  Thread.stop
  close
end

#say(jabber_id, body) ⇒ Object



38
39
40
# File 'lib/jabber_bot.rb', line 38

def say(jabber_id, body)
  send(Message::new(jabber_id, body).set_type(:chat).set_id('1'))
end