{
  "name": "First Response Automation — Gorgias",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gorgias-auto-reply",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Gorgias New Ticket",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "gorgias-auto-reply"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ticket = $input.first().json;\n\nconst hasAgentReply = (ticket.messages || []).some(m => m.source?.type === 'helpdesk');\nif (hasAgentReply) {\n  return [];\n}\n\nconst subject = (ticket.subject || '').toLowerCase();\nconst body = (ticket.messages?.[0]?.body_text || '').toLowerCase();\nconst combined = `${subject} ${body}`;\nconst customerName = ticket.requester?.firstname || 'there';\n\nreturn [{\n  json: {\n    ticketId: ticket.id,\n    customerName,\n    text: combined,\n    shouldReply: true,\n  }\n}];"
      },
      "id": "extract-text",
      "name": "Extract Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const text = $input.first().json.text;\nconst customerName = $input.first().json.customerName;\nconst ticketId = $input.first().json.ticketId;\n\nconst patterns = {\n  order_status: ['where is my order', 'tracking', 'order status', 'when will it arrive'],\n  returns: ['return', 'send back', 'exchange', 'how do i return'],\n  refund: ['refund', 'money back', 'charged', 'when will i get my refund'],\n  order_change: ['cancel', 'change my order', 'wrong address', 'wrong item'],\n  shipping: ['ship to', 'international', 'how long does shipping'],\n};\n\nlet replyBody = '';\nlet matched = false;\n\nfor (const [type, keywords] of Object.entries(patterns)) {\n  if (keywords.some(kw => text.includes(kw))) {\n    matched = true;\n    switch (type) {\n      case 'order_status':\n        replyBody = `Hi ${customerName},\\n\\nThanks for reaching out! You can track your order in real time at your account page. You'll need your order number and the email used at checkout. Tracking typically updates within 24 hours of your order shipping.\\n\\nIf you have any trouble finding it, reply here and we'll look into it right away.\\n\\nBest,\\nThe Support Team`;\n        break;\n      case 'returns':\n        replyBody = `Hi ${customerName},\\n\\nWe're happy to help with your return! Our return window is 30 days from delivery. Items must be unused and in original packaging.\\n\\nYou can start a return from your account page. Once we receive your item, refunds process within 5-7 business days.\\n\\nLet us know if you need anything else.\\n\\nBest,\\nThe Support Team`;\n        break;\n      case 'refund':\n        replyBody = `Hi ${customerName},\\n\\nWe understand you'd like information about a refund. Once a return is received and inspected, refunds are processed within 5-7 business days back to your original payment method.\\n\\nIf you haven't started a return yet, you can do so from your account page.\\n\\nWe're here if you have any other questions.\\n\\nBest,\\nThe Support Team`;\n        break;\n      case 'order_change':\n        replyBody = `Hi ${customerName},\\n\\nThanks for reaching out! Orders can be changed or cancelled within 1 hour of placement. If it's been longer, reply here with your order number and the change you need — we'll do our best to accommodate.\\n\\nBest,\\nThe Support Team`;\n        break;\n      case 'shipping':\n        replyBody = `Hi ${customerName},\\n\\nGreat question! Standard shipping takes 3-5 business days. We also ship internationally to 40+ countries (7-14 business days).\\n\\nOnce your order ships, you'll receive a tracking email within 24 hours.\\n\\nLet us know if you need anything else!\\n\\nBest,\\nThe Support Team`;\n        break;\n    }\n    break;\n  }\n}\n\nif (!matched) return [];\n\nreturn [{\n  json: {\n    ticketId,\n    customerName,\n    replyBody,\n  }\n}];"
      },
      "id": "match-intent",
      "name": "Match Intent & Build Reply",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://your-store.gorgias.com/api/tickets/{{ $json.ticketId }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"body_text\": {{ JSON.stringify($json.replyBody) }},\n  \"channel\": \"email\",\n  \"from_agent\": true,\n  \"source\": {\n    \"type\": \"helpdesk\",\n    \"from\": {\n      \"name\": \"Support Team\",\n      \"address\": \"support@yourstore.com\"\n    }\n  }\n}",
        "options": {
          "retry": {
            "maxTries": 2,
            "waitBetweenTries": 30000
          }
        }
      },
      "id": "post-reply",
      "name": "Post Reply to Gorgias",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, 0],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://your-store.gorgias.com/api/tickets/{{ $json.ticketId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{ \"tags\": [{ \"name\": \"auto-replied\" }] }",
        "options": {}
      },
      "id": "tag-auto-replied",
      "name": "Tag auto-replied",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 0],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    }
  ],
  "connections": {
    "Gorgias New Ticket": {
      "main": [
        [
          {
            "node": "Extract Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Text": {
      "main": [
        [
          {
            "node": "Match Intent & Build Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match Intent & Build Reply": {
      "main": [
        [
          {
            "node": "Post Reply to Gorgias",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Reply to Gorgias": {
      "main": [
        [
          {
            "node": "Tag auto-replied",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-first-response"
  }
}
