slack-bot-server 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51fdaf71346b7717b1f2c2334300d5759492da93
4
- data.tar.gz: fa36c3d1286153e4528e66faa59c6731ed78e8a8
3
+ metadata.gz: 08f9edd260732b19478908af280a1d3acf318190
4
+ data.tar.gz: 1a82cf329ca85099d093e6fb78090adebc44393f
5
5
  SHA512:
6
- metadata.gz: 1e37531bc9a0ce2296ba1efceb0f5ceb348e34b4a6b6083d07923f5dc37d6aeed2a0f41741073dc4691e630ed52b660e7c1ba3e7a8a3397825790740d2bcdb7f
7
- data.tar.gz: 045cb48c928ad23eb314df4c9c257ffd80ab72f8a472d2a23b76d45bae5ebf3c4b265438e14d878b9e938ad09ddab716f33a02077faacca6a08daa1a85f0958e
6
+ metadata.gz: a4da0c83a05616f950969841c32b029ca7d60ffc79056fee1178011c31b5e748adf0f7e470772628524aa7e92db62a1521fe17bf30337d136f332558f833ec65
7
+ data.tar.gz: 951b05156d6d4365c4d0ef64b6ea8f460efc4b612dab3e9f39c58bfcd5479a49126aa08d41cd400845b174748d53583efe95ca88a4f2688a43367bf0b771b6f4
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.3
4
- before_install: gem install bundler -v 1.10.3
3
+ - 2.1
4
+ - 2.2
5
+ before_install: gem install bundler
@@ -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 auth_test['ok']
20
+ raise InvalidToken unless rtm_start_data['ok']
19
21
  end
20
22
 
21
23
  def say(options)
22
- @channel_ids.each do |channel_id|
23
- @api.chat_postMessage(default_message_options.merge(options).merge(channel: channel_id))
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
- @api.chat_postMessage(default_message_options.merge(options.merge(channel: channel)))
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
- channel_id = result['channel']['id']
35
- say(options.merge(channel: channel_id))
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 '#{team}'"
50
- load_im_channels
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
- callbacks = @callbacks[type.to_sym] || []
113
+ matching_callbacks = callbacks[type.to_sym] || []
105
114
  if superclass.respond_to?(:callbacks_for)
106
- callbacks += superclass.callbacks_for(type)
115
+ matching_callbacks += superclass.callbacks_for(type)
107
116
  end
108
- callbacks
117
+ matching_callbacks.reverse
109
118
  end
110
119
 
111
120
  def on(type, &block)
112
- @callbacks ||= {}
113
- @callbacks[type.to_sym] ||= []
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
- relevant_callbacks = self.class.callbacks_for(data["type"])
167
- if relevant_callbacks && relevant_callbacks.any?
168
- relevant_callbacks.each do |c|
169
- instance_exec(data, &c)
170
- end
171
- end
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
- auth_test['user']
200
+ rtm_start_data['self']['name']
189
201
  end
190
202
 
191
203
  def user_id
192
- auth_test['user_id']
204
+ rtm_start_data['self']['id']
193
205
  end
194
206
 
195
- def team
196
- auth_test['team']
207
+ def team_name
208
+ rtm_start_data['team']['name']
197
209
  end
198
210
 
199
- def auth_test
200
- @auth_test ||= @api.auth_test
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 load_im_channels
204
- log "Loading IM channels"
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 load_channels
211
- log "Loading channels"
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'] == 'USLACKBOT'
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
@@ -24,4 +24,3 @@ class SlackBotServer::RedisQueue
24
24
  end
25
25
  end
26
26
  end
27
-
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module SlackBotServer
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.3
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 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api