/
chatbot.rb
70 lines (57 loc) · 1.3 KB
/
chatbot.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
59
60
61
62
63
64
65
66
67
68
69
70
require 'rubygems'
gem 'Ruby-IRC'
require 'IRC'
require 'logger'
DEBUG = false
class Chatbot < IRC
def initialize(*args)
super
@actions = {}
@logger = Logger.new STDOUT
@rooms = %w(test)
IRCEvent.add_callback('endofmotd') {|e| @rooms.each {|r| add_channel(r)}}
IRCEvent.add_callback('privmsg') do |event|
@actions.each_pair do |match_exp, block|
begin
@logger.debug "match_exp = #{match_exp}"
match_data = event.message.match match_exp
if match_data
@logger.info "MATCH #{match_exp}"
block.call event.from, event, match_data
end
rescue
@logger.error "ERROR"
@logger.error $!
end
end
end
end
def add_actions(action_hash)
@actions.merge! action_hash
end
def add_room(room)
@rooms.push room
end
def reply(event, msg)
to = event.channel == nick ? event.from : event.channel
send_message_to to, msg
end
def reply_privately(event, msg)
send_message_to event.from, msg
end
def send_message_to(to, msg)
if msg.sub! %r(^/me\s+), ''
send_action to, msg
else
send_message to, msg
end
end
def one_of(strings)
strings[rand(strings.size)]
end
def debug(msg)
puts msg if DEBUG
end
end
class IRCEvent
end