Files
tangzhuan-yu/app/services/notification_channels.py
T
jinghui b0021953f5 feat: 躺赚鱼 v3.6.4 —— 价格监控换行 + 邮件通知复用系统设置
- 价格监控页超长文本改 break-words 自动换行(规则/关键词/最近一次列 + 历史弹窗),
  消除单行撑宽导致的横向滚动条
- 邮件通知渠道 SMTP 四项改为可留空,发送阶段自动回退「系统设置 → 邮件服务」,
  只需填接收邮箱;去掉强制 587 默认端口
- 「未配置 price_watch 事件的通知渠道」报错改为可操作指引(通知与日志页勾选价格监控)
- email 渠道必填校验收窄为仅接收邮箱

验证:端到端 _run_rule notified=True 邮件真实发出;生产日志 00:25 通知=成功;
CSS break-words 与新提示文案均进构建产物;测试脏数据已清理。
2026-11-16 02:00:11 +08:00

148 lines
5.5 KiB
Python

"""Shared notification-channel validation and normalization helpers."""
from __future__ import annotations
import json
from collections.abc import Mapping
from typing import Any
NOTIFICATION_CHANNEL_REQUIRED_FIELDS = {
"dingtalk": ("webhook_url",),
"feishu": ("webhook_url",),
"bark": ("device_key",),
# 邮件渠道的 SMTP 四项允许留空:留空时发送阶段自动回退到
# 「系统设置 → 邮件服务」里已配置的 SMTP(smtp_server 等),
# 只有接收邮箱是必需的。
"email": ("recipient_email",),
"webhook": ("webhook_url",),
"wechat": ("webhook_url",),
"telegram": ("bot_token", "chat_id"),
}
NOTIFICATION_CHANNEL_TYPE_ALIASES = {
"ding_talk": "dingtalk",
"lark": "feishu",
}
SUPPORTED_NOTIFICATION_CHANNEL_TYPES = frozenset(NOTIFICATION_CHANNEL_REQUIRED_FIELDS)
class NotificationChannelConfigError(ValueError):
"""A safe, user-facing channel configuration error."""
code = "notification_config_invalid"
category = "config"
def normalize_channel_type(channel_type: Any) -> str:
normalized = str(channel_type or "").strip().lower()
return NOTIFICATION_CHANNEL_TYPE_ALIASES.get(normalized, normalized)
def parse_channel_config(config: Any) -> dict[str, Any]:
"""Parse a persisted JSON config or accept an already parsed mapping."""
if isinstance(config, Mapping):
return dict(config)
if isinstance(config, bytes):
config = config.decode("utf-8", errors="replace")
if not isinstance(config, str) or not config.strip():
raise NotificationChannelConfigError("通知渠道配置必须是有效的 JSON")
try:
parsed = json.loads(config)
except (TypeError, json.JSONDecodeError) as exc:
raise NotificationChannelConfigError("通知渠道配置必须是有效的 JSON") from exc
if not isinstance(parsed, dict):
raise NotificationChannelConfigError("通知渠道配置必须是 JSON 对象")
return parsed
def _is_blank(value: Any) -> bool:
return value is None or (isinstance(value, str) and not value.strip())
def validate_channel_config(channel_type: Any, config: Any) -> tuple[str, dict[str, Any]]:
"""Return a canonical type and config, raising without exposing secrets."""
normalized_type = normalize_channel_type(channel_type)
if normalized_type not in NOTIFICATION_CHANNEL_REQUIRED_FIELDS:
raise NotificationChannelConfigError("不支持的通知渠道类型")
config_data = parse_channel_config(config)
missing_fields = [
field
for field in NOTIFICATION_CHANNEL_REQUIRED_FIELDS[normalized_type]
if _is_blank(config_data.get(field))
]
if missing_fields:
raise NotificationChannelConfigError(
f"通知渠道配置缺少字段: {', '.join(missing_fields)}"
)
if normalized_type == "email":
smtp_port = config_data.get("smtp_port")
if _is_blank(smtp_port):
# 未填端口:发送阶段会用系统设置里的 SMTP 配置兜底,这里跳过校验
config_data.pop("smtp_port", None)
else:
try:
smtp_port = int(smtp_port)
except (TypeError, ValueError) as exc:
raise NotificationChannelConfigError("SMTP 端口必须是数字") from exc
if not 1 <= smtp_port <= 65535:
raise NotificationChannelConfigError("SMTP 端口必须在 1-65535 之间")
config_data["smtp_port"] = smtp_port
if normalized_type == "webhook":
http_method = str(config_data.get("http_method", "POST")).strip().upper()
if http_method not in {"POST", "PUT"}:
raise NotificationChannelConfigError("Webhook 请求方法仅支持 POST 或 PUT")
config_data["http_method"] = http_method
headers = config_data.get("headers")
if isinstance(headers, str) and headers.strip():
try:
headers = json.loads(headers)
except json.JSONDecodeError as exc:
raise NotificationChannelConfigError(
"Webhook 请求头必须是有效的 JSON 对象"
) from exc
if not isinstance(headers, dict):
raise NotificationChannelConfigError("Webhook 请求头必须是 JSON 对象")
config_data["headers"] = headers
elif headers is not None and not isinstance(headers, dict):
raise NotificationChannelConfigError("Webhook 请求头必须是 JSON 对象")
return normalized_type, config_data
def validate_notification_channel(
name: Any,
channel_type: Any,
config: Any,
) -> tuple[str, str, str]:
"""Validate a channel record and return its persisted canonical form."""
normalized_name = str(name or "").strip()
if not normalized_name:
raise NotificationChannelConfigError("通知渠道名称不能为空")
if len(normalized_name) > 80:
raise NotificationChannelConfigError("通知渠道名称不能超过 80 个字符")
normalized_type, config_data = validate_channel_config(channel_type, config)
return (
normalized_name,
normalized_type,
json.dumps(config_data, ensure_ascii=False, separators=(",", ":")),
)
__all__ = [
"NOTIFICATION_CHANNEL_REQUIRED_FIELDS",
"NOTIFICATION_CHANNEL_TYPE_ALIASES",
"SUPPORTED_NOTIFICATION_CHANNEL_TYPES",
"NotificationChannelConfigError",
"normalize_channel_type",
"parse_channel_config",
"validate_channel_config",
"validate_notification_channel",
]