{
  "name": "Email Auto-Responder AI",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread",
          "receivedAfter": "={{ $now.minus(1, 'hour').toISO() }}"
        },
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000001",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        250,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an AI email assistant. Analyze the incoming email and draft an appropriate reply. You must respond with a JSON object containing:\n- \"analysis\": brief summary of the email\n- \"category\": one of [inquiry, complaint, scheduling, follow_up, spam, personal]\n- \"confidence\": number 0-1 indicating how confident you are in the draft\n- \"draft_reply\": the full email reply text\n- \"subject\": reply subject line\n\nBe professional, concise, and helpful. Do not agree to anything on behalf of the user. If unsure, indicate that the user will follow up personally."
            },
            {
              "role": "user",
              "content": "=From: {{ $json.from.value[0].address }}\nSubject: {{ $json.subject }}\n\nBody:\n{{ $json.text || $json.snippet }}"
            }
          ]
        },
        "options": {
          "temperature": 0.4,
          "maxTokens": 1024
        }
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000002",
      "name": "OpenAI Analyze & Draft",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        500,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "confidence-check",
              "leftValue": "={{ JSON.parse($json.text).confidence }}",
              "rightValue": 0.7,
              "operator": {
                "type": "number",
                "operation": "gte",
                "name": "filter.operator.gte"
              }
            },
            {
              "id": "not-spam",
              "leftValue": "={{ JSON.parse($json.text).category }}",
              "rightValue": "spam",
              "operator": {
                "type": "string",
                "operation": "notEquals",
                "name": "filter.operator.notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000003",
      "name": "Confidence Check",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}",
        "subject": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).subject }}",
        "emailType": "text",
        "message": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).draft_reply }}",
        "options": {
          "replyTo": "={{ $('Gmail Trigger').item.json.messageId }}"
        }
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000004",
      "name": "Gmail Send Reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1000,
        200
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "your-google-sheet-id",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Email Log",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "timestamp": "={{ $now.toISO() }}",
            "from": "={{ $('Gmail Trigger').item.json.from.value[0].address }}",
            "subject": "={{ $('Gmail Trigger').item.json.subject }}",
            "category": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).category }}",
            "confidence": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).confidence }}",
            "auto_replied": "=true",
            "analysis": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).analysis }}"
          }
        },
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000005",
      "name": "Google Sheets Log",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1250,
        200
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "your-google-sheet-id",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Email Log",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "timestamp": "={{ $now.toISO() }}",
            "from": "={{ $('Gmail Trigger').item.json.from.value[0].address }}",
            "subject": "={{ $('Gmail Trigger').item.json.subject }}",
            "category": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).category }}",
            "confidence": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).confidence }}",
            "auto_replied": "=false",
            "analysis": "={{ JSON.parse($('OpenAI Analyze & Draft').item.json.text).analysis }}"
          }
        },
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000006",
      "name": "Log Skipped Email",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1000,
        450
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "OpenAI Analyze & Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Analyze & Draft": {
      "main": [
        [
          {
            "node": "Confidence Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confidence Check": {
      "main": [
        [
          {
            "node": "Gmail Send Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Skipped Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Send Reply": {
      "main": [
        [
          {
            "node": "Google Sheets Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}