通过 自定义通知 推送消息。
(title: str, content: str)
| 950 | |
| 951 | |
| 952 | def custom_notify(title: str, content: str) -> None: |
| 953 | """ |
| 954 | 通过 自定义通知 推送消息。 |
| 955 | """ |
| 956 | if not push_config.get("WEBHOOK_URL") or not push_config.get("WEBHOOK_METHOD"): |
| 957 | return |
| 958 | |
| 959 | print("自定义通知服务启动") |
| 960 | |
| 961 | WEBHOOK_URL = push_config.get("WEBHOOK_URL") |
| 962 | WEBHOOK_METHOD = push_config.get("WEBHOOK_METHOD") |
| 963 | WEBHOOK_CONTENT_TYPE = push_config.get("WEBHOOK_CONTENT_TYPE") |
| 964 | WEBHOOK_BODY = push_config.get("WEBHOOK_BODY") |
| 965 | WEBHOOK_HEADERS = push_config.get("WEBHOOK_HEADERS") |
| 966 | |
| 967 | if "$title" not in WEBHOOK_URL and "$title" not in WEBHOOK_BODY: |
| 968 | print("请求头或者请求体中必须包含 $title 和 $content") |
| 969 | return |
| 970 | |
| 971 | headers = parse_headers(WEBHOOK_HEADERS) |
| 972 | body = parse_body( |
| 973 | WEBHOOK_BODY, |
| 974 | WEBHOOK_CONTENT_TYPE, |
| 975 | lambda v: v.replace("$title", title.replace("\n", "\\n")).replace( |
| 976 | "$content", content.replace("\n", "\\n") |
| 977 | ), |
| 978 | ) |
| 979 | formatted_url = WEBHOOK_URL.replace( |
| 980 | "$title", urllib.parse.quote_plus(title) |
| 981 | ).replace("$content", urllib.parse.quote_plus(content)) |
| 982 | response = requests.request( |
| 983 | method=WEBHOOK_METHOD, url=formatted_url, headers=headers, timeout=15, data=body |
| 984 | ) |
| 985 | |
| 986 | if response.status_code == 200: |
| 987 | print("自定义通知推送成功!") |
| 988 | else: |
| 989 | print(f"自定义通知推送失败!{response.status_code} {response.text}") |
| 990 | |
| 991 | |
| 992 | def one() -> str: |
nothing calls this directly
no test coverage detected