diff --git a/cmcc/scrip_cmcc_mark.py b/cmcc/scrip_cmcc_mark.py index a023485..c09e396 100644 --- a/cmcc/scrip_cmcc_mark.py +++ b/cmcc/scrip_cmcc_mark.py @@ -157,12 +157,22 @@ def send_notify(title, content): sent = False if hook: try: - body = os.environ.get("WEBHOOK_BODY", "").strip() - if body: - body = body.replace("$title", title).replace("$content", content) - else: + ct = os.environ.get("WEBHOOK_CONTENT_TYPE", "application/json").strip() + tpl = os.environ.get("WEBHOOK_BODY", "").strip() + if tpl: + body = tpl.replace("$title", title).replace("$content", content) + # ★ 模板替换会打破 JSON:content 里有换行/引号时合法 JSON 变非法 → 服务端 400。 + # common.js 的做法是 try JSON.parse 失败就重建,这里对齐(2026-11-09 真机 400 即此)。 + if "json" in ct.lower(): + try: + json.loads(body) + except Exception: + body = json.dumps({"title": title, "content": content}, ensure_ascii=False) + elif "json" in ct.lower(): body = json.dumps({"title": title, "content": content}, ensure_ascii=False) - hdrs = {"Content-Type": os.environ.get("WEBHOOK_CONTENT_TYPE", "application/json")} + else: + body = "title=%s&content=%s" % (title, content) + hdrs = {"Content-Type": ct} for ln in os.environ.get("WEBHOOK_HEADERS", "").splitlines(): if ":" in ln: k, v = ln.split(":", 1)