slack_message 2.4.0 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/CHANGELOG.md +16 -0
- data/README.md +28 -19
- data/docs/01_configuration.md +116 -0
- data/docs/02_posting_a_message.md +138 -0
- data/docs/03_message_dsl.md +387 -0
- data/docs/04_editing_messages.md +87 -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 +53 -0
- data/lib/slack_message/api.rb +83 -23
- data/lib/slack_message/dsl.rb +11 -0
- data/lib/slack_message/error_handling.rb +124 -0
- data/lib/slack_message/response.rb +42 -0
- data/lib/slack_message/rspec.rb +40 -16
- data/lib/slack_message.rb +33 -1
- data/slack_message.gemspec +1 -1
- data/spec/slack_message_spec.rb +62 -6
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 100c64a0e3204d4715ffde4c0a11e0b380c7966289b8b841ef2ab7cb73f657b2
|
4
|
+
data.tar.gz: 3213f9c1817a211f57685002025dc72929f41652ba7c9e69e62d5314508fc3dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 238cdf0160b6c2101cbabdadef3818dd0bcd2163d9a3ababba854affa0fbaca2533489aa203b27a2309af8e85aa7e814786a9ce8eb29dd51d949cefc1d209921
|
7
|
+
data.tar.gz: b0ad7654cfcac475f32eb81ede6e730046ef3721190ae1f255ceaff58331fbf6d4ee570a290dc16e660ca32611cc8639998677c5980f2eaaedd72388293b22df
|
data/.github/workflows/main.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [3.0.2] - 2022-04-16
|
4
|
+
- Fix tests on ruby 3.0.
|
5
|
+
- More adjustments and additions to docs.
|
6
|
+
- Add warnings when overriding notification text and context block.
|
7
|
+
|
8
|
+
## [3.0.1] - 2021-12-22
|
9
|
+
- Major overhaul of error handling and expansion on which errors trigger
|
10
|
+
friendly messages for users.
|
11
|
+
- More additions to the docs, and working github pages integration.
|
12
|
+
- It's my birthday!
|
13
|
+
|
14
|
+
## [3.0.0] - 2021-12-19
|
15
|
+
- Return a more structured object from successful message sends.
|
16
|
+
- Add the ability to edit or delete a message.
|
17
|
+
- Complete overhaul of docs because they were too large.
|
18
|
+
|
3
19
|
## [2.4.0] - 2021-12-13
|
4
20
|
- Add ability to schedule messages, plus some guard rails around that.
|
5
21
|
- Add ability to debug by logging out the total set of params sent to the API.
|
data/README.md
CHANGED
@@ -12,13 +12,18 @@ SlackMessage.post_to('#general') do
|
|
12
12
|
text "We did it @here! :thumbsup:"
|
13
13
|
end
|
14
14
|
```
|
15
|
+
|
16
|
+
### The Docs
|
15
17
|
|
16
|
-
|
18
|
+
You'll find much more information about how to use SlackMessage by visiting
|
19
|
+
[the docs](https://jmmastey.github.io/slack_message).
|
20
|
+
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
### A Rich DSL Focused on Maintainability
|
23
|
+
|
24
|
+
SlackMessage is able to build all kinds of rich messages for you. It focuses on
|
25
|
+
writing code that looks similar to the output messages themselves, with as
|
26
|
+
little repetition and cruft as possible.
|
22
27
|
|
23
28
|
```ruby
|
24
29
|
SlackMessage.post_to('#general') do
|
@@ -48,7 +53,7 @@ SlackMessage.post_to('#general') do
|
|
48
53
|
end
|
49
54
|
```
|
50
55
|
|
51
|
-
|
56
|
+
### Opinionated Stances
|
52
57
|
|
53
58
|
This gem is intended to stay simple. Other Slack gems have lots of config
|
54
59
|
options and abilities, which makes them powerful, but makes them a pain to use.
|
@@ -56,6 +61,8 @@ options and abilities, which makes them powerful, but makes them a pain to use.
|
|
56
61
|
Accordingly, SlackMessage is developed with some strong opinions in mind:
|
57
62
|
|
58
63
|
* SlackMessage has no dependencies. Your lockfile is enough of a mess already.
|
64
|
+
* A DSL that focuses on code that is easy to write, read, and maintain. Only
|
65
|
+
features that can be implemented with that in mind are included.
|
59
66
|
* The code to build a message should look a lot like the message itself. Code
|
60
67
|
that is simple to read and understand is a priority.
|
61
68
|
* Webhooks are passé. Only Slack Apps are supported now.
|
@@ -66,26 +73,28 @@ Accordingly, SlackMessage is developed with some strong opinions in mind:
|
|
66
73
|
to look it up as an email address.
|
67
74
|
* A few little hacks on the block syntax, such as adding a `blank_line` (which
|
68
75
|
doesn't exist in the API), or leading spaces.
|
69
|
-
* Configuration is kept
|
70
|
-
possible should occur just once via configuration and not on every call.
|
76
|
+
* Configuration is kept simple, with helpers for frequently reused bots.
|
71
77
|
|
72
|
-
Some
|
78
|
+
Some changes that are still planned or desired, but not yet added:
|
73
79
|
|
74
80
|
* any interactive elements at all: https://api.slack.com/interactivity/handling
|
75
81
|
* multiple recipients: https://api.slack.com/methods/conversations.open
|
76
|
-
* more
|
77
|
-
*
|
78
|
-
* more and better organized testing capability
|
82
|
+
* more mrkdwn syntax, like quotes or code blocks https://api.slack.com/reference/surfaces/formatting#line-breaks
|
83
|
+
* more and better organized testing capability (for scheduled messages, editing, deleting)
|
79
84
|
* posting ephemeral messages: https://api.slack.com/methods/chat.postEphemeral
|
85
|
+
* easier way to dump / load message responses
|
86
|
+
* updated docs w/ links to BlockBuilder
|
80
87
|
|
81
|
-
Contributing
|
82
|
-
------------
|
88
|
+
### Contributing
|
83
89
|
|
84
|
-
Contributions are very welcome. Fork, fix, submit pull. Since simplicity of API
|
90
|
+
Contributions are very welcome. Fork, fix, submit pull. Since simplicity of API
|
91
|
+
is a strong priority, so opening an issue to discuss possible interface changes
|
92
|
+
would be wise.
|
85
93
|
|
86
|
-
Contribution is expected to conform to the [Contributor
|
94
|
+
Contribution is expected to conform to the [Contributor
|
95
|
+
Covenant](https://github.com/jmmastey/slack_message/blob/master/CODE_OF_CONDUCT.md).
|
87
96
|
|
88
|
-
License
|
89
|
-
------------
|
97
|
+
### License
|
90
98
|
|
91
|
-
This software is released under the [MIT
|
99
|
+
This software is released under the [MIT
|
100
|
+
License](https://github.com/jmmastey/slack_message/blob/master/MIT-LICENSE).
|
@@ -0,0 +1,116 @@
|
|
1
|
+
## Getting Started / Configuration
|
2
|
+
|
3
|
+
To get started sending messages, you'll first need to create a Slack App with
|
4
|
+
some appropriate permissions. It used to be possible to use the Webhook API,
|
5
|
+
but that's long since been deprecated, and apps are pretty [straightforward to
|
6
|
+
create](https://api.slack.com/tutorials/tracks/getting-a-token).
|
7
|
+
|
8
|
+
Generally, make sure your token has permissions for _at least_ `users:read` and
|
9
|
+
`chat:write`. Then, define a default profile for SlackMessage to use for
|
10
|
+
posting.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
SlackMessage.configure do |config|
|
14
|
+
api_token = 'xoxb-11111111111-2222222222-33333333333333333'
|
15
|
+
|
16
|
+
config.add_profile(api_token: api_token)
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
You should keep your token in a safe place like `ENV`. If using this gem with
|
21
|
+
Rails, place this code in somewhere like
|
22
|
+
`config/initializers/slack_message.rb`.
|
23
|
+
|
24
|
+
### Additional Profiles
|
25
|
+
|
26
|
+
If your app uses slack messages for several different purposes, it's common to
|
27
|
+
want to post to different channels as different names / icons / etc. To do that
|
28
|
+
more easily and consistently, you can specify multiple profiles.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
SlackMessage.configure do |config|
|
32
|
+
api_token = 'xoxb-11111111111-2222222222-33333333333333333'
|
33
|
+
|
34
|
+
# default profile
|
35
|
+
config.add_profile(api_token: api_token, name: 'Slack Notifier')
|
36
|
+
|
37
|
+
# additional profiles (see below for usage)
|
38
|
+
config.add_profile(:prod_alert_bot,
|
39
|
+
name: 'Prod Alert Bot'
|
40
|
+
icon: ':mooseandsquirrel:'
|
41
|
+
)
|
42
|
+
config.add_profile(:sidekiq_bot,
|
43
|
+
api_token: ENV.fetch('SIDEKIQ_SLACK_APP_API_TOKEN'),
|
44
|
+
name: 'Sidekiq Bot',
|
45
|
+
)
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
A number of parameters are available to make it simpler to use a profile
|
50
|
+
without specifying repetitive information. You can generally also specify this
|
51
|
+
information on a per-message basis.
|
52
|
+
|
53
|
+
| Config | Default | Value |
|
54
|
+
|-----------------|-----------------|-----------------------------------------------------------------|
|
55
|
+
| api_token | None | Your Slack App API Key. |
|
56
|
+
| name | From Slack App | The bot name for your message. |
|
57
|
+
| icon | From Slack App | Profile icon for your message. Specify as :emoji: or image URL. |
|
58
|
+
| default_channel | None (optional) | Channel / user to post to by default. |
|
59
|
+
|
60
|
+
|
61
|
+
Setting a `default_channel` specifically will allow you to use `post_as`, which
|
62
|
+
is a convenient shortcut for bots that repeatedly post to one channel as a
|
63
|
+
consistent identity.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
SlackMessage.configure do |config|
|
67
|
+
config.add_profile(:red_alert_bot,
|
68
|
+
api_token: ENV.fetch('SLACK_API_TOKEN'),
|
69
|
+
name: 'Red Alerts',
|
70
|
+
icon: ':klaxon:',
|
71
|
+
default_channel: '#red_alerts'
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
SlackMessage.post_as(:red_alert_bot) do
|
76
|
+
text ":ambulance: weeooo weeooo something went wrong"
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
There's no reason you can't use the same API key for several profiles. Profiles
|
81
|
+
are most useful to create consistent name / icon setups for apps with many
|
82
|
+
bots.
|
83
|
+
|
84
|
+
### Debug Mode
|
85
|
+
|
86
|
+
If you'd like to get more information about the messages you send, you can set
|
87
|
+
SlackMessage to debug mode.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
SlackMessage.configure do |config|
|
91
|
+
config.debug
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
You will now see warnings detailing all params sent to the API.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
{
|
99
|
+
:channel=>"#general",
|
100
|
+
:username=>"Builds",
|
101
|
+
:blocks=>[
|
102
|
+
{:type=>"section", :text=>{
|
103
|
+
:type=>"mrkdwn",
|
104
|
+
:text=>"Build Stability is Looking Ruff :dog:"
|
105
|
+
}}
|
106
|
+
],
|
107
|
+
:text=>"Build Issues",
|
108
|
+
:post_at=>1639421171,
|
109
|
+
}
|
110
|
+
```
|
111
|
+
|
112
|
+
Note this includes data that is not included in `SlackMessage.build`.
|
113
|
+
|
114
|
+
---
|
115
|
+
|
116
|
+
Next: [Posting a Message](https://jmmastey.github.io/slack_message/02_posting_a_message)
|
@@ -0,0 +1,138 @@
|
|
1
|
+
## Posting Messages
|
2
|
+
|
3
|
+
As mentioned at the outset, posting a message to Slack is dang easy.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
SlackMessage.post_to('#general') do
|
7
|
+
text "We did it @here! :thumbsup:"
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
That's it! SlackMessage will automatically serialize for the API.
|
12
|
+
|
13
|
+
```json
|
14
|
+
[{"type":"section","text":{"type":"mrkdwn","text":"We did it @here! :thumbsup:"}}]
|
15
|
+
```
|
16
|
+
|
17
|
+
Details like remembering that Slack made a mystifying decision to force you to
|
18
|
+
request "mrkdwn", or requiring your text to be wrapped into a section are
|
19
|
+
handled for you. Building up messages is meant to be as user-friendly as
|
20
|
+
possible.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
SlackMessage.build do
|
24
|
+
text "haiku are easy"
|
25
|
+
text "but sometimes they don't make sense"
|
26
|
+
text "refrigerator"
|
27
|
+
|
28
|
+
context "- unknown author"
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
SlackMessage will combine your text declarations and add any necessary wrappers
|
33
|
+
automatically.
|
34
|
+
|
35
|
+
```json
|
36
|
+
[
|
37
|
+
{
|
38
|
+
"type": "section",
|
39
|
+
"text": {
|
40
|
+
"type": "mrkdwn",
|
41
|
+
"text": "haiku are easy\nbut sometimes they don't make sense\nrefrigerator"
|
42
|
+
}
|
43
|
+
},
|
44
|
+
{
|
45
|
+
"type": "context",
|
46
|
+
"elements": [
|
47
|
+
{
|
48
|
+
"type": "mrkdwn",
|
49
|
+
"text": "- unknown author"
|
50
|
+
}
|
51
|
+
]
|
52
|
+
}
|
53
|
+
]
|
54
|
+
```
|
55
|
+
|
56
|
+
### Direct Messages
|
57
|
+
|
58
|
+
It's just as easy to send messages directly to users. SlackMessage will look
|
59
|
+
for targets that are email-addressish, and look them up for you automatically.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
SlackMessage.post_to('hello@joemastey.com') do
|
63
|
+
text "You specifically did it! :thumbsup:"
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
SlackMessage will compose this into Block Kit syntax and send it on its way!
|
68
|
+
|
69
|
+
### Multiple Profiles
|
70
|
+
|
71
|
+
If you've defined multiple profiles in configuration, you can specify which to
|
72
|
+
use for your message by specifying its name.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
SlackMessage.post_to('#general', as: :sidekiq_bot) do
|
76
|
+
text ":octagonal_sign: A job has failed permanently and needs to be rescued."
|
77
|
+
|
78
|
+
link_button "Sidekiq Dashboard", sidekiq_dashboard_url, style: :danger
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
You can also override profile bot details when sending a message.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
SlackMessage.post_to('#general') do
|
86
|
+
bot_name "CoffeeBot"
|
87
|
+
bot_icon ":coffee:"
|
88
|
+
|
89
|
+
text ":coffee::clock: Time to take a break!"
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
Finally, if your profile specifies a `default_channel`, you can also post with
|
94
|
+
the `post_as` shorthand.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
SlackMessage.post_as(:coffeebot) do
|
98
|
+
text ":coffee::clock: Time to take a break!"
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
### Scheduling a Message
|
103
|
+
|
104
|
+
To schedule a message, simply provide an `at` parameter to your post. Provide
|
105
|
+
either a time object that responds to `to_i`, or an integer that represents a
|
106
|
+
[unix timestamp](https://en.wikipedia.org/wiki/Unix_time) for the time at which
|
107
|
+
you want your message posted.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
SlackMessage.post_to('hello@joemastey.com', at: 20.seconds.from_now) do
|
111
|
+
text "From the top of the key. :basketball:"
|
112
|
+
end
|
113
|
+
|
114
|
+
SlackMessage.post_as(:basketball_bot, at: 20.seconds.from_now) do
|
115
|
+
text "Boom shakalaka! :explosion:"
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
Please note that scheduled messages can't specify a `bot_name` or `bot_icon`,
|
120
|
+
nor can they be scheduled more than 120 days into the future.
|
121
|
+
|
122
|
+
### Best Practices
|
123
|
+
|
124
|
+
From experience, building messages with maintainability in mind is key. Adding
|
125
|
+
lots of flow control and indirection will undermine your ability to understand
|
126
|
+
and change messages later.
|
127
|
+
|
128
|
+
For simple messages, using implicit sections is perfectly fine. However, if you
|
129
|
+
intend to create several sections, it's usually better to just declare them.
|
130
|
+
|
131
|
+
See the [API documentation for
|
132
|
+
chat.postMessage](https://api.slack.com/methods/chat.postMessage) or
|
133
|
+
[chat.scheduleMessage](https://api.slack.com/methods/chat.scheduleMessage) for
|
134
|
+
more information on posting messages.
|
135
|
+
|
136
|
+
---
|
137
|
+
|
138
|
+
Next: [The SlackMessage DSL](https://jmmastey.github.io/slack_message/03_message_dsl)
|