Documentation
¶
Overview ¶
Package slack includes helpers for sending slack webhooks.
Index ¶
- Constants
- type Config
- func (c Config) ChannelOrDefault(inherited ...string) string
- func (c Config) IconEmojiOrDefault(inherited ...string) string
- func (c Config) IconURLOrDefault(inherited ...string) string
- func (c Config) IsZero() bool
- func (c Config) UsernameOrDefault(inherited ...string) string
- func (c Config) WebhookOrDefault(defaults ...string) string
- type Message
- type MessageAttachment
- type MessageAttachmentField
- type MessageOption
- func WithChannel(channel string) MessageOption
- func WithChannelOrDefault(channel string) MessageOption
- func WithIconEmoji(emoji string) MessageOption
- func WithIconEmojiOrDefault(emoji string) MessageOption
- func WithIconURL(url string) MessageOption
- func WithIconURLOrDefault(url string) MessageOption
- func WithMessageAttachment(attachment MessageAttachment) MessageOption
- func WithResponseType(responseType string) MessageOption
- func WithUsername(username string) MessageOption
- func WithUsernameOrDefault(username string) MessageOption
- type MockWebhookSender
- type PostMessageResponse
- type Sender
- type WebhookSender
- func (whs WebhookSender) Defaults() []MessageOption
- func (whs WebhookSender) PostMessage(channel, messageText string, options ...MessageOption) error
- func (whs WebhookSender) PostMessageAndReadResponse(channel, messageText string, options ...MessageOption) (*PostMessageResponse, error)
- func (whs WebhookSender) PostMessageContext(ctx context.Context, channel, messageText string, options ...MessageOption) error
- func (whs WebhookSender) Send(ctx context.Context, message Message) error
- func (whs WebhookSender) SendAndReadResponse(ctx context.Context, message Message) (*PostMessageResponse, error)
Constants ¶
const (
// ErrNon200 is the exception class when a non-200 is returned from slack.
ErrNon200 = "slack; non-200 status code returned from remote"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
APIToken string `json:"apiToken,omitempty" yaml:"apiToken,omitempty" env:"SLACK_TOKEN"`
Username string `json:"username,omitempty" yaml:"username,omitempty" env:"SLACK_USERNAME"`
Channel string `json:"channel,omitempty" yaml:"channel,omitempty" env:"SLACK_CHANNEL"`
IconURL string `json:"iconURL,omitempty" yaml:"iconURL,omitempty" env:"SLACK_ICON_URL"`
IconEmoji string `json:"iconEmoji,omitempty" yaml:"iconEmoji,omitempty" env:"SLACK_ICON_EMOJI"`
Webhook string `json:"webhook,omitempty" yaml:"webhook,omitempty" env:"SLACK_WEBHOOK"`
}
Config represents the required fields for the config.
func (Config) ChannelOrDefault ¶
ChannelOrDefault returns a property or default.
func (Config) IconEmojiOrDefault ¶
IconEmojiOrDefault returns a property or default.
func (Config) IconURLOrDefault ¶
IconURLOrDefault returns a property or default.
func (Config) UsernameOrDefault ¶
UsernameOrDefault returns a property or a default.
func (Config) WebhookOrDefault ¶
WebhookOrDefault returns the webhook url.
type Message ¶
type Message struct {
Username string `json:"username,omitempty"`
Channel string `json:"channel,omitempty"`
AsUser bool `json:"as_user"`
Parse string `json:"parse,omitempty"`
ResponseType string `json:"response_type,omitempty"`
Text string `json:"text"`
UnfurlLinks bool `json:"unfurl_links"`
UnfurlMedia bool `json:"unfurl_media"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Markdown bool `json:"mrkdwn,omitempty"`
EscapeText bool `json:"escape_text"`
ThreadTimestamp string `json:"thread_ts,omitempty"`
ReplyBroadcast bool `json:"reply_broadcast"`
LinkNames int `json:"link_names"`
Attachments []MessageAttachment `json:"attachments"`
// Response-specific fields
BotID string `json:"bot_id,omitempty"`
Type string `json:"type,omitempty"`
SubType string `json:"subtype,omitempty"`
Timestamp string `json:"ts,omitempty"`
}
Message is a message sent to slack.
func ApplyMessageOptions ¶ added in v0.3.2
func ApplyMessageOptions(m Message, options ...MessageOption) Message
ApplyMessageOptions applies a set of options against a message and returns the mutated copy.
func NewMessage ¶ added in v1.20201204.1
func NewMessage(options ...MessageOption) *Message
NewMessage creates a new message with a given set of options.
type MessageAttachment ¶
type MessageAttachment struct {
Title string `json:"title,omitempty"`
Color string `json:"color,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
Fields []MessageAttachmentField `json:"fields,omitempty"`
}
MessageAttachment is an attachment for a message.
type MessageAttachmentField ¶
type MessageAttachmentField struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short"`
}
MessageAttachmentField is a field on an attachment.
type MessageOption ¶ added in v0.3.2
type MessageOption func(m *Message)
MessageOption is a mutator for messages.
func WithChannel ¶ added in v0.3.2
func WithChannel(channel string) MessageOption
WithChannel sets the channel.
func WithChannelOrDefault ¶
func WithChannelOrDefault(channel string) MessageOption
WithChannelOrDefault sets the channel if its unset.
func WithIconEmoji ¶ added in v0.3.2
func WithIconEmoji(emoji string) MessageOption
WithIconEmoji sets the icon emoji.
func WithIconEmojiOrDefault ¶
func WithIconEmojiOrDefault(emoji string) MessageOption
WithIconEmojiOrDefault sets the icon emoji.
func WithIconURL ¶ added in v0.3.2
func WithIconURL(url string) MessageOption
WithIconURL sets the icon url.
func WithIconURLOrDefault ¶
func WithIconURLOrDefault(url string) MessageOption
WithIconURLOrDefault sets the icon url.
func WithMessageAttachment ¶
func WithMessageAttachment(attachment MessageAttachment) MessageOption
WithMessageAttachment adds a message attachment.
func WithResponseType ¶
func WithResponseType(responseType string) MessageOption
WithResponseType sets the response type.
func WithUsername ¶ added in v0.3.2
func WithUsername(username string) MessageOption
WithUsername sets the username.
func WithUsernameOrDefault ¶
func WithUsernameOrDefault(username string) MessageOption
WithUsernameOrDefault sets the username.
type MockWebhookSender ¶ added in v1.20201204.1
type MockWebhookSender chan Message
MockWebhookSender is a mocked sender.
func NewMockWebhookSender ¶ added in v1.20201204.1
func NewMockWebhookSender() MockWebhookSender
NewMockWebhookSender creates a new mock sender.
func (MockWebhookSender) PostMessage ¶ added in v1.20201204.1
func (ms MockWebhookSender) PostMessage(channel, text string, options ...MessageOption) error
PostMessage sends a mocked message.
func (MockWebhookSender) PostMessageContext ¶ added in v1.20201204.1
func (ms MockWebhookSender) PostMessageContext(ctx context.Context, channel, text string, options ...MessageOption) error
PostMessageContext sends a mocked message.
type PostMessageResponse ¶ added in v1.20201204.1
type PostMessageResponse struct {
OK bool `json:"ok"`
Channel string `json:"channel,omitempty"`
Timestamp string `json:"ts,omitempty"`
Message Message `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
PostMessageResponse is a slack response
type Sender ¶ added in v0.3.1
type Sender interface {
Send(context.Context, Message) error
PostMessage(string, string, ...MessageOption) error
PostMessageContext(context.Context, string, string, ...MessageOption) error
}
Sender is a type that can send slack messages.
type WebhookSender ¶
type WebhookSender struct {
*webutil.RequestSender
Config *Config
}
WebhookSender sends slack webhooks.
func New ¶ added in v1.20201204.1
func New(cfg *Config) *WebhookSender
New creates a new webhook sender.
func (WebhookSender) Defaults ¶
func (whs WebhookSender) Defaults() []MessageOption
Defaults returns default message options.
func (WebhookSender) PostMessage ¶ added in v1.20201204.1
func (whs WebhookSender) PostMessage(channel, messageText string, options ...MessageOption) error
PostMessage posts a basic message to a given channel.
func (WebhookSender) PostMessageAndReadResponse ¶ added in v1.20201204.1
func (whs WebhookSender) PostMessageAndReadResponse(channel, messageText string, options ...MessageOption) (*PostMessageResponse, error)
PostMessageAndReadResponse posts a basic message to a given channel and returns the deserialized response
func (WebhookSender) PostMessageContext ¶ added in v1.20201204.1
func (whs WebhookSender) PostMessageContext(ctx context.Context, channel, messageText string, options ...MessageOption) error
PostMessageContext posts a basic message to a given chanel with a given context.
func (WebhookSender) Send ¶
func (whs WebhookSender) Send(ctx context.Context, message Message) error
Send sends a slack hook.
func (WebhookSender) SendAndReadResponse ¶ added in v1.20201204.1
func (whs WebhookSender) SendAndReadResponse(ctx context.Context, message Message) (*PostMessageResponse, error)
SendAndReadResponse sends a slack hook and returns the deserialized response