from __future__ import annotations import json from django.conf import settings from .context import NotificationContext from .recipient import ResolvedFeishuTarget def absolute_result_url(path: str) -> str: base_url = getattr(settings, "PUBLIC_BASE_URL", "http://127.0.0.1:8000").rstrip("/") if not path: return base_url if path.startswith("http://") or path.startswith("https://"): return path return f"{base_url}/{path.lstrip('/')}" def build_message_summary(context: NotificationContext, target: ResolvedFeishuTarget) -> str: lines = [ context.title, f"批次:{context.workflow_batch_no}", f"状态:{context.workflow_status}", f"发起人:{context.trigger_username}", f"接收人:{target.display_name}", *context.summary_lines, f"下一步:{context.next_step}", ] return "\n".join(line for line in lines if line) def build_feishu_post_message(context: NotificationContext, target: ResolvedFeishuTarget) -> dict: result_url = absolute_result_url(context.result_path) content = [ [{"tag": "text", "text": f"{context.title}\n"}], [{"tag": "text", "text": f"流程:{context.workflow_name}\n"}], [{"tag": "text", "text": f"批次:{context.workflow_batch_no}\n"}], [{"tag": "text", "text": f"状态:{context.workflow_status}\n"}], [{"tag": "text", "text": f"发起人:{context.trigger_username}\n"}], ] for line in context.summary_lines: content.append([{"tag": "text", "text": f"{line}\n"}]) content.extend( [ [{"tag": "text", "text": f"下一步:{context.next_step}\n"}], [{"tag": "a", "text": "查看系统结果", "href": result_url}], ] ) return { "receive_id": target.identifier_value, "msg_type": "post", "content": json.dumps( { "zh_cn": { "title": context.title, "content": content, } }, ensure_ascii=False, ), }