Share

外观
风格

notify-service

2026年2月7日 · 工具

notify-service(统一通知基础服务)

把通知能力做成项目级基础设施,所有脚本/项目都可复用。

目标

  • 统一入口:bun tools/notify.ts send ...
  • 多通道:ntfy / telegram / bark / all
  • 可配置优先级、标签、默认通道
  • 支持环境变量覆盖,便于 CI/CD 与跨项目复用

配置文件

默认配置:tasks/config/notify.yaml

示例:

default: all

ntfy:
  server: https://<REDACTED_TOKEN>
  topic: <REDACTED_TOKEN>
  priority: default
  token: ""
  username: ""
  password: ""

telegram:
  bot_token: ""
  chat_id: ""
  message_thread_id:
  disable_preview: true

bark:
  server: https://<REDACTED_TOKEN>
  key: ""

rules:
  on_failure:
    enabled: true
    priority: high

CLI 用法

# 默认通道(取 notify.yaml 的 default)
bun tools/notify.ts send --title "任务完成" --body "RSS 已生成"

# 指定 Telegram
bun tools/notify.ts send --channel telegram --title "告警" --body "db backup failed"

# 指定 ntfy,并附带 tags
bun tools/notify.ts send --channel ntfy --title "部署成功" --body "prod ok" --tags rocket,white_check_mark

# 从 stdin 读正文
printf '这是正文' | bun tools/notify.ts send --title "stdin 测试" --stdin

环境变量覆盖

适合放在 CI secret 或不同项目的 .env

NOTIFY_DEFAULT_CHANNEL=all

NOTIFY_NTFY_SERVER=https://<REDACTED_TOKEN>
NOTIFY_NTFY_TOPIC=<REDACTED_TOKEN>
NOTIFY_NTFY_TOKEN=

NOTIFY_TELEGRAM_BOT_TOKEN=
NOTIFY_TELEGRAM_CHAT_ID=
NOTIFY_TELEGRAM_THREAD_ID=

NOTIFY_BARK_SERVER=https://<REDACTED_TOKEN>
NOTIFY_BARK_KEY=

在新项目复用

方式 A:直接调 CLI(最简单)

bun <REDACTED_TOKEN> send --title "build" --body "done"

方式 B:代码内复用库

import { dispatchNotify } from "<REDACTED_TOKEN>";

await dispatchNotify(config, {
  title: "任务完成",
  body: "hello",
  priority: "default",
});

说明

  • ntfy 发送采用 JSON publish,避免部分运行时对非 ASCII header 的限制。
  • all 会并发尝试所有已配置通道,只要有一个成功即视为发送成功。