{
  "name": "Ticket Triage",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        80,
        -400
      ],
      "id": "2a4ff79e-8072-44b5-9d3d-d5258554db60",
      "name": "When clicking ‘Execute workflow’"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_DOCUMENTID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "YOUR_SHEETNAME",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        256,
        -432
      ],
      "id": "4bbbceae-1710-46c1-acf3-908ab595b829",
      "name": "Get row(s) in sheet"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "12c3704b-4a62-49a5-b0b0-149fac5b3db7",
              "name": "ticket_id",
              "value": "={{ $json.ticket_id }}",
              "type": "number"
            },
            {
              "id": "dd12d31f-2aad-4305-9b2d-43e8790f3ce3",
              "name": "ticket_text",
              "value": "={{ $json.ticket_text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        80,
        -176
      ],
      "id": "2867b89a-bdec-4ba4-8d52-0371cfe4d44d",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-3.1-flash-lite",
          "mode": "list",
          "cachedResultName": "models/gemini-3.1-flash-lite"
        },
        "messages": {
          "values": [
            {
              "content": "=You are a support ticket classifier for a bank. Read the customer message and choose exactly ONE category from this list:\n\ncard_arrival: the customer is asking about the delivery or arrival of a new card\nlost_or_stolen_card: the customer's card is lost or stolen\ndeclined_card_payment: a card payment was declined\npending_transfer: a transfer is pending\ntransfer_not_received_by_recipient: a transfer was made but the recipient has not received it\ncash_withdrawal_not_recognised: the customer does not recognise a cash withdrawal\npasscode_forgotten: the customer has forgotten their passcode\nverify_my_identity: the customer needs help verifying their identity\nrequest_refund: the customer wants a refund\ntransaction_charged_twice: the customer was charged twice for one transaction\n\nAlso give:\nconfidence: high, medium or low (how sure you are of the category)\nurgency: high if money is already lost or at risk right now (stolen card, unrecognised withdrawal, double charge); medium if a payment or transfer is stuck or failing; low if it is a general question\n\nReply with JSON only, in exactly this shape:\n{\"category\": \"...\", \"confidence\": \"...\", \"urgency\": \"...\"}\n\nCustomer message:\n{{ $json.ticket_text }}"
            }
          ]
        },
        "jsonOutput": true,
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1.2,
      "position": [
        640,
        -416
      ],
      "id": "37760e16-ccf6-4aca-9b33-42fe6ad05b98",
      "name": "Message a model",
      "retryOnFail": true,
      "waitBetweenTries": 5000,
      "maxTries": 5
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        464,
        -288
      ],
      "id": "6608798a-a5ca-4cd9-9307-15b1ebdbd74c",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "jsCode": "const raw = $json.content.parts[0].text;\nlet out = { category: 'PARSE_ERROR', confidence: '', urgency: '' };\ntry {\n  const p = typeof raw === 'string'\n    ? JSON.parse(raw.replace(/```json|```/g, '').trim())\n    : raw;\n  out = { category: p.category, confidence: p.confidence, urgency: p.urgency };\n} catch (e) {}\nreturn {\n  json: {\n    ticket_id: $('Loop Over Items').item.json.ticket_id,\n    predicted_category: out.category,\n    confidence: out.confidence,\n    urgency: out.urgency\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        768,
        -192
      ],
      "id": "fa0df3c3-85c3-4f6d-a1ab-f80bcee8cfcb",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "YOUR_DOCUMENTID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "YOUR_SHEETNAME",
          "mode": "id"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "predicted_category": "={{ $json.predicted_category }}",
            "confidence": "={{ $json.confidence }}",
            "urgency": "={{ $json.urgency }}",
            "ticket_id": "={{ $json.ticket_id }}"
          },
          "matchingColumns": [
            "ticket_id"
          ],
          "schema": [
            {
              "id": "ticket_id",
              "displayName": "ticket_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "ticket_text",
              "displayName": "ticket_text",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "true_category",
              "displayName": "true_category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "predicted_category",
              "displayName": "predicted_category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "confidence",
              "displayName": "confidence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "urgency",
              "displayName": "urgency",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "correct",
              "displayName": "correct",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1056,
        -384
      ],
      "id": "eeaa6efe-6c06-40eb-8f0f-d2686a03258d",
      "name": "Update row in sheet",
      "retryOnFail": true,
      "maxTries": 5,
      "waitBetweenTries": 5000
    },
    {
      "parameters": {
        "amount": 6
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1040,
        -192
      ],
      "id": "8e83158a-cad3-41e6-905e-9abf36cdb6ff",
      "name": "Wait"
    }
  ],
  "pinData": {},
  "connections": {
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Get row(s) in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) in sheet": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Update row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update row in sheet": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "nodeGroups": [],
  "tags": []
}