Files
plugins/packages/notify/channels.mjs
T
Monica Everett cc7f4327e5 notify 0.1.2: a Gotify channel (#593, UnspokenSpell)
notify 0.1.2 adds a Gotify channel (UnspokenSpell): POST <server>/message with X-Gotify-Key, title and message; priority only when the channel sets it (an explicit 0 stays 0), so the application's own default applies otherwise; extras when set. Needs server and token. post() also reads Gotify's errorDescription on failure. Gotify is listed in the package README (with priority's fallback and extras), the root README and the registry summary.

Answers the 11-01 review at f2f5806: priority no longer defaults to 5, the sibling channel lists name Gotify, extras is documented and tested, errorDescription is read.

Run at the reviewed head f84a6a7 on mote:
- bun test packages/notify: 58 pass, 0 fail
- full bun test with HOME=$(mktemp -d): 4551 pass, 9 skip, 0 fail
- Broken by hand, each red then restored: priority back to `?? 5` (1 fail), errorDescription dropped (1 fail)
- A real Gotify server (3.1.1, 127.0.0.1, app with defaultPriority 2) through channels.mjs send(): no priority -> stored with priority 2; priority 0 -> stored 0; wrong token -> {ok:false,status:401,error:"you need to provide a valid access token or user credentials to access this api"}

Not tried: Gotify behind a reverse proxy at this head (tried at f2f5806, unchanged path code).
2026-11-02 03:37:00 +08:00

103 lines
4.4 KiB
JavaScript

// Where a notification goes. Each channel is one HTTPS POST to a service
// the user chose, with their own topic, key or webhook; nothing is sent
// anywhere else. send() never throws: it answers { ok, status?, error? }.
import { createHmac } from "node:crypto"
const TIMEOUT = 10_000
async function post(url, body, headers = {}, form = false) {
const res = await fetch(url, {
method: "POST",
headers: { "content-type": form ? "application/x-www-form-urlencoded" : "application/json", ...headers },
body: form ? new URLSearchParams(body).toString() : JSON.stringify(body),
signal: AbortSignal.timeout(TIMEOUT),
})
// the IM robots answer 200 with an error code in the body
let data
try { data = await res.json() } catch {}
const code = data?.code ?? data?.errcode ?? data?.StatusCode
const okBody = data?.ok !== false && (code == null || code === 0 || code === 200)
return res.ok && okBody ? { ok: true, status: res.status } : { ok: false, status: res.status, error: String(data?.msg ?? data?.errmsg ?? data?.description ?? data?.errorDescription ?? data?.message ?? res.statusText ?? "").slice(0, 200) }
}
const trimSlash = (s) => String(s).replace(/\/+$/, "")
export const kinds = {
// ntfy (ntfy.sh or your own server): JSON publish to the server root
ntfy: (c, m) => post(trimSlash(c.server ?? "https://ntfy.sh"), {
topic: c.topic, title: m.title, message: m.body, priority: c.priority ?? 3, tags: c.tags ?? [m.ok ? "white_check_mark" : "x"],
}, c.token ? { authorization: `Bearer ${c.token}` } : {}),
// Bark (iOS): api.day.app or your own bark-server
bark: (c, m) => post(`${trimSlash(c.server ?? "https://api.day.app")}/push`, {
device_key: c.key, title: m.title, body: m.body, group: c.group ?? "magpie", ...(c.sound ? { sound: c.sound } : {}),
}),
// Gotify: self-hosted push notifications
gotify: (c, m) => post(`${trimSlash(c.server)}/message`, {
title: m.title, message: m.body, ...(c.priority != null ? { priority: c.priority } : {}), ...(c.extras ? { extras: c.extras } : {}),
}, { "X-Gotify-Key": c.token }),
// Server酱 Turbo (SCT…) and Server酱³ (sctp<uid>t…)
serverchan: (c, m) => {
const key = String(c.key ?? "")
const v3 = key.match(/^sctp(\d+)t/i)
const url = v3 ? `https://${v3[1]}.push.ft07.com/send/${key}.send` : `https://sctapi.ftqq.com/${key}.send`
return post(url, { title: m.title, desp: m.body }, {}, true)
},
// 飞书 / Lark custom robot, with its optional signing secret
feishu: (c, m) => {
const body = { msg_type: "text", content: { text: `${m.title}\n${m.body}` } }
if (c.secret) {
const timestamp = String(Math.floor(Date.now() / 1000))
body.timestamp = timestamp
body.sign = createHmac("sha256", `${timestamp}\n${c.secret}`).update("").digest("base64")
}
return post(c.webhook, body)
},
// 钉钉 custom robot, with its optional 加签 secret
dingtalk: (c, m) => {
let url = c.webhook
if (c.secret) {
const timestamp = String(Date.now())
const sign = createHmac("sha256", c.secret).update(`${timestamp}\n${c.secret}`).digest("base64")
url += `${url.includes("?") ? "&" : "?"}timestamp=${timestamp}&sign=${encodeURIComponent(sign)}`
}
return post(url, { msgtype: "text", text: { content: `${m.title}\n${m.body}` } })
},
// 企业微信 group robot
wecom: (c, m) => post(c.webhook, { msgtype: "text", text: { content: `${m.title}\n${m.body}` } }),
// Telegram bot
telegram: (c, m) => post(`${trimSlash(c.server ?? "https://api.telegram.org")}/bot${c.token}/sendMessage`, {
chat_id: c.chatId, text: `${m.title}\n${m.body}`, disable_web_page_preview: true,
}),
// anything else: the event as JSON
webhook: (c, m) => post(c.url, { title: m.title, body: m.body, event: m.event }, c.headers ?? {}),
}
// what each kind needs before it can send
const needs = {
ntfy: ["topic"], bark: ["key"], gotify: ["server", "token"], serverchan: ["key"], feishu: ["webhook"], dingtalk: ["webhook"],
wecom: ["webhook"], telegram: ["token", "chatId"], webhook: ["url"],
}
export function usable(c) {
if (!c || typeof c !== "object" || c.off) return false
const n = needs[c.type]
return !!n && n.every((k) => c[k] != null && String(c[k]).trim() !== "")
}
export async function send(c, m) {
try {
if (!usable(c)) return { ok: false, error: "channel not configured" }
return await kinds[c.type](c, m)
} catch (e) {
return { ok: false, error: String(e?.message ?? e).slice(0, 200) }
}
}