Skip to content

jeremyforan/go-flocks-of-blocks

Repository files navigation

Flocks Of Blocks

Go Reference

Go Report Card

Flocks of Blocks is a Go library that helps compose Slack messages using the Block Framework.

⚠️ Warning

This package is in heavy flux at the moment as I work to incorporate feedback from various sources.

Why

After building Slack bots in Go, I looked for a faster way of composing block messages. I started utilizing the Go templating library to build ad-hoc messages. This package provides an intuitive way of generating Slack messages using templates and a functional approach heavily inspired by the Charm

Installation

go get github.com/jeremyforan/go-flocks-of-blocks

Usage

Basic Usage

Build the Plain Text example

package main

import (
	"fmt"
	fobs "github.com/jeremyforan/go-flocks-of-blocks"
)

func main() {
	text := fobs.NewPlainText("This is a plain text section block.").EnableEmoji()
	section := fobs.NewSection().SetText(text)
	basic := fobs.NewMessage().AddBlock(section)

	fmt.Println(basic)

Outputs:

{
	"blocks": [
		{
			"type": "section",
			"text": {
				"type": "plain_text",
				"text": "This is a plain text section block.",
				"emoji": true
			}
		}
	]
}

This can be condensed

package main

import (
	"fmt"
	fobs "github.com/jeremyforan/go-flocks-of-blocks"
)

func main() {
	text := fobs.NewPlainText("This is a plain text section block.").EnableEmoji()
	fmt.Println(fobs.NewMessage().AddBlock(text.Section()))
}

Real World Example

Here is an Example

real world example

Composing the example message:

package main

import (
	"fmt"
	fobs "github.com/jeremyforan/go-flocks-of-blocks"
)

func main() {

	// create a new message
	msg := fobs.NewMessage()

	// Add a header
	header := fobs.NewHeader("Device Summary")
	msg = msg.AddBlock(header)

	// Add some info
	info := fobs.NewSection().AddMrkdownField("*IP:* 192.168.0.1").AddMrkdownField("*Area:* basement")
	msg = msg.AddBlock(info)

	// Add some more info but in a single line
	msg = msg.AddBlock(fobs.NewSection().AddMrkdownField("*Hardware:* Linksys WRT-54G").AddMrkdownField("*Uptime:* 7 Days, 3 Months"))

	// Add the info message to
	ssid := fobs.NewSection().AddMrkdownField("*SSID:* FreshPrinceOfDonair")
	msg = msg.AddBlock(ssid)

	// make a "reset" button
	resetButton := fobs.NewButton("Reboot Device", "actionId-0").SetValue("click_me_123")

	// Let's assume we want to add a note based on some arbitrary bool value
	rfIssue := true
	if rfIssue {
		note := fobs.NewPlainText("*high levels of RF interference detected consider scan")
		msg = msg.AddBlock(note.Context())

		// We want to add the Danger styleing to the button due to the 'issue'
		resetButton = resetButton.MakeStyleDanger()
	}

	// Add the reset button to the message
	msg = msg.AddBlock(resetButton.Actions())

	// Generate a link that paste the body into the Slack interactive Block Kit Builder for validation
	fmt.Println(msg.GenerateKitBuilderUrl())
}

Philosophy

Slack messages should be easy and fun to compose. Most Slack messages are simple and, as a result, less likely to violate any of Slack message's restrictions:

IE: Maximum length for this field is 255 characters.

Therefore the package should try and minimize the amount of errors the user has to handle.

A functional approach to building the assets will be more concise and intuitive:

button := NewButton("Click This", "button1").MakeStyleDanger().AddUrl(url)

Roadmap

There are three major phases to this project:

1) Version 0.0.x (Develope)

  • Explore different implementation and design approaches including more composition
  • Gather user feedback

2) Version 1.0.x (Productionized)

  • Establish a consistant naming convention
  • Decide on the degree of structure composition
  • Complete code coverage

3) Version 1.1.x (Maintain)

  • Add component validation
  • Make a really great Logo. Like sticker worthy!

About

A Go package that helps compose Slack block messages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages