Help us shape the future of Go! Take the 2026 Go Developer Survey

slackwebhook

package module
v0.0.0-...-627aa7e Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2018 License: BSD-2-Clause Imports: 3 Imported by: 0

README

go-slack-webhook

Minimum implementation for posting payload to Slack WebHook URL.

Design Background

There are a bunch of other great Slack library in Golang. Please use them at most cases.

However, sometimes you want to reduce the build size of binary as small as possible, and everything you need is a minimum implementation for posting to the Slack Webhook URL.

NOTE

Slack Attachments struct is currently borrowed from https://github.com/nlopes/slack.

Install

go get -u github.com/kenju/go-slack-webhook

Usage

import "github.com/kenju/go-slack-webhook"

func main() {
  pl := &slackwebhook.SlackPayload{
    Channel: os.Getenv("SLACK_CHANNEL"),
    Attachments: []slackwebhook.Attachment{
      {
        Color: "danger",
        Title: "this is a title"
        Text:  "this is a text",
        Fields: []slackwebhook.AttachmentField{
          {"foo", "bar", true},
        },
      },
    },
  }
  resp, err := slackwebhook.SendSlack(pl, os.Getenv("SLACK_WEBHOOK_URL"),)
  if err != nil {
    log.Fatal(err)
  }
  log.Println(resp)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SendSlack

func SendSlack(pl *SlackPayload, webHookURL string) (*http.Response, error)

SendSlack send SlackPayload body via webHookURL.

Types

type Attachment

type Attachment struct {
	Color    string `json:"color,omitempty"`
	Fallback string `json:"fallback"`

	CallbackID string `json:"callback_id,omitempty"`
	ID         int    `json:"id,omitempty"`

	AuthorID      string `json:"author_id,omitempty"`
	AuthorName    string `json:"author_name,omitempty"`
	AuthorSubname string `json:"author_subname,omitempty"`
	AuthorLink    string `json:"author_link,omitempty"`
	AuthorIcon    string `json:"author_icon,omitempty"`

	Title     string `json:"title,omitempty"`
	TitleLink string `json:"title_link,omitempty"`
	Pretext   string `json:"pretext,omitempty"`
	Text      string `json:"text"`

	ImageURL string `json:"image_url,omitempty"`
	ThumbURL string `json:"thumb_url,omitempty"`

	Fields     []AttachmentField  `json:"fields,omitempty"`
	Actions    []AttachmentAction `json:"actions,omitempty"`
	MarkdownIn []string           `json:"mrkdwn_in,omitempty"`

	Footer     string `json:"footer,omitempty"`
	FooterIcon string `json:"footer_icon,omitempty"`

	Ts json.Number `json:"ts,omitempty"`
}

Attachment contains all the information for an attachment

type AttachmentAction

type AttachmentAction struct {
	Name            string                        `json:"name"`                       // Required.
	Text            string                        `json:"text"`                       // Required.
	Style           string                        `json:"style,omitempty"`            // Optional. Allowed values: "default", "primary", "danger".
	Type            string                        `json:"type"`                       // Required. Must be set to "button" or "select".
	Value           string                        `json:"value,omitempty"`            // Optional.
	DataSource      string                        `json:"data_source,omitempty"`      // Optional.
	MinQueryLength  int                           `json:"min_query_length,omitempty"` // Optional. Default value is 1.
	Options         []AttachmentActionOption      `json:"options,omitempty"`          // Optional. Maximum of 100 options can be provided in each menu.
	SelectedOptions []AttachmentActionOption      `json:"selected_options,omitempty"` // Optional. The first element of this array will be set as the pre-selected option for this menu.
	OptionGroups    []AttachmentActionOptionGroup `json:"option_groups,omitempty"`    // Optional.
	Confirm         *ConfirmationField            `json:"confirm,omitempty"`          // Optional.
	URL             string                        `json:"url,omitempty"`              // Optional.
}

AttachmentAction is a button or menu to be included in the attachment. Required when using message buttons or menus and otherwise not useful. A maximum of 5 actions may be provided per attachment.

type AttachmentActionOption

type AttachmentActionOption struct {
	Text        string `json:"text"`                  // Required.
	Value       string `json:"value"`                 // Required.
	Description string `json:"description,omitempty"` // Optional. Up to 30 characters.
}

AttachmentActionOption the individual option to appear in action menu.

type AttachmentActionOptionGroup

type AttachmentActionOptionGroup struct {
	Text    string                   `json:"text"`    // Required.
	Options []AttachmentActionOption `json:"options"` // Required.
}

AttachmentActionOptionGroup is a semi-hierarchal way to list available options to appear in action menu.

type AttachmentField

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

AttachmentField contains information for an attachment field An Attachment can contain multiple of these

type ConfirmationField

type ConfirmationField struct {
	Title       string `json:"title,omitempty"`        // Optional.
	Text        string `json:"text"`                   // Required.
	OkText      string `json:"ok_text,omitempty"`      // Optional. Defaults to "Okay"
	DismissText string `json:"dismiss_text,omitempty"` // Optional. Defaults to "Cancel"
}

ConfirmationField are used to ask users to confirm actions

type SlackPayload

type SlackPayload struct {
	Channel     string       `json:"channel"`
	Name        string       `json:"name"`
	Text        string       `json:"text"`
	Attachments []Attachment `json:"attachments"`
}

SlackPayload describes the body to send to Slack.

Jump to

Keyboard shortcuts

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