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 ¶
var Disable = triState(&disable)
Disable represents a pointer to false value, for *bool.
var Enable = triState(&enable)
Enable represents a pointer to true value, for *bool.
Functions ¶
func Send ¶
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))
}
}
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"`
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 ¶
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))
}
}
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))
}
}
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.