Class: Dog::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/dog/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, config_path) ⇒ Bot

Returns a new instance of Bot.



5
6
7
8
9
10
11
# File 'lib/dog/bot.rb', line 5

def initialize(connection, config_path)
  @config_path = config_path
  @connection = connection
  @commands = []
  @rooms = []
  @memory = {}
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



3
4
5
# File 'lib/dog/bot.rb', line 3

def commands
  @commands
end

#memoryObject

Returns the value of attribute memory.



3
4
5
# File 'lib/dog/bot.rb', line 3

def memory
  @memory
end

Instance Method Details

#_from_self(message) ⇒ Object



98
99
100
101
102
103
# File 'lib/dog/bot.rb', line 98

def _from_self(message)
  @rooms.each do |room|
    return true if "#{room}@conference.#{@connection.jid.domain}/#{@connection.jid.node}" == message.from
  end
  false
end

#configObject



86
87
88
89
90
91
92
# File 'lib/dog/bot.rb', line 86

def config
  config = Configure.parse_path(@config_path)

  @commands = config.commands
  @tasks = config.scheduled_tasks
  join(*config.chat_rooms)
end

#helpObject



94
95
96
# File 'lib/dog/bot.rb', line 94

def help
  self.commands.map(&:help).inject(:+)
end

#join(*room_names) ⇒ Object



79
80
81
82
83
84
# File 'lib/dog/bot.rb', line 79

def join(*room_names)
  room_names.each do |room_name|
    @connection.join room_name
    @rooms << room_name
  end
end

#process(message) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/dog/bot.rb', line 26

def process(message)
  response = respond_to(message)

  if response.is_a?(Symbol)
    respond_to_action(message, response)
  else
    response
  end
end

#process_chat_message(message) ⇒ Object



13
14
15
16
# File 'lib/dog/bot.rb', line 13

def process_chat_message(message)
  response = process(message.body)
  say(message.from, response) unless response.nil?
end

#process_group_chat_message(message) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/dog/bot.rb', line 18

def process_group_chat_message(message)
  return if _from_self(message) || message.delayed?

  response = process message.body

  say_to_chat(message.from.node, response) unless response.nil?
end

#respond_to(text) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/dog/bot.rb', line 36

def respond_to(text)
  @commands.each do |command|
    response = command.respond_to text
    return response unless response.nil?
  end

  nil
end

#respond_to_action(message, kind) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dog/bot.rb', line 45

def respond_to_action(message, kind)
  case kind
  when :join then
    room_name = message.split.last
    join room_name
    "joined #{room_name}"
  when :help then
    help
  when :reload then
    config
    "config reloaded"
  when :run_task
    title = message.split[3..-1].join(" ")
    return nil if title.empty?
    task = @tasks.find { |t| t.title == title }
    task.run(self) unless task.nil?
  else "invalid action"
  end
end

#say(to, message) ⇒ Object



65
66
67
# File 'lib/dog/bot.rb', line 65

def say(to, message)
  @connection.say(to, message)
end

#say_to_all_chat_rooms(message) ⇒ Object



73
74
75
76
77
# File 'lib/dog/bot.rb', line 73

def say_to_all_chat_rooms(message)
  @rooms.each do |room|
    say_to_chat(room, message)
  end
end

#say_to_chat(to, message) ⇒ Object



69
70
71
# File 'lib/dog/bot.rb', line 69

def say_to_chat(to, message)
  @connection.say_to_chat(to, message)
end