Skip to content

Notices

Package notify posts short notices for people, such as operational alerts, to a chat service's incoming webhook. It needs no bot, token or connection: the webhook URL is the whole configuration, and it decides the service.

import "github.com/plexusone/omnichat/notify"

n, err := notify.NewWebhook(os.Getenv("ALERT_WEBHOOK_URL"))
if err != nil {
    return err
}
err = n.Notify(ctx, notify.Notice{
    Title:  "Sign-in mail halted",
    Text:   "The hourly budget was used up.",
    Fields: []notify.Field{{Name: "Sent", Value: "20 of 20"}},
    Level:  notify.LevelCritical,
})

Services

The URL's host decides the service:

Host Service Payload
hooks.slack.com Slack incoming webhook text in mrkdwn
discord.com, discordapp.com or a *.discord.com host, under /api/webhooks/ Discord channel webhook content in markdown, cut to 2,000 characters, with allowed_mentions empty
anything else generic JSON with title, text, level and fields

WithService sets the service instead, for a webhook behind a proxy.

The title, text and fields are plain text: markup in them is escaped, so a notice cannot mention @everyone, <!channel> or form links.

Levels

LevelInfo (the default), LevelWarning and LevelCritical. Slack and Discord show warnings and critical notices with a [WARNING] or [CRITICAL] prefix on the title; the generic form carries level.

Making a webhook

  • Slack: create an app at api.slack.com/apps, turn on Incoming Webhooks, and add a webhook to the channel. The URL starts with https://hooks.slack.com/services/.
  • Discord: in the channel's settings, Integrations, Webhooks, New Webhook, then Copy Webhook URL.

Security

  • The URL is a credential. Anyone with it can post to the channel. Keep it in the environment or a secret store, never in a configuration file.
  • Errors never include the URL. Delivery errors name the service and the status or the network failure only.
  • https only, except http to a loopback address for tests.
  • Bounded: without WithHTTPClient, a delivery times out after 10 seconds.