{
  "name": "TrustField Agentic Reply Catcher",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agentic-reply",
        "responseMode": "onReceived"
      },
      "id": "webhook",
      "name": "Resend Inbound Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json.body || $input.first().json;\nconst text = (body.text || body.html || '').toLowerCase();\nlet label = 'objection';\nif (/unsubscribe|remove me|opt out/.test(text)) label = 'unsubscribe';\nelse if (/out of office|ooo/.test(text)) label = 'ooo';\nelse if (/wrong person|not the right/.test(text)) label = 'wrong_person';\nelse if (/interested|let's talk|schedule|demo/.test(text)) label = 'interested';\nreturn [{ json: { label, from: body.from, subject: body.subject, snippet: text.slice(0, 200) } }];"
      },
      "id": "classify",
      "name": "Classify Reply",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.label}}",
              "operation": "equals",
              "value2": "interested"
            }
          ]
        }
      },
      "id": "if_interested",
      "name": "Interested?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{$env.TELEGRAM_CHANNEL_ID}}",
        "text": "=Agentic reply INTERESTED from {{$json.from}}\n{{$json.snippet}}"
      },
      "id": "telegram_alert",
      "name": "Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        960,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.TRUSTFIELD_API_URL}}/api/admin/agentic/reply-inbound",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "company",
              "value": "={{$json.company}}"
            },
            {
              "name": "classification",
              "value": "={{$json.label}}"
            },
            {
              "name": "reply_ref",
              "value": "=reply:{{$json.message_id}}"
            },
            {
              "name": "from_email",
              "value": "={{$json.from}}"
            }
          ]
        }
      },
      "id": "api_log",
      "name": "Log Reply API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        960,
        400
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Resend Inbound Webhook": {
      "main": [
        [
          {
            "node": "Classify Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Reply": {
      "main": [
        [
          {
            "node": "Log Reply API",
            "type": "main",
            "index": 0
          },
          {
            "node": "Interested?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Interested?": {
      "main": [
        [
          {
            "node": "Telegram Alert",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "meta": {
    "description": "Wire Resend inbound to webhook. Manual reply log: ./scripts/outreach_mark_replied.sh"
  }
}