Class: BotBase

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, botname: 'Nicole', notifier: nil, log: nil, debug: false) ⇒ BotBase

Returns a new instance of BotBase.



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

def initialize(config=nil, botname: 'Nicole', notifier: nil, log: nil,
               debug: false)

  @botname, @notifier, @log, @h, @debug = botname, notifier, log, nil, debug

  if config then

    @h = SimpleConfig.new(config).to_h
    puts '@h: ' + @h.inspect if @debug
    @modules = initialize_modules(@h[:modules])

  end

end

Instance Attribute Details

#channel_lockObject

Returns the value of attribute channel_lock.



13
14
15
# File 'lib/botbase.rb', line 13

def channel_lock
  @channel_lock
end

#logObject (readonly)

Returns the value of attribute log.



12
13
14
# File 'lib/botbase.rb', line 12

def log
  @log
end

#message_prefixObject

Returns the value of attribute message_prefix.



13
14
15
# File 'lib/botbase.rb', line 13

def message_prefix
  @message_prefix
end

Instance Method Details

#notice(msg) ⇒ Object

displays debug messages from modules



32
33
34
35
# File 'lib/botbase.rb', line 32

def notice(msg)
  @notifier.notice msg if @notifier
  puts msg
end

#received(sender = 'user01', msg, mode: :voicechat, echo_node: 'node1') ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/botbase.rb', line 37

def received(sender='user01', msg, mode: :voicechat, echo_node: 'node1')

  msg.rstrip!

  if msg.downcase == 'exit' then
    @channel_lock, @message_prefix = nil, nil
  end

  log.info 'BotBase/received: ' + msg if log
  self.restart if msg == @botname + ' restart'

  r = nil

  detected = if @channel_lock then

    if @debug then
      notice 'botbase: inside channel locked'
      notice 'botbase: fullmsg:' + (@message_prefix.to_s + msg).inspect
    end

    r = @modules[@channel_lock].query(@message_prefix.to_s + msg,
                                      mode: mode, echo_node: echo_node)
    puts 'r: ' + r.inspect if @debug
    r and r.length > 0

  else

    if @debug then
      puts 'before modules.detect'
      puts '@modules.values: ' + @modules.values\
          .map {|x| x.inspect[0..100] }.join("\n")
    end

    @modules.detect do |name, obj|
      puts 'name: ' + name.inspect if @debug
      r = obj.query(msg, mode: mode, echo_node: echo_node)
      r and r.length > 0
    end

  end


  if detected then

    puts 'detected: ' + detected.inspect[0..200] + ' ...' if @debug

    if mode == :voicechat then
      MTLite.new(r).to_s.gsub(/ +https:\/\/[\S]+/,'')
    else
      MTLite.new(r).to_html
    end

  else
    ''
  end

end

#restartObject



95
96
97
98
99
100
101
# File 'lib/botbase.rb', line 95

def restart

  log.info 'BotBase/restart: restarting ...' if log
  @modules = initialize_modules(@h[:modules]) if @h
  notice "echo: #{@botname} is now ready"

end