slack_message 2.3.0 → 3.0.1
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/.github/workflows/main.yml +1 -1
- data/.gitignore +1 -0
- data/CHANGELOG.md +21 -0
- data/README.md +32 -308
- data/docs/01_configuration.md +116 -0
- data/docs/02_posting_a_message.md +134 -0
- data/docs/03_message_dsl.md +326 -0
- data/docs/04_editing_messages.md +88 -0
- data/docs/05_deleting_messages.md +45 -0
- data/docs/06_notifying_users.md +62 -0
- data/docs/07_testing.md +49 -0
- data/docs/_config.yml +6 -0
- data/docs/index.md +49 -0
- data/lib/slack_message/api.rb +101 -23
- data/lib/slack_message/configuration.rb +11 -1
- data/lib/slack_message/dsl.rb +7 -5
- data/lib/slack_message/error_handling.rb +124 -0
- data/lib/slack_message/response.rb +42 -0
- data/lib/slack_message/rspec.rb +45 -21
- data/lib/slack_message.rb +37 -5
- data/slack_message.gemspec +2 -2
- data/spec/slack_message_spec.rb +62 -8
- metadata +14 -4
- data/Gemfile.lock +0 -40
data/lib/slack_message/api.rb
CHANGED
@@ -10,6 +10,10 @@ module SlackMessage::Api
|
|
10
10
|
raise ArgumentError, "Tried to find profile by invalid email address '#{email}'"
|
11
11
|
end
|
12
12
|
|
13
|
+
if SlackMessage::Configuration.debugging?
|
14
|
+
warn [email, profile].inspect
|
15
|
+
end
|
16
|
+
|
13
17
|
response = look_up_user_by_email(email, profile)
|
14
18
|
|
15
19
|
if response.code != "200"
|
@@ -24,18 +28,11 @@ module SlackMessage::Api
|
|
24
28
|
raise SlackMessage::ApiError, "Unable to parse JSON response from Slack API\n#{response.body}"
|
25
29
|
end
|
26
30
|
|
27
|
-
|
28
|
-
raise SlackMessage::ApiError, "Received an error because your authentication token isn't properly configured."
|
29
|
-
elsif payload.include?("error") && payload["error"] == "users_not_found"
|
30
|
-
raise SlackMessage::ApiError, "Couldn't find a user with the email '#{email}'."
|
31
|
-
elsif payload.include?("error")
|
32
|
-
raise SlackMessage::ApiError, "Received error response from Slack during user lookup:\n#{response.body}"
|
33
|
-
end
|
34
|
-
|
31
|
+
SlackMessage::ErrorHandling.raise_user_lookup_errors(response, target, profile)
|
35
32
|
payload["user"]["id"]
|
36
33
|
end
|
37
34
|
|
38
|
-
def post(payload, target, profile)
|
35
|
+
def post(payload, target, profile, time)
|
39
36
|
params = {
|
40
37
|
channel: target,
|
41
38
|
username: payload.custom_bot_name || profile[:name],
|
@@ -56,25 +53,68 @@ module SlackMessage::Api
|
|
56
53
|
raise ArgumentError, "Couldn't figure out icon '#{icon}'. Try :emoji: or a URL."
|
57
54
|
end
|
58
55
|
|
56
|
+
if !time.nil?
|
57
|
+
params[:post_at] = time.to_i
|
58
|
+
|
59
|
+
if payload.custom_bot_name || payload.custom_bot_icon
|
60
|
+
raise ArgumentError, "Sorry, setting an image / emoji icon for scheduled messages isn't supported."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
if SlackMessage::Configuration.debugging?
|
65
|
+
warn params.inspect
|
66
|
+
end
|
67
|
+
|
59
68
|
response = post_message(profile, params)
|
69
|
+
|
70
|
+
SlackMessage::ErrorHandling.raise_post_response_errors(response, params, profile)
|
71
|
+
SlackMessage::Response.new(response, profile[:handle])
|
72
|
+
end
|
73
|
+
|
74
|
+
def update(payload, message, profile)
|
75
|
+
params = {
|
76
|
+
channel: message.channel,
|
77
|
+
ts: message.timestamp,
|
78
|
+
blocks: payload.render,
|
79
|
+
text: payload.custom_notification
|
80
|
+
}
|
81
|
+
|
82
|
+
if params[:blocks].length == 0
|
83
|
+
raise ArgumentError, "Tried to send an entirely empty message."
|
84
|
+
end
|
85
|
+
|
86
|
+
if SlackMessage::Configuration.debugging?
|
87
|
+
warn params.inspect
|
88
|
+
end
|
89
|
+
|
90
|
+
response = update_message(profile, params)
|
60
91
|
body = JSON.parse(response.body)
|
61
92
|
error = body.fetch("error", "")
|
62
93
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
94
|
+
SlackMessage::ErrorHandling.raise_post_response_errors(response, message, profile)
|
95
|
+
SlackMessage::Response.new(response, profile[:handle])
|
96
|
+
end
|
97
|
+
|
98
|
+
def delete(message, profile)
|
99
|
+
params = if message.scheduled?
|
100
|
+
{
|
101
|
+
channel: message.channel,
|
102
|
+
scheduled_message_id: message.scheduled_message_id,
|
103
|
+
}
|
104
|
+
else
|
105
|
+
{
|
106
|
+
channel: message.channel,
|
107
|
+
ts: message.timestamp,
|
108
|
+
}
|
76
109
|
end
|
77
110
|
|
111
|
+
if SlackMessage::Configuration.debugging?
|
112
|
+
warn params.inspect
|
113
|
+
end
|
114
|
+
|
115
|
+
response = delete_message(profile, params)
|
116
|
+
|
117
|
+
SlackMessage::ErrorHandling.raise_delete_response_errors(response, message, profile)
|
78
118
|
response
|
79
119
|
end
|
80
120
|
|
@@ -84,6 +124,7 @@ module SlackMessage::Api
|
|
84
124
|
|
85
125
|
def look_up_user_by_email(email, profile)
|
86
126
|
uri = URI("https://slack.com/api/users.lookupByEmail?email=#{email}")
|
127
|
+
|
87
128
|
request = Net::HTTP::Get.new(uri).tap do |req|
|
88
129
|
req['Authorization'] = "Bearer #{profile[:api_token]}"
|
89
130
|
req['Content-type'] = "application/json; charset=utf-8"
|
@@ -95,7 +136,44 @@ module SlackMessage::Api
|
|
95
136
|
end
|
96
137
|
|
97
138
|
def post_message(profile, params)
|
98
|
-
uri =
|
139
|
+
uri = if params.has_key?(:post_at)
|
140
|
+
URI("https://slack.com/api/chat.scheduleMessage")
|
141
|
+
else
|
142
|
+
URI("https://slack.com/api/chat.postMessage")
|
143
|
+
end
|
144
|
+
|
145
|
+
request = Net::HTTP::Post.new(uri).tap do |req|
|
146
|
+
req['Authorization'] = "Bearer #{profile[:api_token]}"
|
147
|
+
req['Content-type'] = "application/json; charset=utf-8"
|
148
|
+
req.body = params.to_json
|
149
|
+
end
|
150
|
+
|
151
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
152
|
+
http.request(request)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def update_message(profile, params)
|
157
|
+
uri = URI("https://slack.com/api/chat.update")
|
158
|
+
|
159
|
+
request = Net::HTTP::Post.new(uri).tap do |req|
|
160
|
+
req['Authorization'] = "Bearer #{profile[:api_token]}"
|
161
|
+
req['Content-type'] = "application/json; charset=utf-8"
|
162
|
+
req.body = params.to_json
|
163
|
+
end
|
164
|
+
|
165
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
166
|
+
http.request(request)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def delete_message(profile, params)
|
171
|
+
uri = if params.has_key?(:scheduled_message_id)
|
172
|
+
URI("https://slack.com/api/chat.deleteScheduledMessage")
|
173
|
+
else
|
174
|
+
URI("https://slack.com/api/chat.delete")
|
175
|
+
end
|
176
|
+
|
99
177
|
request = Net::HTTP::Post.new(uri).tap do |req|
|
100
178
|
req['Authorization'] = "Bearer #{profile[:api_token]}"
|
101
179
|
req['Content-type'] = "application/json; charset=utf-8"
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module SlackMessage::Configuration
|
2
2
|
@@profiles = {}
|
3
|
+
@@debug = false
|
3
4
|
|
4
5
|
def self.reset
|
5
|
-
@@profiles
|
6
|
+
@@profiles = {}
|
7
|
+
@@debug = false
|
6
8
|
end
|
7
9
|
|
8
10
|
def self.configure
|
@@ -36,4 +38,12 @@ module SlackMessage::Configuration
|
|
36
38
|
|
37
39
|
@@profiles[handle]
|
38
40
|
end
|
41
|
+
|
42
|
+
def self.debug
|
43
|
+
@@debug = true
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.debugging?
|
47
|
+
@@debug
|
48
|
+
end
|
39
49
|
end
|
data/lib/slack_message/dsl.rb
CHANGED
@@ -111,12 +111,14 @@ class SlackMessage::Dsl
|
|
111
111
|
# replace emails w/ real user IDs
|
112
112
|
def enrich_text(text_body)
|
113
113
|
text_body.scan(SlackMessage::EMAIL_TAG_PATTERN).each do |email_tag|
|
114
|
-
|
115
|
-
|
114
|
+
begin
|
115
|
+
raw_email = email_tag.gsub(/[><]/, '')
|
116
|
+
user_id = SlackMessage::Api::user_id_for(raw_email, profile)
|
116
117
|
|
117
|
-
|
118
|
-
|
119
|
-
|
118
|
+
text_body.gsub!(email_tag, "<@#{user_id}>") if user_id
|
119
|
+
rescue SlackMessage::ApiError => e
|
120
|
+
# swallow errors for not-found users
|
121
|
+
end
|
120
122
|
end
|
121
123
|
|
122
124
|
text_body
|
@@ -0,0 +1,124 @@
|
|
1
|
+
class SlackMessage::ErrorHandling
|
2
|
+
PERMISSIONS_ERRORS = ["token_revoked", "token_expired", "invalid_auth", "not_authed",
|
3
|
+
"team_access_not_granted", "no_permission", "missing_scope",
|
4
|
+
"not_allowed_token_type", "ekm_access_denied"]
|
5
|
+
|
6
|
+
def self.raise_post_response_errors(response, params, profile)
|
7
|
+
body = JSON.parse(response.body)
|
8
|
+
error = body.fetch("error", "")
|
9
|
+
|
10
|
+
if ["invalid_blocks", "invalid_blocks_format"].include?(error)
|
11
|
+
raise SlackMessage::ApiError, "Couldn't send Slack message because the serialized message had an invalid format"
|
12
|
+
elsif error == "channel_not_found"
|
13
|
+
raise SlackMessage::ApiError, "Tried to send Slack message to non-existent channel or user '#{params[:channel]}'"
|
14
|
+
|
15
|
+
# scheduling messages
|
16
|
+
elsif error == "invalid_time"
|
17
|
+
raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested an invalid time '#{params[:post_at]}'"
|
18
|
+
elsif error == "time_in_past"
|
19
|
+
raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested a time in the past (or too close to now) '#{params[:post_at]}'"
|
20
|
+
elsif error == "time_too_far"
|
21
|
+
raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested a time more than 120 days in the future '#{params[:post_at]}'"
|
22
|
+
|
23
|
+
|
24
|
+
elsif PERMISSIONS_ERRORS.include?(error)
|
25
|
+
raise SlackMessage::ApiError, "Couldn't send Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
|
26
|
+
elsif error == "message_too_long"
|
27
|
+
raise SlackMessage::ApiError, "Tried to send Slack message, but the message was too long"
|
28
|
+
elsif error == "invalid_arguments"
|
29
|
+
raise SlackMessage::ApiError, "Tried to send Slack message with invalid payload"
|
30
|
+
elsif ["rate_limited", "ratelimited"].include?(error)
|
31
|
+
raise SlackMessage::ApiError, "Couldn't send Slack message because you've reached your rate limit"
|
32
|
+
elsif response.code == "302"
|
33
|
+
raise SlackMessage::ApiError, "Got 302 response while posting to Slack. Check your API key for profile '#{profile[:handle]}'"
|
34
|
+
elsif response.code != "200"
|
35
|
+
raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
|
36
|
+
elsif !(error.nil? || error == "")
|
37
|
+
raise SlackMessage::ApiError, "Received error response from Slack during message posting:\n#{response.body}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.raise_update_response_errors(response, message, profile)
|
42
|
+
body = JSON.parse(response.body)
|
43
|
+
error = body.fetch("error", "")
|
44
|
+
|
45
|
+
if ["invalid_blocks", "invalid_blocks_format"].include?(error)
|
46
|
+
raise SlackMessage::ApiError, "Couldn't update Slack message because the serialized message had an invalid format"
|
47
|
+
elsif error == "channel_not_found"
|
48
|
+
raise SlackMessage::ApiError, "Tried to update Slack message to non-existent channel or user '#{message.channel}'"
|
49
|
+
|
50
|
+
elsif error == "message_not_found"
|
51
|
+
raise SlackMessage::ApiError, "Tried to update Slack message, but the message wasn't found (timestamp '#{message.timestamp}' for channel '#{message.channel}'"
|
52
|
+
elsif error == "cant_update_message"
|
53
|
+
raise SlackMessage::ApiError, "Couldn't update message because the message type isn't able to be updated, or #{profile[:handle]} isn't allowed to update it"
|
54
|
+
elsif error == "edit_window_closed"
|
55
|
+
raise SlackMessage::ApiError, "Couldn't update message because it's too old"
|
56
|
+
|
57
|
+
|
58
|
+
elsif PERMISSIONS_ERRORS.include?(error)
|
59
|
+
raise SlackMessage::ApiError, "Couldn't update Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
|
60
|
+
elsif error == "message_too_long"
|
61
|
+
raise SlackMessage::ApiError, "Tried to update Slack message, but the message was too long"
|
62
|
+
elsif error == "invalid_arguments"
|
63
|
+
raise SlackMessage::ApiError, "Tried to update Slack message with invalid payload"
|
64
|
+
elsif ["rate_limited", "ratelimited"].include?(error)
|
65
|
+
raise SlackMessage::ApiError, "Couldn't update Slack message because you've reached your rate limit"
|
66
|
+
elsif response.code == "302"
|
67
|
+
raise SlackMessage::ApiError, "Got 302 response while updating a message. Check your API key for profile '#{profile[:handle]}'"
|
68
|
+
elsif response.code != "200"
|
69
|
+
raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
|
70
|
+
elsif !(error.nil? || error == "")
|
71
|
+
raise SlackMessage::ApiError, "Received error response from Slack during message update:\n#{response.body}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.raise_delete_response_errors(response, message, profile)
|
76
|
+
body = JSON.parse(response.body)
|
77
|
+
error = body.fetch("error", "")
|
78
|
+
|
79
|
+
if error == "channel_not_found"
|
80
|
+
raise SlackMessage::ApiError, "Tried to delete Slack message in non-existent channel '#{message.channel}'"
|
81
|
+
|
82
|
+
elsif error == "invalid_scheduled_message_id"
|
83
|
+
raise SlackMessage::ApiError, "Can't delete message because the ID was invalid, or the message has already posted (#{message.scheduled_message_id})"
|
84
|
+
elsif error == "message_not_found"
|
85
|
+
raise SlackMessage::ApiError, "Tried to delete Slack message, but the message wasn't found (timestamp '#{message.timestamp}' for channel '#{message.channel}')"
|
86
|
+
elsif error == "cant_delete_message"
|
87
|
+
raise SlackMessage::ApiError, "Can't delete message because '#{profile[:handle]}' doesn't have permission to"
|
88
|
+
elsif error == "compliance_exports_prevent_deletion"
|
89
|
+
raise SlackMessage::ApiError, "Can't delete message because team compliance settings prevent it"
|
90
|
+
|
91
|
+
|
92
|
+
elsif PERMISSIONS_ERRORS.include?(error)
|
93
|
+
raise SlackMessage::ApiError, "Couldn't delete Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
|
94
|
+
elsif ["rate_limited", "ratelimited"].include?(error)
|
95
|
+
raise SlackMessage::ApiError, "Couldn't delete Slack message because you've reached your rate limit"
|
96
|
+
elsif response.code == "302"
|
97
|
+
raise SlackMessage::ApiError, "Got 302 response while deleting a message. Check your API key for profile '#{profile[:handle]}'"
|
98
|
+
elsif response.code != "200"
|
99
|
+
raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
|
100
|
+
elsif !(error.nil? || error == "")
|
101
|
+
raise SlackMessage::ApiError, "Received error response from Slack during message delete:\n#{response.body}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.raise_user_lookup_response_errors(payload)
|
106
|
+
error = payload["error"]
|
107
|
+
|
108
|
+
if error == "users_not_found"
|
109
|
+
raise SlackMessage::ApiError, "Couldn't find a user with the email '#{email}'"
|
110
|
+
|
111
|
+
|
112
|
+
elsif PERMISSIONS_ERRORS.include?(error)
|
113
|
+
raise SlackMessage::ApiError, "Couldn't look up users because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
|
114
|
+
elsif error
|
115
|
+
raise SlackMessage::ApiError, "Received error response from Slack during user lookup:\n#{response.body}"
|
116
|
+
elsif response.code == "302"
|
117
|
+
raise SlackMessage::ApiError, "Got 302 response during user lookup. Check your API key for profile '#{profile[:handle]}'"
|
118
|
+
elsif response.code != "200"
|
119
|
+
raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
|
120
|
+
elsif !(error.nil? || error == "")
|
121
|
+
raise SlackMessage::ApiError, "Received error response from Slack during user lookup:\n#{response.body}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class SlackMessage::Response
|
2
|
+
attr_reader :channel, :timestamp, :profile_handle, :scheduled_message_id, :original_response
|
3
|
+
|
4
|
+
def initialize(api_response, profile_handle)
|
5
|
+
@original_response = JSON.parse(api_response.body)
|
6
|
+
@ok = @original_response["ok"]
|
7
|
+
@channel = @original_response["channel"]
|
8
|
+
|
9
|
+
@timestamp = @original_response["ts"]
|
10
|
+
@scheduled_message_id = @original_response["scheduled_message_id"]
|
11
|
+
|
12
|
+
@profile_handle = profile_handle
|
13
|
+
end
|
14
|
+
|
15
|
+
def marshal_dump
|
16
|
+
[ @profile_handle, @channel, @timestamp, @original_response, @ok, @original_response ]
|
17
|
+
end
|
18
|
+
|
19
|
+
def marshal_load(data)
|
20
|
+
@profile_handle, @channel, @timestamp, @original_response, @ok, @original_response = data
|
21
|
+
end
|
22
|
+
|
23
|
+
def sent_to_user?
|
24
|
+
channel =~ /^D.*/ # users are D for DM, channels start w/ C
|
25
|
+
end
|
26
|
+
|
27
|
+
def scheduled?
|
28
|
+
!!scheduled_message_id
|
29
|
+
end
|
30
|
+
|
31
|
+
def inspect
|
32
|
+
identifier = if scheduled?
|
33
|
+
"scheduled_message_id=#{scheduled_message_id}"
|
34
|
+
else
|
35
|
+
"timestamp=#{timestamp}"
|
36
|
+
end
|
37
|
+
|
38
|
+
ok_msg = @ok ? "ok" : "error"
|
39
|
+
|
40
|
+
"<SlackMessage::Response #{ok_msg} profile_handle=:#{profile_handle} channel=#{channel} #{identifier}>"
|
41
|
+
end
|
42
|
+
end
|
data/lib/slack_message/rspec.rb
CHANGED
@@ -14,10 +14,15 @@ require 'rspec/mocks'
|
|
14
14
|
# it can be cleaned up properly.
|
15
15
|
#
|
16
16
|
|
17
|
+
# TODO: test helpers for scheduled messages, editing and deleting, and
|
18
|
+
# notification text. And realistically, overhaul all this.
|
19
|
+
|
17
20
|
module SlackMessage::RSpec
|
18
21
|
extend RSpec::Matchers::DSL
|
19
22
|
|
20
23
|
@@listeners = []
|
24
|
+
@@custom_response = {}
|
25
|
+
@@response_code = '200'
|
21
26
|
|
22
27
|
def self.register_expectation_listener(expectation_instance)
|
23
28
|
@@listeners << expectation_instance
|
@@ -27,6 +32,11 @@ module SlackMessage::RSpec
|
|
27
32
|
@@listeners.delete(expectation_instance)
|
28
33
|
end
|
29
34
|
|
35
|
+
def self.reset_custom_responses
|
36
|
+
@@custom_response = {}
|
37
|
+
@@response_code = '200'
|
38
|
+
end
|
39
|
+
|
30
40
|
FauxResponse = Struct.new(:code, :body)
|
31
41
|
|
32
42
|
def self.included(_)
|
@@ -36,22 +46,24 @@ module SlackMessage::RSpec
|
|
36
46
|
listener.record_call(params.merge(profile: profile))
|
37
47
|
end
|
38
48
|
|
39
|
-
response = {
|
40
|
-
"
|
41
|
-
"
|
42
|
-
"
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
49
|
+
response = {
|
50
|
+
"ok" => true,
|
51
|
+
"channel" => "C12345678",
|
52
|
+
"ts" => "1635863996.002300",
|
53
|
+
"message" => { "type"=>"message", "subtype"=>"bot_message",
|
54
|
+
"text"=>"foo",
|
55
|
+
"ts"=>"1635863996.002300",
|
56
|
+
"username"=>"SlackMessage",
|
57
|
+
"icons"=>{"emoji"=>":successkid:"},
|
58
|
+
"bot_id"=>"B1234567890",
|
59
|
+
"blocks"=> [{"type"=>"section",
|
60
|
+
"block_id"=>"hAh7",
|
61
|
+
"text"=>{"type"=>"mrkdwn", "text"=>"foo", "verbatim"=>false}}
|
62
|
+
]
|
63
|
+
}
|
64
|
+
}.merge(@@custom_response).to_json
|
65
|
+
|
66
|
+
return FauxResponse.new(@@response_code, response)
|
55
67
|
end
|
56
68
|
|
57
69
|
SlackMessage::Api.undef_method(:look_up_user_by_email)
|
@@ -61,6 +73,18 @@ module SlackMessage::RSpec
|
|
61
73
|
end
|
62
74
|
end
|
63
75
|
|
76
|
+
def self.respond_with(response = {}, code: '200')
|
77
|
+
raise ArgumentError, "custom response must be a hash" unless response.is_a? Hash
|
78
|
+
|
79
|
+
@@custom_response = response
|
80
|
+
@@response_code = code
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.reset_mock_response
|
84
|
+
@@custom_response = {}
|
85
|
+
@@response_code = '200'
|
86
|
+
end
|
87
|
+
|
64
88
|
# w/ channel
|
65
89
|
matcher :post_slack_message_to do |expected|
|
66
90
|
match do |actual|
|
@@ -206,11 +230,11 @@ module SlackMessage::RSpec
|
|
206
230
|
SlackMessage::RSpec.unregister_expectation_listener(self)
|
207
231
|
|
208
232
|
@captured_calls
|
209
|
-
.
|
210
|
-
.
|
211
|
-
.
|
212
|
-
.
|
213
|
-
.
|
233
|
+
.select { |call| !@channel || call[:channel] == @channel }
|
234
|
+
.select { |call| !@profile || [call[:profile][:handle], call[:username]].include?(@profile) }
|
235
|
+
.select { |call| !@content || call.fetch(:blocks).to_s =~ @content }
|
236
|
+
.select { |call| !@icon || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) == @icon }
|
237
|
+
.select { |call| !@icon_matching || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) =~ @icon_matching }
|
214
238
|
.any?
|
215
239
|
end
|
216
240
|
|
data/lib/slack_message.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module SlackMessage
|
2
|
+
require 'slack_message/response'
|
2
3
|
require 'slack_message/dsl'
|
4
|
+
require 'slack_message/error_handling'
|
3
5
|
require 'slack_message/api'
|
4
6
|
require 'slack_message/configuration'
|
5
7
|
|
@@ -21,7 +23,7 @@ module SlackMessage
|
|
21
23
|
Api.user_id_for(email, profile)
|
22
24
|
end
|
23
25
|
|
24
|
-
def self.post_to(target, as: :default, &block)
|
26
|
+
def self.post_to(target, as: :default, at: nil, &block)
|
25
27
|
profile = Configuration.profile(as)
|
26
28
|
|
27
29
|
payload = Dsl.new(block, profile).tap do |instance|
|
@@ -30,23 +32,53 @@ module SlackMessage
|
|
30
32
|
|
31
33
|
target = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN
|
32
34
|
|
33
|
-
Api.post(payload, target, profile)
|
35
|
+
Api.post(payload, target, profile, at)
|
34
36
|
end
|
35
37
|
|
36
|
-
def self.post_as(profile_name, &block)
|
38
|
+
def self.post_as(profile_name, at: nil, &block)
|
37
39
|
profile = Configuration.profile(profile_name)
|
38
40
|
if profile[:default_channel].nil?
|
39
41
|
raise ArgumentError, "Sorry, you need to specify a default_channel for profile #{profile_name} to use post_as"
|
40
42
|
end
|
41
43
|
|
44
|
+
target = profile[:default_channel]
|
42
45
|
payload = Dsl.new(block, profile).tap do |instance|
|
43
46
|
instance.instance_eval(&block)
|
44
47
|
end
|
45
48
|
|
46
|
-
target = profile[:default_channel]
|
47
49
|
target = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN
|
48
50
|
|
49
|
-
Api.post(payload, target, profile)
|
51
|
+
Api.post(payload, target, profile, at)
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.update(message, &block)
|
55
|
+
unless message.is_a?(SlackMessage::Response)
|
56
|
+
raise ArgumentError, "You must pass in a SlackMessage::Response to update a message"
|
57
|
+
end
|
58
|
+
|
59
|
+
if message.scheduled?
|
60
|
+
raise ArgumentError, "Sorry, scheduled messages cannot be updated. You will need to delete the message and schedule a new one."
|
61
|
+
end
|
62
|
+
|
63
|
+
profile = Configuration.profile(message.profile_handle)
|
64
|
+
payload = Dsl.new(block, profile).tap do |instance|
|
65
|
+
instance.instance_eval(&block)
|
66
|
+
end
|
67
|
+
|
68
|
+
Api.update(payload, message, profile)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.delete(message)
|
72
|
+
unless message.is_a?(SlackMessage::Response)
|
73
|
+
raise ArgumentError, "You must pass in a SlackMessage::Response to delete a message"
|
74
|
+
end
|
75
|
+
|
76
|
+
if message.sent_to_user?
|
77
|
+
raise ArgumentError, "It's not possible to delete messages sent directly to users."
|
78
|
+
end
|
79
|
+
|
80
|
+
profile = Configuration.profile(message.profile_handle)
|
81
|
+
Api.delete(message, profile)
|
50
82
|
end
|
51
83
|
|
52
84
|
def self.build(profile_name = :default, &block)
|
data/slack_message.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = 'slack_message'
|
3
|
-
gem.version = "
|
3
|
+
gem.version = "3.0.1"
|
4
4
|
gem.summary = "A nice DSL for composing rich messages in Slack"
|
5
5
|
gem.authors = ["Joe Mastey"]
|
6
6
|
gem.email = 'hello@joemastey.com'
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
"source_code_uri" => "http://github.com/jmmastey/slack_message",
|
19
19
|
}
|
20
20
|
|
21
|
-
gem.required_ruby_version = '>= 2.
|
21
|
+
gem.required_ruby_version = '>= 2.5.0'
|
22
22
|
|
23
23
|
gem.add_development_dependency "rspec", "3.10.0"
|
24
24
|
gem.add_development_dependency "pry", "0.14.1"
|
data/spec/slack_message_spec.rb
CHANGED
@@ -111,10 +111,6 @@ RSpec.describe SlackMessage do
|
|
111
111
|
}.to post_slack_message_with_icon_matching(/thisperson/)
|
112
112
|
end
|
113
113
|
|
114
|
-
it "lets you assert notification text" do
|
115
|
-
# TODO :|
|
116
|
-
end
|
117
|
-
|
118
114
|
it "can assert more generally too tbh" do
|
119
115
|
expect {
|
120
116
|
SlackMessage.post_to('#general') { text "foo" }
|
@@ -123,8 +119,6 @@ RSpec.describe SlackMessage do
|
|
123
119
|
end
|
124
120
|
|
125
121
|
describe "API convenience" do
|
126
|
-
let(:profile) { SlackMessage::Configuration.profile(:default) }
|
127
|
-
|
128
122
|
before do
|
129
123
|
SlackMessage.configure do |config|
|
130
124
|
config.clear_profiles!
|
@@ -142,13 +136,73 @@ RSpec.describe SlackMessage do
|
|
142
136
|
expect {
|
143
137
|
SlackMessage.post_to('#general') { text("Not Tagged: hello@joemastey.com ") }
|
144
138
|
}.to post_to_slack.with_content_matching(/hello@joemastey.com/)
|
139
|
+
end
|
145
140
|
|
146
|
-
|
147
|
-
allow(SlackMessage::Api).to receive(:user_id_for).and_raise(SlackMessage::ApiError)
|
141
|
+
it "is graceful about those failures" do
|
142
|
+
allow(SlackMessage::Api).to receive(:user_id_for).with('nuffin@nuffin.nuffin', any_args).and_raise(SlackMessage::ApiError)
|
143
|
+
allow(SlackMessage::Api).to receive(:user_id_for).with('hello@joemastey.com', any_args).and_return('ABC123')
|
148
144
|
|
149
145
|
expect {
|
150
146
|
SlackMessage.post_to('#general') { text("Not User: <nuffin@nuffin.nuffin>") }
|
151
147
|
}.to post_to_slack.with_content_matching(/\<nuffin@nuffin.nuffin\>/)
|
148
|
+
|
149
|
+
expect {
|
150
|
+
SlackMessage.post_to('#general') { text("Not User: <nuffin@nuffin.nuffin>, User: <hello@joemastey.com>") }
|
151
|
+
}.to post_to_slack.with_content_matching(/ABC123/)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "error handling" do
|
156
|
+
before do
|
157
|
+
SlackMessage.configure do |config|
|
158
|
+
config.clear_profiles!
|
159
|
+
config.add_profile(name: 'default profile', api_token: 'abc123')
|
160
|
+
config.add_profile(:schmoebot, name: 'Schmoe', api_token: 'abc123', icon: ':schmoebot:', default_channel: '#schmoes')
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
after do
|
165
|
+
SlackMessage::RSpec.reset_mock_response
|
166
|
+
end
|
167
|
+
|
168
|
+
it "raises nice error messages when API methods return errors" do
|
169
|
+
SlackMessage::RSpec.respond_with('error' => 'nuffin')
|
170
|
+
|
171
|
+
expect {
|
172
|
+
SlackMessage.post_to('#general') { text 'nuh uh' }
|
173
|
+
}.to raise_error(SlackMessage::ApiError)
|
174
|
+
|
175
|
+
expect {
|
176
|
+
SlackMessage.post_as(:schmoebot) { text 'nuh uh' }
|
177
|
+
}.to raise_error(SlackMessage::ApiError)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "raises for redirects" do
|
181
|
+
SlackMessage::RSpec.respond_with(code: '302')
|
182
|
+
|
183
|
+
expect {
|
184
|
+
SlackMessage.post_to('#general') { text 'nuh uh' }
|
185
|
+
}.to raise_error(SlackMessage::ApiError)
|
186
|
+
end
|
187
|
+
|
188
|
+
it "raises errors w/ updates too" do
|
189
|
+
message = SlackMessage.post_to('#general') { text 'nuh uh' }
|
190
|
+
|
191
|
+
SlackMessage::RSpec.respond_with('error' => 'bad choice')
|
192
|
+
|
193
|
+
expect {
|
194
|
+
SlackMessage.update(message) { text 'nuh uh' }
|
195
|
+
}.to raise_error(SlackMessage::ApiError)
|
196
|
+
end
|
197
|
+
|
198
|
+
it "even raises errors during deletes" do
|
199
|
+
message = SlackMessage.post_to('#general') { text 'nuh uh' }
|
200
|
+
|
201
|
+
SlackMessage::RSpec.respond_with('error' => 'bad choice')
|
202
|
+
|
203
|
+
expect {
|
204
|
+
SlackMessage.delete(message) { text 'nuh uh' }
|
205
|
+
}.to raise_error(SlackMessage::ApiError)
|
152
206
|
end
|
153
207
|
end
|
154
208
|
end
|