Simple Slack client that uses templates rather than block builder functions.

  • By LFSGroup
  • Last update: Nov 18, 2022
  • Comments: 2

aeolic

Opinionated approach to sending slack requests to a channel:

(using a simple http post request, no slack apis)

  1. You provide the BlockBuild json via block kit builder, as a golang template
  2. You call the interface method, with the auth, channel id and payload required (see examples below)
  3. The aeloic code makes the slack call
  4. Profit



Getting started


Adding Templates

Create a directory and add files with the suffix tmpl.json. When creating a new instance of aeolic, pass in the directory path, aeolic will automatically load all files matching the suffix.

template names are the names of the file without the suffix, for example:

file: basic.tmpl.json template name: basic




Making the slack call

Create a new instance with the slack token and the path to your templates directory.

	c, err := aeolic.New("<your-slack-token>", "<path/to/template/dir>")

	if err != nil {
        // handle error
	}

Make the api call

	if err := c.SendMessage("<your-slack-channel-id>", "<template-name>", map[string]string{
		"hello": "world",
	}); err != nil {
        // handle error
	}

Provide your own template map


Using go embed feature you can provide your own template map, the template name will be the key.

import (
        _ "embed"
)

//go:embed templates/basic.tmpl.json
var basicTemplate string

func main() {

	customMap := map[string]string{
		"basic": basicTemplate,
	}

	c := aeolic.NewWithMap(<token>, customMap)

	if err := c.SendMessage(channel, "basic", map[string]string{
		"user_name": "Allan Bond",
	}); err != nil {
		log.Fatal("failed ", err)
	}

}

Provide your own file system

Using go embed feature you can provide your own file system.

import (
	_ "embed"
)
//go:embed templates/*.tmpl.json
var content embed.FS

func main() {

	c := aeolic.NewWithFS(<token>, content, "templates")

	if err := c.SendMessage(channel, "basic", map[string]string{
		"user_name": "Allan Bond",
	}); err != nil {
		log.Fatal("failed ", err)
	}

}



Error Handling


More context is provided if it's an api error

	if err := c.SendMessage("<your-slack-channel-id>", "<template-name>", map[string]string{
		"hello": "world",
	}); err != nil {
                var apiErr *aeolic.APIError
                if errors.As(err, &apiErr) {
                        // non 2xx,3xx response for example: 
                        // StatusCode: 400
                        // StatusText: Bad Request
                        // Message: "invalid_blocks"
                        // Context: "https://api.slack.com/methods/chat.postMessage#errors"

                
                        /** ... */
                }

                // handle other errors
                fmt.Println(err)
	}

Download

aeolic.zip

Comments(2)

  • 1

    use-text-template

    For slack, this is double encoding the output templates. This is very visbile in dates

    html/template: 2022-11-21T09:45:46+11:00 text/template: 2022-11-21T09:47:44+11:00

  • 2

    withTemplates bug fix

    Found an issue with TrimRight removing extra characters from the end of template names. This would cause a template not found error when sending slack messages.

    Changed TrimRight -> TrimSuffix in withTemplates()