{
  "name": "Hotline — Follow-up Sequence",
  "meta": {
    "description": "Day-2 nudge and day-5 breakup email, scheduled per lead, cancelled the moment the lead replies, books, or closes."
  },
  "nodes": [
    {
      "id": "c1",
      "name": "Every Hour",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 300],
      "parameters": { "rule": { "interval": [{ "field": "hours", "hoursInterval": 1 }] } }
    },
    {
      "id": "c2",
      "name": "Leads Due for Day-2",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [220, 180],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM leads WHERE stage IN ('new','qualified','contacted') AND email_status = 'sent' AND created_at < NOW() - INTERVAL '2 days' AND created_at > NOW() - INTERVAL '3 days' AND followup_day2_at IS NULL"
      },
      "notes": "Only leads who got the instant reply but haven't replied, booked, or closed. The stage filter is the cancellation rule: a reply flips the stage, which removes the lead from this query."
    },
    {
      "id": "c3",
      "name": "Leads Due for Day-5",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [220, 420],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM leads WHERE stage IN ('new','qualified','contacted') AND email_status = 'sent' AND created_at < NOW() - INTERVAL '5 days' AND created_at > NOW() - INTERVAL '6 days' AND followup_day2_at IS NOT NULL AND followup_day5_at IS NULL"
      }
    },
    {
      "id": "c4",
      "name": "Draft Day-2 Nudge (Claude)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 180],
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "x-api-key", "value": "={{ $env.ANTHROPIC_API_KEY }}" },
            { "name": "anthropic-version", "value": "2023-06-01" }
          ]
        },
        "sendBody": true,
        "jsonBody": "={\"model\": \"claude-sonnet-4-5-20250929\", \"max_tokens\": 600, \"messages\": [{\"role\": \"user\", \"content\": \"Write a 3-sentence day-2 follow-up in the lead's language. Reference their original request, re-offer the two time slots, zero pressure. Lead: \" + JSON.stringify($json)}]}"
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "c5",
      "name": "Draft Day-5 Breakup (Claude)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 420],
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "x-api-key", "value": "={{ $env.ANTHROPIC_API_KEY }}" },
            { "name": "anthropic-version", "value": "2023-06-01" }
          ]
        },
        "sendBody": true,
        "jsonBody": "={\"model\": \"claude-sonnet-4-5-20250929\", \"max_tokens\": 600, \"messages\": [{\"role\": \"user\", \"content\": \"Write a 3-sentence day-5 breakup email in the lead's language: gracious close, leave the door open, one-line reply option. Lead: \" + JSON.stringify($json)}]}"
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "c6",
      "name": "Send via Resend",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [700, 300],
      "parameters": {
        "method": "POST",
        "url": "https://api.resend.com/emails",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [{ "name": "Authorization", "value": "=Bearer {{ $env.RESEND_API_KEY }}" }]
        },
        "sendBody": true,
        "jsonBody": "={\"from\": \"Maya at Northwind <quotes@northwind.example>\", \"to\": [$json.email], \"subject\": $json.subject, \"text\": $json.body}"
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1500
    },
    {
      "id": "c7",
      "name": "Mark Sent + Log",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [920, 300],
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads SET followup_day2_at = COALESCE(followup_day2_at, NOW()), followup_day5_at = CASE WHEN $2 = 'day5' THEN NOW() ELSE followup_day5_at END WHERE id = $1; INSERT INTO runs (id, lead_id, status, duration_ms, steps) VALUES (gen_random_uuid()::text, $1, 'success', 0, $3)",
        "options": { "queryReplacement": "={{ [$json.id, $json.sequenceStep, $json.steps] }}" }
      },
      "notes": "Idempotency: the *_at columns are only set once, and the eligibility queries require them to be NULL — so an hourly tick can never send the same nudge twice."
    }
  ],
  "connections": {
    "Every Hour": {
      "main": [[
        { "node": "Leads Due for Day-2", "type": "main", "index": 0 },
        { "node": "Leads Due for Day-5", "type": "main", "index": 0 }
      ]]
    },
    "Leads Due for Day-2": { "main": [[{ "node": "Draft Day-2 Nudge (Claude)", "type": "main", "index": 0 }]] },
    "Leads Due for Day-5": { "main": [[{ "node": "Draft Day-5 Breakup (Claude)", "type": "main", "index": 0 }]] },
    "Draft Day-2 Nudge (Claude)": { "main": [[{ "node": "Send via Resend", "type": "main", "index": 0 }]] },
    "Draft Day-5 Breakup (Claude)": { "main": [[{ "node": "Send via Resend", "type": "main", "index": 0 }]] },
    "Send via Resend": { "main": [[{ "node": "Mark Sent + Log", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1", "timezone": "America/Montreal" }
}
