slackbot_frd 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slackbot_frd/initializer/bot_starter.rb +3 -4
- data/lib/slackbot_frd/lib/slack_connection.rb +76 -30
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb2881a44943ab20e70ea1b0805c5f402273e273
|
4
|
+
data.tar.gz: ead056bcf941443b3618bea5aaff6d7a620743f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc6ca7b4a660ec8e3de5f0fbb91b473e5be6ea7b22929d3023d5df5a0766978c1f4202f45f11f86c7c856bfcaaf07339ecec11d195cf93b87777734c01d58c84
|
7
|
+
data.tar.gz: dcd3c54bd7714e2f69a095c593caedaa5b6d361c6448b26af0655365a8895ed4b909a0cfb66a6f8d2cc83040e5585618c1e8ef31cc16a9779b123473d2e08fbf
|
@@ -12,8 +12,8 @@ rescue LoadError
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class BotStarter
|
15
|
-
def self.start_bots(errors_file, token, botdir,
|
16
|
-
bot_enabled = ->(bot) {
|
15
|
+
def self.start_bots(errors_file, token, botdir, enabled_bots)
|
16
|
+
bot_enabled = ->(bot) { enabled_bots.empty? || enabled_bots.include?(bot) }
|
17
17
|
|
18
18
|
# Create a new Connection to pass to the bot classes
|
19
19
|
slack_connection = SlackbotFrd::SlackConnection.new(token, errors_file)
|
@@ -24,9 +24,8 @@ class BotStarter
|
|
24
24
|
# instantiate them, and then call their add_callbacks method
|
25
25
|
ObjectSpace.each_object(Class).select do |klass|
|
26
26
|
if klass != SlackbotFrd::Bot && klass.ancestors.include?(SlackbotFrd::Bot) && bot_enabled.call(klass.name)
|
27
|
-
SlackbotFrd::Log.debug("Instantiating class '#{klass.to_s}'")
|
27
|
+
SlackbotFrd::Log.debug("Instantiating and adding callbacks to class '#{klass.to_s}'")
|
28
28
|
b = klass.new
|
29
|
-
SlackbotFrd::Log.debug("Adding callbacks to bot '#{klass}'")
|
30
29
|
b.add_callbacks(slack_connection)
|
31
30
|
bots.push(b)
|
32
31
|
end
|
@@ -25,8 +25,7 @@ module SlackbotFrd
|
|
25
25
|
|
26
26
|
def initialize(token, errors_file)
|
27
27
|
unless token
|
28
|
-
|
29
|
-
raise NoTokenError.new
|
28
|
+
log_and_add_to_error_file("No token passed to #{self.class}")
|
30
29
|
end
|
31
30
|
|
32
31
|
@token = token
|
@@ -55,11 +54,14 @@ module SlackbotFrd
|
|
55
54
|
SlackbotFrd::Log::info("#{self.class}: starting event machine")
|
56
55
|
|
57
56
|
EM.run do
|
58
|
-
|
57
|
+
begin
|
58
|
+
wss_url = SlackbotFrd::SlackMethods::RtmStart.wss_url(@token)
|
59
|
+
rescue SocketError => e
|
60
|
+
log_and_add_to_error_file(socket_error_message(e))
|
61
|
+
end
|
62
|
+
|
59
63
|
unless wss_url
|
60
|
-
|
61
|
-
SlackbotFrd::Log.error(str)
|
62
|
-
File.append(@errors_file, "#{str}\n") if @errors_file
|
64
|
+
log_and_add_to_error_file("No Real Time stream opened by slack. Check for network connection and correct authentication token")
|
63
65
|
return
|
64
66
|
end
|
65
67
|
@ws = Faye::WebSocket::Client.new(wss_url)
|
@@ -89,36 +91,49 @@ module SlackbotFrd
|
|
89
91
|
end
|
90
92
|
|
91
93
|
def on_message(user = :any, channel = :any, &block)
|
92
|
-
|
94
|
+
wrap_user_or_channel_lookup_on_callback('on_message', user, channel) do
|
95
|
+
@on_message_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
|
96
|
+
end
|
93
97
|
end
|
94
98
|
|
95
99
|
def on_channel_left(user = :any, channel = :any, &block)
|
96
|
-
|
100
|
+
wrap_user_or_channel_lookup_on_callback('on_message_channel_left', user, channel) do
|
101
|
+
@on_channel_left_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
|
102
|
+
end
|
97
103
|
end
|
98
104
|
|
99
105
|
def on_channel_joined(user = :any, channel = :any, &block)
|
100
|
-
|
101
|
-
|
102
|
-
|
106
|
+
wrap_user_or_channel_lookup_on_callback('on_message_channel_joined', user, channel) do
|
107
|
+
u = user_name_to_id(user)
|
108
|
+
c = channel_name_to_id(channel)
|
109
|
+
@on_channel_joined_callbacks.add(u, c, block)
|
110
|
+
end
|
103
111
|
end
|
104
112
|
|
105
113
|
def send_message_as_user(channel, message)
|
106
114
|
unless @ws
|
107
|
-
|
108
|
-
raise NotConnectedError.new("Not connected to wss stream")
|
115
|
+
log_and_add_to_error_file("Cannot send message '#{message}' as user to channel '#{channel}' because not connected to wss stream")
|
109
116
|
end
|
110
117
|
|
111
|
-
|
112
|
-
id: event_id,
|
113
|
-
type: "message",
|
114
|
-
channel: channel_name_to_id(channel),
|
115
|
-
text: message
|
116
|
-
}.to_json)
|
118
|
+
SlackbotFrd::Log::debug("#{self.class}: Sending message '#{message}' as user to channel '#{channel}'")
|
117
119
|
|
118
|
-
|
120
|
+
begin
|
121
|
+
resp = @ws.send({
|
122
|
+
id: event_id,
|
123
|
+
type: "message",
|
124
|
+
channel: channel_name_to_id(channel),
|
125
|
+
text: message
|
126
|
+
}.to_json)
|
127
|
+
|
128
|
+
SlackbotFrd::Log::debug("#{self.class}: Received response: #{resp}")
|
129
|
+
rescue SocketError => e
|
130
|
+
log_and_add_to_error_file(socket_error_message(e))
|
131
|
+
end
|
119
132
|
end
|
120
133
|
|
121
134
|
def send_message(channel, message, username, avatar, avatar_is_emoji)
|
135
|
+
SlackbotFrd::Log::debug("#{self.class}: Sending message '#{message}' as user '#{username}' to channel '#{channel}'")
|
136
|
+
|
122
137
|
resp = SlackbotFrd::SlackMethods::ChatPostMessage.postMessage(
|
123
138
|
@token,
|
124
139
|
channel_name_to_id(channel),
|
@@ -127,7 +142,8 @@ module SlackbotFrd
|
|
127
142
|
avatar,
|
128
143
|
avatar_is_emoji
|
129
144
|
)
|
130
|
-
|
145
|
+
|
146
|
+
SlackbotFrd::Log::debug("#{self.class}: Received response: #{resp}")
|
131
147
|
end
|
132
148
|
|
133
149
|
def restrict_actions_to_channels_joined(value = true)
|
@@ -170,6 +186,17 @@ module SlackbotFrd
|
|
170
186
|
@channel_name_to_id[nc]
|
171
187
|
end
|
172
188
|
|
189
|
+
private
|
190
|
+
def wrap_user_or_channel_lookup_on_callback(callback_name, user, channel)
|
191
|
+
begin
|
192
|
+
return yield
|
193
|
+
rescue SlackbotFrd::InvalidChannelError => e
|
194
|
+
log_and_add_to_error_file("Unable to add #{callback_name} callback for channel '#{channel}'. Lookup of channel name to ID failed. Check network connection, and ensure channel exists and is accessible")
|
195
|
+
rescue SlackbotFrd::InvalidUserError => e
|
196
|
+
log_and_add_to_error_file("Unable to add #{callback_name} callback for user '#{user}'. Lookup of channel name to ID failed. Check network connection and ensure user exists")
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
173
200
|
private
|
174
201
|
def normalize_channel_name(channel_name)
|
175
202
|
return channel_name[1..-1] if channel_name.start_with?('#')
|
@@ -254,20 +281,39 @@ module SlackbotFrd
|
|
254
281
|
|
255
282
|
private
|
256
283
|
def refresh_user_info
|
257
|
-
|
258
|
-
|
259
|
-
|
284
|
+
begin
|
285
|
+
users_list = SlackbotFrd::SlackMethods::UsersList.new(@token).connect
|
286
|
+
@user_id_to_name = users_list.ids_to_names
|
287
|
+
@user_name_to_id = users_list.names_to_ids
|
288
|
+
rescue SocketError => e
|
289
|
+
log_and_add_to_error_file(socket_error_message(e))
|
290
|
+
end
|
260
291
|
end
|
261
292
|
|
262
293
|
private
|
263
294
|
def refresh_channel_info
|
264
|
-
|
265
|
-
|
266
|
-
|
295
|
+
begin
|
296
|
+
channels_list = SlackbotFrd::SlackMethods::ChannelsList.new(@token).connect
|
297
|
+
@channel_id_to_name = channels_list.ids_to_names
|
298
|
+
@channel_name_to_id = channels_list.names_to_ids
|
299
|
+
|
300
|
+
im_channels_list = SlackbotFrd::SlackMethods::ImChannelsList.new(@token).connect
|
301
|
+
@channel_id_to_name.merge!(im_channels_list.ids_to_names)
|
302
|
+
@channel_name_to_id.merge!(im_channels_list.names_to_ids)
|
303
|
+
rescue SocketError => e
|
304
|
+
log_and_add_to_error_file(socket_error_message(e))
|
305
|
+
end
|
306
|
+
end
|
267
307
|
|
268
|
-
|
269
|
-
|
270
|
-
|
308
|
+
private
|
309
|
+
def socket_error_message(e)
|
310
|
+
"SocketError: Check your connection: #{e.message}"
|
311
|
+
end
|
312
|
+
|
313
|
+
private
|
314
|
+
def log_and_add_to_error_file(err)
|
315
|
+
SlackbotFrd::Log::error(err)
|
316
|
+
File.append(@errors_file, "#{err}\n")
|
271
317
|
end
|
272
318
|
end
|
273
319
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slackbot_frd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Porter
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.19'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: daemons
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '1.2'
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '1.2'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: json
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|