feat(email): send attachments as WhatsApp documents, fix forward sender
- Add /send-document endpoint to WhatsApp bridge (base64 document send) - save_email_as_note() now saves attachment files to disk alongside note - email_digest: extract original sender for Fwd: emails so header shows the real author, not the forwarder; send attachment files after summary - email_forward: send attachment files as documents after text parts - Add extract_original_sender() and save_email_attachment_files() helpers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ Usage:
|
||||
"""
|
||||
|
||||
import sys
|
||||
import base64
|
||||
import subprocess
|
||||
import requests
|
||||
from pathlib import Path
|
||||
@@ -15,7 +16,7 @@ from pathlib import Path
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from tools.email_process import save_unread_emails
|
||||
from tools.email_process import save_unread_emails, extract_original_sender
|
||||
from src.config import Config
|
||||
|
||||
BRIDGE_URL = "http://127.0.0.1:8098"
|
||||
@@ -35,6 +36,8 @@ def generate_summary(filepath: str, subject: str, from_full: str, date: str) ->
|
||||
except Exception as e:
|
||||
return f"[Eroare la citirea fișierului: {e}]"
|
||||
|
||||
display_from = extract_original_sender(subject, email_content, from_full)
|
||||
|
||||
prompt = f"""Mai jos este conținutul unui email. Scrie un rezumat factual pentru WhatsApp.
|
||||
|
||||
EMAIL:
|
||||
@@ -43,18 +46,14 @@ EMAIL:
|
||||
Instrucțiuni:
|
||||
- Începe cu header-ul fix (fără modificări):
|
||||
SUBIECT: {subject}
|
||||
De la: {from_full}
|
||||
De la: {display_from}
|
||||
Primit: {date}
|
||||
---
|
||||
- Dacă emailul este un forward (subiect începe cu Fwd:/Fw: sau conține "---------- Forwarded message"):
|
||||
* Ignoră complet persoana care a forwardat. Nu o menționez în rezumat.
|
||||
* Identifică expeditorul original din corpul emailului (câmpurile From/De la din headerul forwarded).
|
||||
* Rezumatul trebuie să fie despre mesajul original, ca și cum ar fi fost primit direct de la acel expeditor.
|
||||
- Ignoră complet orice persoană care a forwardat emailul. Nu o menționă în rezumat.
|
||||
- Scrie rezumatul în stil briefing: factual, clar, persoana a 3-a.
|
||||
* Prima propoziție: cine a trimis mesajul original, ce, cui.
|
||||
* Ce conține mesajul — concret și direct. Omite politețuri, scuze și amabilități; include doar faptele.
|
||||
* Ce conține mesajul — concret și direct. Omite politețuri și amabilități; include doar faptele.
|
||||
* Dacă există termene, date, locuri sau acțiuni cerute — menționează-le explicit.
|
||||
* Dacă există atașamente — listează-le la final: "Atașat: ..."
|
||||
* Dacă există linkuri acționabile (formulare, documente), adaugă o secțiune LINKURI la final.
|
||||
- Nu adăuga secțiuni goale sau care nu se aplică emailului.
|
||||
- Plain text, fără markdown. Fără emoji.
|
||||
@@ -89,6 +88,24 @@ def send_whatsapp(to: str, text: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def send_whatsapp_document(to: str, filepath: str) -> bool:
|
||||
"""Trimite un fișier ca document WhatsApp prin bridge."""
|
||||
try:
|
||||
path = Path(filepath)
|
||||
data_b64 = base64.b64encode(path.read_bytes()).decode()
|
||||
import mimetypes
|
||||
mimetype = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
||||
resp = requests.post(
|
||||
f"{BRIDGE_URL}/send-document",
|
||||
json={"to": to, "filename": path.name, "mimetype": mimetype, "data_base64": data_b64},
|
||||
timeout=30,
|
||||
)
|
||||
return resp.json().get("ok", False)
|
||||
except Exception as e:
|
||||
print(f"[eroare send-document] {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def run_digest():
|
||||
print("📬 Verific emailuri necitite...")
|
||||
saved = save_unread_emails()
|
||||
@@ -110,6 +127,7 @@ def run_digest():
|
||||
subject = result["subject"]
|
||||
from_full = result.get("from_full", result.get("from", ""))
|
||||
date = result.get("date", "")
|
||||
attachment_paths = result.get("attachment_paths", [])
|
||||
print(f"📧 Procesez: {subject}")
|
||||
|
||||
summary = generate_summary(filepath, subject, from_full, date)
|
||||
@@ -117,6 +135,8 @@ def run_digest():
|
||||
if DRY_RUN:
|
||||
print("\n--- REZUMAT (dry-run) ---")
|
||||
print(summary)
|
||||
if attachment_paths:
|
||||
print(f"Atașamente: {attachment_paths}")
|
||||
print("------------------------\n")
|
||||
else:
|
||||
ok = send_whatsapp(owner_jid, summary)
|
||||
@@ -125,6 +145,14 @@ def run_digest():
|
||||
else:
|
||||
print(f"❌ Trimitere eșuată: {subject}")
|
||||
|
||||
for att_path in attachment_paths:
|
||||
ok_att = send_whatsapp_document(owner_jid, att_path)
|
||||
name = Path(att_path).name
|
||||
if ok_att:
|
||||
print(f"✅ Atașament trimis: {name}")
|
||||
else:
|
||||
print(f"❌ Atașament eșuat: {name}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_digest()
|
||||
|
||||
Reference in New Issue
Block a user