slack

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 10 Imported by: 0

README

Go Reference GitHub go.mod Go version GitHub release (latest by date) Go Report Card Actions Status

Slack Client

A simple client to send messages to channels in Slack.

Example

package main

import {
    "fmt"

    slack "github.com/tommzn/go-slack"
}

func main() {

    client := slack.New()
    header := "Greeting!"
    channel := "<ChannelId>"

    // Send a message with header to a channel
    if err := client.SendToChannel("Hello Slack", channel, &header); err != nil {
        fmt.Println(err)
    }
    
    // Set default channel
    client.WithChannel(channel)
    // Send a message to default channel, including a header.
    if err := client.Send("Hello Slack", &header); err != nil {
        fmt.Println(err)
    }

    // Send a message without a header
    if err := client.Send("Hello Slack", nil); err != nil {
        fmt.Println(err)
    }
}

Auth Token

Each request to Slack Web API needs an auth token. This client expects an auth token provided by env variable SLACK_TOKEN. As an alternative you pass a SecretsManager to NewFromConfig to obtain a token from different sources.

Config

A default channel can be provided by config. See https://github.com/tommzn/go-config.

Example Config File

Following config file defines "Channel05" as default channel.


slack:
  channel: Channel05

Documentation

Overview

Package slack provides a simple client to send messages to Slack channels.

Index

Constants

View Source
const SLACK_POST_MESSAGE_API = "https://slack.com/api/chat.postMessage"

SLACK_POST_MESSAGE_API is the endpoint provides by Slack's web api to send messages.

View Source
const SLACK_TOKEN = "SLACK_TOKEN"

SLACK_TOKEN is used to obtain an auth token from environment or if available by assigned secrets mananger.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is used to send messages to Slack using it's web api.

func New

func New() *Client

New will return a client to send messages to Slack. Auth token will be taken from environment using SLACK_TOKEN. For other token sources use secrets manager with NewFromConfig.

func NewFromConfig

func NewFromConfig(conf config.Config, secretsManager secrets.SecretsManager) *Client

NewFromConfig returns a client to send messages to Slack. You can pass a config to set a default channel and secrets managet to obtain auth token. Both are optional.

func (*Client) Send

func (client *Client) Send(message string, header *string) error

Send will send passed message to previous assigned channel. Passed message is send as section block in plain text. Header is optional and will be send as a header block if passed.

func (*Client) SendToChannel

func (client *Client) SendToChannel(message, channel string, header *string) error

SendToChannel will send passed message to given channel. Message is send as section block in plain text. Header is optional and will be send as a header block if passed.

func (*Client) WithChannel

func (client *Client) WithChannel(channel string) *Client

WithChannel assign passed channel id to client. Usefull if most messages should be send to one channel.

Jump to

Keyboard shortcuts

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