Files
clientflow_backend/app/message_cleaner.py
2026-06-09 22:55:58 +01:00

66 lines
1.7 KiB
Python

from typing import Any, Dict
def get_nested(data: Dict[str, Any], *keys: str) -> Any:
current: Any = data
for key in keys:
if not isinstance(current, dict):
return None
current = current.get(key)
return current
def clean_email_reply_text(content: str) -> str:
text = str(content or "").replace("\r\n", "\n").replace("\r", "\n").strip()
cut_markers = [
"\nDe:",
"\nEnviada:",
"\nEnviado:",
"\nAssunto:",
"\nPara:",
"\nCc:",
"\nÀs ",
"\nOn ",
"\nFrom:",
"\nSent:",
"\nSubject:",
"\nTo:",
"\n-----Original Message-----",
"\n________________________________",
]
cut_at = len(text)
for marker in cut_markers:
idx = text.find(marker)
if idx != -1:
cut_at = min(cut_at, idx)
cleaned = text[:cut_at].strip()
lines = []
for line in cleaned.split("\n"):
if line.strip().startswith(">"):
continue
lines.append(line)
return "\n".join(lines).strip()
def extract_chatwoot_content(payload: Dict[str, Any], message: Dict[str, Any]) -> str:
content = (
get_nested(message, "content_attributes", "email", "html_content", "reply")
or get_nested(message, "content_attributes", "email", "text_content", "reply")
or get_nested(payload, "content_attributes", "email", "html_content", "reply")
or get_nested(payload, "content_attributes", "email", "text_content", "reply")
or message.get("processed_message_content")
or message.get("content")
or payload.get("content")
or ""
)
return clean_email_reply_text(str(content or ""))