hooks

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	ActionWebhookCreate = "webhook.create"
	ActionWebhookRevoke = "webhook.revoke"
)

Audit actions written by this package. Detail never carries the token.

View Source
const (
	PostsPerMinute = 30
	PostBurst      = 10
)

Rate limit of one webhook: 30 posts per minute, burst 10.

View Source
const MaxBodyBytes = 256 << 10

MaxBodyBytes is the largest request body a webhook accepts.

View Source
const MaxUsernameChars = 64

MaxUsernameChars is the longest sender name shown with a webhook message.

Variables

View Source
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

func CleanUsername(s string) string

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

func DecodeBody(contentType string, body []byte) ([]byte, error)

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

func Mrkdwn(s string) string

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~~
&lt; &gt; &amp;   ->  < > &, 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.

func Plain

func Plain(s string) string

Plain translates text Slack shows literally (titles, plain_text elements, field names): entities are decoded and links and mentions handled as in Mrkdwn, but no formatting is applied.

Types

type CreateInput

type CreateInput struct {
	ChannelID string
	Name      string
}

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

func Translate(raw []byte) (Message, error)

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 New

func New(o Options) *Service

New returns a Service.

func (*Service) Allow

func (s *Service) Allow(webhookID string) (bool, time.Duration)

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

func (s *Service) Deliver(ctx context.Context, t Target, m Message) (Result, error)

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

func (s *Service) List(ctx context.Context) ([]protocol.Webhook, error)

List returns every webhook, revoked ones included, oldest first. It never returns a token.

func (*Service) Lookup

func (s *Service) Lookup(ctx context.Context, token string) (Target, error)

Lookup finds the live webhook for a path token. Anything wrong with it is ErrNotFound: the token is unknown or malformed, the webhook is revoked, its pseudo-app is disabled or its bot deactivated, or the bot left the channel.

func (*Service) Revoke

func (s *Service) Revoke(ctx context.Context, aud audit.Actor, id string) error

Revoke ends a webhook: its URL answers 404 from now on and its pseudo-app is disabled. Revoking a revoked webhook changes nothing.

type Target

type Target struct {
	ID        string
	ChannelID string
	AppID     string
	// BotUserID is the pseudo-app's bot account, the author of every message.
	BotUserID string
	Name      string
}

Target is the webhook a token opens.

Jump to

Keyboard shortcuts

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