slack

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2018 License: Apache-2.0 Imports: 5 Imported by: 12

README

slack CircleCI

A library to send messages to via SlackIncoming Webhook API, written in Go. It provides dialects for Slack and Mattermost.

See GoDoc for more.

TL;DR

package main

import (
	"github.com/int128/slack"
	"log"
)

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

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

This section is empty.

Functions

func Send

func Send(WebhookURL string, message *Message) error

Send sends the message to Slack via Incomming Webhook 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() {
	if err := slack.Send(webhook, &slack.Message{
		Username:  "mybot",
		IconEmoji: ":star:",
		Text:      "Hello World!",
	}); err != nil {
		panic(fmt.Errorf("Could not send the message to Slack: %s", err))
	}
}

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"`
	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"` // pretext, text, fields
}

Attachment represents an attachment of a message. 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 Webhook 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.

type Message

type Message struct {
	Username    string       `json:"username,omitempty"`
	IconEmoji   string       `json:"icon_emoji,omitempty"`
	IconURL     string       `json:"icon_url,omitempty"`
	Text        string       `json:"text,omitempty"`
	Attachments []Attachment `json:"attachments,omitempty"`
}

Message represents a message sent via Incoming Webhook API. See https://api.slack.com/docs/message-formatting for details.

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