This is a ruby gem supporting easy construction, sending, editing, and threading of messages and threads.
Are you ready? Because this is going to be so easy.
# Send a simple message directly
sent_message = SlackLine.message("Something happened!", to: "#general").post
# Construct a more complex message, then send it
msg = SlackLine.message do
section do
context "Don't worry! If this message surprises you, context @foobar"
text "A thing has happened"
text "Yeah, it happened for sure"
link "How bad?", problems_path(problem.id)
end
text "More details.."
end
sent_message = msg.post(to: "#general")
# Send a _thread_ of messages (strings generate simple messages)
sent_thread = SlackLine.thread("First text", "Second text", msg, to: "#general").post
# You can also build them inline via dsl
sent_thread = SlackLine.thread(to: "@dm_recipient") do
message do
context "yeah"
text "That's right"
end
message(already_built_message)
message "this makes a basic message directly"
end.postAnd then once you've sent a message or thread, you'll have a SentMessage
or SentThread object (which is basically an Array of SentMessages). You
can call SentMessage#update on any of those messages to edit them
after the fact - this is especially useful for keeping a message in a
public channel updated with the state of the process it's linking to.
update accepts a String, a Message, or a block defining a message.
To update a SentThread, you'll need to choose message:
sent_message.update "Edit: never mind, false alarm"
sent_thread.first.update "Problem Resolved!"
sent_thread.detect { |m| m =~ /Not yet safe/ }&.update do
text "it's safe now"
endThe only required setup is an OAuth token - each of these options can
be set via ENV or SlackLine.configure:
slack_tokenorSLACK_LINE_SLACK_TOKEN(required) - this allows the library to send messages and make API requests.look_up_usersorSLACK_LINE_LOOK_UP_USERS(default false) - if your workspace refuses to turn@somebodymentions into links or notifications, you can set this and we'll parse them out, then use the slack API to map them to user/group IDs.bot_nameorSLACK_LINE_BOT_NAME- what to call the bot that's posting (in slack). The default is to use its default name.default_channelorSLACK_LINE_DEFAULT_CHANNEL- a target name (either a channel with the leading pound-sign, or a user's handle with a leading at-sign). When not supplied, allsendcalls are required to specify a target instead.per_message_delayorSLACK_LINE_PER_MESSAGE_DELAYis a float, defaulting to 0.0. SlackLine willsleepfor that duration after each message is posted, to allow you to avoid hitting rate-limits from posting many messages in a row.per_thread_delayorSLACK_LINE_PER_THREAD_DELAYis a float as well - SlackLine willsleepfor this duration after each thread is posted, and after each non-thread message is posted.cache_pathorSLACK_LINE_CACHE_PATH- a directory path for disk-caching the users and groups lists fetched from the Slack API. When set, SlackLine will use the lightly gem to cache results to disk, which avoids redundant API calls across separate runs. Requireslightlyto be installed as an optional dependency - if it is not available, aDiskCaching::NoLightlyerror will be raised at runtime.cache_durationorSLACK_LINE_CACHE_DURATION- how long cached data remains valid (default"15m"). Accepts a plain integer number of seconds, or a number followed by a unit suffix:s(seconds),m(minutes),h(hours), ord(days). For example:"30m","2h","1d", or"900".
You can just set those via the environment variables, but you can also set them on the singleton configuration object:
SlackLine.configure do |config|
config.slack_token = ENV["SLACK_TOKEN"]
config.look_up_users = true
config.bot_name = "CI Bot"
config.default_channel = "#ci-flow"
config.per_message_delay = 0.2
config.per_thread_delay = 2.0
config.cache_path = "/tmp/slack_line_cache"
config.cache_duration = "1h"
endIf you're working in a context where you need to support multiple SlackLine configurations, don't worry! The singleton central config is what the singleton central Client uses (that's what all of the top-level SlackLine methods dispatch to), but you can construct additional clients with their own configs easily:
Slackline.configure do |config|
config.slack_token = ENV["TOKEN"]
config.default_channel = "#general"
config.bot_name = "FooBot"
end
# Now SlackLine.message (et al) will use SlackLine.client,
# configured as above.
BAR_SLACK = SlackLine::Client.new(default_channel: "#team-bar", bot_name: "BarBot")
# And now you can call those methods on `BAR_SLACK` to use a different
# default channel and name
BAR_SLACK.thread("Message 1", "Message 2").post
BAR_SLACK.message("Message 3", to: "#bar-team-3").postIn order to post/update messages, the app behind your SLACK_LINE_TOKEN can use
these permissions:
chat:write- send messages at all.chat:write.public- send messages to public channels your app isn't a member of (so you don't need to invite them to the relevant channels to make them work).im:write- start direct messages with individuals.users:readandusergroups:read- look up users/groups for (a) messaging them directly or (b) supporting thelook_up_usersconfig option (for those more restrictive workspaces)