{
  "nodes": [
    {
      "id": "fd8ec780-8941-4736-88e7-cbc148f1deb7",
      "name": "Monitor Support Emails",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -1008,
        112
      ],
      "parameters": {
        "filters": {
          "sender": "",
          "labelIds": [
            "INBOX"
          ],
          "readStatus": "unread"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "ff2c6f81-1137-4ef5-b96d-69f08c538d9d",
      "name": "AI Analysis Engine",
      "type": "n8n-nodes-base.openAi",
      "position": [
        -752,
        112
      ],
      "parameters": {
        "prompt": {
          "messages": [
            {
              "role": "system",
              "content": "You are an expert customer support analyst. Analyze the email and provide: 1) Sentiment (Positive/Neutral/Negative/Critical), 2) Urgency Level (Low/Medium/High/Critical), 3) Category (Technical/Billing/Feature Request/Bug Report/General Inquiry), 4) Key Issues (bullet points), 5) Suggested Response (professional and empathetic). Return as JSON."
            },
            {
              "content": "Email Subject: {{ $json.subject }}\n\nEmail Body: {{ $json.body }}\n\nFrom: {{ $json.from }}"
            }
          ]
        },
        "options": {
          "maxTokens": 1000,
          "temperature": 0.3
        },
        "resource": "chat",
        "requestOptions": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "8b706330-4e9f-40f6-b4f8-02fe2988eed4",
      "name": "Parse & Enrich Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -496,
        112
      ],
      "parameters": {
        "jsCode": "const aiResponse = JSON.parse($input.first().json.choices[0].message.content);\nconst emailData = $('Monitor Support Emails').first().json;\n\nreturn {\n  json: {\n    timestamp: new Date().toISOString(),\n    customer_email: emailData.from,\n    customer_name: emailData.from.split('<')[0].trim(),\n    subject: emailData.subject,\n    body: emailData.body,\n    email_id: emailData.id,\n    thread_id: emailData.threadId,\n    \n    // AI Analysis Results\n    sentiment: aiResponse.sentiment,\n    urgency: aiResponse.urgency_level,\n    category: aiResponse.category,\n    key_issues: aiResponse.key_issues,\n    suggested_response: aiResponse.suggested_response,\n    \n    // Scoring\n    priority_score: calculatePriority(aiResponse.urgency_level, aiResponse.sentiment),\n    requires_immediate_attention: aiResponse.urgency_level === 'Critical' || aiResponse.sentiment === 'Critical'\n  }\n};\n\nfunction calculatePriority(urgency, sentiment) {\n  const urgencyScores = { 'Low': 1, 'Medium': 2, 'High': 3, 'Critical': 4 };\n  const sentimentScores = { 'Positive': 0, 'Neutral': 1, 'Negative': 2, 'Critical': 3 };\n  return (urgencyScores[urgency] || 2) * 25 + (sentimentScores[sentiment] || 1) * 10;\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "52100d0c-5fb5-4e9f-a9bd-06a18a3d548f",
      "name": "Route by Urgency",
      "type": "n8n-nodes-base.switch",
      "position": [
        -256,
        112
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "",
                    "rightValue": ""
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "93c91813-4e80-4d68-9481-ae3ec127f519",
      "name": "Alert Critical Issues",
      "type": "n8n-nodes-base.slack",
      "position": [
        0,
        0
      ],
      "parameters": {
        "text": "\ud83d\udea8 *CRITICAL CUSTOMER ISSUE DETECTED*\n\n*Customer:* {{ $json.customer_name }} ({{ $json.customer_email }})\n*Subject:* {{ $json.subject }}\n*Sentiment:* {{ $json.sentiment }} | *Urgency:* {{ $json.urgency }}\n*Category:* {{ $json.category }}\n*Priority Score:* {{ $json.priority_score }}/110\n\n*Key Issues:*\n{{ $json.key_issues }}\n\n*Suggested Response:*\n```\n{{ $json.suggested_response }}\n```\n\n*Action Required:* Respond within 30 minutes\n*Email ID:* {{ $json.email_id }}",
        "otherOptions": {
          "mrkdwn": true
        },
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "9e35f1a2-7e66-4798-bbe9-fd843fd4d67e",
      "name": "Log Routine Tickets",
      "type": "n8n-nodes-base.slack",
      "position": [
        0,
        208
      ],
      "parameters": {
        "text": "\ud83d\udce7 *New Support Ticket*\n\n*Customer:* {{ $json.customer_name }}\n*Category:* {{ $json.category }} | *Priority:* {{ $json.priority_score }}\n*Sentiment:* {{ $json.sentiment }}\n\n*Subject:* {{ $json.subject }}",
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "50556621-9a8c-4a12-8c6b-78fd614a2e37",
      "name": "Log to Airtable Database",
      "type": "n8n-nodes-base.airtable",
      "position": [
        256,
        112
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "appXXXXXXXXXXXXXX"
        },
        "table": {
          "__rl": true,
          "mode": "id",
          "value": "tblSupportTickets"
        },
        "columns": {
          "value": {
            "Status": "Open",
            "Subject": "={{ $json.subject }}",
            "Urgency": "={{ $json.urgency }}",
            "Category": "={{ $json.category }}",
            "Email_ID": "={{ $json.email_id }}",
            "Sentiment": "={{ $json.sentiment }}",
            "Thread_ID": "={{ $json.thread_id }}",
            "Timestamp": "={{ $json.timestamp }}",
            "Email_Body": "={{ $json.body }}",
            "Key_Issues": "={{ $json.key_issues }}",
            "Customer_Name": "={{ $json.customer_name }}",
            "Customer_Email": "={{ $json.customer_email }}",
            "Priority_Score": "={{ $json.priority_score }}",
            "Suggested_Response": "={{ $json.suggested_response }}",
            "Requires_Immediate_Attention": "={{ $json.requires_immediate_attention }}"
          },
          "mappingMode": "defineBelow"
        },
        "options": {},
        "operation": "create"
      },
      "typeVersion": 2.1
    },
    {
      "id": "39e8213d-f382-4778-9d13-8036b2e52696",
      "name": "Update Analytics Dashboard",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        512,
        112
      ],
      "parameters": {
        "columns": {
          "value": {
            "Date": "={{ $json.timestamp.split('T')[0] }}",
            "Time": "={{ $json.timestamp.split('T')[1].split('.')[0] }}",
            "Email": "={{ $json.customer_email }}",
            "Status": "Open",
            "Subject": "={{ $json.subject }}",
            "Urgency": "={{ $json.urgency }}",
            "Category": "={{ $json.category }}",
            "Critical": "={{ $json.requires_immediate_attention }}",
            "Customer": "={{ $json.customer_name }}",
            "Priority": "={{ $json.priority_score }}",
            "Sentiment": "={{ $json.sentiment }}"
          },
          "mappingMode": "defineBelow"
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "id",
          "value": "gid=0"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "970abf02-9fac-4109-8fc2-909f333d3224",
      "name": "Generate Insights",
      "type": "n8n-nodes-base.openAi",
      "position": [
        752,
        112
      ],
      "parameters": {
        "prompt": {
          "messages": [
            {
              "role": "system",
              "content": "Based on the analysis, generate 3 data insights: 1) Trend identification, 2) Risk assessment, 3) Actionable recommendation for the support team."
            },
            {
              "content": "Ticket Data:\nSentiment: {{ $json.sentiment }}\nCategory: {{ $json.category }}\nUrgency: {{ $json.urgency }}\nIssues: {{ $json.key_issues }}\n\nProvide insights in JSON format with keys: trend, risk, recommendation"
            }
          ]
        },
        "options": {
          "maxTokens": 500,
          "temperature": 0.5
        },
        "resource": "chat",
        "requestOptions": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "ab329b35-05bb-42ec-bfe1-9f8ba4ec6772",
      "name": "Store AI Insights",
      "type": "n8n-nodes-base.airtable",
      "position": [
        1008,
        112
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "appXXXXXXXXXXXXXX"
        },
        "table": {
          "__rl": true,
          "mode": "id",
          "value": "tblInsights"
        },
        "columns": {
          "value": {
            "Record_ID": "={{ $('Log to Airtable Database').item.json.id }}",
            "AI_Insights": "={{ JSON.parse($json.choices[0].message.content) }}",
            "Generated_At": "={{ new Date().toISOString() }}"
          },
          "mappingMode": "defineBelow"
        },
        "options": {},
        "operation": "update"
      },
      "typeVersion": 2.1
    },
    {
      "id": "02b48c6d-c266-4f92-913d-c76367bb5d96",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Watches Gmail inbox for new unread emails\n"
      },
      "typeVersion": 1
    },
    {
      "id": "98d2e12e-be2a-440b-baf5-8cd6120e4271",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -784,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Analyzes sentiment, urgency, and categorizes support requests\n"
      },
      "typeVersion": 1
    },
    {
      "id": "4310037a-a46e-43f0-bd9d-d32af017d9b8",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -528,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Structures AI output and calculates priority scores\n"
      },
      "typeVersion": 1
    },
    {
      "id": "81b8f33b-303c-4cf1-905d-6065d3a5b026",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Routes tickets based on urgency classification level\n"
      },
      "typeVersion": 1
    },
    {
      "id": "b7f1ae71-fe6d-4f64-8193-e36ae264af13",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        -112
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Sends immediate Slack alerts for critical tickets\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d30db0f5-e392-4114-93b8-674bee6121ec",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        368
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Posts standard priority tickets to Slack channel\n"
      },
      "typeVersion": 1
    },
    {
      "id": "393db2f6-ee8c-4f45-96c0-645c5c530619",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Stores complete ticket data in Airtable base\n"
      },
      "typeVersion": 1
    },
    {
      "id": "279d3e6a-e014-4c23-8ccb-d635b25c0fb3",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        480,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Logs ticket metrics to Google Sheets dashboard\n"
      },
      "typeVersion": 1
    },
    {
      "id": "08bb68e3-a7ad-42fa-aea7-5abc319e2b27",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Creates AI-powered trends and risk assessments daily\n"
      },
      "typeVersion": 1
    },
    {
      "id": "acc25615-d10a-4d12-b1cb-4ecb28c972da",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        976,
        0
      ],
      "parameters": {
        "width": 176,
        "height": 96,
        "content": "Saves generated insights back to Airtable records\n"
      },
      "typeVersion": 1
    },
    {
      "id": "9ca1492d-826d-45ca-880d-35210ed527c9",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1568,
        -112
      ],
      "parameters": {
        "width": 464,
        "height": 528,
        "content": "# Workflow Description\n\nAI-powered customer support automation that monitors Gmail, analyzes email sentiment and urgency, routes critical issues to Slack, logs all tickets to Airtable and Google Sheets, then generates actionable insights. \n\nPrioritizes responses, tracks metrics, and improves support team efficiency through intelligent triage.\n\n---\n\n**Created by Daniel Shashko**  \nhttps://www.linkedin.com/in/daniel-shashko/"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Route by Urgency": {
      "main": [
        [
          {
            "node": "Alert Critical Issues",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Insights": {
      "main": [
        [
          {
            "node": "Store AI Insights",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Analysis Engine": {
      "main": [
        [
          {
            "node": "Parse & Enrich Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Routine Tickets": {
      "main": [
        [
          {
            "node": "Log to Airtable Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse & Enrich Data": {
      "main": [
        [
          {
            "node": "Route by Urgency",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Alert Critical Issues": {
      "main": [
        [
          {
            "node": "Log to Airtable Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Monitor Support Emails": {
      "main": [
        [
          {
            "node": "AI Analysis Engine",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log to Airtable Database": {
      "main": [
        [
          {
            "node": "Update Analytics Dashboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Analytics Dashboard": {
      "main": [
        [
          {
            "node": "Generate Insights",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}