aeolic

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2022 License: MIT Imports: 14 Imported by: 0

README

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)
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	StatusText string
	Message    string
	Context    string
}

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

type Client

type Client struct {
	DefaultHeaders map[string]string
	Templates      map[string]string
	HTTPClient     httpClient
}

func New

func New(apiKey string, templateDir string) (Client, error)

New - returns a new client with the templates loaded from the provided directory.

func NewWithFS

func NewWithFS(apiKey string, dir fs.FS, templateDir string) (Client, error)

NewWithFS - returns a new client with the templates loaded from the provided directory and file system.

func NewWithMap

func NewWithMap(apiKey string, templateMap map[string]string) Client

working example: cmd/embed_slack/main.go

func (*Client) SendMessage

func (c *Client) SendMessage(channel string, templateName string, body any) error

SendMessage - post a slack message to a channel

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL