-
Notifications
You must be signed in to change notification settings - Fork 4
/
infobot.rb
58 lines (48 loc) · 1.78 KB
/
infobot.rb
1
2
3
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
require 'yaml'
class Infobot < CampfireBot::Plugin
Infobot::DEFINE_REGEXP = /(no, )*(.+) is ([^\?]+)(?!\?)$/
Infobot::RESPOND_REGEXP = /(what's|what is|who is|who's|where|where's|how's|how is) ([^\?]+)(?=\?)*/
# if BOT_ENVIRONMENT == 'development'
on_message Regexp.new("^#{bot.config['nickname']},\\s+#{RESPOND_REGEXP.source}", Regexp::IGNORECASE), :respond
on_message Regexp.new("^#{bot.config['nickname']},\\s+#{DEFINE_REGEXP.source}", Regexp::IGNORECASE), :define
on_command 'reload_facts', :reload
# end
def initialize
# puts "entering initialize()"
end
def respond(msg)
# puts "entering respond()"
@facts = init()
puts msg[:message]
puts msg[:message] =~ RESPOND_REGEXP # Regexp.new("^#{Bot.instance.config['nickname']},\\s+#{RESPOND_REGEXP.source}", Regexp::IGNORECASE)
puts $1, $2, $3
if [email protected]_key?($2.downcase)
msg.speak("Sorry, I don't know what #{$2} is.")
else
fact = @facts[$2.downcase]
msg.speak("#{msg[:person].split(" ")[0]}, #{$2} is #{fact}.")
end
end
def define(msg)
# puts 'entering define()'
@facts = init()
# puts @facts
# puts msg[:message]
puts msg[:message] =~ Regexp.new("^#{bot.config['nickname']},\\s+#{DEFINE_REGEXP.source}", Regexp::IGNORECASE)
# puts @define_regexp
# puts $1, $2, $3
@facts[$2.downcase] = $3
msg.speak("Okay, #{$2} is now #{$3}")
File.open(File.join(File.dirname(__FILE__), 'infobot.yml'), 'w') do |out|
YAML.dump(@facts, out)
end
end
def init
# puts "entering init()"
YAML::load(File.read(File.join(BOT_ROOT, 'tmp', 'infobot.yml')))
end
def reload(msg)
@facts = init()
msg.speak("ok, reloaded #{@facts.size} facts")
end
end