""" feed.py โ€” Feedback email sender for STEM Copilot. Uses Resend API to deliver user feedback to subhansh.stembotix@gmail.com. Set RESEND_API_KEY in HF Secrets (or .env) before deploying. """ import os import base64 import resend #type:ignore RESEND_API_KEY = os.environ.get("RESEND_API_KEY", "") resend.api_key = RESEND_API_KEY TO_EMAIL = "subhansh.stembotix@gmail.com" FROM_EMAIL = "STEM Copilot " _STAR_FILLED = "★" _STAR_EMPTY = "☆" _RATING_LABELS = {"1": "Poor", "2": "Fair", "3": "Good", "4": "Excellent"} _CATEGORY_LABELS = { "bug": "Technical Issue / Bug Report", "feature": "Feature Request", "iam": "IAM / Access Policy", "documentation": "Documentation", "other": "Other", } def _stars(n: int, max_n: int = 5) -> str: return _STAR_FILLED * n + _STAR_EMPTY * (max_n - n) def _row(label: str, value: str) -> str: return f""" {label} {value} """ def build_html( user_id: str, user_email: str, user_name: str, overall: int, ease: int, quality: int, category: str, message: str, ) -> str: overall_stars = _stars(overall) ease_label = _RATING_LABELS.get(str(ease), str(ease)) quality_label = _RATING_LABELS.get(str(quality), str(quality)) category_label = _CATEGORY_LABELS.get(category, category) display_from = user_email or user_id if user_name and user_name.strip(): display_from = f"{user_name.strip()} <{user_email}>" if user_email else user_name.strip() return f"""

New Feedback — STEM Copilot

{_row("From", display_from)} {_row("Overall Rating", f"{overall_stars} ({overall}/5)")} {_row("Ease of Use", ease_label)} {_row("Response Quality", quality_label)} {_row("Category", category_label)}

Message

{message}

Sent automatically by STEM Copilot ยท STEMbotix

""" def send_feedback( user_id: str, user_email: str, user_name: str = "", overall: int = 5, ease: int = 4, quality: int = 4, category: str = "other", message: str = "", attachments: list[dict] | None = None, ) -> bool: """ Send feedback email via Resend. attachments: list of {"filename": str, "content": base64_str, "content_type": str} Returns True on success. """ if not RESEND_API_KEY: print("[FEED] RESEND_API_KEY not set โ€” skipping email.") return False html = build_html(user_id, user_email, user_name, overall, ease, quality, category, message) payload: dict = { "from": FROM_EMAIL, "to": [TO_EMAIL], "subject": f"[STEM Copilot Feedback] {_CATEGORY_LABELS.get(category, category)} ({overall}/5)", "html": html, } if attachments: payload["attachments"] = [ { "filename": a["filename"], "content": a["content"], # base64 string } for a in attachments[:5] # cap at 5 ] try: resp = resend.Emails.send(payload) print(f"[FEED] Email sent โ€” id={resp.get('id')}") return True except Exception as exc: print(f"[FEED] Exception: {exc}") return False