slack

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2019 License: Apache-2.0 Imports: 6 Imported by: 10

README

slack GoDoc CircleCI codecov

This is a Go package for sending messages via Slack Incoming Webhooks API and Mattermost Incoming Webhooks API. It provides dialects for Slack and Mattermost.

See GoDoc.

Examples

package main

import (
	"log"

	"github.com/int128/slack"
)

const webhook = "https://hooks.slack.com/services/..."

func main() {
	if err := slack.Send(webhook, &slack.Message{
		Username:  "mybot",
		IconEmoji: ":star:",
		Text:      "Hello World!",
	}); err != nil {
		log.Fatalf("Could not send the message to Slack: %s", err)
	}
}

See also example/main.go.

Contributions

This is an open source software licensed under Apache-2.0. Feel free to open issues and pull requests.

Documentation

Overview

Package slack provides a client for Slack Incoming Webhooks API. See https://api.slack.com/docs/messages for details of the API.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Disable = triState(&disable)

Disable represents a pointer to false value, for *bool.

View Source
var Enable = triState(&enable)

Enable represents a pointer to true value, for *bool.

Functions

func Send

func Send(WebhookURL string, message *Message) error

Send sends the message to Slack via Incomming Webhooks API. It returns an error if a HTTP client returned non-2xx status or network error.

Example
package main

import (
	"fmt"

	"github.com/int128/slack"
)

const webhook = "https://hooks.slack.com/services/..."

func main() {
	err := slack.Send(webhook, &slack.Message{
		Username:  "mybot",
		IconEmoji: ":star:",
		Text:      "Hello World!",
	})
	if err != nil {
		panic(fmt.Errorf("could not send the message to Slack: %s", err))
	}
}
Output:

Types

type Attachment

type Attachment struct {
	Fallback   string             `json:"fallback,omitempty"`
	Color      string             `json:"color,omitempty"`
	Pretext    string             `json:"pretext,omitempty"`
	AuthorName string             `json:"author_name,omitempty"`
	AuthorLink string             `json:"author_link,omitempty"`
	AuthorIcon string             `json:"author_icon,omitempty"`
	Title      string             `json:"title,omitempty"`
	TitleLink  string             `json:"title_link,omitempty"`
	Text       string             `json:"text,omitempty"`
	Fields     []AttachmentField  `json:"fields,omitempty"`
	Actions    []AttachmentAction `json:"actions,omitempty"`
	ImageURL   string             `json:"image_url,omitempty"`
	ThumbURL   string             `json:"thumb_url,omitempty"`
	Footer     string             `json:"footer,omitempty"`
	FooterIcon string             `json:"footer_icon,omitempty"`
	Timestamp  int64              `json:"ts,omitempty"`
	MrkdwnIn   []string           `json:"mrkdwn_in,omitempty"` // Valid values are pretext, text, fields
}

Attachment represents an attachment of a message. See https://api.slack.com/docs/message-attachments for details.

type AttachmentAction added in v1.4.0

type AttachmentAction struct {
	Type  string `json:"type,omitempty"`
	Text  string `json:"text,omitempty"`
	URL   string `json:"url,omitempty"`
	Style string `json:"style,omitempty"`
}

AttachmentAction represents an action in an attachment. See https://api.slack.com/docs/message-attachments for details.

type AttachmentField

type AttachmentField struct {
	Title string `json:"title,omitempty"`
	Value string `json:"value,omitempty"`
	Short bool   `json:"short,omitempty"`
}

AttachmentField represents a field in an attachment. See https://api.slack.com/docs/message-attachments for details.

type Client

type Client struct {
	WebhookURL string       // Webhook URL (mandatory)
	HTTPClient *http.Client // Default to http.DefaultClient
}

Client provides a client for Slack Incoming Webhooks API.

func (*Client) Send

func (c *Client) Send(message *Message) error

Send sends the message to Slack. It returns an error if a HTTP client returned non-2xx status or network error.

Example
package main

import (
	"fmt"

	"github.com/int128/slack"
)

const webhook = "https://hooks.slack.com/services/..."

func main() {
	c := &slack.Client{
		WebhookURL: webhook,
		HTTPClient: nil, // urlfetch.Client(ctx) on App Engine
	}
	err := c.Send(&slack.Message{
		Username:  "mybot",
		IconEmoji: ":star:",
		Text:      "Hello World!",
	})
	if err != nil {
		panic(fmt.Errorf("could not send the message to Slack: %s", err))
	}
}
Output:

type ErrorResponse added in v1.3.0

type ErrorResponse interface {
	StatusCode() int // non-2xx status code
	Body() string    // Response body
}

ErrorResponse represents an error response from Slack API. See https://api.slack.com/incoming-webhooks#handling_errors for details.

func GetErrorResponse added in v1.3.0

func GetErrorResponse(err error) ErrorResponse

GetErrorResponse returns ErrorResponse if Slack API returned an error response.

Example
package main

import (
	"fmt"

	"github.com/int128/slack"
)

const webhook = "https://hooks.slack.com/services/..."

func main() {
	err := slack.Send(webhook, &slack.Message{
		Text: "Hello World!",
	})
	if err != nil {
		if resp := slack.GetErrorResponse(err); resp != nil {
			if resp.StatusCode() >= 500 {
				// you can retry sending the message
			}
		}
		panic(fmt.Errorf("could not send the message to Slack: %s", err))
	}
}
Output:

type Message

type Message struct {
	Username    string       `json:"username,omitempty"`
	Channel     string       `json:"channel,omitempty"`
	IconEmoji   string       `json:"icon_emoji,omitempty"`
	IconURL     string       `json:"icon_url,omitempty"`
	Text        string       `json:"text,omitempty"`
	Mrkdwn      triState     `json:"mrkdwn,omitempty"`       // Set false to disable formatting.
	UnfurlMedia triState     `json:"unfurl_media,omitempty"` // Set false to disable unfurling.
	UnfurlLinks triState     `json:"unfurl_links,omitempty"` // Set true to enable unfurling.
	Attachments []Attachment `json:"attachments,omitempty"`
}

Message represents a message sent via Incoming Webhooks API.

See https://api.slack.com/docs/message-formatting and https://api.slack.com/docs/message-link-unfurling.

Directories

Path Synopsis
Package dialect provides functionality for easily switching Slack or Mattermost.
Package dialect provides functionality for easily switching Slack or Mattermost.

Jump to

Keyboard shortcuts

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