Class: Dat::ChatBot

Inherits:
Object
  • Object
show all
Defined in:
lib/dat/bots/chatbot.rb

Constant Summary collapse

DATBOT_INFO =
{ :jid => '[email protected]', :password => 'robotdat' }

Instance Method Summary collapse

Constructor Details

#initialize(log_path_prefix = nil) ⇒ ChatBot

Returns a new instance of ChatBot.



11
12
13
14
# File 'lib/dat/bots/chatbot.rb', line 11

def initialize(log_path_prefix=nil)
  @logger = Logger.new(log_path_prefix)
  @interface = Interface.new(@logger)
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dat/bots/chatbot.rb', line 16

def run
  @client = Jabber::Client.new(Jabber::JID.new(DATBOT_INFO[:jid]))
  @client.connect
  @client.auth(DATBOT_INFO[:password])
  @client.send(Jabber::Presence.new)

  @client.add_message_callback do |m|
    if m.type != :error && !m.composing? && !m.body.to_s.strip.empty?
      body = m.body.to_s
      from = m.from.bare.to_s
      @logger[from].log(body)
      response = @interface.respond(from, body).to_s.strip
      if !response.empty?
        msg = Jabber::Message.new(from, response)
        msg.set_type(:chat)
        @client.send(msg)
        @logger[from].log(response)
      end
    end
  end

  Thread.stop
  @client.stop
end