notify

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 18 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 notify is the domain-free notification half shared by programs that need alerts: the Channel interface, a per-service Notifier, and ntfy, generic webhook, Discord, Slack and email channels.

IMPORTANT: Channel secrets (ntfy topic URL, webhook URL) must never be stored in a database or logged. Callers read them from root-only credential files and pass them to channel constructors.

Index

Constants

View Source
const (
	ChannelTypeWebhook = "webhook"
	ChannelTypeEmail   = "email"
)

ChannelTypeWebhook and ChannelTypeEmail join ntfy as channel types.

View Source
const (
	// EmailSecurityStartTLS upgrades a plain connection (usually port 587).
	EmailSecurityStartTLS = "starttls"
	// EmailSecurityTLS connects encrypted from the start (usually port 465).
	EmailSecurityTLS = "tls"
	// EmailSecurityNone sends unencrypted; only sensible on a trusted LAN relay.
	EmailSecurityNone = "none"
)

Email connection security modes.

Variables

View Source
var ErrInvalidChannel = errors.New("invalid notification channel")

ErrInvalidChannel marks channel settings that are incomplete or wrong.

View Source
var ErrNoChannels = errors.New("no notifications are set up for this service (set up a notification channel first)")

ErrNoChannels is returned by Notifier.Send when the event's service has no registered delivery channel. It is a loud signal, not a silent success: a caller like SweepFreshness must not treat a freshness breach as reported when nothing was actually able to deliver it, or a service configured after the fact would never hear about a breach that happened before its channels existed.

Functions

This section is empty.

Types

type Channel

type Channel interface {
	// Send delivers the event, returning a non-nil error if delivery
	// failed. ctx bounds how long delivery may block (e.g. so a
	// pending SIGTERM is not held up by a stuck HTTP call while the
	// caller's lock is held).
	Send(ctx context.Context, event Event) error
	// Type returns a short label for this channel (e.g. "ntfy", "webhook").
	Type() string
}

Channel is the interface for a notification delivery mechanism.

type DiscordChannel

type DiscordChannel struct {
	// contains filtered or unexported fields
}

DiscordChannel posts events to a Discord webhook URL.

func NewDiscordChannel

func NewDiscordChannel(url string) *DiscordChannel

NewDiscordChannel creates a Discord channel; url is a secret.

func (*DiscordChannel) Send

func (c *DiscordChannel) Send(ctx context.Context, event Event) error

Send posts the event. Neither the error nor logs built from it contain the webhook URL (see stripURLSecret).

func (*DiscordChannel) Type

func (c *DiscordChannel) Type() string

Type returns "discord" or "slack".

type EmailChannel

type EmailChannel struct {
	EmailSettings
	// Username is the login name for the mail server; empty means no login
	Username string
	Password string // from the credential store; never logged
	// Product prefixes the subject line, e.g. "Example" gives "[Example] ...".
	Product string
	// TLSConfig is injected by tests; nil uses the host's name.
	TLSConfig *tls.Config
}

EmailChannel delivers events as plain-text email over SMTP.

func (*EmailChannel) Address

func (e *EmailChannel) Address() string

Address returns the mail server, for display.

func (*EmailChannel) Send

func (e *EmailChannel) Send(ctx context.Context, event Event) error

Send emails the event to every recipient. The error never contains the password.

func (*EmailChannel) Type

func (e *EmailChannel) Type() string

Type returns "email".

type EmailSettings

type EmailSettings struct {
	// Host is the mail server address
	Host string `json:"host"`
	// Port is the mail server port
	Port int `json:"port"`
	// Security selects plain, STARTTLS or implicit TLS
	Security string `json:"security"`
	// From is the sender address
	From string `json:"from"`
	// To lists the recipient addresses
	To []string `json:"to"`
}

EmailSettings are an email channel's non-secret settings. The password lives in the channel's token credential file.

func (EmailSettings) Validate

func (s EmailSettings) Validate() error

Validate checks the settings are complete enough to send with.

type Event

type Event struct {
	// Service names what the event is about (a product, host or service).
	Service string
	// Type is the kind of event.
	Type EventType
	// Message is the human-readable event description.
	Message string
	// Detail includes structured information (run ID, state, etc).
	Detail string
	// Severity is "error", "warning", or "info" for display purposes.
	Severity string
	// Timestamp is when the event occurred.
	Timestamp time.Time
	// Title, when set, is the headline channels show instead of a
	// generated one (e.g. "Example test notification").
	Title string
	// Extra holds additional top-level fields for the webhook JSON
	// body. They are added after the standard fields, in key order,
	// and are covered by the signature. A key that names a standard
	// field (service, type, severity, message, detail, timestamp) is
	// ignored, so Extra can never change one. Other channels ignore
	// Extra.
	Extra map[string]any
	// Fields are extra labelled facts about the event. The email body
	// shows them in order, one "Name: Value" line each, between the
	// Service and When lines. Other channels ignore Fields.
	Fields []Field
}

Event describes a notification event.

type EventType

type EventType string

EventType describes the kind of notification being sent.

const (
	// EventTest is the test notification a channel's test-send delivers
	// through the very same Send path as real events.
	EventTest EventType = "test"
)

type Field

type Field struct {
	// Name is the label shown before the colon.
	Name string
	// Value is the text shown after it; line breaks become spaces.
	Value string
}

Field is one labelled fact in Event.Fields, e.g. {"Region", "eu"}.

type Notifier

type Notifier struct {

	// Resolve, when set, is asked for a service's channels at send time
	// whenever none were registered for it up front. This is what lets
	// a long-running a long-running process notify about a service added
	// after it started (from the web UI, say) — or pick up a channel
	// credential file written after startup — without a restart. An
	// error from Resolve is returned from Send, never swallowed.
	Resolve func(service string) ([]Channel, error)
	// contains filtered or unexported fields
}

Notifier sends events to the channels registered for a service.

func NewNotifier

func NewNotifier() *Notifier

NewNotifier creates a new notifier with empty channel registry.

func (*Notifier) RegisterChannel

func (n *Notifier) RegisterChannel(service string, ch Channel)

RegisterChannel registers a delivery channel for a service. Multiple channels can be registered per service; each Send call delivers to all.

func (*Notifier) Send

func (n *Notifier) Send(ctx context.Context, event Event) error

Send delivers an event to all registered channels for that service. If delivery fails on any channel, every failure is collected and returned together (via errors.Join) so one channel's outage can never hide another's — the isolation-of-failure principle. All channels are attempted regardless of individual failures.

If the service has no registered channel at all, Send returns ErrNoChannels rather than silently succeeding: notifications are optional to configure, but once a caller asks to send one, "nobody was listening" must be visible, not indistinguishable from success.

type NtfyChannel

type NtfyChannel struct {
	Topic string // ntfy topic URL from credential file
	// Secret is the optional access credential for a protected ntfy
	// server, from the credential store: a bearer token, or — when
	// Username is set — that user's password.
	Secret string
	// Username is the optional ntfy login name
	Username string
	Client   *http.Client // injected for tests; nil defaults to clientWithTimeout()
}

NtfyChannel sends notifications via ntfy.sh HTTP POST. Secrets (topic URL) are passed from the caller and never logged.

func NewNtfyChannel

func NewNtfyChannel(topic string) *NtfyChannel

NewNtfyChannel creates a channel that sends to the given ntfy topic. topic must be a full URL read from a root-only credential file.

func (*NtfyChannel) Send

func (n *NtfyChannel) Send(ctx context.Context, event Event) error

Send delivers the event to ntfy.

Neither the returned error nor any log line built from it may contain the ntfy topic URL: it is a secret read from a root-only credential file . See stripURLSecret.

func (*NtfyChannel) Type

func (n *NtfyChannel) Type() string

Type returns "ntfy".

type SlackChannel

type SlackChannel struct {
	// contains filtered or unexported fields
}

SlackChannel posts events to a Slack incoming-webhook URL.

func NewSlackChannel

func NewSlackChannel(url string) *SlackChannel

NewSlackChannel creates a Slack channel; url is a secret.

func (*SlackChannel) Send

func (c *SlackChannel) Send(ctx context.Context, event Event) error

Send posts the event. Neither the error nor logs built from it contain the webhook URL (see stripURLSecret).

func (*SlackChannel) Type

func (c *SlackChannel) Type() string

Type returns "discord" or "slack".

type WebhookChannel

type WebhookChannel struct {
	URL    string       // webhook URL from credential file
	Client *http.Client // injected for tests; nil defaults to clientWithTimeout()
	// Secret, when set, signs each body: the SignatureHeader header
	// carries "sha256=" and the hex HMAC-SHA256 of the body.
	Secret string
	// SignatureHeader names the signature header; empty means
	// "X-Signature".
	SignatureHeader string
	// contains filtered or unexported fields
}

WebhookChannel sends notifications via a generic HTTP webhook. Secrets (webhook URL) are passed from the caller and never logged.

func NewWebhookChannel

func NewWebhookChannel(url string) *WebhookChannel

NewWebhookChannel creates a channel that POSTs to the given URL. url must be read from a root-only credential file.

func (*WebhookChannel) Send

func (w *WebhookChannel) Send(ctx context.Context, event Event) error

Send delivers the event as JSON to the webhook URL.

Neither the returned error nor any log line built from it may contain the webhook URL: it is a secret read from a root-only credential file . See stripURLSecret.

func (*WebhookChannel) Type

func (w *WebhookChannel) Type() string

Type returns "webhook".

Jump to

Keyboard shortcuts

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