Uptime Kuma 中的通知提供商使系统能够在监控状态更改时通过各种外部服务发送警报。该系统包含超过 70 种不同的通知提供商,涵盖电子邮件服务、消息平台、Webhook 和短信网关。本文档详细介绍了通知系统的架构、可用提供商和实现细节。
通知系统围绕两个主要组件构建
Notification 类(位于 server/notification.js)- 管理所有提供商的注册并处理通知操作NotificationProvider 基类 - 定义所有通知提供商实现的接口来源:server/notification.js76-290 server/notification-providers/notification-provider.js
通知提供商在 Notification.init() 方法中注册
初始化过程会创建所有通知提供商的实例,并将它们注册到 providerList 对象中
当需要发送通知时,Notification.send() 方法会查找相应的提供商并调用其 send() 方法
来源:server/notification.js86-175 server/notification.js186-192
通知系统通过前端和后端组件的组合与 Uptime Kuma 的其余部分集成
来源:src/components/NotificationDialog.vue1-373 src/components/notifications/index.js1-154
当监控器的状态发生变化时,通知流程遵循以下顺序
来源:server/notification.js186-192
Uptime Kuma 包含广泛的通知提供商,分类如下
通知提供商的完整列表注册在 src/components/notifications/index.js 中,并在 server/notification.js:91-164 中实例化。
来源:server/notification.js1-75 src/components/NotificationDialog.vue113-189 src/components/notifications/index.js79-151
通知系统的前端围绕几个 Vue 组件构建
NotificationDialog.vue:用于添加、编辑和测试通知的主组件
notifications/index.js:映射通知类型与其相应表单组件的注册表
提供商特定表单:每个提供商的独立 Vue 组件,例如
Telegram.vue:Telegram 通知配置SMTP.vue:电子邮件通知配置Webhook.vue:Webhook 通知配置以下是系统中注册的通知类型示例
| 类型 | 显示名称 | 类别 |
|---|---|---|
| smtp | 电子邮件 (SMTP) | 通用 |
| telegram | Telegram | 消息传递 |
| discord | Discord | 消息传递 |
| webhook | Webhook | API |
| slack | Slack | 消息传递 |
| pushover | Pushover | 推送通知 |
| gotify | Gotify | 推送通知 |
| AliyunSMS | AliyunSMS (阿里云短信服务) | Regional |
| DingDing | DingDing (钉钉自定义机器人) | Regional |
来源:src/components/NotificationDialog.vue1-373 src/components/notifications/index.js1-154
SMTP 提供商使用 nodemailer 发送电子邮件通知
SMTP 配置表单(SMTP.vue)提供了服务器设置、身份验证和消息自定义字段。
来源:server/notification-providers/smtp.js1-78 src/components/notifications/SMTP.vue1-144
Telegram 提供商使用 Telegram Bot API 发送消息
Telegram 配置表单(Telegram.vue)包含 Bot Token、Chat ID 和消息格式化选项的字段。
来源:server/notification-providers/telegram.js1-45 src/components/notifications/Telegram.vue1-190
Webhook 提供商向自定义端点发送 HTTP POST 请求
Webhook 配置表单(Webhook.vue)允许用户指定端点 URL、内容类型和自定义标头。
来源:server/notification-providers/webhook.js1-56 src/components/notifications/Webhook.vue1-93
许多通知提供商支持使用 Liquid 模板语言自定义消息内容。这允许根据监控器和心跳数据动态生成内容。
模板系统提供以下变量
| 可变 | 描述 |
|---|---|
{{ msg }} | 通知消息 |
{{ monitorJSON }} | 监控器的详细信息(仅适用于 UP/DOWN 通知) |
{{ heartbeatJSON }} | 心跳的详细信息(仅适用于 UP/DOWN 通知) |
{{ name }} | 服务名称 |
{{ status }} | 当前状态(UP/DOWN) |
{{ hostnameOrURL }} | 监控器的主机名或 URL |
前端为模板编辑提供了专用组件
这两个组件都包含文档和变量占位符,以帮助用户创建有效的模板。
来源:src/components/TemplatedInput.vue1-76 src/components/TemplatedTextarea.vue1-81
要在 UI 中创建新通知
通知必须分配给监控器才能正常工作,这可以在创建/编辑监控器时完成,或通过使用上述选项完成。
来源:src/components/NotificationDialog.vue1-373
通知系统包含一个测试机制,可通过通知配置对话框访问。当用户点击“Test”(测试)按钮时,系统会
此功能在 testNotification Socket.IO 事件处理程序和 NotificationDialog.vue 文件中的 test 方法中实现。