slack

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: BSD-3-Clause Imports: 5 Imported by: 1

README

go-slack

SFO Museum's opinionated Go package for doing things with Slack.

Documentation

Go Reference

Example

package main

import (
	"context"
	"github.com/sfomuseum/go-slack"
)

func main() {

	ctx := context.Background()

	webhook_uri := "https://hooks.slack.com/services/.../.../..."
	channel := "test"
	text := "hello world"

	wh, _ := slack.NewWebhook(ctx, webhook_uri)

	m := &slack.Message{
		Channel: channel,
		Text:    text,
	}

	wh.Post(ctx, m)
}

Error handling omitted for the sake of brevity.

Tools

$> make cli
go build -mod vendor -o bin/to-slack cmd/to-slack/main.go
to-slack

Post a message to a Slack channel. The principal difference between to-slack and other similar tools is the use of Go Cloud runtimevar URIs to define Slack Webhook URIs. This allows these otherwise sensitive values to be stored and retrieved from a variety of different storage mechanisms.

$> ./bin/to-slack -h
Post a message to a Slack channel.
Usage:
	 ./bin/to-slack [options] message
Valid options are:
  -channel string
    	A valid Slack channel to post to.
  -message-per-line
    	Send a message for each line when reading input from STDIN.
  -prefix string
    	Optional prefix to prepend each message with.
  -stdin
    	Read input from STDIN
  -webhook-uri string
    	A valid gocloud.dev/runtimevar URI containing a Slack webhook URL to post to.

For example:

$> ./bin/to-slack -channel test -webhook-uri 'constant://?val=https://hooks.slack.com/services/.../.../...' testing

Or posting data read from STDIN:

$> echo "wub wub wub" | ./bin/to-slack -channel test -webhook-uri 'constant://?val=https://hooks.slack.com/services/.../.../...' -stdin

Or posting data reading Webhook URL information from an AWS Parameter Store secret:

$> echo "wub wub wub" | ./bin/to-slack -channel test -webhook 'awsparamstore://{SECRET_NAME}?region={REGION}&credentials={CREDENTIALS}' -stdin
Runtimevar(s)

The following Go Cloud runtimevar services are supported by the to-slack tool:

It is possible to load runtimevar data from AWS Parameter Store using aaronland/go-aws-session -style credential strings. For example:

awsparamstore://{KEY}?region={REGION}&credentials={CREDENTIALS}

Credentials for AWS sessions are defined as string labels. They are:

Label Description
env: Read credentials from AWS defined environment variables.
iam: Assume AWS IAM credentials are in effect.
{AWS_PROFILE_NAME} Use the profile from the default AWS credentials location.
{AWS_CREDENTIALS_PATH}:{AWS_PROFILE_NAME} Use the profile from a user-defined AWS credentials location.
Future work

It occurs to me that this tool could be extended easily enough to act as a Lambda function which would allow messages posted to an S3 bucket to be dispatched to Slack. That's an interesting idea but likely overkill. In any event SFO Museum has no need for this functionality (yet).

See also

Documentation

Overview

package slack is SFO Museum's opinionated package for doing things with Slack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	// The name of the channel to post the message to.
	Channel string `json:"channel"`
	// The body of the message to post to a channel.
	Text string `json:"text"`
}

type Message defines a struct for messages posted to Slack

type Webhook

type Webhook struct {
	// contains filtered or unexported fields
}

type Webhook defines a struct for sending messages to a Slack Webhook URL.

func NewWebhook

func NewWebhook(ctx context.Context, url string) (*Webhook, error)

NewWebhook returns a new `Webhook` instance for posting messages to 'url'.

func (*Webhook) Post

func (wh *Webhook) Post(ctx context.Context, m *Message) error

Post will post 'm' to the Webhook URL associated with 'wh'.

Directories

Path Synopsis
cmd
package cmd provides opinionated command-line tools for doing things with Slack.
package cmd provides opinionated command-line tools for doing things with Slack.
to-slack
post is a command-line tool for posting messages to a Slack channel.
post is a command-line tool for posting messages to a Slack channel.
package writer implements the io.Writer interface for posting messages to Slack.
package writer implements the io.Writer interface for posting messages to Slack.

Jump to

Keyboard shortcuts

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