feat(email): add digest and forward commands to WhatsApp
Digest summarizes unread emails via Claude CLI; forward sends raw content (split to 4096 chars). Wired as /email digest and /email forward slash commands, plus instant per-guild sync on ready. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
133
tools/email_digest.py
Normal file
133
tools/email_digest.py
Normal file
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Email digest: procesează emailuri necitite și trimite rezumate pe WhatsApp.
|
||||
|
||||
Usage:
|
||||
python3 tools/email_digest.py # Run digest
|
||||
python3 tools/email_digest.py --dry-run # Afișează rezumatele fără a trimite
|
||||
"""
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
import requests
|
||||
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 src.config import Config
|
||||
|
||||
BRIDGE_URL = "http://127.0.0.1:8098"
|
||||
DRY_RUN = "--dry-run" in sys.argv
|
||||
|
||||
|
||||
def get_owner_jid() -> str:
|
||||
config = Config(PROJECT_ROOT / "config.json")
|
||||
owner = config.get("whatsapp.owner", "")
|
||||
return f"{owner}@s.whatsapp.net"
|
||||
|
||||
|
||||
def generate_summary(filepath: str, subject: str, from_full: str, date: str) -> str:
|
||||
"""Citește conținutul emailului și generează rezumat via Claude CLI."""
|
||||
try:
|
||||
email_content = Path(filepath).read_text(encoding="utf-8")
|
||||
except Exception as e:
|
||||
return f"[Eroare la citirea fișierului: {e}]"
|
||||
|
||||
prompt = f"""Mai jos este conținutul unui email. Generează un rezumat pentru WhatsApp.
|
||||
|
||||
EMAIL:
|
||||
{email_content}
|
||||
|
||||
Format obligatoriu (plain text, fără markdown):
|
||||
|
||||
SUBIECT: {subject}
|
||||
De la: {from_full}
|
||||
Primit: {date}
|
||||
---
|
||||
[Rezumat compact dar complet — include TOATE detaliile importante:
|
||||
- ce se întâmplă / despre ce e vorba
|
||||
- cine organizează / cine trimite
|
||||
- când (date, deadline-uri, perioade)
|
||||
- unde (locație dacă există)
|
||||
- ce trebuie să facă cititorul (acțiune clară)
|
||||
- condiții de participare (vârstă, criterii etc.) dacă există
|
||||
- ce este inclus/oferit dacă e relevant
|
||||
Fiecare punct pe linie separată. Fără redundanță, fără filler.]
|
||||
|
||||
LINKURI:
|
||||
[Listează TOATE linkurile acționabile: formulare, înscrieri, site-uri, documente, social media]
|
||||
[Format: "Descriere scurtă: https://..." — câte un link per linie]
|
||||
[Dacă nu există linkuri, omite secțiunea]
|
||||
|
||||
[Semnătura expeditorului din email pe ultima linie după —]
|
||||
|
||||
Fără emoji dacă nu sunt în original. Răspunde DOAR cu rezumatul, nimic altceva."""
|
||||
|
||||
result = subprocess.run(
|
||||
["claude", "--print", prompt],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60,
|
||||
stdin=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
return f"[Eroare la generarea rezumatului pentru: {subject}]\n{result.stderr.strip()[:200]}"
|
||||
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def send_whatsapp(to: str, text: str) -> bool:
|
||||
"""Trimite mesaj pe WhatsApp prin bridge."""
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{BRIDGE_URL}/send",
|
||||
json={"to": to, "text": text},
|
||||
timeout=15,
|
||||
)
|
||||
data = resp.json()
|
||||
return data.get("ok", False)
|
||||
except Exception as e:
|
||||
print(f"[eroare send] {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def run_digest():
|
||||
print("📬 Verific emailuri necitite...")
|
||||
saved = save_unread_emails()
|
||||
|
||||
if not saved:
|
||||
print("Niciun email nou de procesat.")
|
||||
return
|
||||
|
||||
owner_jid = get_owner_jid()
|
||||
|
||||
for result in saved:
|
||||
if not result.get("ok"):
|
||||
print(f"⚠️ Sărit: {result.get('error')}")
|
||||
continue
|
||||
|
||||
filepath = result["file"]
|
||||
subject = result["subject"]
|
||||
from_full = result.get("from_full", result.get("from", ""))
|
||||
date = result.get("date", "")
|
||||
print(f"📧 Procesez: {subject}")
|
||||
|
||||
summary = generate_summary(filepath, subject, from_full, date)
|
||||
|
||||
if DRY_RUN:
|
||||
print("\n--- REZUMAT (dry-run) ---")
|
||||
print(summary)
|
||||
print("------------------------\n")
|
||||
else:
|
||||
ok = send_whatsapp(owner_jid, summary)
|
||||
if ok:
|
||||
print(f"✅ Trimis pe WhatsApp: {subject}")
|
||||
else:
|
||||
print(f"❌ Trimitere eșuată: {subject}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_digest()
|
||||
178
tools/email_forward.py
Normal file
178
tools/email_forward.py
Normal file
@@ -0,0 +1,178 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Email forward: forwardează emailuri necitite direct pe WhatsApp fără rezumat.
|
||||
|
||||
Usage:
|
||||
python3 tools/email_forward.py # Run forward
|
||||
python3 tools/email_forward.py --dry-run # Afișează fără a trimite
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
import requests
|
||||
from pathlib import Path
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from tools.email_process import (
|
||||
IMAP_SERVER, IMAP_PORT, IMAP_USER, IMAP_PASS,
|
||||
WHITELIST, decode_mime_header, extract_sender_email, get_email_body
|
||||
)
|
||||
from src.config import Config
|
||||
import imaplib
|
||||
import email as email_lib
|
||||
|
||||
BRIDGE_URL = "http://127.0.0.1:8098"
|
||||
DRY_RUN = "--dry-run" in sys.argv
|
||||
MAX_WA_LENGTH = 4096
|
||||
|
||||
|
||||
def get_owner_jid() -> str:
|
||||
config = Config(PROJECT_ROOT / "config.json")
|
||||
owner = config.get("whatsapp.owner", "")
|
||||
return f"{owner}@s.whatsapp.net"
|
||||
|
||||
|
||||
def extract_original_sender(body: str, from_full: str) -> tuple[str, str]:
|
||||
"""Dacă e email forwarded, extrage expeditorul și data originale."""
|
||||
# Caută header-ul tipic de forward: "De la: X\nDate: Y" sau "From: X\nDate: Y"
|
||||
match = re.search(
|
||||
r'(?:De la|From):\s*(.+?)\n(?:Date|Data):\s*(.+?)(?:\n|$)',
|
||||
body, re.IGNORECASE
|
||||
)
|
||||
if match:
|
||||
return match.group(1).strip(), match.group(2).strip()
|
||||
return from_full, ""
|
||||
|
||||
|
||||
def format_for_whatsapp(subject: str, from_full: str, date: str, body: str) -> str:
|
||||
"""Curăță corpul emailului și îl formatează pentru WhatsApp."""
|
||||
# Extrage expeditorul original dacă e forward
|
||||
original_from, original_date = extract_original_sender(body, from_full)
|
||||
display_from = original_from
|
||||
display_date = original_date or date
|
||||
|
||||
# Elimină header-ul markdown din fișierul KB (# Titlu, **De la:**, **Data:**, **Salvat:**)
|
||||
body = re.sub(r'^#.+\n?', '', body, flags=re.MULTILINE)
|
||||
body = re.sub(r'^\*\*(?:De la|Data|Salvat):\*\*.+\n?', '', body, flags=re.MULTILINE)
|
||||
# Elimină header-ul forwarded (---------- Forwarded message ---------)
|
||||
body = re.sub(r'-{5,}\s*Forwarded message\s*-{5,}.*?\n', '', body, flags=re.IGNORECASE)
|
||||
# Elimină liniile De la/From/Date/Subject/To din header-ul forwarded
|
||||
body = re.sub(r'^(?:De la|From|Date|Data|Subject|To):.+\n?', '', body, flags=re.IGNORECASE | re.MULTILINE)
|
||||
# Elimină comentariile KB (<!-- Echo: ... -->)
|
||||
body = re.sub(r'<!--.*?-->', '', body, flags=re.DOTALL)
|
||||
# Elimină separatoarele --- rămase
|
||||
body = re.sub(r'^\s*---\s*$', '', body, flags=re.MULTILINE)
|
||||
# Elimină linii goale consecutive (mai mult de 2)
|
||||
body = re.sub(r'\n{3,}', '\n\n', body)
|
||||
# Elimină spații trailing
|
||||
body = '\n'.join(line.rstrip() for line in body.splitlines())
|
||||
body = body.strip()
|
||||
|
||||
header = f"*{subject}*\nDe la: {display_from}\nPrimit: {display_date}\n---\n"
|
||||
full = header + body
|
||||
|
||||
if len(full) <= MAX_WA_LENGTH:
|
||||
return [full]
|
||||
|
||||
# Împarte în bucăți la limita WhatsApp, tăind pe linie întreagă
|
||||
parts = []
|
||||
remaining = full
|
||||
while remaining:
|
||||
if len(remaining) <= MAX_WA_LENGTH:
|
||||
parts.append(remaining)
|
||||
break
|
||||
cut = remaining[:MAX_WA_LENGTH].rfind('\n')
|
||||
# Dacă nu găsește newline sau e prea aproape de început, taie la limită
|
||||
if cut < MAX_WA_LENGTH // 2:
|
||||
cut = MAX_WA_LENGTH
|
||||
parts.append(remaining[:cut])
|
||||
remaining = remaining[cut:].lstrip('\n')
|
||||
|
||||
total = len(parts)
|
||||
if total > 1:
|
||||
parts = [f"{p}\n\n[{i+1}/{total}]" for i, p in enumerate(parts)]
|
||||
|
||||
return parts
|
||||
|
||||
|
||||
def send_whatsapp(to: str, text: str) -> bool:
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{BRIDGE_URL}/send",
|
||||
json={"to": to, "text": text},
|
||||
timeout=15,
|
||||
)
|
||||
return resp.json().get("ok", False)
|
||||
except Exception as e:
|
||||
print(f"[eroare send] {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def fetch_unread_emails():
|
||||
"""Preia emailurile necitite din inbox fără a le salva sau marca ca citite."""
|
||||
mail = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
|
||||
mail.login(IMAP_USER, IMAP_PASS)
|
||||
mail.select('INBOX')
|
||||
|
||||
status, messages = mail.search(None, 'UNSEEN')
|
||||
email_ids = messages[0].split() if messages[0] else []
|
||||
results = []
|
||||
|
||||
for eid in email_ids:
|
||||
# BODY.PEEK nu marchează ca citit
|
||||
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
|
||||
|
||||
results.append({
|
||||
'subject': decode_mime_header(msg['Subject']),
|
||||
'from_full': from_addr,
|
||||
'date': msg['Date'],
|
||||
'body': get_email_body(msg),
|
||||
})
|
||||
|
||||
mail.logout()
|
||||
return results
|
||||
|
||||
|
||||
def run_forward():
|
||||
print("Verific emailuri necitite...")
|
||||
emails = fetch_unread_emails()
|
||||
|
||||
if not emails:
|
||||
print("Niciun email nou de procesat.")
|
||||
return
|
||||
|
||||
owner_jid = get_owner_jid()
|
||||
|
||||
for em in emails:
|
||||
subject = em['subject']
|
||||
print(f"Trimit: {subject}")
|
||||
parts = format_for_whatsapp(subject, em['from_full'], em['date'], em['body'])
|
||||
|
||||
if DRY_RUN:
|
||||
for i, part in enumerate(parts):
|
||||
print(f"\n--- FORWARD {i+1}/{len(parts)} (dry-run) ---")
|
||||
print(part)
|
||||
print("------------------------\n")
|
||||
else:
|
||||
for part in parts:
|
||||
ok = send_whatsapp(owner_jid, part)
|
||||
if not ok:
|
||||
print(f"Trimitere esuata: {subject}")
|
||||
break
|
||||
else:
|
||||
print(f"Trimis pe WhatsApp ({len(parts)} mesaje): {subject}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_forward()
|
||||
@@ -193,7 +193,9 @@ def save_email_as_note(eid: str) -> dict:
|
||||
'ok': True,
|
||||
'file': str(filepath),
|
||||
'subject': subject,
|
||||
'from': sender_email
|
||||
'from': sender_email,
|
||||
'from_full': from_addr,
|
||||
'date': date_str,
|
||||
}
|
||||
|
||||
def save_unread_emails():
|
||||
|
||||
Reference in New Issue
Block a user