Code Monkey home page Code Monkey logo

slack-ruby-block-kit's Introduction

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

slack-ruby-block-kit's People

Contributors

anvox avatar bmorton avatar caalberts avatar cga1123 avatar deecewan avatar deepsource-autofix[bot] avatar deepsourcebot avatar dependabot-preview[bot] avatar dependabot[bot] avatar dimerman avatar eityans avatar fxn avatar getoutofmybakery avatar hvtroller avatar jcat4 avatar luvtechno avatar nate-r-a avatar pbendersky avatar rinasergeeva avatar rspeicher avatar ryanwilsonperkin avatar sophieklm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

slack-ruby-block-kit's Issues

[Feature Request] Rich Text Block Support

Hi @CGA1123, this is a great gem and really simplifies working with Slack Block Kit in a scripted way!

I notice that rich text block and it's various elements aren't currently supported. I think they're comparatively new and could be quite complicated to support.

Is this something you're aware of and likely to implement?

Trying to lay something out in a table in Slack is pretty frustrating and the next best solution is using Rich Text's list options.

Rendering `.blockkit` with ActionView template handling

Hey! As I mentioned in #175, we're working on improving how we handle interactions with Slack within our monolithic codebase at FireHydrant. One thing that has made this substantially easier to work with is separating out our code that generates views into Rails' ActionView layer. This path doesn't seem well-documented or well-traveled, but using jbuilder as inspiration, I've got an implementation in our codebase that handles turning a index.json.blockkit-style template into BlockKit JSON.

Essentially, it lets you write a view that looks like this:

index.json.blockkit

metadata.type "modal"
metadata.title @title

blocks.section do |section|
  section.plain_text text: "Hello, World!"
end

ExampleController

class ExampleController < ApplicationController
  def index
    @title = "Hello Ruby"
    render :index, formats: [:json]
  end
end

There's not much code to implement this as it's mostly glue between Rails and this library (along with some extra metadata stuff that sits alongside blocks in the response payload). The biggest maintenance cost with this would be keeping up with and testing against new Rails versions to ensure broad support there.

Would this be worth including in this library? Would it be better to keep it separate and maybe throw a link to that library?

Add support for `focus_on_load` parameter in blocks

November 2021 Slack Changelog includes an additional focus_on_load parameter.

We should add support to these blocks for this parameter:

  • Checkbox groups
  • Date picker element
  • Multi-select menu element
    • Static
    • External
    • User list
    • Conversations list
    • Public channels list
  • Plain-text input element
  • Radio button group element
  • Select menu element
    • Static
    • External
    • User list
    • Conversations list
    • Public channels list
  • Time picker element

Restructuring how initial options are set for multi and single select elements

I think it might be interesting to add an initial: keyword to #option. i.e.

      instance.checkbox_group(action_id: '__ACTION_ID__') do |checkboxes|
        checkboxes.option(value: '__VALUE__', text: '__TEXT__', initial: true)
      end

Having looked at #46 I think if we go with adding initial: we can remove the initial: from the initializers that are currently there.

We might also think about removing the #initial methods at that point -- I can't think of a use case for them anymore if we have initial: available on #option.

Originally posted by @CGA1123 in #44 (comment)

Better Interface

The current interface is a bit cumbersome, and ugly, leading to code like this ๐Ÿ˜– :

title_text = Slack::BlockKit::Composition::Text.markdown(text: "title")
status_text = Slack::BlockKit::Composition::Text.markdown(text: "hungry")
time_text = Slack::BlockKit::Composition::Text.markdown(text: "noon")

blocks = []

blocks << Slack::BlockKit::Layout::Section.new(
  text: title_text,
  fields: [status_text, time_text]
)

blocks << Slack::BlockKit::Layout::Context.new(
  elements: [....]
)

blocks.map(&:as_json)

would be nice to have something like:

blocks = Slack::BlockKit.blocks do |blocks|
  blocks.section do |section|
    section.title('title')
    section.elements do |elements|
      ....
    end
  end
end

blocks.as_json

Probably what would be nice to add:

  • Slack::BlockKit.blocks

  • Slack::BlockKit::Blocks

  • Slack::BlockKit::Blocks#as_json

  • Slack::BlockKit::Blocks#append

  • Slack::BlockKit::Blocks#section

  • Slack::BlockKit::Blocks#divider

  • Slack::BlockKit::Blocks#image

  • Slack::BlockKit::Blocks#context

  • Slack::BlockKit::Layout::Section#title

  • Slack::BlockKit::Layout::Section#append

Enforcing BlockKit limits

In looking to replace our internal BlockKit implementation with this one, something that came up is that we'd lose the support we built for enforcing Slack's BlockKit limits on text fields and number of elements in various blocks.

Is this support that you'd be interested in accepting a pull request for? Do you have thoughts about how this should be implemented in this library?

add timepicker to input

I'm trying to add a timepicker as an input to blocks section, however, there's nothing in the input layout to yield as a timepicker

JSON attribute should be `initial_options` instead of `initial_option`

@CGA1123 Either this is a bug or Slack actually allows singular version of the attribute.

Implement use of response_url

According to: https://api.slack.com/surfaces/modals/using#modal_response_url, blocks can pass through response_url_enabled to get a response_url which enables best practice usage of ephemeral messages. This seems like a basic adding and passing of a parameter in:

  • Slack::BlockKit::Element::ConversationsSelect#initialize
  • Slack::BlockKit::Element::ConversationsSelect#as_json
  • Slack::BlockKit::Layout::Input#conversation_select

Happy to submit a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.