slackbot_frd 0.0.8 → 0.1.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 +4 -4
- data/bin/slackbot-frd +10 -9
- data/lib/slackbot_frd/lib/slack_connection.rb +88 -41
- data/lib/slackbot_frd/lib/user_channel_callbacks.rb +7 -7
- data/lib/slackbot_frd/slack_methods/channels_info.rb +3 -3
- data/lib/slackbot_frd/slack_methods/channels_invite.rb +3 -3
- data/lib/slackbot_frd/slack_methods/chat_delete.rb +33 -0
- data/lib/slackbot_frd/slack_methods/chat_post_message.rb +14 -7
- data/lib/slackbot_frd/slack_methods/reactions_add.rb +40 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e04887e283e05c9f2dfa317752af3b3afc0aff3
|
4
|
+
data.tar.gz: 50cd078d62ba3fd848fb971b8e8bc3c7e4b462c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8da01ed22e52b72b5fe4f87b3c7c7c561f65e65ed0d4c3214e40e2a585620454d16680aad862ef4f6b35a66b64136397fa29bbf8d6a974fac27366f2cf2b96e6
|
7
|
+
data.tar.gz: 6cee701b7c18747ace4a3732df3d866997df8d84ebb1155e4e99d83a72187a4eccc5eda8659444f7a6572adc0a609963e17a47991cd137e6274ea201eef277f8
|
data/bin/slackbot-frd
CHANGED
@@ -43,7 +43,7 @@ class SlackbotFrdBin < Thor
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
desc "new", "Generate a new slackbot_frd project"
|
46
|
+
desc "new <project-name>", "Generate a new slackbot_frd project"
|
47
47
|
long_desc <<-LONGDESC
|
48
48
|
new will generate a skeleton for a new slackbot_frd project
|
49
49
|
LONGDESC
|
@@ -86,6 +86,8 @@ class SlackbotFrdBin < Thor
|
|
86
86
|
"botdir" : ".",
|
87
87
|
"daemonize" : false,
|
88
88
|
"bots" : [],
|
89
|
+
"log_level" : "debug",
|
90
|
+
"log_file" : "my-cool-bot.log",
|
89
91
|
"my_bots_config_option" : "<bot-specific-option>"
|
90
92
|
}
|
91
93
|
CONFIG_FILE_SKELETON
|
@@ -314,18 +316,17 @@ class SlackbotFrdBin < Thor
|
|
314
316
|
class ExampleBot < SlackbotFrd::Bot
|
315
317
|
def add_callbacks(slack_connection)
|
316
318
|
# When a user sends a message, echo that message back in reverse
|
317
|
-
slack_connection.on_message do |user
|
318
|
-
slack_connection.send_message(channel, message.reverse)
|
319
|
+
slack_connection.on_message do |user:, channel:, message:, timestamp:|
|
320
|
+
slack_connection.send_message(channel: channel, message: message.reverse)
|
319
321
|
end
|
320
322
|
|
321
323
|
# When a user joins a channel, greet that user as a bot
|
322
|
-
slack_connection.on_channel_joined do |user
|
324
|
+
slack_connection.on_channel_joined do |user:, channel:|
|
323
325
|
slack_connection.send_message_as_user(
|
324
|
-
channel,
|
325
|
-
":skull: ohai \#{user}, welcome to #\#{channel}! :ghost:",
|
326
|
-
"Graveyard Greeting Bot",
|
327
|
-
":graveyard:"
|
328
|
-
true
|
326
|
+
channel: channel,
|
327
|
+
message: ":skull: ohai \#{user}, welcome to #\#{channel}! :ghost:",
|
328
|
+
username: "Graveyard Greeting Bot",
|
329
|
+
emoji: ":graveyard:"
|
329
330
|
)
|
330
331
|
end
|
331
332
|
end
|
@@ -91,69 +91,72 @@ module SlackbotFrd
|
|
91
91
|
@on_disconnected_callbacks.push(block)
|
92
92
|
end
|
93
93
|
|
94
|
-
def on_message(user
|
94
|
+
def on_message(user: :any, channel: :any, &block)
|
95
95
|
wrap_user_or_channel_lookup_on_callback('on_message', user, channel) do
|
96
|
-
@on_message_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
|
96
|
+
@on_message_callbacks.add(user: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
def on_channel_left(user
|
100
|
+
def on_channel_left(user: :any, channel: :any, &block)
|
101
101
|
wrap_user_or_channel_lookup_on_callback('on_message_channel_left', user, channel) do
|
102
|
-
@on_channel_left_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
|
102
|
+
@on_channel_left_callbacks.add(user: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
-
def on_channel_joined(user
|
106
|
+
def on_channel_joined(user: :any, channel: :any, &block)
|
107
107
|
wrap_user_or_channel_lookup_on_callback('on_message_channel_joined', user, channel) do
|
108
108
|
u = user_name_to_id(user)
|
109
109
|
c = channel_name_to_id(channel)
|
110
|
-
@on_channel_joined_callbacks.add(u, c, block)
|
110
|
+
@on_channel_joined_callbacks.add(user: u, channel: c, callback: block)
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
def send_message(channel,
|
115
|
-
|
116
|
-
|
114
|
+
def send_message(channel:, message:, username: nil, avatar_emoji: nil, avatar_url: nil)
|
115
|
+
if username && (avatar_emoji || avatar_url)
|
116
|
+
send_message_as_bot(
|
117
|
+
channel: channel,
|
118
|
+
message: message,
|
119
|
+
username: username,
|
120
|
+
avatar_emoji: avatar_emoji,
|
121
|
+
avatar_url: avatar_url
|
122
|
+
)
|
123
|
+
else
|
124
|
+
send_message_as_user(channel: channel, message: message)
|
117
125
|
end
|
126
|
+
end
|
118
127
|
|
119
|
-
|
128
|
+
def delete_message(channel:, timestamp:)
|
129
|
+
SlackbotFrd::Log.debug("#{self.class}: Deleting message with timestamp '#{timestamp}' from channel '#{channel}'")
|
120
130
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
text: message
|
127
|
-
}.to_json)
|
131
|
+
resp = SlackbotFrd::SlackMethods::ChatDelete.delete(
|
132
|
+
token: @token,
|
133
|
+
channel: channel_name_to_id(channel),
|
134
|
+
timestamp: timestamp
|
135
|
+
)
|
128
136
|
|
129
|
-
|
130
|
-
rescue SocketError => e
|
131
|
-
log_and_add_to_error_file(socket_error_message(e))
|
132
|
-
end
|
137
|
+
SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
|
133
138
|
end
|
134
139
|
|
135
|
-
def
|
136
|
-
SlackbotFrd::Log.debug("#{self.class}:
|
140
|
+
def post_reaction(name:, channel: nil, timestamp: nil)
|
141
|
+
SlackbotFrd::Log.debug("#{self.class}: Posting reaction '#{name}' to channel '#{channel}' with timestamp '#{timestamp}'")
|
137
142
|
|
138
|
-
resp = SlackbotFrd::SlackMethods::
|
139
|
-
@token,
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
avatar,
|
144
|
-
avatar_is_emoji
|
143
|
+
resp = SlackbotFrd::SlackMethods::ReactionsAdd.add(
|
144
|
+
token: @token,
|
145
|
+
name: name,
|
146
|
+
channel: channel_name_to_id(channel),
|
147
|
+
timestamp: timestamp
|
145
148
|
)
|
146
149
|
|
147
150
|
SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
|
148
151
|
end
|
149
152
|
|
150
|
-
def invite_user(user, channel)
|
153
|
+
def invite_user(user: user, channel: channel)
|
151
154
|
SlackbotFrd::Log.debug("#{self.class}: Inviting user '#{user}' to channel '#{channel}'")
|
152
155
|
|
153
156
|
resp = SlackbotFrd::SlackMethods::ChannelsInvite.invite(
|
154
|
-
@token,
|
155
|
-
user_name_to_id(user),
|
156
|
-
channel_name_to_id(channel),
|
157
|
+
token: @token,
|
158
|
+
user: user_name_to_id(user),
|
159
|
+
channel: channel_name_to_id(channel),
|
157
160
|
)
|
158
161
|
|
159
162
|
SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
|
@@ -220,6 +223,44 @@ module SlackbotFrd
|
|
220
223
|
@channel_name_to_id[nc]
|
221
224
|
end
|
222
225
|
|
226
|
+
private
|
227
|
+
def send_message_as_user(channel: channel, message: message)
|
228
|
+
unless @ws
|
229
|
+
log_and_add_to_error_file("Cannot send message '#{message}' as user to channel '#{channel}' because not connected to wss stream")
|
230
|
+
end
|
231
|
+
|
232
|
+
SlackbotFrd::Log.debug("#{self.class}: Sending message '#{message}' as user to channel '#{channel}'")
|
233
|
+
|
234
|
+
begin
|
235
|
+
resp = @ws.send({
|
236
|
+
id: event_id,
|
237
|
+
type: "message",
|
238
|
+
channel: channel_name_to_id(channel),
|
239
|
+
text: message
|
240
|
+
}.to_json)
|
241
|
+
|
242
|
+
SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
|
243
|
+
rescue SocketError => e
|
244
|
+
log_and_add_to_error_file(socket_error_message(e))
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
private
|
249
|
+
def send_message_as_bot(channel:, message:, username:, avatar_emoji: nil, avatar_url: nil)
|
250
|
+
SlackbotFrd::Log.debug("#{self.class}: Sending message '#{message}' as bot user '#{username}' to channel '#{channel}'")
|
251
|
+
|
252
|
+
resp = SlackbotFrd::SlackMethods::ChatPostMessage.postMessage(
|
253
|
+
token: @token,
|
254
|
+
channel: channel_name_to_id(channel),
|
255
|
+
message: message,
|
256
|
+
username: username,
|
257
|
+
avatar_emoji: avatar_emoji,
|
258
|
+
avatar_url: avatar_url
|
259
|
+
)
|
260
|
+
|
261
|
+
SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
|
262
|
+
end
|
263
|
+
|
223
264
|
private
|
224
265
|
def wrap_user_or_channel_lookup_on_callback(callback_name, user, channel)
|
225
266
|
begin
|
@@ -227,7 +268,7 @@ module SlackbotFrd
|
|
227
268
|
rescue SlackbotFrd::InvalidChannelError => e
|
228
269
|
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")
|
229
270
|
rescue SlackbotFrd::InvalidUserError => e
|
230
|
-
log_and_add_to_error_file("Unable to add #{callback_name} callback for user '#{user}'. Lookup of
|
271
|
+
log_and_add_to_error_file("Unable to add #{callback_name} callback for user '#{user}'. Lookup of user name to ID failed. Check network connection and ensure user exists")
|
231
272
|
end
|
232
273
|
end
|
233
274
|
|
@@ -268,6 +309,7 @@ module SlackbotFrd
|
|
268
309
|
user = :bot if message["subtype"] == "bot_message"
|
269
310
|
channel = message["channel"]
|
270
311
|
text = message["text"]
|
312
|
+
ts = message["ts"]
|
271
313
|
|
272
314
|
unless user
|
273
315
|
SlackbotFrd::Log.warn("#{self.class}: Chat message doesn't include user! message: #{message}")
|
@@ -279,7 +321,7 @@ module SlackbotFrd
|
|
279
321
|
return
|
280
322
|
end
|
281
323
|
|
282
|
-
@on_message_callbacks.where_include_all(user, channel).each do |callback|
|
324
|
+
@on_message_callbacks.where_include_all(user: user, channel: channel).each do |callback|
|
283
325
|
# instance_exec allows the user to call send_message and send_message_as_user
|
284
326
|
# without prefixing like this: slack_connection.send_message()
|
285
327
|
#
|
@@ -287,7 +329,12 @@ module SlackbotFrd
|
|
287
329
|
# for now we aren't going to do it
|
288
330
|
#
|
289
331
|
#instance_exec(user_id_to_name(user), channel_id_to_name(channel), text, &callback)
|
290
|
-
callback.call(
|
332
|
+
callback.call(
|
333
|
+
user: user_id_to_name(user),
|
334
|
+
channel: channel_id_to_name(channel),
|
335
|
+
message: text,
|
336
|
+
timestamp: ts
|
337
|
+
)
|
291
338
|
end
|
292
339
|
end
|
293
340
|
|
@@ -297,8 +344,8 @@ module SlackbotFrd
|
|
297
344
|
user = message["user"]
|
298
345
|
user = :bot if message["subtype"] == "bot_message"
|
299
346
|
channel = message["channel"]
|
300
|
-
@on_channel_joined_callbacks.where_include_all(user, channel).each do |callback|
|
301
|
-
callback.call(user_id_to_name(user), channel_id_to_name(channel))
|
347
|
+
@on_channel_joined_callbacks.where_include_all(user: user, channel: channel).each do |callback|
|
348
|
+
callback.call(user: user_id_to_name(user), channel: channel_id_to_name(channel))
|
302
349
|
end
|
303
350
|
end
|
304
351
|
|
@@ -308,8 +355,8 @@ module SlackbotFrd
|
|
308
355
|
user = message["user"]
|
309
356
|
user = :bot if message["subtype"] == "bot_message"
|
310
357
|
channel = message["channel"]
|
311
|
-
@on_channel_left_callbacks.where_include_all(user, channel).each do |callback|
|
312
|
-
callback.call(user_id_to_name(user), channel_id_to_name(channel))
|
358
|
+
@on_channel_left_callbacks.where_include_all(user: user, channel: channel).each do |callback|
|
359
|
+
callback.call(user: user_id_to_name(user), channel: channel_id_to_name(channel))
|
313
360
|
end
|
314
361
|
end
|
315
362
|
|
@@ -8,7 +8,7 @@ module SlackbotFrd
|
|
8
8
|
@conditions[:any][:any] = []
|
9
9
|
end
|
10
10
|
|
11
|
-
def init(user
|
11
|
+
def init(user:, channel:)
|
12
12
|
unless user
|
13
13
|
Log::error("#{self.class}: Invalid user '#{user}'")
|
14
14
|
raise InvalidUserError.new
|
@@ -23,13 +23,13 @@ module SlackbotFrd
|
|
23
23
|
@conditions[user][channel] ||= []
|
24
24
|
end
|
25
25
|
|
26
|
-
def add(user
|
27
|
-
init(user, channel)
|
26
|
+
def add(user:, channel:, callback:)
|
27
|
+
init(user: user, channel: channel)
|
28
28
|
@conditions[user][channel].push(callback)
|
29
29
|
end
|
30
30
|
|
31
|
-
def where(user
|
32
|
-
init(user, channel)
|
31
|
+
def where(user:, channel:)
|
32
|
+
init(user: user, channel: channel)
|
33
33
|
@conditions[user][channel] || []
|
34
34
|
end
|
35
35
|
|
@@ -37,8 +37,8 @@ module SlackbotFrd
|
|
37
37
|
@conditions[:any][:any] || []
|
38
38
|
end
|
39
39
|
|
40
|
-
def where_include_all(user
|
41
|
-
init(user, channel)
|
40
|
+
def where_include_all(user:, channel:)
|
41
|
+
init(user: user, channel: channel)
|
42
42
|
retval = @conditions[:any][:any].dup || []
|
43
43
|
retval.concat(@conditions[user][:any] || [])
|
44
44
|
retval.concat(@conditions[:any][channel] || [])
|
@@ -9,11 +9,11 @@ module SlackbotFrd
|
|
9
9
|
|
10
10
|
attr_reader :response
|
11
11
|
|
12
|
-
def self.members(token
|
13
|
-
ChannelsInfo.new(token, channel).connect.members
|
12
|
+
def self.members(token:, channel:)
|
13
|
+
ChannelsInfo.new(token: token, channel: channel).connect.members
|
14
14
|
end
|
15
15
|
|
16
|
-
def initialize(token
|
16
|
+
def initialize(token:, channel:)
|
17
17
|
@token = token
|
18
18
|
@channel = channel
|
19
19
|
end
|
@@ -9,11 +9,11 @@ module SlackbotFrd
|
|
9
9
|
|
10
10
|
attr_reader :response
|
11
11
|
|
12
|
-
def self.invite(token
|
13
|
-
ChannelsInvite.new(token, user, channel).run
|
12
|
+
def self.invite(token:, user:, channel:)
|
13
|
+
ChannelsInvite.new(token: token, user: user, channel: channel).run
|
14
14
|
end
|
15
15
|
|
16
|
-
def initialize(token
|
16
|
+
def initialize(token:, user:, channel:)
|
17
17
|
@token = token
|
18
18
|
@user = user
|
19
19
|
@channel = channel
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module SlackbotFrd
|
5
|
+
module SlackMethods
|
6
|
+
class ChatDelete
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'https://slack.com/api/chat.delete'
|
9
|
+
|
10
|
+
def self.delete(token:, channel:, timestamp:)
|
11
|
+
r = ChatDelete.new(token: token, channel: channel, timestamp: timestamp)
|
12
|
+
r.delete
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(token:, channel:, timestamp:)
|
16
|
+
@token = token
|
17
|
+
@channel = channel
|
18
|
+
@timestamp = timestamp
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete
|
22
|
+
body = {
|
23
|
+
token: @token,
|
24
|
+
channel: @channel,
|
25
|
+
ts: @timestamp,
|
26
|
+
}
|
27
|
+
|
28
|
+
@response = self.class.post('', :body => body)
|
29
|
+
@response
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -7,18 +7,25 @@ module SlackbotFrd
|
|
7
7
|
include HTTParty
|
8
8
|
base_uri 'https://slack.com/api/chat.postMessage'
|
9
9
|
|
10
|
-
def self.postMessage(token
|
11
|
-
r = ChatPostMessage.new(
|
10
|
+
def self.postMessage(token:, channel:, message:, username: nil, avatar_emoji: nil, avatar_url: nil)
|
11
|
+
r = ChatPostMessage.new(
|
12
|
+
token: token,
|
13
|
+
channel: channel,
|
14
|
+
message: message,
|
15
|
+
username: username,
|
16
|
+
avatar_emoji: avatar_emoji,
|
17
|
+
avatar_url: avatar_url
|
18
|
+
)
|
12
19
|
r.postMessage
|
13
20
|
end
|
14
21
|
|
15
|
-
def initialize(token
|
22
|
+
def initialize(token:, channel:, message:, username: nil, avatar_emoji: nil, avatar_url: nil)
|
16
23
|
@token = token
|
17
24
|
@channel = channel
|
18
25
|
@message = message
|
19
26
|
@username = username
|
20
|
-
@
|
21
|
-
@
|
27
|
+
@avatar_emoji = avatar_emoji
|
28
|
+
@avatar_url = avatar_url
|
22
29
|
end
|
23
30
|
|
24
31
|
def postMessage
|
@@ -31,8 +38,8 @@ module SlackbotFrd
|
|
31
38
|
if @username
|
32
39
|
body.merge!({ username: @username })
|
33
40
|
|
34
|
-
if @
|
35
|
-
body.merge!({ icon_emoji: @
|
41
|
+
if @avatar_emoji
|
42
|
+
body.merge!({ icon_emoji: @avatar_emoji })
|
36
43
|
else
|
37
44
|
body.merge!({ icon_url: @avatar })
|
38
45
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module SlackbotFrd
|
5
|
+
module SlackMethods
|
6
|
+
class ReactionsAdd
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'https://slack.com/api/reactions.add'
|
9
|
+
|
10
|
+
def self.add(token:, name:, channel: nil, timestamp: nil)
|
11
|
+
r = ReactionsAdd.new(token: token, name: name, channel: channel, timestamp: timestamp)
|
12
|
+
r.add
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(token:, name:, channel: nil, timestamp: nil)
|
16
|
+
@token = token
|
17
|
+
@name = name
|
18
|
+
@channel = channel
|
19
|
+
@timestamp = timestamp
|
20
|
+
end
|
21
|
+
|
22
|
+
def add
|
23
|
+
body = {
|
24
|
+
token: @token,
|
25
|
+
name: @name
|
26
|
+
}
|
27
|
+
|
28
|
+
if @channel && @timestamp
|
29
|
+
body.merge!({
|
30
|
+
channel: @channel,
|
31
|
+
timestamp: @timestamp
|
32
|
+
})
|
33
|
+
end
|
34
|
+
|
35
|
+
@response = self.class.post('', :body => body)
|
36
|
+
@response
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Porter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -175,8 +175,10 @@ files:
|
|
175
175
|
- lib/slackbot_frd/slack_methods/channels_info.rb
|
176
176
|
- lib/slackbot_frd/slack_methods/channels_invite.rb
|
177
177
|
- lib/slackbot_frd/slack_methods/channels_list.rb
|
178
|
+
- lib/slackbot_frd/slack_methods/chat_delete.rb
|
178
179
|
- lib/slackbot_frd/slack_methods/chat_post_message.rb
|
179
180
|
- lib/slackbot_frd/slack_methods/im_channels_list.rb
|
181
|
+
- lib/slackbot_frd/slack_methods/reactions_add.rb
|
180
182
|
- lib/slackbot_frd/slack_methods/rtm_start.rb
|
181
183
|
- lib/slackbot_frd/slack_methods/users_list.rb
|
182
184
|
homepage: https://github.com/FreedomBen/slackbot_frd
|