fix(email): digest nu mai creează notițe KB — fetch direct IMAP
email_digest.py folosea save_unread_emails() care salva în memory/kb/emails/. Notițele KB trebuie create DOAR de heartbeat. Acum digest-ul face fetch direct din IMAP (ca email_forward.py), fără side effects pe KB. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,16 +7,24 @@ Usage:
|
|||||||
python3 tools/email_digest.py --dry-run # Afișează rezumatele fără a trimite
|
python3 tools/email_digest.py --dry-run # Afișează rezumatele fără a trimite
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import base64
|
import base64
|
||||||
|
import mimetypes
|
||||||
import subprocess
|
import subprocess
|
||||||
import requests
|
import requests
|
||||||
|
import imaplib
|
||||||
|
import email as email_lib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||||
sys.path.insert(0, str(PROJECT_ROOT))
|
sys.path.insert(0, str(PROJECT_ROOT))
|
||||||
|
|
||||||
from tools.email_process import save_unread_emails, extract_original_sender
|
from tools.email_process import (
|
||||||
|
IMAP_SERVER, IMAP_PORT, IMAP_USER, IMAP_PASS,
|
||||||
|
WHITELIST, decode_mime_header, extract_sender_email,
|
||||||
|
get_email_body, extract_original_sender,
|
||||||
|
)
|
||||||
from src.config import Config
|
from src.config import Config
|
||||||
|
|
||||||
BRIDGE_URL = "http://127.0.0.1:8098"
|
BRIDGE_URL = "http://127.0.0.1:8098"
|
||||||
@@ -29,23 +37,77 @@ def get_owner_jid() -> str:
|
|||||||
return f"{owner}@s.whatsapp.net"
|
return f"{owner}@s.whatsapp.net"
|
||||||
|
|
||||||
|
|
||||||
def generate_summary(filepath: str, subject: str, from_full: str, date: str) -> str:
|
def fetch_unread_emails() -> list[dict]:
|
||||||
"""Citește conținutul emailului și generează rezumat via Claude CLI."""
|
"""Preia emailurile necitite din inbox fără a salva KB notes."""
|
||||||
try:
|
mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
|
||||||
email_content = Path(filepath).read_text(encoding="utf-8")
|
mail.login(IMAP_USER, IMAP_PASS)
|
||||||
except Exception as e:
|
mail.select('INBOX')
|
||||||
return f"[Eroare la citirea fișierului: {e}]"
|
|
||||||
|
|
||||||
display_from = extract_original_sender(subject, email_content, from_full)
|
status, messages = mail.search(None, 'UNSEEN')
|
||||||
|
email_ids = messages[0].split() if messages[0] else []
|
||||||
|
results = []
|
||||||
|
|
||||||
|
for eid in email_ids:
|
||||||
|
status, data = mail.fetch(eid, "(BODY.PEEK[])")
|
||||||
|
if status != "OK":
|
||||||
|
continue
|
||||||
|
msg = email_lib.message_from_bytes(data[0][1])
|
||||||
|
|
||||||
|
from_addr = decode_mime_header(msg['From'])
|
||||||
|
sender_email = extract_sender_email(from_addr)
|
||||||
|
|
||||||
|
if sender_email not in WHITELIST:
|
||||||
|
continue
|
||||||
|
|
||||||
|
attachment_data = {}
|
||||||
|
if msg.is_multipart():
|
||||||
|
for part in msg.walk():
|
||||||
|
fname = part.get_filename()
|
||||||
|
if fname:
|
||||||
|
fname = decode_mime_header(fname)
|
||||||
|
payload = part.get_payload(decode=True)
|
||||||
|
if payload:
|
||||||
|
attachment_data[fname] = payload
|
||||||
|
|
||||||
|
results.append({
|
||||||
|
'id': eid.decode(),
|
||||||
|
'subject': decode_mime_header(msg['Subject']),
|
||||||
|
'from_full': from_addr,
|
||||||
|
'date': msg['Date'],
|
||||||
|
'body': get_email_body(msg),
|
||||||
|
'attachments': list(attachment_data.keys()),
|
||||||
|
'attachment_data': attachment_data,
|
||||||
|
})
|
||||||
|
|
||||||
|
mail.logout()
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def mark_as_seen(email_ids: list[str]) -> None:
|
||||||
|
try:
|
||||||
|
mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
|
||||||
|
mail.login(IMAP_USER, IMAP_PASS)
|
||||||
|
mail.select('INBOX')
|
||||||
|
for eid in email_ids:
|
||||||
|
mail.store(eid.encode(), '+FLAGS', '\\Seen')
|
||||||
|
mail.logout()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[warn] Marcare Seen esuata: {e}", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_summary(subject: str, from_full: str, date: str, body: str) -> str:
|
||||||
|
"""Generează rezumat email via Claude CLI."""
|
||||||
|
display_from = extract_original_sender(subject, body, from_full)
|
||||||
|
display_subject = re.sub(r'^(Fwd?|Fw)\s*[:\s]\s*', '', subject, flags=re.IGNORECASE).strip() or subject
|
||||||
|
|
||||||
prompt = f"""Mai jos este conținutul unui email. Scrie un rezumat factual pentru WhatsApp.
|
prompt = f"""Mai jos este conținutul unui email. Scrie un rezumat factual pentru WhatsApp.
|
||||||
|
|
||||||
EMAIL:
|
EMAIL:
|
||||||
{email_content}
|
{body}
|
||||||
|
|
||||||
Instrucțiuni:
|
Instrucțiuni:
|
||||||
- Începe cu header-ul fix (fără modificări):
|
- Începe cu header-ul fix (fără modificări):
|
||||||
SUBIECT: {subject}
|
SUBIECT: {display_subject}
|
||||||
De la: {display_from}
|
De la: {display_from}
|
||||||
Primit: {date}
|
Primit: {date}
|
||||||
---
|
---
|
||||||
@@ -76,22 +138,19 @@ Instrucțiuni:
|
|||||||
|
|
||||||
|
|
||||||
def send_whatsapp(to: str, text: str) -> bool:
|
def send_whatsapp(to: str, text: str) -> bool:
|
||||||
"""Trimite mesaj pe WhatsApp prin bridge."""
|
|
||||||
try:
|
try:
|
||||||
resp = requests.post(
|
resp = requests.post(
|
||||||
f"{BRIDGE_URL}/send",
|
f"{BRIDGE_URL}/send",
|
||||||
json={"to": to, "text": text},
|
json={"to": to, "text": text},
|
||||||
timeout=15,
|
timeout=15,
|
||||||
)
|
)
|
||||||
data = resp.json()
|
return resp.json().get("ok", False)
|
||||||
return data.get("ok", False)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[eroare send] {e}", file=sys.stderr)
|
print(f"[eroare send] {e}", file=sys.stderr)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def send_discord_webhook(text: str) -> bool:
|
def send_discord_webhook(text: str) -> bool:
|
||||||
"""Trimite mesaj pe Discord via webhook (max 2000 chars per mesaj)."""
|
|
||||||
config = Config(PROJECT_ROOT / "config.json")
|
config = Config(PROJECT_ROOT / "config.json")
|
||||||
url = config.get("discord.email_webhook_url", "")
|
url = config.get("discord.email_webhook_url", "")
|
||||||
if not url:
|
if not url:
|
||||||
@@ -109,16 +168,13 @@ def send_discord_webhook(text: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def send_whatsapp_document(to: str, filepath: str) -> bool:
|
def send_whatsapp_document(to: str, filename: str, data: bytes) -> bool:
|
||||||
"""Trimite un fișier ca document WhatsApp prin bridge."""
|
|
||||||
try:
|
try:
|
||||||
path = Path(filepath)
|
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
|
||||||
data_b64 = base64.b64encode(path.read_bytes()).decode()
|
|
||||||
import mimetypes
|
|
||||||
mimetype = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
|
||||||
resp = requests.post(
|
resp = requests.post(
|
||||||
f"{BRIDGE_URL}/send-document",
|
f"{BRIDGE_URL}/send-document",
|
||||||
json={"to": to, "filename": path.name, "mimetype": mimetype, "data_base64": data_b64},
|
json={"to": to, "filename": filename, "mimetype": mimetype,
|
||||||
|
"data_base64": base64.b64encode(data).decode()},
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
return resp.json().get("ok", False)
|
return resp.json().get("ok", False)
|
||||||
@@ -128,57 +184,55 @@ def send_whatsapp_document(to: str, filepath: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def run_digest():
|
def run_digest():
|
||||||
print("📬 Verific emailuri necitite...")
|
print("Verific emailuri necitite...")
|
||||||
saved = save_unread_emails()
|
emails = fetch_unread_emails()
|
||||||
|
|
||||||
owner_jid = get_owner_jid()
|
owner_jid = get_owner_jid()
|
||||||
|
|
||||||
if not saved:
|
if not emails:
|
||||||
print("Niciun email nou de procesat.")
|
print("Niciun email nou de procesat.")
|
||||||
if not DRY_RUN:
|
if not DRY_RUN:
|
||||||
send_whatsapp(owner_jid, "📭 Nu sunt emailuri noi.")
|
send_whatsapp(owner_jid, "Nu sunt emailuri noi.")
|
||||||
return
|
return
|
||||||
|
|
||||||
for result in saved:
|
sent_ids = []
|
||||||
if not result.get("ok"):
|
for em in emails:
|
||||||
print(f"⚠️ Sărit: {result.get('error')}")
|
subject = em['subject']
|
||||||
continue
|
print(f"Procesez: {subject}")
|
||||||
|
|
||||||
filepath = result["file"]
|
summary = generate_summary(subject, em['from_full'], em['date'], em['body'])
|
||||||
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)
|
|
||||||
|
|
||||||
if DRY_RUN:
|
if DRY_RUN:
|
||||||
print("\n--- REZUMAT (dry-run) ---")
|
print("\n--- REZUMAT (dry-run) ---")
|
||||||
print(summary)
|
print(summary)
|
||||||
if attachment_paths:
|
if em.get('attachments'):
|
||||||
print(f"Atașamente: {attachment_paths}")
|
print(f"Atașamente: {em['attachments']}")
|
||||||
print("------------------------\n")
|
print("------------------------\n")
|
||||||
else:
|
else:
|
||||||
ok = send_whatsapp(owner_jid, summary)
|
ok = send_whatsapp(owner_jid, summary)
|
||||||
if ok:
|
if ok:
|
||||||
print(f"✅ Trimis pe WhatsApp: {subject}")
|
print(f"Trimis pe WhatsApp: {subject}")
|
||||||
else:
|
else:
|
||||||
print(f"❌ Trimitere eșuată: {subject}")
|
print(f"Trimitere esuata: {subject}")
|
||||||
|
|
||||||
ok_dc = send_discord_webhook(summary)
|
ok_dc = send_discord_webhook(summary)
|
||||||
if ok_dc:
|
if ok_dc:
|
||||||
print(f"✅ Trimis pe Discord: {subject}")
|
print(f"Trimis pe Discord: {subject}")
|
||||||
else:
|
else:
|
||||||
print(f"❌ Discord eșuat: {subject}")
|
print(f"Discord eșuat: {subject}")
|
||||||
|
|
||||||
for att_path in attachment_paths:
|
for fname, fdata in em.get('attachment_data', {}).items():
|
||||||
ok_att = send_whatsapp_document(owner_jid, att_path)
|
ok_att = send_whatsapp_document(owner_jid, fname, fdata)
|
||||||
name = Path(att_path).name
|
|
||||||
if ok_att:
|
if ok_att:
|
||||||
print(f"✅ Atașament trimis: {name}")
|
print(f"Atașament trimis: {fname}")
|
||||||
else:
|
else:
|
||||||
print(f"❌ Atașament eșuat: {name}")
|
print(f"Atașament eșuat: {fname}")
|
||||||
|
|
||||||
|
if ok:
|
||||||
|
sent_ids.append(em['id'])
|
||||||
|
|
||||||
|
if sent_ids and not DRY_RUN:
|
||||||
|
mark_as_seen(sent_ids)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user