Documentation
¶
Overview ¶
Package notifier dispatches a single message to multiple notification channels concurrently.
A Notifier is constructed with one or more provider options and then used to broadcast a Message:
n := notifier.New(
notifier.WithDingTalk(os.Getenv("DINGTALK_TOKEN"), os.Getenv("DINGTALK_SECRET")),
notifier.WithBark(os.Getenv("BARK_KEY"), ""),
)
if err := n.Send(ctx, notifier.Message{
Subject: "alert",
Content: "the disk is full",
}); err != nil {
log.Print(err)
}
Each provider runs in its own goroutine; failures are aggregated into a *MultiError so a single broken channel does not silence the others.
Channel-specific options (Bark sound, Lark markdown, ...) travel via Message.Extras using string keys defined in the corresponding provider package.
Custom providers ¶
Anything that implements provider.Provider can be registered with WithProvider. The provider package documents the contract.
Index ¶
- Constants
- Variables
- type Format
- type Message
- type MultiError
- type Notifier
- type Option
- func WithBark(key, server string) Option
- func WithDingTalk(token, secret string) Option
- func WithFeishu(token, secret string) Option
- func WithHTTPClient(hc *http.Client) Option
- func WithLark(token, secret string) Option
- func WithProvider(p Provider) Option
- func WithServerChan(userID, sendKey string) Option
- func WithTelegram(token, chatID string) Option
- func WithWeCom(key string) Option
- type Provider
- type SendError
Constants ¶
const FormatMarkdown = provider.FormatMarkdown
FormatMarkdown signals the message body uses Markdown.
const FormatText = provider.FormatText
FormatText is plain text, the default Format.
Variables ¶
var ErrNoProvider = provider.ErrNoProvider
ErrNoProvider is returned by Send when no providers are configured.
Functions ¶
This section is empty.
Types ¶
type Format ¶ added in v0.3.0
Format is an alias of provider.Format, describing the wire format of Message.Content.
type Message ¶ added in v0.3.0
Message is an alias of provider.Message — the payload broadcast to providers.
type MultiError ¶ added in v0.3.0
type MultiError = provider.MultiError
MultiError is an alias of provider.MultiError aggregating per-provider failures from a single Send call.
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
Notifier dispatches a Message to all configured providers concurrently. Use New to construct one and Send to deliver a message.
func (*Notifier) HTTPClient ¶ added in v0.3.0
HTTPClient returns the shared *http.Client used by convenience helpers such as WithDingTalk to construct providers. Returns nil if WithHTTPClient was never called; provider packages then fall back to their own default client.
func (*Notifier) Send ¶
Send dispatches msg to every configured provider concurrently. It returns:
- ErrNoProvider if no providers are configured
- *MultiError if at least one provider failed; successful providers are not reported
- nil if all providers succeeded
ctx is forwarded unchanged to each provider; cancelling it aborts every in-flight request. A failure in one provider does not prevent the others from running.
type Option ¶
type Option func(*Notifier)
Option configures a Notifier. Options are applied in order, so WithHTTPClient must precede any WithXxx convenience helper that should use the shared client.
func WithBark ¶
WithBark registers a Bark provider. server is the host (e.g. "api.day.app"); pass an empty string to use the public default.
func WithDingTalk ¶
WithDingTalk registers a DingTalk webhook provider with the given access token and (optional) secret.
func WithFeishu ¶
WithFeishu registers a Feishu webhook provider.
func WithHTTPClient ¶ added in v0.3.0
WithHTTPClient sets the *http.Client used by the convenience helpers (WithDingTalk, WithBark, ...) when they construct providers. Providers added via WithProvider keep whatever client they were configured with.
func WithProvider ¶ added in v0.3.0
WithProvider registers a Provider. A nil Provider is silently ignored so callers can pass the result of pkg.New directly even when its required field is empty.
func WithServerChan ¶ added in v0.1.1
WithServerChan registers a Server 酱 provider. userID may be empty for the modern SCT API.
func WithTelegram ¶ added in v0.3.0
WithTelegram registers a Telegram bot provider. token is the bot token from @BotFather; chatID is the destination chat (numeric or "@channel" form).
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Basic broadcast example: send a single message to every configured channel concurrently.
|
Basic broadcast example: send a single message to every configured channel concurrently. |
|
internal
|
|
|
httpx
Package httpx provides a small HTTP helper shared by all notifier providers.
|
Package httpx provides a small HTTP helper shared by all notifier providers. |
|
Package provider defines the Provider interface that every notifier channel implements, plus the shared Message and error types.
|
Package provider defines the Provider interface that every notifier channel implements, plus the shared Message and error types. |
|
bark
Package bark implements a notifier.Provider for the Bark iOS app (https://day.app/).
|
Package bark implements a notifier.Provider for the Bark iOS app (https://day.app/). |
|
dingtalk
Package dingtalk implements a notifier.Provider for DingTalk group robots (https://open.dingtalk.com/).
|
Package dingtalk implements a notifier.Provider for DingTalk group robots (https://open.dingtalk.com/). |
|
feishu
Package feishu implements a notifier.Provider for Feishu custom bots (https://www.feishu.cn/).
|
Package feishu implements a notifier.Provider for Feishu custom bots (https://www.feishu.cn/). |
|
lark
Package lark implements a notifier.Provider for Lark custom bots (https://www.larksuite.com/).
|
Package lark implements a notifier.Provider for Lark custom bots (https://www.larksuite.com/). |
|
serverchan
Package serverchan implements a notifier.Provider for Server 酱 (https://sct.ftqq.com/).
|
Package serverchan implements a notifier.Provider for Server 酱 (https://sct.ftqq.com/). |
|
telegram
Package telegram implements a notifier.Provider for Telegram Bot API (https://core.telegram.org/bots/api#sendmessage).
|
Package telegram implements a notifier.Provider for Telegram Bot API (https://core.telegram.org/bots/api#sendmessage). |
|
wecom
Package wecom implements a notifier.Provider for WeCom (企业微信) group robots (https://developer.work.weixin.qq.com/document/path/91770).
|
Package wecom implements a notifier.Provider for WeCom (企业微信) group robots (https://developer.work.weixin.qq.com/document/path/91770). |