feat(notify): the remaining notification services of Uptime Kuma
Adds 93 services, bringing the total to 109 plus webhook and email: chat and messengers (DingTalk, Feishu, LINE, Nextcloud Talk, Threema, WhatsApp gateways, Nostr, Home Assistant, …), push (Bark, Gorush, ServerChan, …), SMS and calls (Twilio, Plivo, 46elks, Alibaba Cloud, Teltonika routers, …), email APIs (SendGrid, Brevo, Resend, Microsoft Graph, …) and incident management (Grafana OnCall, Jira Service Management, Splunk On-Call, Squadcast, SIGNL4, …). Customers bring their own credentials; secrets are sealed like the others. Services pair incidents by their ID where they can resolve alerts. Signed requests (DingTalk, Nextcloud Talk, Alibaba Cloud) and replies that report failures with status 200 are handled. Nostr messages are sealed and gift-wrapped (NIP-17/59) with go-nostr, but sent over our own WebSocket connections so relays go through the network guard. Messages now carry the monitor's name, ID and target. The channel form groups services, the i18n test derives provider keys from the registry, and channel types may contain digits. Refs #92 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
465fda60dc
commit
52819e64bb
20 files changed
+3239
-666
No files matched your search
+150
-29
@@ -3,12 +3,15 @@ package notify
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -29,6 +32,22 @@ type Message struct {
|
||||
// Key identifies the incident across its events, for services that
|
||||
// pair "opened" with "resolved" such as PagerDuty.
|
||||
Key uuid.UUID
|
||||
// Monitor, MonitorID and Target describe the monitor, for services
|
||||
// with fields of their own for them.
|
||||
Monitor string
|
||||
MonitorID uuid.UUID
|
||||
Target string
|
||||
}
|
||||
|
||||
// status is "down", "up" or "test".
|
||||
func (m Message) status() string {
|
||||
switch m.Event {
|
||||
case Opened:
|
||||
return "down"
|
||||
case Resolved:
|
||||
return "up"
|
||||
}
|
||||
return "test"
|
||||
}
|
||||
|
||||
// Field is a setting of a provider. Secret fields are stored sealed and
|
||||
@@ -45,7 +64,10 @@ type Field struct {
|
||||
|
||||
// Provider delivers messages to one kind of service.
|
||||
type Provider struct {
|
||||
Type string
|
||||
Type string
|
||||
// Group sorts the service in forms: "chat", "push", "sms", "email" or
|
||||
// "incident".
|
||||
Group string
|
||||
Fields []Field
|
||||
send func(ctx context.Context, c *client, cfg map[string]string, m Message) error
|
||||
}
|
||||
@@ -68,12 +90,20 @@ func Lookup(t string) (Provider, bool) {
|
||||
return Provider{}, false
|
||||
}
|
||||
|
||||
type client struct{ hc *http.Client }
|
||||
type client struct {
|
||||
hc *http.Client
|
||||
d *net.Dialer
|
||||
}
|
||||
|
||||
// transport is replaced in tests to reach fixed service addresses.
|
||||
var transport = func(d *net.Dialer) http.RoundTripper {
|
||||
return &http.Transport{DialContext: d.DialContext, DisableKeepAlives: true}
|
||||
}
|
||||
|
||||
func newClient(allowPrivate bool) *client {
|
||||
d := netguard.Dialer(allowPrivate, 5*time.Second)
|
||||
return &client{hc: &http.Client{
|
||||
Transport: &http.Transport{DialContext: d.DialContext, DisableKeepAlives: true},
|
||||
return &client{d: d, hc: &http.Client{
|
||||
Transport: transport(d),
|
||||
// Redirects would bypass the address the customer configured.
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
}}
|
||||
@@ -82,9 +112,16 @@ func newClient(allowPrivate bool) *client {
|
||||
// do sends a request and turns non-2xx answers into errors that quote the
|
||||
// start of the service's reply, which usually says what is wrong.
|
||||
func (c *client) do(ctx context.Context, method, rawURL string, header http.Header, body io.Reader) error {
|
||||
_, err := c.call(ctx, method, rawURL, header, body)
|
||||
return err
|
||||
}
|
||||
|
||||
// call is do returning the reply, for services that report failures in
|
||||
// successful answers.
|
||||
func (c *client) call(ctx context.Context, method, rawURL string, header http.Header, body io.Reader) ([]byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, rawURL, body)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
for k, vs := range header {
|
||||
req.Header[k] = vs
|
||||
@@ -95,29 +132,111 @@ func (c *client) do(ctx context.Context, method, rawURL string, header http.Head
|
||||
if errors.As(err, &uerr) {
|
||||
// The URL may carry a secret, as Telegram's bot token in the path:
|
||||
// name only the host.
|
||||
return fmt.Errorf("%s: %w", req.URL.Host, uerr.Err)
|
||||
return nil, fmt.Errorf("%s: %w", req.URL.Host, uerr.Err)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
reply, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
reply, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return fmt.Errorf("%s answered %d: %s", req.URL.Host, resp.StatusCode, strings.TrimSpace(string(reply)))
|
||||
return nil, fmt.Errorf("%s answered %d: %s", req.URL.Host, resp.StatusCode, truncate(strings.TrimSpace(string(reply)), 300))
|
||||
}
|
||||
return nil
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
func (c *client) json(ctx context.Context, method, rawURL string, header http.Header, v any) error {
|
||||
_, err := c.callJSON(ctx, method, rawURL, header, v)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *client) callJSON(ctx context.Context, method, rawURL string, header http.Header, v any) ([]byte, error) {
|
||||
body, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if header == nil {
|
||||
header = http.Header{}
|
||||
}
|
||||
header.Set("Content-Type", "application/json")
|
||||
return c.do(ctx, method, rawURL, header, bytes.NewReader(body))
|
||||
return c.call(ctx, method, rawURL, header, bytes.NewReader(body))
|
||||
}
|
||||
|
||||
// form posts URL-encoded values and returns the reply.
|
||||
func (c *client) form(ctx context.Context, rawURL string, header http.Header, v url.Values) ([]byte, error) {
|
||||
if header == nil {
|
||||
header = http.Header{}
|
||||
}
|
||||
header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
return c.call(ctx, http.MethodPost, rawURL, header, strings.NewReader(v.Encode()))
|
||||
}
|
||||
|
||||
// get sends a GET with query parameters and returns the reply.
|
||||
func (c *client) get(ctx context.Context, rawURL string, header http.Header, q url.Values) ([]byte, error) {
|
||||
if len(q) > 0 {
|
||||
sep := "?"
|
||||
if strings.Contains(rawURL, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
rawURL += sep + q.Encode()
|
||||
}
|
||||
return c.call(ctx, http.MethodGet, rawURL, header, nil)
|
||||
}
|
||||
|
||||
// basic returns an Authorization header with HTTP basic credentials.
|
||||
func basic(user, password string) http.Header {
|
||||
return http.Header{"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+password))}}
|
||||
}
|
||||
|
||||
func bearer(token string) http.Header { return http.Header{"Authorization": {"Bearer " + token}} }
|
||||
|
||||
// replyCheck decodes a JSON reply and fails with what bad reports, for
|
||||
// services that answer 200 to failed requests.
|
||||
func replyCheck(reply []byte, err error, bad func(map[string]any) string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v map[string]any
|
||||
if json.Unmarshal(reply, &v) != nil {
|
||||
return fmt.Errorf("unexpected answer: %s", truncate(string(reply), 200))
|
||||
}
|
||||
if msg := bad(v); msg != "" {
|
||||
return errors.New(truncate(msg, 300))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// str formats a JSON value from a reply for messages and comparisons.
|
||||
func str(v any) string {
|
||||
switch v := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return v
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||||
}
|
||||
b, _ := json.Marshal(v)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// list splits comma, semicolon or newline separated values.
|
||||
func list(s string) []string {
|
||||
var out []string
|
||||
for _, v := range strings.FieldsFunc(s, func(r rune) bool { return r == ',' || r == ';' || r == '\n' }) {
|
||||
if v = strings.TrimSpace(v); v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// or returns s, or def when s is blank.
|
||||
func or(s, def string) string {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return def
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// text joins title, body and link for services that take plain text.
|
||||
@@ -138,14 +257,16 @@ var webhookField = Field{Name: "webhook_url", Required: true, Secret: true, Kind
|
||||
|
||||
// Providers lists the supported services. Webhook and email are handled by
|
||||
// their own types; Apprise reaches the services not listed here.
|
||||
var Providers = []Provider{
|
||||
{Type: "slack", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
var Providers = slices.Concat(baseProviders, chatProviders, pushProviders, smsProviders, emailProviders, incidentProviders)
|
||||
|
||||
var baseProviders = []Provider{
|
||||
{Type: "slack", Group: "chat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{"text": m.text()})
|
||||
}},
|
||||
{Type: "discord", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
{Type: "discord", Group: "chat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{"content": m.text(), "username": "UptimeSebi"})
|
||||
}},
|
||||
{Type: "teams", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
{Type: "teams", Group: "chat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
// Teams workflows take Adaptive Cards.
|
||||
blocks := []map[string]any{{"type": "TextBlock", "text": m.Title, "weight": "Bolder", "size": "Medium", "wrap": true}}
|
||||
if m.Body != "" {
|
||||
@@ -160,20 +281,20 @@ var Providers = []Provider{
|
||||
"type": "message", "attachments": []map[string]any{{"contentType": "application/vnd.microsoft.card.adaptive", "content": card}},
|
||||
})
|
||||
}},
|
||||
{Type: "googlechat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
{Type: "googlechat", Group: "chat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{"text": m.text()})
|
||||
}},
|
||||
{Type: "mattermost", Fields: []Field{webhookField, {Name: "channel", Kind: "text"}}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
{Type: "mattermost", Group: "chat", Fields: []Field{webhookField, {Name: "channel", Kind: "text"}}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
body := map[string]any{"text": m.text(), "username": "UptimeSebi"}
|
||||
if cfg["channel"] != "" {
|
||||
body["channel"] = cfg["channel"]
|
||||
}
|
||||
return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, body)
|
||||
}},
|
||||
{Type: "rocketchat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
{Type: "rocketchat", Group: "chat", Fields: []Field{webhookField}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{"text": m.text()})
|
||||
}},
|
||||
{Type: "telegram", Fields: []Field{
|
||||
{Type: "telegram", Group: "chat", Fields: []Field{
|
||||
{Name: "bot_token", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "chat_id", Required: true, Kind: "text"},
|
||||
{Name: "thread_id", Kind: "number"},
|
||||
@@ -189,7 +310,7 @@ var Providers = []Provider{
|
||||
}
|
||||
return c.json(ctx, http.MethodPost, api+"/bot"+cfg["bot_token"]+"/sendMessage", nil, body)
|
||||
}},
|
||||
{Type: "matrix", Fields: []Field{
|
||||
{Type: "matrix", Group: "chat", Fields: []Field{
|
||||
{Name: "homeserver_url", Required: true, Kind: "url"},
|
||||
{Name: "access_token", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "room_id", Required: true, Kind: "text"},
|
||||
@@ -199,7 +320,7 @@ var Providers = []Provider{
|
||||
h := http.Header{"Authorization": {"Bearer " + cfg["access_token"]}}
|
||||
return c.json(ctx, http.MethodPut, u, h, map[string]any{"msgtype": "m.text", "body": m.text()})
|
||||
}},
|
||||
{Type: "ntfy", Fields: []Field{
|
||||
{Type: "ntfy", Group: "push", Fields: []Field{
|
||||
{Name: "server_url", Kind: "url", Default: "https://ntfy.sh"},
|
||||
{Name: "topic", Required: true, Kind: "text"},
|
||||
{Name: "priority", Kind: "select", Options: []string{"3", "1", "2", "4", "5"}, Default: "4"},
|
||||
@@ -221,7 +342,7 @@ var Providers = []Provider{
|
||||
}
|
||||
return c.do(ctx, http.MethodPost, server+"/"+url.PathEscape(cfg["topic"]), h, strings.NewReader(m.Body))
|
||||
}},
|
||||
{Type: "gotify", Fields: []Field{
|
||||
{Type: "gotify", Group: "push", Fields: []Field{
|
||||
{Name: "server_url", Required: true, Kind: "url"},
|
||||
{Name: "app_token", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "priority", Kind: "number", Default: "8"},
|
||||
@@ -231,7 +352,7 @@ var Providers = []Provider{
|
||||
return c.json(ctx, http.MethodPost, trimURL(cfg["server_url"])+"/message", h,
|
||||
map[string]any{"title": m.Title, "message": m.Body + "\n" + m.URL, "priority": prio})
|
||||
}},
|
||||
{Type: "pushover", Fields: []Field{
|
||||
{Type: "pushover", Group: "push", Fields: []Field{
|
||||
{Name: "user_key", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "app_token", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "priority", Kind: "select", Options: []string{"0", "-1", "1"}, Default: "1"},
|
||||
@@ -247,13 +368,13 @@ var Providers = []Provider{
|
||||
h := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
|
||||
return c.do(ctx, http.MethodPost, pushoverURL, h, strings.NewReader(form.Encode()))
|
||||
}},
|
||||
{Type: "pushbullet", Fields: []Field{{Name: "access_token", Required: true, Secret: true, Kind: "text"}},
|
||||
{Type: "pushbullet", Group: "push", Fields: []Field{{Name: "access_token", Required: true, Secret: true, Kind: "text"}},
|
||||
send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
h := http.Header{"Access-Token": {cfg["access_token"]}}
|
||||
return c.json(ctx, http.MethodPost, pushbulletURL, h,
|
||||
map[string]any{"type": "note", "title": m.Title, "body": m.Body + "\n" + m.URL})
|
||||
}},
|
||||
{Type: "signal", Fields: []Field{
|
||||
{Type: "signal", Group: "chat", Fields: []Field{
|
||||
{Name: "api_url", Required: true, Kind: "url"},
|
||||
{Name: "number", Required: true, Kind: "text"},
|
||||
{Name: "recipients", Required: true, Kind: "text"},
|
||||
@@ -269,7 +390,7 @@ var Providers = []Provider{
|
||||
return c.json(ctx, http.MethodPost, trimURL(cfg["api_url"])+"/v2/send", nil,
|
||||
map[string]any{"message": m.text(), "number": cfg["number"], "recipients": recipients})
|
||||
}},
|
||||
{Type: "pagerduty", Fields: []Field{
|
||||
{Type: "pagerduty", Group: "incident", Fields: []Field{
|
||||
{Name: "routing_key", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "severity", Kind: "select", Options: []string{"critical", "error", "warning", "info"}, Default: "critical"},
|
||||
}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error {
|
||||
@@ -288,7 +409,7 @@ var Providers = []Provider{
|
||||
}
|
||||
return c.json(ctx, http.MethodPost, pagerdutyURL, nil, body)
|
||||
}},
|
||||
{Type: "opsgenie", Fields: []Field{
|
||||
{Type: "opsgenie", Group: "incident", Fields: []Field{
|
||||
{Name: "api_key", Required: true, Secret: true, Kind: "text"},
|
||||
{Name: "region", Kind: "select", Options: []string{"us", "eu"}, Default: "us"},
|
||||
{Name: "priority", Kind: "select", Options: []string{"P1", "P2", "P3", "P4", "P5"}, Default: "P2"},
|
||||
@@ -307,7 +428,7 @@ var Providers = []Provider{
|
||||
"priority": cfg["priority"], "source": "UptimeSebi",
|
||||
})
|
||||
}},
|
||||
{Type: "apprise", Fields: []Field{
|
||||
{Type: "apprise", Group: "other", Fields: []Field{
|
||||
{Name: "api_url", Required: true, Kind: "url"},
|
||||
{Name: "urls", Secret: true, Kind: "text"},
|
||||
{Name: "config_key", Kind: "text"},
|
||||
|
||||
Reference in new issue
Block a user