Documentation
¶
Overview ¶
Package notify sends notifications to offline push channels: an ntfy topic, a generic webhook and/or a chat destination (Discord, Slack, Telegram). Sending is asynchronous through a small bounded queue; nothing here ever carries terminal output (callers only pass wire.NotificationData, whose body is a fixed phrase). Every push is titled with the host it comes from, and the last failure of each channel is kept (on disk, across restarts) until a push to it succeeds. On ntfy, the notifications of one session share a sequence ID: a newer one replaces the older, and Clear dismisses it. On Discord and Telegram, a newer message of a session replaces the older (sent, then the older deleted), and Clear marks it resolved (see chatref.go).
Index ¶
- Constants
- func ClickURL(clickBase, hostID, sessionID string) string
- func ClickURLOf(clickBase, hostID string, n wire.NotificationData) string
- func Enabled(cfg wire.NotifyConfig) bool
- func Fold(ns []wire.NotificationData, lang string) wire.NotificationData
- func IsSessionKey(key string) bool
- func LoadFailures(path string) (map[string]wire.NotifyFailure, error)
- func Only(cfg wire.NotifyConfig, channels []string) (wire.NotifyConfig, error)
- func PageURL(clickPage, hostID, sessionID string) string
- func PageURLOf(clickPage, hostID string, n wire.NotificationData) string
- func ReplaceKey(n wire.NotificationData) string
- func SequenceID(sessionID string) string
- func Text(lang string, p Phrase, args ...any) string
- func UsageLimitBody(lang string, threshold int, resetsAt, now time.Time) string
- func UsageLimitTitle(lang, harness, window, label string) string
- func ValidClickPage(clickPage string) bool
- func Wants(cfg wire.NotifyConfig, n wire.NotificationData) bool
- type ChatDest
- type ChatMessage
- type Digester
- type Phrase
- type Sender
- func (s *Sender) ChatSessions(cfg wire.NotifyConfig) []string
- func (s *Sender) Clear(cfg wire.NotifyConfig, sessionID string) bool
- func (s *Sender) Close()
- func (s *Sender) LastErrors() map[string]wire.NotifyFailure
- func (s *Sender) Push(ctx context.Context, cfg wire.NotifyConfig, n wire.NotificationData) error
- func (s *Sender) Reconfigure(cfg wire.NotifyConfig) bool
- func (s *Sender) ResolveChat(cfg wire.NotifyConfig, sessionID string) bool
- func (s *Sender) Send(cfg wire.NotifyConfig, n wire.NotificationData) bool
- func (s *Sender) SendDigest(cfg wire.NotifyConfig, ns []wire.NotificationData) bool
Constants ¶
const ( ServiceDiscord = "discord" ServiceSlack = "slack" ServiceTelegram = "telegram" )
Services of a chat destination (notify.chat).
const Timeout = 5 * time.Second
Timeout bounds one HTTP request.
Variables ¶
This section is empty.
Functions ¶
func ClickURL ¶ added in v0.4.0
ClickURL is the link an ntfy push of sessionID (may be "") opens (its Click): clickBase with h=<hostID> and, for a session, s=<sessionID> added to its query. It is "" — no link — when clickBase or hostID is empty or clickBase is not an absolute URL. Nothing else goes into the link.
func ClickURLOf ¶ added in v0.17.0
func ClickURLOf(clickBase, hostID string, n wire.NotificationData) string
ClickURLOf is the link an ntfy push of n opens (ClickURL), with sc= and r= for a schedule's run, u= for a limit window.
func Enabled ¶
func Enabled(cfg wire.NotifyConfig) bool
Enabled reports whether cfg has any channel to send to.
func Fold ¶
func Fold(ns []wire.NotificationData, lang string) wire.NotificationData
Fold combines notifications into one. A single notification is returned unchanged; several become reason "digest" with Count set, at the most serious level of them (error, warn, info), titled in lang.
func IsSessionKey ¶ added in v0.19.0
IsSessionKey reports whether key (a ReplaceKey, or a session of ChatSessions) is a session's: a schedule's or a limit window's is settled by no session.
func LoadFailures ¶ added in v0.4.0
func LoadFailures(path string) (map[string]wire.NotifyFailure, error)
LoadFailures reads a last failures file (Sender). A missing file is no failure; the map is never nil, even with an error.
func Only ¶ added in v0.8.0
func Only(cfg wire.NotifyConfig, channels []string) (wire.NotifyConfig, error)
Only keeps the channels of cfg that channels names (wire.Channel*) enabled; none named keeps them all. An unknown name is an error.
func PageURL ¶ added in v0.8.0
PageURL is the link to the open page clickPage for a push of sessionID (may be ""): h=<hostID> and, for a session, s=<sessionID> in its fragment (`<click_page>#h=…&s=…`), so they never reach the page's server. It is "" — no link — when hostID is empty or clickPage is not a notify.click_page (ValidClickPage; empty included).
func PageURLOf ¶ added in v0.17.0
func PageURLOf(clickPage, hostID string, n wire.NotificationData) string
PageURLOf is the link to the open page for a push of n (PageURL), with sc= and r= for a schedule's run, u= for a limit window.
func ReplaceKey ¶ added in v0.17.0
func ReplaceKey(n wire.NotificationData) string
ReplaceKey is what n replaces and is replaced by: its schedule's for a notification of a schedule's run, its window's for a limit window's (usageReplaceKey), else its session ("": none).
func SequenceID ¶ added in v0.4.0
SequenceID is the ntfy sequence ID of session sessionID's notifications: the id itself when ntfy accepts it (1–64 of [A-Za-z0-9_-]; stagent's 16 hex characters always are), else a hash of it that is.
func Text ¶ added in v0.4.0
Text is phrase p in lang (English for an unknown language), formatted with args.
func UsageLimitBody ¶ added in v0.19.0
UsageLimitBody is the body of a notification in lang: the window passed threshold (80 or 100) and resets at resetsAt, or (threshold 0) it reset. The reset time is the host's wall clock: HH:mm within a day of now, else with the month and day.
func UsageLimitTitle ¶ added in v0.19.0
UsageLimitTitle is the title of a notification about window (a wire.UsageWindow* id or "<minutes>m") of harness in lang; label is the window's quota group ("" when it has none).
func ValidClickPage ¶ added in v0.8.0
ValidClickPage reports whether clickPage is a notify.click_page: an absolute https URL with neither a query nor a fragment.
func Wants ¶ added in v0.4.0
func Wants(cfg wire.NotifyConfig, n wire.NotificationData) bool
Wants reports whether the reasons of cfg (with defaults applied) push n: an exited notification is abnormal when its level is warn.
Types ¶
type ChatDest ¶ added in v0.8.0
type ChatDest struct {
Service string
// contains filtered or unexported fields
}
ChatDest is where a notify.chat URL sends. endpoint holds the webhook token or the bot token: it is a secret and never goes into an error or a log.
type ChatMessage ¶ added in v0.8.0
ChatMessage names a message a chat service took, for replacing it later: Discord's message id, or Telegram's chat id and message id. Slack's Incoming Webhooks name none (zero value).
type Digester ¶
type Digester struct {
// contains filtered or unexported fields
}
Digester collects the notifications arriving within one window: the first notification opens a window, and when it closes the collected notifications are flushed together (the caller folds them, see Fold).
func NewDigester ¶
func NewDigester(flush func([]wire.NotificationData)) *Digester
NewDigester calls flush for every closed window that still holds notifications, from a timer goroutine.
func (*Digester) Add ¶
func (d *Digester) Add(n wire.NotificationData, window time.Duration)
Add queues n into the current window, opening one of the given length if none is open.
type Phrase ¶ added in v0.4.0
type Phrase int
Phrase is one of the fixed texts stagent writes into notifications. Program output never becomes a phrase.
const ( PhraseWaitingInput Phrase = iota PhraseTurnComplete // PhraseNeedsApproval // followed by ": <tool>" when known PhraseExited // PhraseExitedCode // %d: exit code PhraseSessionLost // PhraseTerminal // push body of a program's own notification PhraseBell // in-app body of a bare BEL PhraseTest // notify.test PhraseOpen // the chat destination's button to the open page PhraseResolved // title suffix of a chat message marked resolved // Digest titles (%d: count) and the line closing a long digest body. PhraseDigest PhraseDigestTurnComplete PhraseDigestNeedsApproval PhraseDigestWaitingInput PhraseDigestExited PhraseDigestMore // The bodies of a schedule's notifications (reason schedule) and the // digest title of several. PhraseScheduleSucceeded PhraseScheduleFixed PhraseScheduleFailed PhraseScheduleFailedCode // %d: exit code PhraseScheduleTimedOut // PhraseScheduleDenied // %d: refused actions PhraseScheduleUndetermined // PhraseScheduleSkipped // PhraseScheduleAutoStopped // %d: failures in a row PhraseScheduleGUILogin // %s: the agent PhraseDigestSchedule // %d: count // The bodies of a limit window's notifications (reason usage_limit), // the window in their titles and the digest title of several. PhraseLimitUsed // %d: the share passed (80, 100), %s: the reset time PhraseLimitReset // PhraseWindowFiveHour // PhraseWindowSevenDay // PhraseWindowSevenDayModel // %s: the model ("Opus") or quota group PhraseWindowFiveHourScope // %s: the quota group ("Claude and GPT models") PhraseWindowDays // %d: days PhraseWindowHours // %d: hours PhraseWindowMinutes // %d: minutes PhraseDigestUsageLimit // %d: count // The body of a question with several questions (%s: the first, %d: // how many); one question's body is its text alone. PhraseQuestions )
type Sender ¶
type Sender struct {
// contains filtered or unexported fields
}
Sender delivers notifications to the enabled channels of a config.
func NewSender ¶
func NewSender(failures, chatMessages, hostID string, logf func(format string, args ...any)) *Sender
NewSender returns a Sender keeping the last failure per channel in the file failures and the unresolved chat messages in the file chatMessages (both loaded now, rewritten when they change; chatMessages may be "": in memory only) and linking pushes to hostID (see ClickURL, PageURL). logf (may be nil) receives delivery errors.
func (*Sender) ChatSessions ¶ added in v0.8.0
func (s *Sender) ChatSessions(cfg wire.NotifyConfig) []string
ChatSessions returns the sessions with an unresolved chat message sent through cfg's chat URL (from before a restart, for the daemon to take back).
func (*Sender) Clear ¶ added in v0.4.0
func (s *Sender) Clear(cfg wire.NotifyConfig, sessionID string) bool
Clear queues what follows session sessionID settling, behind the pushes queued before it: the dismissal of its ntfy notification (ntfy's clear: marked read and removed from the notification drawer) and its chat messages marked resolved. The generic webhook gets nothing.
func (*Sender) Close ¶
func (s *Sender) Close()
Close stops the worker after the queued pushes (each request bounded by Timeout; a chat 429 adds a wait of Timeout at most and one resend).
func (*Sender) LastErrors ¶ added in v0.4.0
func (s *Sender) LastErrors() map[string]wire.NotifyFailure
LastErrors returns the last failed push per channel (never nil).
func (*Sender) Push ¶
func (s *Sender) Push(ctx context.Context, cfg wire.NotifyConfig, n wire.NotificationData) error
Push sends n, titled with the host (labelled), to every enabled channel of cfg synchronously and records each channel's outcome (LastErrors). notify.test reports its error, which never contains the ntfy topic, the webhook URL or the chat URL. A 429 is not resent.
func (*Sender) Reconfigure ¶ added in v0.8.0
func (s *Sender) Reconfigure(cfg wire.NotifyConfig) bool
Reconfigure takes notify config cfg. A chat URL other than the one configured before (changed, or none now) forgets the chat's last failure at once, and the outcomes of pushes still queued for the old URL are not recorded: a failure of the old URL says nothing about the new one. The first config and disabling the chat keep it. Then, behind the pushes queued before it, the chat messages sent through another chat URL are forgotten, never edited through the old URL.
func (*Sender) ResolveChat ¶ added in v0.8.0
func (s *Sender) ResolveChat(cfg wire.NotifyConfig, sessionID string) bool
ResolveChat queues marking session sessionID's chat messages resolved, as Clear does, without touching ntfy: the session is gone (it did not survive a restart).
func (*Sender) Send ¶
func (s *Sender) Send(cfg wire.NotifyConfig, n wire.NotificationData) bool
Send queues n for delivery to cfg's enabled channels without blocking. It reports false when the queue is full (the push is dropped) or the sender is closed.
func (*Sender) SendDigest ¶ added in v0.8.0
func (s *Sender) SendDigest(cfg wire.NotifyConfig, ns []wire.NotificationData) bool
SendDigest queues the notifications of one digest window (see Fold) like Send. A chat message of several remembers their sessions: it is marked resolved once they have all settled.