From da35ebfa337c35320a9e1c93428a40c1b0afa522 Mon Sep 17 00:00:00 2001 From: Cp0204 Date: Sun, 14 Apr 2024 00:14:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=9F=E8=BF=9B=20qinglong=20=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=A8=A1=E5=9D=97=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sendNotify.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/sendNotify.py b/sendNotify.py index ab07ae1..2bd0f7a 100644 --- a/sendNotify.py +++ b/sendNotify.py @@ -213,7 +213,7 @@ def feishu_bot(title: str, content: str) -> None: data = {"msg_type": "text", "content": {"text": f"{title}\n\n{content}"}} response = requests.post(url, data=json.dumps(data)).json() - if response.get("StatusCode") == 0: + if response.get("StatusCode") == 0 or response.get("code") == 0: print("飞书 推送成功!") else: print("飞书 推送失败!错误信息如下:\n", response) @@ -749,13 +749,14 @@ def parse_headers(headers): return parsed -def parse_string(input_string): +def parse_string(input_string, value_format_fn=None): matches = {} pattern = r"(\w+):\s*((?:(?!\n\w+:).)*)" regex = re.compile(pattern) for match in regex.finditer(input_string): key, value = match.group(1).strip(), match.group(2).strip() try: + value = value_format_fn(value) if value_format_fn else value json_value = json.loads(value) matches[key] = json_value except: @@ -763,11 +764,11 @@ def parse_string(input_string): return matches -def parse_body(body, content_type): +def parse_body(body, content_type, value_format_fn=None): if not body or content_type == "text/plain": return body - parsed = parse_string(body) + parsed = parse_string(body, value_format_fn) if content_type == "application/x-www-form-urlencoded": data = urllib.parse.urlencode(parsed, doseq=True) @@ -780,18 +781,6 @@ def parse_body(body, content_type): return parsed -def format_notify_content(url, body, title, content): - if "$title" not in url and "$title" not in body: - return {} - - formatted_url = url.replace("$title", urllib.parse.quote_plus(title)).replace( - "$content", urllib.parse.quote_plus(content) - ) - formatted_body = body.replace("$title", title).replace("$content", content) - - return formatted_url, formatted_body - - def custom_notify(title: str, content: str) -> None: """ 通过 自定义通知 推送消息。 @@ -808,18 +797,21 @@ def custom_notify(title: str, content: str) -> None: WEBHOOK_BODY = push_config.get("WEBHOOK_BODY") WEBHOOK_HEADERS = push_config.get("WEBHOOK_HEADERS") - formatUrl, formatBody = format_notify_content( - WEBHOOK_URL, WEBHOOK_BODY, title, content - ) - - if not formatUrl and not formatBody: + if "$title" not in WEBHOOK_URL and "$title" not in WEBHOOK_BODY: print("请求头或者请求体中必须包含 $title 和 $content") return headers = parse_headers(WEBHOOK_HEADERS) - body = parse_body(formatBody, WEBHOOK_CONTENT_TYPE) + body = parse_body( + WEBHOOK_BODY, + WEBHOOK_CONTENT_TYPE, + lambda v: v.replace("$title", title).replace("$content", content), + ) + formatted_url = WEBHOOK_URL.replace( + "$title", urllib.parse.quote_plus(title) + ).replace("$content", urllib.parse.quote_plus(content)) response = requests.request( - method=WEBHOOK_METHOD, url=formatUrl, headers=headers, timeout=15, data=body + method=WEBHOOK_METHOD, url=formatted_url, headers=headers, timeout=15, data=body ) if response.status_code == 200: