Update: Add multi-channel support via tags

This commit is contained in:
OpenClaw Agent 2026-02-07 17:07:54 +00:00
parent ab9a800785
commit d5344e244e
2 changed files with 35 additions and 10 deletions

View File

@ -40,6 +40,7 @@ id: INTEGER PRIMARY KEY
name: TEXT NOT NULL -- 通道名称,如 "告警群" name: TEXT NOT NULL -- 通道名称,如 "告警群"
type: TEXT NOT NULL -- apprise 协议,如 "discord", "telegram" type: TEXT NOT NULL -- apprise 协议,如 "discord", "telegram"
config: JSON -- 配置参数URL、token 等) config: JSON -- 配置参数URL、token 等)
tags: JSON DEFAULT [] -- 标签,如 ["alerts", "production"]
is_active: BOOLEAN DEFAULT 1 is_active: BOOLEAN DEFAULT 1
created_at: TIMESTAMP created_at: TIMESTAMP
updated_at: TIMESTAMP updated_at: TIMESTAMP
@ -68,8 +69,8 @@ variables: JSON
``` ```
### 核心功能 ### 核心功能
1. **通道管理**: 增删改查通知通道 1. **通道管理**: 增删改查通知通道,支持标签分类
2. **发送通知**: POST /api/notify 接收通知请求 2. **发送通知**: POST /api/notify 支持批量发送到多个通道或按标签发送
3. **历史记录**: 查看发送历史和统计 3. **历史记录**: 查看发送历史和统计
4. **手动发送**: Web 界面手动触发通知 4. **手动发送**: Web 界面手动触发通知

View File

@ -68,11 +68,14 @@ class Channel(Base):
name = Column(String, nullable=False, unique=True) # 通道名称,如 "告警群" name = Column(String, nullable=False, unique=True) # 通道名称,如 "告警群"
type = Column(String, nullable=False) # apprise 协议,如 "discord" type = Column(String, nullable=False) # apprise 协议,如 "discord"
config = Column(JSON, nullable=False) # 配置参数 config = Column(JSON, nullable=False) # 配置参数
tags = Column(JSON, default=list) # 标签,如 ["alerts", "urgent"]
is_active = Column(Boolean, default=True) is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow) created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
``` ```
**tags 字段示例**: `["production", "alerts", "discord"]` - 用于批量选择通道
**config 字段示例**: **config 字段示例**:
```json ```json
{ {
@ -132,25 +135,45 @@ class Template(Base):
### 4.1 通知发送 API ### 4.1 通知发送 API
#### POST /api/notify #### POST /api/notify
发送通知到指定通道。 发送通知到指定通道(支持批量发送)
**请求体**: **请求体**:
```json ```json
{ {
"channel": "告警群", // 通道名称或 ID "channels": ["告警群", "运维群"], // 通道名称列表(与 tags 二选一)
"tags": ["alerts", "production"], // 标签列表,发送到所有匹配的通道
"title": "服务器告警", // 可选 "title": "服务器告警", // 可选
"body": "CPU 使用率超过 90%", "body": "CPU 使用率超过 90%",
"priority": "high" // 可选,默认 normal "priority": "high" // 可选,默认 normal
} }
``` ```
**说明**:
- `channels``tags` 二选一,不能同时为空
- 使用 `channels`: 精确指定通道名称列表
- 使用 `tags`: 发送到包含任一标签的所有通道
**响应**: **响应**:
```json ```json
{ {
"success": true, "success": true,
"notification_id": 123, "results": [
{
"channel": "告警群", "channel": "告警群",
"status": "sent" "channel_id": 1,
"status": "sent",
"notification_id": 123
},
{
"channel": "运维群",
"channel_id": 2,
"status": "sent",
"notification_id": 124
}
],
"total": 2,
"sent": 2,
"failed": 0
} }
``` ```
@ -159,7 +182,8 @@ class Template(Base):
**请求**: **请求**:
``` ```
GET /api/notify?channel=告警群&body=测试消息&priority=normal GET /api/notify?channels=告警群,运维群&body=测试消息&priority=normal
GET /api/notify?tags=alerts,urgent&body=测试消息&priority=high
``` ```
### 4.2 通道管理 API ### 4.2 通道管理 API