Class: Cogibara::MessageHandler
- Inherits:
-
Object
- Object
- Cogibara::MessageHandler
- Defined in:
- lib/cogibara/message_handler.rb
Instance Method Summary collapse
- #derive_structure(message) ⇒ Object
- #handle(message) ⇒ Object
- #process_confirm_deny(message, obj) ⇒ Object
- #process_core(message, obj) ⇒ Object
- #process_default(msg, obj) ⇒ Object
- #process_instruction(message, obj) ⇒ Object
- #process_intent(message, obj) ⇒ Object
Instance Method Details
#derive_structure(message) ⇒ Object
138 139 140 |
# File 'lib/cogibara/message_handler.rb', line 138 def derive_structure() response = Cogibara::dispatcher.call("maluuba",) end |
#handle(message) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cogibara/message_handler.rb', line 4 def handle() if(.is_a? String) = Message.new(, "local") end Responder.new.send_reply("@think",.clientID) if Cogibara::config.soft_parse .structure = derive_structure() end msg = .text @verbose = Cogibara::config.verbose @name = Cogibara::config.name if msg puts "core" if @verbose reply = process_core(msg, ) unless reply reply = process_confirm_deny(msg, ) unless reply splitmsg = msg splitmsg = splitmsg.split puts "instruction #{splitmsg[0]}" if @verbose if splitmsg[0]==@name = splitmsg .shift = .join(' ') # slicemessage.slice!(@name) = .strip puts "instructed #{}" if @verbose # if @soft_parse reply = process_instruction(, ) unless reply puts "defaults" if @verbose reply = process_default(msg, ) end elsif Cogibara::config.active_mode puts "active module #{Cogibara::config.active_mode} processing #{msg}" if @verbose reply = process_instruction(Cogibara::config.active_mode + " " + msg, ) unless reply puts "defaultx" if @verbose reply = process_default(msg, ) end elsif .structure[:category] reply = process_intent(msg, ) unless reply puts "defaulty" if @verbose reply = process_default(msg, ) end else puts "defaultz" if @verbose reply = process_default(msg, ) end end end puts reply if @verbose Responder.new.send_reply("@listen",.clientID) Cogibara::say Message.new("#{@name}: " + reply.to_s, .clientID) reply end end |
#process_confirm_deny(message, obj) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cogibara/message_handler.rb', line 143 def process_confirm_deny(, obj) if == "yes" if Cogibara::confirmer.waiting? Cogibara::confirmer.confirm_action "Confirmed!" end else nil end end |
#process_core(message, obj) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cogibara/message_handler.rb', line 71 def process_core(, obj) @yourname = "person" = .chomp if == "hey #{@name}" "what's up #{@yourname}?" elsif == "reset mode" Cogibara::config.active_mode = nil Cogibara::config.active_file_mode = nil "Disabling any active modules" elsif /your name is now/ =~ .slice!("your name is now ") @name = .chomp "OK, you can call me #{@name}" elsif /my name is/ =~ .slice!("my name is ") @yourname = .chomp "OK, I'll call you #{@yourname}" elsif == "what time is it" "it is #{Time.now.hour} o'clock and #{Time.now.min} minutes" else nil end end |
#process_default(msg, obj) ⇒ Object
95 96 97 98 99 |
# File 'lib/cogibara/message_handler.rb', line 95 def process_default(msg, obj) chatmsg = Message.new(msg, obj.clientID, obj.structure) puts chatmsg.inspect if @verbose Cogibara::dispatcher.call("chat", chatmsg) end |
#process_instruction(message, obj) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cogibara/message_handler.rb', line 101 def process_instruction(, obj) puts "instruction: #{.split[0]} (#{Cogibara::dispatcher.registered?(.split[0])})" if @verbose if =~ /\Aset mode/ .slice!("set mode") Cogibara::config.active_mode = .chomp.strip "Going into #{Cogibara::config.active_mode} mode" elsif =~ /\Aset file mode/ .slice!("set file mode") Cogibara::config.active_file_mode = .chomp.strip "Handling files with #{Cogibara::config.active_file_mode}" elsif Cogibara::dispatcher.registered?(.split[0]) splitmsg = .split Responder.new.send_reply("@search",obj.clientID) puts "recognized instruction #{.split[0]} (#{splitmsg})" if @verbose mod = splitmsg[0] splitmsg.shift Cogibara::dispatcher.call(mod, Message.new(splitmsg.join(' '), obj.clientID, obj.structure)) else puts "didn't understand instruction #{.split[0]}" if @verbose nil end end |
#process_intent(message, obj) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cogibara/message_handler.rb', line 125 def process_intent(, obj) if (obj.structure[:action] && Cogibara::dispatcher.registered?(obj.structure[:action].to_s)) puts "recognized instruction #{obj.structure[:action]}" if @verbose Cogibara::dispatcher.call(obj.structure[:category].to_s,obj) elsif (obj.structure[:category] && Cogibara::dispatcher.registered?(obj.structure[:category].to_s)) puts "recognized instruction #{obj.structure[:category]}" if @verbose Cogibara::dispatcher.call(obj.structure[:category].to_s,obj) else puts "didn't understand instruction #{.split[0]}" if @verbose nil end end |