ForcePilot/backend/package/yuxi/channels/adapters/imessage/approval_buttons.py

57 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
def format_approval_message(request_id: str, action: str, payload: dict[str, Any] | None = None) -> str:
"""生成审批请求消息文本。
iMessage 不支持内联按钮使用文本命令模拟审批
"""
lines = [
"Pending Approval Required",
f"Action: {action}",
f"Request ID: {request_id}",
"",
]
if payload:
for key, val in payload.items():
val_str = str(val)[:100]
lines.append(f" {key}: {val_str}")
lines.extend(
[
"",
"Reply with:",
f" approve {request_id} — to approve",
f" reject {request_id} — to reject",
]
)
return "\n".join(lines)
def format_approval_result(request_id: str, approved: bool, message: str = "") -> str:
status = "APPROVED" if approved else "REJECTED"
lines = [f"Approval {status}: {request_id}"]
if message:
lines.append(f" {message}")
return "\n".join(lines)
def parse_approval_command(text: str) -> tuple[str | None, str | None]:
"""解析审批文本命令。
返回 (action, request_id)其中 action "approve" "reject"
"""
text_lower = text.strip().lower()
parts = text_lower.split(maxsplit=1)
if len(parts) < 2:
return None, None
action = parts[0]
request_id = parts[1].strip()
if action in ("approve", "reject"):
return action, request_id
return None, None