slack-bot-server 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/lib/slack_bot_server/bot.rb +56 -39
- data/lib/slack_bot_server/redis_queue.rb +0 -1
- data/lib/slack_bot_server/remote_control.rb +8 -0
- data/lib/slack_bot_server/server.rb +11 -0
- data/lib/slack_bot_server/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08f9edd260732b19478908af280a1d3acf318190
|
4
|
+
data.tar.gz: 1a82cf329ca85099d093e6fb78090adebc44393f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4da0c83a05616f950969841c32b029ca7d60ffc79056fee1178011c31b5e748adf0f7e470772628524aa7e92db62a1521fe17bf30337d136f332558f833ec65
|
7
|
+
data.tar.gz: 951b05156d6d4365c4d0ef64b6ea8f460efc4b612dab3e9f39c58bfcd5479a49126aa08d41cd400845b174748d53583efe95ca88a4f2688a43367bf0b771b6f4
|
data/.travis.yml
CHANGED
data/lib/slack_bot_server/bot.rb
CHANGED
@@ -2,6 +2,8 @@ require 'slack'
|
|
2
2
|
require 'slack/client'
|
3
3
|
|
4
4
|
class SlackBotServer::Bot
|
5
|
+
SLACKBOT_USER_ID = 'USLACKBOT'
|
6
|
+
|
5
7
|
attr_reader :key
|
6
8
|
|
7
9
|
class InvalidToken < RuntimeError; end
|
@@ -15,24 +17,28 @@ class SlackBotServer::Bot
|
|
15
17
|
@connected = false
|
16
18
|
@running = false
|
17
19
|
|
18
|
-
raise InvalidToken unless
|
20
|
+
raise InvalidToken unless rtm_start_data['ok']
|
19
21
|
end
|
20
22
|
|
21
23
|
def say(options)
|
22
|
-
@
|
23
|
-
|
24
|
+
@api.chat_postMessage(default_message_options.merge(options))
|
25
|
+
end
|
26
|
+
|
27
|
+
def broadcast(options)
|
28
|
+
@channel_ids.each do |channel|
|
29
|
+
say(options.merge(channel: channel))
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
27
33
|
def reply(options)
|
28
34
|
channel = @last_received_data['channel']
|
29
|
-
|
35
|
+
say(options.merge(channel: channel))
|
30
36
|
end
|
31
37
|
|
32
38
|
def say_to(user_id, options)
|
33
39
|
result = @api.im_open(user: user_id)
|
34
|
-
|
35
|
-
say(options.merge(channel:
|
40
|
+
channel = result['channel']['id']
|
41
|
+
say(options.merge(channel: channel))
|
36
42
|
end
|
37
43
|
|
38
44
|
def call(method, args)
|
@@ -46,9 +52,8 @@ class SlackBotServer::Bot
|
|
46
52
|
|
47
53
|
@ws.on :open do |event|
|
48
54
|
@connected = true
|
49
|
-
log "connected to '#{
|
50
|
-
|
51
|
-
load_channels
|
55
|
+
log "connected to '#{team_name}'"
|
56
|
+
run_callbacks(:start)
|
52
57
|
end
|
53
58
|
|
54
59
|
@ws.on :message do |event|
|
@@ -100,18 +105,21 @@ class SlackBotServer::Bot
|
|
100
105
|
@default_message_options ||= {}
|
101
106
|
end
|
102
107
|
|
108
|
+
def callbacks
|
109
|
+
@callbacks ||= {}
|
110
|
+
end
|
111
|
+
|
103
112
|
def callbacks_for(type)
|
104
|
-
|
113
|
+
matching_callbacks = callbacks[type.to_sym] || []
|
105
114
|
if superclass.respond_to?(:callbacks_for)
|
106
|
-
|
115
|
+
matching_callbacks += superclass.callbacks_for(type)
|
107
116
|
end
|
108
|
-
|
117
|
+
matching_callbacks.reverse
|
109
118
|
end
|
110
119
|
|
111
120
|
def on(type, &block)
|
112
|
-
|
113
|
-
|
114
|
-
@callbacks[type.to_sym] << block
|
121
|
+
callbacks[type.to_sym] ||= []
|
122
|
+
callbacks[type.to_sym] << block
|
115
123
|
end
|
116
124
|
|
117
125
|
def on_mention(&block)
|
@@ -136,6 +144,10 @@ class SlackBotServer::Bot
|
|
136
144
|
end
|
137
145
|
end
|
138
146
|
|
147
|
+
on :start do
|
148
|
+
load_channels
|
149
|
+
end
|
150
|
+
|
139
151
|
on :im_created do |data|
|
140
152
|
channel_id = data['channel']['id']
|
141
153
|
log "Adding new IM channel: #{channel_id}"
|
@@ -162,13 +174,13 @@ class SlackBotServer::Bot
|
|
162
174
|
|
163
175
|
def handle_message(event)
|
164
176
|
data = MultiJson.load(event.data)
|
165
|
-
if data["type"]
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
177
|
+
run_callbacks(data["type"], data) if data["type"]
|
178
|
+
end
|
179
|
+
|
180
|
+
def run_callbacks(type, data=nil)
|
181
|
+
relevant_callbacks = self.class.callbacks_for(type)
|
182
|
+
relevant_callbacks.each do |c|
|
183
|
+
instance_exec(data, &c)
|
172
184
|
end
|
173
185
|
end
|
174
186
|
|
@@ -185,33 +197,31 @@ class SlackBotServer::Bot
|
|
185
197
|
end
|
186
198
|
|
187
199
|
def user
|
188
|
-
|
200
|
+
rtm_start_data['self']['name']
|
189
201
|
end
|
190
202
|
|
191
203
|
def user_id
|
192
|
-
|
204
|
+
rtm_start_data['self']['id']
|
193
205
|
end
|
194
206
|
|
195
|
-
def
|
196
|
-
|
207
|
+
def team_name
|
208
|
+
rtm_start_data['team']['name']
|
197
209
|
end
|
198
210
|
|
199
|
-
def
|
200
|
-
|
211
|
+
def load_channels
|
212
|
+
log "Loading channels"
|
213
|
+
@im_channel_ids = rtm_start_data['ims'].map { |d| d['id'] }
|
214
|
+
log im_channels: @im_channel_ids
|
215
|
+
@channel_ids = rtm_start_data['channels'].select { |d| d['is_member'] == true }.map { |d| d['id'] }
|
216
|
+
log channels: @channel_ids
|
201
217
|
end
|
202
218
|
|
203
|
-
def
|
204
|
-
|
205
|
-
result = @api.im_list
|
206
|
-
@im_channel_ids = result['ims'].map { |d| d['id'] }
|
207
|
-
log im_channels: @im_channel_ids
|
219
|
+
def websocket_url
|
220
|
+
rtm_start_data['url']
|
208
221
|
end
|
209
222
|
|
210
|
-
def
|
211
|
-
|
212
|
-
result = @api.channels_list(exclude_archived: 1)
|
213
|
-
@channel_ids = result['channels'].select { |d| d['is_member'] == true }.map { |d| d['id'] }
|
214
|
-
log channels: @channel_ids
|
223
|
+
def rtm_start_data
|
224
|
+
@rtm_start_data ||= @api.post('rtm.start')
|
215
225
|
end
|
216
226
|
|
217
227
|
def is_im_channel?(id)
|
@@ -220,7 +230,14 @@ class SlackBotServer::Bot
|
|
220
230
|
|
221
231
|
def bot_message?(data)
|
222
232
|
data['subtype'] == 'bot_message' ||
|
223
|
-
data['user'] ==
|
233
|
+
data['user'] == SLACKBOT_USER_ID ||
|
234
|
+
data['user'] == user_id ||
|
235
|
+
change_to_previous_bot_message?(data)
|
236
|
+
end
|
237
|
+
|
238
|
+
def change_to_previous_bot_message?(data)
|
239
|
+
data['subtype'] == 'message_changed' &&
|
240
|
+
data['previous_message']['user'] == user_id
|
224
241
|
end
|
225
242
|
|
226
243
|
def websocket_url
|
@@ -17,10 +17,18 @@ class SlackBotServer::RemoteControl
|
|
17
17
|
@queue.push([:remove_bot, key])
|
18
18
|
end
|
19
19
|
|
20
|
+
def broadcast(key, message_data)
|
21
|
+
@queue.push([:broadcast, key, message_data])
|
22
|
+
end
|
23
|
+
|
20
24
|
def say(key, message_data)
|
21
25
|
@queue.push([:say, key, message_data])
|
22
26
|
end
|
23
27
|
|
28
|
+
def say_to(key, user_id, message_data)
|
29
|
+
@queue.push([:say_to, key, user_id, message_data])
|
30
|
+
end
|
31
|
+
|
24
32
|
def call(key, method, args)
|
25
33
|
@queue.push([:call, [key, method, args]])
|
26
34
|
end
|
@@ -77,10 +77,21 @@ class SlackBotServer::Server
|
|
77
77
|
when :remove_bot
|
78
78
|
key = args.first
|
79
79
|
remove_bot(key)
|
80
|
+
when :broadcast
|
81
|
+
key, message_data = args
|
82
|
+
log "[#{key}] #{message_data}"
|
83
|
+
bot = bot(key)
|
84
|
+
bot.broadcast(message_data)
|
80
85
|
when :say
|
81
86
|
key, message_data = args
|
87
|
+
log "[#{key}] #{message_data}"
|
82
88
|
bot = bot(key)
|
83
89
|
bot.say(message_data)
|
90
|
+
when :say_to
|
91
|
+
key, user_id, message_data = args
|
92
|
+
log "[#{key}] (#{user_id}) #{message_data}"
|
93
|
+
bot = bot(key)
|
94
|
+
bot.say_to(user_id, message_data)
|
84
95
|
when :call
|
85
96
|
key, method, method_args = args
|
86
97
|
bot = bot(key)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-bot-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Adam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: slack-api
|