Class: BotBaseModuleConversation

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

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, package_src: nil, default_package: nil, default_job: nil, callback: nil) ⇒ BotBaseModuleConversation

Returns a new instance of BotBaseModuleConversation.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/botbase-module-conversation.rb', line 15

def initialize(host: nil, package_src: nil, 
            default_package: nil, default_job: nil, callback: nil)    

  @bot = callback
  @rsc = RSC.new host, package_src
  
  a = run(default_package, default_job)
  
  @doc = Rexle.new("<conversations/>")

  add_phrases(a)
  
end

Instance Method Details

#query(sender = 'user01', said, mode: :voicechat, echo_node: 'node') ⇒ Object



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
70
71
72
73
74
75
76
# File 'lib/botbase-module-conversation.rb', line 29

def query(sender='user01', said, mode: :voicechat, echo_node: 'node')    
  
  found = @phrases.detect {|pattern, _|  said =~ /#{pattern}/i }
  
  if found then
    
    if @bot.log then
      @bot.log.info "BotBaseModuleConversation/query:" + 
          " found %s in response to %s" % [found.inspect, said]
    end
  
    _, rsc_command, context_tags = found
    package, job = rsc_command.split

    h = said.match(/#{found.first}/i).named_captures
    r = run(package, job, h)
    
    a, tags = [], []
    
    a << if r.is_a? String then
      r
    elsif r.is_a? Hash then
      
      if r[:msg] then
        tags.concat r[:tags].split
        r[:msg]
      else
        r
      end
      
    elsif r.is_a? Array then
      add_phrases(r)        
    end
    
    tags.concat context_tags.split if context_tags
    a << tags
    
    if @bot.log then
      @bot.log.info "BotBaseModuleConversation/query/result:" + 
          " result %s in response to %s" % [a.to_json, said]
    end      
    a.first
    
  else  
    no_match_found()
  end
  
end