Class: Dog::Bot
- Inherits:
-
Object
- Object
- Dog::Bot
- Defined in:
- lib/dog/bot.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#memory ⇒ Object
Returns the value of attribute memory.
Instance Method Summary collapse
- #_from_self(message) ⇒ Object
- #config ⇒ Object
- #help ⇒ Object
-
#initialize(connection, config_path) ⇒ Bot
constructor
A new instance of Bot.
- #join(*room_names) ⇒ Object
- #process(message) ⇒ Object
- #process_chat_message(message) ⇒ Object
- #process_group_chat_message(message) ⇒ Object
- #respond_to(text) ⇒ Object
- #respond_to_action(message, kind) ⇒ Object
- #say(to, message) ⇒ Object
- #say_to_all_chat_rooms(message) ⇒ Object
- #say_to_chat(to, message) ⇒ Object
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
#commands ⇒ Object
Returns the value of attribute commands.
3 4 5 |
# File 'lib/dog/bot.rb', line 3 def commands @commands end |
#memory ⇒ Object
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() @rooms.each do |room| return true if "#{room}@conference.#{@connection.jid.domain}/#{@connection.jid.node}" == .from end false end |
#config ⇒ Object
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 |
#help ⇒ Object
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() response = respond_to() if response.is_a?(Symbol) respond_to_action(, response) else response end end |
#process_chat_message(message) ⇒ Object
13 14 15 16 |
# File 'lib/dog/bot.rb', line 13 def () response = process(.body) say(.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 () return if _from_self() || .delayed? response = process .body say_to_chat(.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(, kind) case kind when :join then room_name = .split.last join room_name "joined #{room_name}" when :help then help when :reload then config "config reloaded" when :run_task title = .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, ) @connection.say(to, ) 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() @rooms.each do |room| say_to_chat(room, ) end end |
#say_to_chat(to, message) ⇒ Object
69 70 71 |
# File 'lib/dog/bot.rb', line 69 def say_to_chat(to, ) @connection.say_to_chat(to, ) end |