Documentation
¶
Overview ¶
Package hooks is the incoming-webhook domain (plan pipeline step 12): a secret URL, POST /hooks/{token}, that posts into one channel as a pseudo-app. It accepts the native payload ({"text", "username"?, "alert"?}) and a subset of the Slack webhook format (text, username, icon_emoji, attachments, Block Kit blocks), as JSON or as a form with a payload= field, and translates it to safe Markdown.
A webhook is a webhooks row plus an apps row and a bot user created with it (the pseudo-app), which authors the messages and owns the alerts. The secret is shown once; only its SHA-256 is stored. Every message it posts is stored with mentions switched off, so a payload can never notify anyone, and its username is only a display name carried in the message.
Index ¶
- Constants
- Variables
- func CleanUsername(s string) string
- func DecodeBody(contentType string, body []byte) ([]byte, error)
- func Mrkdwn(s string) string
- func Plain(s string) string
- type CreateInput
- type Message
- type Options
- type Result
- type Service
- func (s *Service) Allow(webhookID string) (bool, time.Duration)
- func (s *Service) Create(ctx context.Context, admin chat.Actor, aud audit.Actor, in CreateInput) (protocol.Webhook, string, error)
- func (s *Service) Deliver(ctx context.Context, t Target, m Message) (Result, error)
- func (s *Service) List(ctx context.Context) ([]protocol.Webhook, error)
- func (s *Service) Lookup(ctx context.Context, token string) (Target, error)
- func (s *Service) Revoke(ctx context.Context, aud audit.Actor, id string) error
- type Target
Constants ¶
const ( ActionWebhookCreate = "webhook.create" ActionWebhookRevoke = "webhook.revoke" )
Audit actions written by this package. Detail never carries the token.
const ( PostsPerMinute = 30 PostBurst = 10 )
Rate limit of one webhook: 30 posts per minute, burst 10.
const MaxBodyBytes = 256 << 10
MaxBodyBytes is the largest request body a webhook accepts.
const MaxUsernameChars = 64
MaxUsernameChars is the longest sender name shown with a webhook message.
Variables ¶
var ErrUnsupportedMedia = errors.New("Content-Type must be application/json or application/x-www-form-urlencoded")
ErrUnsupportedMedia is returned by DecodeBody for a Content-Type other than application/json and application/x-www-form-urlencoded.
Functions ¶
func CleanUsername ¶
CleanUsername makes the sender name of a webhook message safe to show: no control or formatting characters (bidirectional overrides included), blanks collapsed, at most MaxUsernameChars characters.
func DecodeBody ¶
DecodeBody returns the JSON document in a webhook request body: the body itself for application/json, the payload= field for application/x-www-form-urlencoded (Slack's legacy form).
func Mrkdwn ¶
Mrkdwn translates Slack mrkdwn into Markdown that is safe to store and show:
<https://x|text> -> [text](<https://x>) (http, https and mailto only;
any other scheme is plain text)
<https://x> -> [https://x](<https://x>)
*bold* _italic_ ~strike~ -> **bold** *italic* ~~strike~~
< > & -> < > &, escaped so they stay literal
<@U1> <#C1> <!channel> <!here> <!everyone> <!subteam^S1> -> removed
Everything else that Markdown would interpret (brackets, backslashes, stray emphasis marks, a leading #) is escaped, so no raw HTML, image, link or mention can come out of text the sender did not mark as such.
Types ¶
type CreateInput ¶
CreateInput are the inputs of Create.
type Message ¶
type Message struct {
// Body is the Markdown text of a chat message (empty for an alert).
Body string
// Username is the sender name shown instead of the webhook's own; it is
// never matched against a real user.
Username string
// Alert is set for a native alert payload; the alerts service posts it.
Alert *protocol.AlertInput
}
Message is a webhook payload, translated.
func Translate ¶
Translate turns a webhook JSON document into a Message. A document with an alert object is native: {"text", "username"?, "alert": {severity, title, text, fields, link, dedupe_key, renotify_after_s?}}. Anything else is read as the Slack subset (text, username, icon_emoji, attachments, blocks) and translated to Markdown. An empty result is a validation error.
type Options ¶
type Options struct {
DB *store.DB
Chat *chat.Service
Apps *apps.Service
Alerts *alerts.Service
// Now is the clock of the rate limit; nil means time.Now.
Now func() time.Time
}
Options configure a Service. All fields but Now are required.
type Result ¶
type Result struct {
// Alert is set when the payload was an alert; Transition is then what the
// alerts service did with it.
Alert bool
Transition string
}
Result is what a delivery did.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the webhooks domain service.
func (*Service) Allow ¶
Allow takes one post from the webhook's bucket and reports the wait when the bucket is empty.
func (*Service) Create ¶
func (s *Service) Create(ctx context.Context, admin chat.Actor, aud audit.Actor, in CreateInput) (protocol.Webhook, string, error)
Create makes a webhook for a public or private channel: a pseudo-app named name with a bot user that joins the channel, and a secret token. admin must be an admin (the bot is added to the channel as one). It returns the webhook and the token, which is not recoverable afterwards.
func (*Service) Deliver ¶
Deliver posts a translated payload for the webhook. Text goes in as a chat message authored by the pseudo-app with the sender name as display override; an alert goes through the alerts service with the pseudo-app as its source. Neither can mention anyone.
func (*Service) List ¶
List returns every webhook, revoked ones included, oldest first. It never returns a token.