Class: BotBaseModuleDemo

Inherits:
Object
  • Object
show all
Defined in:
lib/botbase-module-demo.rb

Overview

A service module used by the BotBase gem

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, phrases: {}, callback: nil, debug: false) ⇒ BotBaseModuleDemo

Returns a new instance of BotBaseModuleDemo.



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
# File 'lib/botbase-module-demo.rb', line 9

def initialize(x=nil, phrases: {}, callback: nil, debug: false)
  
  @phrases, @debug = phrases, debug
  service_modules = nil
  
  a = if service_modules then
  
    service_modules

  else
    
    @h = {
        "^What's the time?" => "'The time is ' + Time.now.strftime(\"%-I:%M%p\")",
        "hello?" => %q("hello #{sender}, how can I help you?" % sender),
        'unknown' => "\"I'm sorry, I don't understand what you said.\"",
        'where are you?' => 
            %q("I'm in [Edinburgh](https://en.wikipedia.org/wiki/Edinburgh)")
    }
    [@h]

  end
  
  a.each {|x|  @phrases.merge! x }  

end

Instance Method Details

#query(sender = 'user01', msg, mode: :chat, echo_node: 'node1') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/botbase-module-demo.rb', line 35

def query(sender='user01', msg, mode: :chat, echo_node: 'node1')
  r = @phrases.detect {|k,v|  msg =~ /#{k}/i }

  raw_msg = if r then
    r.last 
  else  
    @h['unknown']      
  end
  
  response = eval raw_msg   
end