Description
Summary Project Overview We need a Google Apps Script that automatically classifies incoming Gmail emails using the OpenAI API and applies Gmail labels based on the classification result. The script runs on a time-driven trigger (every 5 minutes), processes unread emails, and assigns the correct label. Requirements Core functionality: Time-driven trigger (every 5 minutes) that picks up unread emails from the inbox Sends each email's subject, sender, and body (first 500 characters) to the OpenAI API (GPT-4o-mini) for classification Parses the API response and applies the matching Gmail label Marks processed emails so they are not classified again (e.g. custom label or processed flag) Fallback label ("Egyéb" / "Other") for emails the model cannot confidently classify Categories (configurable): The category list will be defined as a simple array/object at the top of the script so the client can add, rename, or remove categories without touching the rest of the code. Initial set: Számla (Invoices) Megrendelés (Orders) Reklamáció (Complaints) Ajánlatkérés (Quote requests) Marketing (Newsletters, promotions) Belső (Internal communication) Egyéb (Other / fallback) Technical requirements: Pure Google Apps Script (no external hosting, no Node.js) OpenAI API key stored in Script Properties (not hardcoded) System prompt stored as a constant for easy editing Error handling: log failures to a Google Sheet (timestamp, email subject, error message) so nothing is silently lost Rate limiting awareness: if the inbox has a large backlog, process in batches (max 20 emails per run) to stay within Apps Script execution limits (6 min / run) The script must work with Gmail accounts Deliverables Complete Apps Script project (.gs files), ready to deploy Setup instructions: how to add the API key, create the labels, set up the trigger