{
  "name": "Suggest a macro from the ticket text",
  "nodes": [
    {
      "parameters": {
        "content": "## Suggest a macro from the ticket text\n\nPortfolio demo only. This workflow is an original example for demonstration. It is not client work and it does not represent a deployment for any customer.\n\nRun **Run demo** to execute the offline rules engine. The live branch is used only when the incoming item sets `demo` to false and the placeholder credentials have been replaced.",
        "height": 220,
        "width": 520,
        "color": 4
      },
      "id": "a0cac200-9643-4b62-bb35-11340ef04ac4",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -40
      ]
    },
    {
      "parameters": {},
      "id": "f781a472-8d40-4a6e-9994-4bd320f72b44",
      "name": "Run demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        40,
        280
      ],
      "notes": "CLI and the n8n editor start here. An empty manual run loads the bundled sample.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "macro-suggestion",
        "authentication": "none",
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "id": "f4fbe8a7-9eb7-47ea-8661-9972ffee399c",
      "name": "Inbound webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        40,
        460
      ],
      "notes": "POST JSON to this path. Include demo:false only after credentials are connected.",
      "notesInFlow": true,
      "webhookId": "e292f9a2-e82f-4fdc-baa3-da119311a1fa"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const sample = {\n  \"demo\": true,\n  \"subject\": \"I cannot log in\",\n  \"body\": \"Password reset does not give me access.\",\n  \"email\": \"pat@example.com\"\n};\nconst incoming = $input.first()?.json ?? {};\nconst provided = Object.keys(incoming).filter((key) => incoming[key] !== undefined && incoming[key] !== null && incoming[key] !== '');\nconst base = provided.length ? { ...sample, ...incoming } : { ...sample };\nconst flag = incoming.demo;\nbase.demo = !(flag === false || flag === 'false' || flag === 0 || flag === '0');\nreturn [{ json: base }];\n"
      },
      "id": "1c029ffb-dcf2-45ec-8461-a15dd3f5c954",
      "name": "Normalize input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        360
      ],
      "notes": "Merges the request over the bundled sample. demo stays true unless the caller explicitly turns it off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nconst produced = (() => {\n\nconst haystack = String(item.body || item.body || item.subject || item.summary || '').toLowerCase();\nconst hits = [];\nif (['refund', 'invoice', 'charge', 'payment'].some((word) => haystack.includes(word))) {\n  hits.push({ id: 'billing', label: 'Billing', weight: 3 });\n}\nif (['error', 'bug', 'broken', 'down', '500'].some((word) => haystack.includes(word))) {\n  hits.push({ id: 'technical', label: 'Technical', weight: 3 });\n}\nif (['login', 'password', 'access', 'invite'].some((word) => haystack.includes(word))) {\n  hits.push({ id: 'account', label: 'Account', weight: 2 });\n}\nif (['hello', 'question'].some((word) => haystack.includes(word))) {\n  hits.push({ id: 'general', label: 'General', weight: 1 });\n}\nhits.sort((a, b) => b.weight - a.weight);\nconst winner = hits[0] || { id: 'general', label: 'general', weight: 0 };\nconst draft = item.draft || ('Thanks for the note. We logged this as ' + winner.label + ' and will follow up shortly.');\nconst record = {\n  label: winner.id,\n  labelName: winner.label,\n  confidence: hits.length ? Math.min(0.95, 0.45 + hits.length * 0.15) : 0.3,\n  subject: item.subject || winner.label,\n  body: draft,\n  email: item.from || item.email || '',\n  priority: winner.id,\n  title: (item.subject || winner.label),\n};\nreturn {\n  summary: 'Ticket classified as ' + winner.label + ' (' + record.confidence.toFixed(2) + ').',\n  route: winner.id,\n  record,\n  slackText: winner.label + ': ' + (item.subject || haystack.slice(0, 80)),\n  actions: [{ app: 'mailbox', operation: 'label-and-draft', payload: record }],\n};\n\n})();\nif (!produced || typeof produced.summary !== 'string' || !produced.summary.trim()) {\n  throw new Error('Apply rules must return a summary string');\n}\nreturn [{\n  json: {\n    ...item,\n    ...produced,\n    demo: item.demo !== false,\n  },\n}];\n"
      },
      "id": "ccbc30c3-ec1e-4463-a4b3-879ad5e87c58",
      "name": "Apply rules",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        620,
        360
      ],
      "notes": "Deterministic stand-in for the model or enrichment API. Replace or precede this with a live model node when you turn demo mode off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c8d3c7b7-3c15-48e7-a283-17582cda6e4d",
              "leftValue": "={{ $json.demo }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "cd2a6143-681d-41fe-9792-ad263daf1a47",
      "name": "Demo mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        360
      ],
      "notes": "True: package the offline result. False: run the credentialed apps.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nreturn [{\n  json: {\n    demo: true,\n    status: 'demo_ready',\n    workflow: 'macro-suggestion',\n    summary: item.summary,\n    route: item.route ?? null,\n    record: item.record ?? {},\n    actions: item.actions ?? [],\n    slackText: item.slackText ?? item.summary,\n    notes: 'Demo mode did not call external apps. Send {\"demo\": false} after placeholder credentials are replaced.',\n  },\n}];\n"
      },
      "id": "4a3fb9a9-facb-41ca-b0ea-66a2ea80929c",
      "name": "Package demo result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1180,
        180
      ]
    },
    {
      "parameters": {
        "authentication": "apiToken",
        "resource": "ticket",
        "operation": "create",
        "description": "={{ $json.record.body || $json.summary }}",
        "jsonParameters": false,
        "additionalFields": {
          "subject": "={{ $json.record.subject || $json.summary }}"
        }
      },
      "id": "aa321efd-604b-47f6-bbbd-56b733e9a7bf",
      "name": "Create Zendesk ticket",
      "type": "n8n-nodes-base.zendesk",
      "typeVersion": 1,
      "position": [
        1180,
        760
      ],
      "credentials": {
        "zendeskApi": {
          "id": "PLACEHOLDER_ZENDESKAPI",
          "name": "Zendesk account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#macros"
        },
        "text": "={{ $json.slackText || $json.summary }}",
        "otherOptions": {}
      },
      "id": "c019af9a-6e49-48e8-806f-6fd86b2f2e7f",
      "name": "Post #macros",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1460,
        760
      ],
      "credentials": {
        "slackOAuth2Api": {
          "id": "PLACEHOLDER_SLACKOAUTH2API",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Live path\n\nReplace every `PLACEHOLDER_*` credential, then POST the sample with `\"demo\": false`.\n\nThe nodes on this branch use the `record` and `actions` fields produced by Apply rules.",
        "height": 180,
        "width": 420,
        "color": 6
      },
      "id": "cf7cbf35-6093-42dc-bd3d-f8c361abad1d",
      "name": "Live path",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1120,
        520
      ]
    }
  ],
  "connections": {
    "Run demo": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inbound webhook": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize input": {
      "main": [
        [
          {
            "node": "Apply rules",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply rules": {
      "main": [
        [
          {
            "node": "Demo mode?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Demo mode?": {
      "main": [
        [
          {
            "node": "Package demo result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Zendesk ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Zendesk ticket": {
      "main": [
        [
          {
            "node": "Post #macros",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "The ticket class picks a macro family. The draft on the record is the suggested reply, posted to Slack for the agent."
  },
  "active": false
}
