Add WeCom machine name for multi-node push labels.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-30 17:28:32 +08:00
parent 021b0d0a22
commit 8ceb521049
6 changed files with 62 additions and 5 deletions
+7 -1
View File
@@ -558,19 +558,20 @@ async def put_runtime_settings(
class NotifySettingsBody(BaseModel):
enabled: bool | None = None
webhook_url: str | None = None
machine_name: str | None = Field(default=None, max_length=64)
def _notify_payload() -> dict:
from ..notify import wecom
from ..env_store import mask_secret
s = get_settings()
url = wecom.wecom_webhook_url()
return {
"enabled": wecom.wecom_enabled(),
"webhook_configured": bool(url),
"webhook_url_masked": mask_secret(url) if url else None,
"venue_label": wecom.venue_label(),
"machine_name": wecom.wecom_machine_name() or "",
}
@@ -591,8 +592,13 @@ async def put_notify_settings(
if body.webhook_url is not None and body.webhook_url.strip():
updates["WECOM_WEBHOOK_URL"] = body.webhook_url.strip()
get_db().set_setting("wecom_webhook_url", body.webhook_url.strip())
if body.machine_name is not None:
name = body.machine_name.strip()[:64]
updates["WECOM_MACHINE_NAME"] = name
get_db().set_setting("wecom_machine_name", name)
if updates:
upsert_env_keys(updates)
get_settings.cache_clear()
return _notify_payload()
+1
View File
@@ -85,6 +85,7 @@ class Settings(BaseSettings):
# 企业微信群机器人
wecom_enabled: bool = False
wecom_webhook_url: str = ""
wecom_machine_name: str = ""
@property
def is_sim(self) -> bool:
+24 -1
View File
@@ -55,6 +55,18 @@ def wecom_webhook_url() -> str:
return ""
def wecom_machine_name() -> str:
"""多机推送区分用的机器名(设置页 / WECOM_MACHINE_NAME)。"""
s = get_settings()
name = (getattr(s, "wecom_machine_name", None) or "").strip()
if name:
return name[:64]
try:
return (get_db().get_setting("wecom_machine_name", "") or "").strip()[:64]
except Exception:
return ""
def venue_label() -> str | None:
"""实盘时返回「实盘·交易所」;SIM 不展示盘口标签。"""
s = get_settings()
@@ -74,13 +86,21 @@ def venue_label() -> str | None:
def build_markdown(*, tag: str, title: str, lines: list[str] | None = None) -> str:
body = "\n".join(f"> {ln}" if not ln.startswith(">") else ln for ln in (lines or []))
machine = wecom_machine_name()
venue = venue_label()
head = f"## 【{venue}{title}" if venue else f"## {title}"
prefix_parts: list[str] = []
if machine:
prefix_parts.append(f"{machine}")
if venue:
prefix_parts.append(f"{venue}")
head = f"## {''.join(prefix_parts)}{title}"
parts = [
head,
f"> **标识**: `{tag}`",
f"> **时间**: {time.strftime('%Y-%m-%d %H:%M:%S')}",
]
if machine:
parts.append(f"> **机器**: {machine}")
if body:
parts.append("")
parts.append(body)
@@ -216,7 +236,10 @@ def notify_fault(*, title: str, detail: str, dedupe_key: str | None = None) -> N
def notify_test() -> tuple[bool, str]:
venue = venue_label()
machine = wecom_machine_name()
lines = ["企业微信通知已连通。"]
if machine:
lines.append(f"机器名称: **{machine}**")
if venue:
lines.append(f"当前盘口标签: **{venue}**")
content = build_markdown(