Module: SlackBot
- Included in:
- Bot
- Defined in:
- lib/slack/bot.rb,
lib/slack/user.rb,
lib/slack/slack.rb,
lib/slack/channel.rb,
lib/slack/message.rb,
lib/slack/matchers/matcher_group.rb
Defined Under Namespace
Classes: Bot, Channel, Matcher, MatcherGroup, Message, User
Constant Summary
collapse
- SLACK_AUTH_URL =
'https://slack.com/api/rtm.start?token='
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#debug=(value) ⇒ Object
13
14
15
|
# File 'lib/slack/slack.rb', line 13
def debug=(value)
@debug = value
end
|
#socket ⇒ Object
Returns the value of attribute socket.
12
13
14
|
# File 'lib/slack/slack.rb', line 12
def socket
@socket
end
|
#team_info ⇒ Object
Returns the value of attribute team_info.
14
15
16
|
# File 'lib/slack/slack.rb', line 14
def team_info
@team_info
end
|
Instance Method Details
#[](key) ⇒ Object
165
166
167
|
# File 'lib/slack/slack.rb', line 165
def [](key)
@team_info[key]
end
|
#channel(id) ⇒ Object
119
120
121
|
# File 'lib/slack/slack.rb', line 119
def channel(id)
channels[id]
end
|
#channels ⇒ Object
115
116
117
|
# File 'lib/slack/slack.rb', line 115
def channels
@channels ||= load_channels
end
|
#debug? ⇒ Boolean
175
176
177
|
# File 'lib/slack/slack.rb', line 175
def debug?
@debug
end
|
#get_url ⇒ Object
22
23
24
25
26
27
|
# File 'lib/slack/slack.rb', line 22
def get_url
data = Net::HTTP.get(URI(SLACK_AUTH_URL + @key))
json = JSON.parse(data)
@team_info = json
json['url']
end
|
#hook(action, *args) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/slack/slack.rb', line 63
def hook(action, *args)
if self.respond_to? "#{action}_matcher"
unless @matchers.has_key? action
matcher_group = MatcherGroup.new(action)
self.send("#{action}_matcher", matcher_group)
@matchers[action] = matcher_group
end
begin
@matchers[action].respond_for(args.first)
rescue Exception => e
puts e.message
puts e.backtrace.join "\n"
end
elsif self.respond_to? action
begin
send(action, *args)
rescue Exception => e
puts e.message
puts e.backtrace.join "\n"
end
end
end
|
#initialize(auth_key, options = {}) ⇒ Object
16
17
18
19
20
|
# File 'lib/slack/slack.rb', line 16
def initialize(auth_key, options={})
@debug = options[:log]
@key = auth_key.strip
@matchers = Hash.new
end
|
#load_channels ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/slack/slack.rb', line 107
def load_channels
channels = Hash.new
@team_info['channels'].each do |chan|
channels[chan['id']] = Channel.new chan, self
end
channels
end
|
#load_user_channels ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/slack/slack.rb', line 123
def load_user_channels
channels = Hash.new
@team_info['ims'].each do |chan|
channels[chan['user']] = Channel.new chan, self
end
channels
end
|
#load_users ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/slack/slack.rb', line 145
def load_users
users = Hash.new
(@team_info['users'] + @team_info['bots']).map do |info|
users[info['id']] = User.new info
end
users
end
|
#log(type, message) ⇒ Object
169
170
171
172
173
|
# File 'lib/slack/slack.rb', line 169
def log(type, message)
if debug?
puts "#{type}: #{message}"
end
end
|
#me ⇒ Object
161
162
163
|
# File 'lib/slack/slack.rb', line 161
def me
@team_info['self']
end
|
#post(channel, message) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/slack/slack.rb', line 86
def post(channel, message)
if channel.is_a? String
chan = channel
elsif channel.is_a? Channel
chan = channel.id
else
raise "Not a valid channel: #{channel}"
end
data = {
id: 1,
type: 'message',
channel: chan,
text: message.to_s
}
@socket.send data.to_json
end
|
#reply_to(msg, text) ⇒ Object
103
104
105
|
# File 'lib/slack/slack.rb', line 103
def reply_to(msg, text)
post(msg['channel'], text)
end
|
#run ⇒ 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
|
# File 'lib/slack/slack.rb', line 29
def run
url = get_url()
EM.run do
@socket = ws = Faye::WebSocket::Client.new(url)
ws.on :open do |event|
log(:open, event.to_s)
hook(:opened)
end
ws.on :message do |event|
log(:action, event.data)
begin
json = JSON.parse(event.data)
type = json.delete('type')
if type
hook(type, Message.new(json, self))
else
hook(:unknown, json)
end
rescue JSON::ParserError => e
log(:error, e.message)
end
end
ws.on :close do |event|
log(:close, "#{event.code} #{event.reason}")
@socket = ws = nil
hook(:closed)
end
end
end
|
#user(id) ⇒ Object
157
158
159
|
# File 'lib/slack/slack.rb', line 157
def user(id)
users[id]
end
|
#user_channel(user) ⇒ Object
135
136
137
138
139
140
141
142
|
# File 'lib/slack/slack.rb', line 135
def user_channel(user)
if user.is_a? User
id = user.id
else
id = user
end
user_channels[id]
end
|
#user_channels ⇒ Object
131
132
133
|
# File 'lib/slack/slack.rb', line 131
def user_channels
@user_channels ||= load_user_channels
end
|
#users ⇒ Object
153
154
155
|
# File 'lib/slack/slack.rb', line 153
def users
@users ||= load_users
end
|