{
  "name": "Executive Inbox Copilot",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": true,
        "filters": {
          "q": "is:unread -category:social -category:promotions"
        }
      },
      "id": "1",
      "name": "Gmail Inbox Watcher",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [100, 300]
    },
    {
      "parameters": {
        "jsCode": "return {\n  // Operational Toggles\n  enableTelegramAlerts: true,\n  enableGmailDrafting: true,\n  \n  // Brand Voice & Persona Parameters\n  founderName: \"Founder Name\",\n  companyName: \"[Your Company]\",\n  urgencyThreshold: 4, // Alert via Telegram for urgency >= 4\n  \n  // Destination Credentials\n  telegramChatId: \"your_telegram_chat_id_here\",\n  \n  // Brand Voice Style Guide Guidelines\n  styleGuide: `\n    Tonal Voice: Direct, professional, helpful, but highly concise. Avoid corporate jargon, fluff, or overly enthusiastic greetings (no \"Hope this email finds you well!\" or \"Great news!\").\n    Formatting: Short paragraphs (1-2 sentences max), clear bullet points for choices, lowercase headings where appropriate.\n    Objective: Solve problems quickly. If a meeting is requested, provide your calendar link. If a technical issue is raised, acknowledge it, state that the team is looking into it, and provide a rough estimate.\n    Sign-off: Best, [Founder Name]\n  `\n};"
      },
      "id": "2",
      "name": "Workflow Settings",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [300, 300]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "completions",
        "model": "gpt-4o-mini",
        "messagesUi": {
          "messageValues": [
            {
              "role": "system",
              "message": "=You are an elite Executive Assistant and Inbox Copilot for {{ $node[\"Workflow Settings\"].json.founderName }}, the founder of {{ $node[\"Workflow Settings\"].json.companyName }}.\n\nYour task is to analyze an incoming email and generate a JSON response with the following properties:\n{\n  \"urgency\": 1-5 (1: spam/newsletter, 5: immediate customer crisis, direct sales inquiry, or contract signoff),\n  \"category\": \"Sales\", \"Support\", \"Billing\", \"Partnership\", \"Newsletter\", or \"Other\",\n  \"urgency_reason\": \"Brief 1-sentence justification for the score\",\n  \"summary\": \"A highly concise bulleted summary of the email's core message and asks (max 3 bullets)\",\n  \"requires_reply\": true/false,\n  \"draft_reply\": \"A highly optimized email reply drafted in the founder's exact voice, strictly following the Style Guide rules provided below.\"\n}\n\nSTYLE GUIDE RULES:\n{{ $node[\"Workflow Settings\"].json.styleGuide }}\n\nIMPORTANT: Return ONLY raw, valid JSON. Do not include markdown code block formatting or markdown wrapping."
            },
            {
              "role": "user",
              "message": "=SENDER: {{ $node[\"Gmail Inbox Watcher\"].json.from.email }} ({{ $node[\"Gmail Inbox Watcher\"].json.from.name }})\nSUBJECT: {{ $node[\"Gmail Inbox Watcher\"].json.subject }}\nBODY:\n{{ $node[\"Gmail Inbox Watcher\"].json.text }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "id": "3",
      "name": "Analyze & Classify via AI",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [500, 300]
    },
    {
      "parameters": {
        "jsCode": "const aiResult = JSON.parse($node[\"Analyze & Classify via AI\"].json.choices[0].message.content);\nconst gmailData = $node[\"Gmail Inbox Watcher\"].json;\nconst config = $node[\"Workflow Settings\"].json;\n\nreturn {\n  senderEmail: gmailData.from.email,\n  senderName: gmailData.from.name || \"Contact\",\n  subject: gmailData.subject,\n  messageId: gmailData.id,\n  threadId: gmailData.threadId,\n  urgency: aiResult.urgency || 1,\n  category: aiResult.category || \"Other\",\n  urgencyReason: aiResult.urgency_reason || \"None\",\n  summary: aiResult.summary || \"No summary generated.\",\n  requiresReply: aiResult.requires_reply || false,\n  draftReply: aiResult.draft_reply || \"\",\n  gmailLink: `https://mail.google.com/mail/u/0/#inbox/${gmailData.threadId}`,\n  config: config\n};"
      },
      "id": "4",
      "name": "Parse AI Output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [700, 300]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.config.enableGmailDrafting }}",
              "value2": true
            },
            {
              "value1": "={{ $json.requiresReply }}",
              "value2": true
            }
          ]
        }
      },
      "id": "5",
      "name": "Create Draft?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [900, 200]
    },
    {
      "parameters": {
        "emailType": "reply",
        "messageId": "={{ $json.messageId }}",
        "body": "={{ $json.draftReply }}",
        "options": {}
      },
      "id": "6",
      "name": "Create Gmail Draft",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1100, 100]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $node[\"Parse AI Output\"].json.config.enableTelegramAlerts }}",
              "value2": true
            },
            {
              "value1": "={{ $node[\"Parse AI Output\"].json.urgency }}",
              "value2": ">=",
              "value3": "={{ $node[\"Parse AI Output\"].json.config.urgencyThreshold }}"
            }
          ]
        }
      },
      "id": "7",
      "name": "Send Alert?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1300, 300]
    },
    {
      "parameters": {
        "chatId": "={{ $node[\"Parse AI Output\"].json.config.telegramChatId }}",
        "text": "📬 *[Inbox Copilot Alert]*\n\n👤 *Sender:* {{ $node[\"Parse AI Output\"].json.senderName }} ({{ $node[\"Parse AI Output\"].json.senderEmail }})\n📧 *Subject:* {{ $node[\"Parse AI Output\"].json.subject }}\n\n🚨 *Priority:* {{ $node[\"Parse AI Output\"].json.urgency }}/5\n📌 *Category:* {{ $node[\"Parse AI Output\"].json.category }}\n💡 *Reason:* {{ $node[\"Parse AI Output\"].json.urgencyReason }}\n\n📝 *Summary:*\n{{ $node[\"Parse AI Output\"].json.summary }}\n\n🤖 *Voice Draft Preview:*\n\"{{ $node[\"Parse AI Output\"].json.draftReply }}\"\n\n🔗 *Action Required:* A draft reply has been created in your Gmail thread. Click below to review and send:\n[Open Gmail Thread]({{ $node[\"Parse AI Output\"].json.gmailLink }})",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "8",
      "name": "Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1500, 200]
    }
  ],
  "connections": {
    "Gmail Inbox Watcher": {
      "main": [
        [
          {
            "node": "Workflow Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Workflow Settings": {
      "main": [
        [
          {
            "node": "Analyze & Classify via AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze & Classify via AI": {
      "main": [
        [
          {
            "node": "Parse AI Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Output": {
      "main": [
        [
          {
            "node": "Create Draft?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Draft?": {
      "main": [
        [
          {
            "node": "Create Gmail Draft",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Alert?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Gmail Draft": {
      "main": [
        [
          {
            "node": "Send Alert?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Alert?": {
      "main": [
        [
          {
            "node": "Telegram Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {}
}
