Skip to content

nevinera/slack_line

Repository files navigation

SlackLine

This is a ruby gem supporting easy construction, sending, editing, and threading of messages and threads.

Usage

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.post

And 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"
end

Configuration

The only required setup is an OAuth token - each of these options can be set via ENV or SlackLine.configure:

  • slack_token or SLACK_LINE_SLACK_TOKEN (required) - this allows the library to send messages and make API requests.
  • look_up_users or SLACK_LINE_LOOK_UP_USERS (default false) - if your workspace refuses to turn @somebody mentions 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_name or SLACK_LINE_BOT_NAME - what to call the bot that's posting (in slack). The default is to use its default name.
  • default_channel or SLACK_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, all send calls are required to specify a target instead.
  • per_message_delay or SLACK_LINE_PER_MESSAGE_DELAY is a float, defaulting to 0.0. SlackLine will sleep for that duration after each message is posted, to allow you to avoid hitting rate-limits from posting many messages in a row.
  • per_thread_delay or SLACK_LINE_PER_THREAD_DELAY is a float as well - SlackLine will sleep for this duration after each thread is posted, and after each non-thread message is posted.
  • cache_path or SLACK_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. Requires lightly to be installed as an optional dependency - if it is not available, a DiskCaching::NoLightly error will be raised at runtime.
  • cache_duration or SLACK_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), or d (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"
end

Multiple Configurations

If 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").post

Slack App Permissions

In 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:read and usergroups:read - look up users/groups for (a) messaging them directly or (b) supporting the look_up_users config option (for those more restrictive workspaces)

About

Easy slack messaging, editing, and threading

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages