Files

40 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Per-form switch for the existing hardcoded submission notification."""
import requests
def normalize_webhook(value=None):
if value is None:
return {'enabled': False}
if not isinstance(value, dict):
raise ValueError('Webhook 配置必须是对象')
enabled = value.get('enabled', False)
if not isinstance(enabled, bool):
raise ValueError('Webhook enabled 必须为布尔值')
return {'enabled': enabled}
def send_form_webhook(form, who, *, is_overwrite=False, uploaded_files=None):
from config import NOTIFY_WEBHOOK
if not NOTIFY_WEBHOOK:
return False
try:
webhook = normalize_webhook(form.get('webhook'))
except ValueError:
return False
if not webhook['enabled']:
return False
action = '覆盖' if is_overwrite else '提交'
message = (f"{who}{action}成功,表单标题:{form.get('title', '未命名表单')},"
f"表单id:{form.get('id', '')}")
try:
# Preserve the original built-in URL, GET request and per-file notifications.
for filename in uploaded_files or [None]:
content = f'{message},文件:{filename}' if filename is not None else message
response = requests.get(f"https://api.chuckfang.com/c415ce0b/ 表单提交通知/{content}", timeout=3)
response.raise_for_status()
return True
except requests.RequestException:
# Keep failures separate from the already-saved submission.
print(f"Webhook 通知失败,表单 {form.get('id', '')} 的提交已保存")
return False