feat: 实现通知渠道管理功能
All checks were successful
continuous-integration/drone/push Build is passing

- 新增 platform_notification_channels 表管理通知渠道(钉钉/企微机器人)
- 新增通知渠道管理页面,支持创建、编辑、测试、删除
- 定时任务增加通知渠道选择和企微应用选择
- 脚本执行支持返回值(result变量),自动发送到配置的渠道
- 调度器执行脚本后根据配置自动发送通知

使用方式:
1. 在「通知渠道」页面为租户配置钉钉/企微机器人
2. 创建定时任务时选择通知渠道
3. 脚本中设置 result = {'content': '内容', 'title': '标题'}
4. 任务执行后自动发送到配置的渠道
This commit is contained in:
2026-01-28 17:02:20 +08:00
parent d9fa9708ce
commit 2fbba63884
12 changed files with 800 additions and 25 deletions

View File

@@ -10,6 +10,7 @@ from sqlalchemy.orm import Session
from ..database import SessionLocal
from ..models.scheduled_task import ScheduledTask, TaskLog
from ..models.notification_channel import NotificationChannel
from .script_executor import ScriptExecutor
@@ -167,6 +168,7 @@ class SchedulerService:
success = False
output = ''
error = ''
result = None
try:
# 解析输入参数
@@ -177,7 +179,11 @@ class SchedulerService:
if task.execution_type == 'webhook':
success, output, error = await self._execute_webhook(task)
else:
success, output, error = await self._execute_script(db, task, trace_id, params)
success, output, error, result = await self._execute_script(db, task, trace_id, params)
# 如果脚本执行成功且有返回内容,发送通知
if success and result and result.get('content'):
await self._send_notifications(db, task, result)
except Exception as e:
error = str(e)
@@ -218,10 +224,10 @@ class SchedulerService:
async def _execute_script(self, db: Session, task: ScheduledTask, trace_id: str, params: dict):
"""执行脚本任务"""
if not task.script_content:
return False, '', '脚本内容为空'
return False, '', '脚本内容为空', None
executor = ScriptExecutor(db)
success, output, error = executor.execute(
success, output, error, result = executor.execute(
script_content=task.script_content,
task_id=task.id,
tenant_id=task.tenant_id,
@@ -230,8 +236,117 @@ class SchedulerService:
timeout=300 # 默认超时
)
return success, output, error
return success, output, error, result
async def _send_notifications(self, db: Session, task: ScheduledTask, result: dict):
"""发送通知到配置的渠道"""
content = result.get('content', '')
title = result.get('title', task.task_name)
if not content:
return
# 获取通知渠道配置
channel_ids = task.notify_channels
if isinstance(channel_ids, str):
try:
channel_ids = json.loads(channel_ids)
except:
channel_ids = []
if not channel_ids:
channel_ids = []
# 发送到通知渠道
for channel_id in channel_ids:
try:
channel = db.query(NotificationChannel).filter(
NotificationChannel.id == channel_id,
NotificationChannel.is_enabled == True
).first()
if not channel:
continue
await self._send_to_channel(channel, content, title)
except Exception as e:
print(f"发送通知到渠道 {channel_id} 失败: {e}")
# 发送到企微应用
if task.notify_wecom_app_id:
try:
await self._send_to_wecom_app(db, task.notify_wecom_app_id, content, title, task.tenant_id)
except Exception as e:
print(f"发送企微应用消息失败: {e}")
async def _send_to_channel(self, channel: NotificationChannel, content: str, title: str):
"""发送消息到通知渠道"""
if channel.channel_type == 'dingtalk_bot':
payload = {
"msgtype": "markdown",
"markdown": {
"title": title,
"text": content
}
}
else: # wecom_bot
payload = {
"msgtype": "markdown",
"markdown": {
"content": f"**{title}**\n\n{content}"
}
}
async with httpx.AsyncClient(timeout=10) as client:
response = await client.post(channel.webhook_url, json=payload)
result = response.json()
if result.get('errcode') != 0:
print(f"通知发送失败: {result}")
async def _send_to_wecom_app(self, db: Session, app_id: int, content: str, title: str, tenant_id: str):
"""发送消息到企微应用"""
from ..models.tenant_wechat_app import TenantWechatApp
app = db.query(TenantWechatApp).filter(TenantWechatApp.id == app_id).first()
if not app:
return
# 获取 access_token
access_token = await self._get_wecom_access_token(app.corp_id, app.app_secret)
if not access_token:
return
# 发送消息
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}"
payload = {
"touser": "@all",
"msgtype": "markdown",
"agentid": app.agent_id,
"markdown": {
"content": f"**{title}**\n\n{content}"
}
}
async with httpx.AsyncClient(timeout=10) as client:
response = await client.post(url, json=payload)
result = response.json()
if result.get('errcode') != 0:
print(f"企微应用消息发送失败: {result}")
async def _get_wecom_access_token(self, corp_id: str, app_secret: str) -> Optional[str]:
"""获取企微 access_token"""
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={app_secret}"
async with httpx.AsyncClient(timeout=10) as client:
response = await client.get(url)
result = response.json()
if result.get('errcode') == 0:
return result.get('access_token')
else:
print(f"获取企微 access_token 失败: {result}")
return None
async def _send_alert(self, task: ScheduledTask, error: str):
"""发送失败告警"""
if not task.alert_webhook: