/slack-ruby-block-kit

A ruby wrapper for Slack's Block Kit

Primary LanguageRubyMIT LicenseMIT

Gem Version codecov

Slack::BlockKit

A collection of ruby objects that represent Slack's block kit.

You can play around with the block kit using Slack's block kit builder!

The 'blocks' availables are split in line with how Slack documents them, that is:

Installation

Add this line to your application's Gemfile:

gem 'slack-ruby-block-kit'

And then execute:

$ bundle

Or install it yourself as:

$ gem install slack-ruby-block-kit

Finally, require this:

require 'slack-ruby-block-kit'

Examples

Here are a few examples that might help you get started!

require 'faraday'
require 'slack-ruby-block-kit'
require 'json'

a_prebuilt_block = Slack::BlockKit::Layout::Section.new
text = Slack::BlockKit::Composition::Mrkdwn.new(text: ':wave: *hello*')
an_image = Slack::BlockKit::Element::Image.new(image_url: 'https://git.io/fjDW8', alt_text: 'a picture')
a_prebuilt_block.accessorise(an_image)
a_prebuilt_block.text = text

blocks = Slack::BlockKit.blocks do |b|
  b.section do |s|
    s.plain_text(text: 'Some plain text message!')
    s.button(text: 'A button that is important', style: 'primary', action_id: 'id')
  end

  b.divider

  b.context do |c|
    c.mrkdwn(text: '_some italicised text for context_')
  end

  b.append(a_prebuilt_block)
end

body = { blocks: blocks.as_json }
webhook_url = 'https://hooks.slack.com/services/your/webhook/url'

response = Faraday.post(
  webhook_url,
  body.to_json,
  'Content-Type' => 'application/json'
)

This will create a message like this:

example block message

See ./examples, and ./examples/README.md, for more worked examples and guidance.

You can also check out the slackerduty project for some more examples, Slackerduty::Alert and Slackerduty::Blocks may be helpful places to start.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/CGA1123/slack-ruby-block-kit

See issues if you want any inspiration as to what to help with!

License

The gem is available as open source under the terms of the MIT License.

Releasing

Releasing is automated via the .github/workflows/release.yml which runs via Trusted Publishing authenticating with RubyGems via GitHub Action's OIDC integration.

  • Update CHANGELOG for the latest version (remember to update the comparison links + add in an Unreleased section again)
  • Update lib/slack/block_kit/version.rb and run bundle to update the Gemfile.lock
  • Push that!
  • Run the Release workflow