{
  "name": "Lead Triage → Telegram",
  "nodes": [
    {
      "id": "1bcbcf2f-1cfa-5182-90e7-92a47f77c3df",
      "name": "Lead Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "lead",
        "responseMode": "onReceived",
        "options": {}
      },
      "webhookId": "95c3d8bf-9fe4-51d3-8daa-66832b2383f1"
    },
    {
      "id": "d044b728-8489-5c3b-bbef-e6adab5f3013",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ],
      "parameters": {
        "mode": "manual",
        "includeOtherFields": true,
        "assignments": {
          "assignments": [
            {
              "id": "d451f3b6-347c-5895-846e-a046640ffc44",
              "name": "TELEGRAM_CHAT_ID",
              "value": "",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "eb86c9b2-7bb8-5491-90ad-49e42f893bdb",
      "name": "Score & Store Lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "jsCode": "const req = $('Lead Webhook').first().json;\nconst b = req.body || {};\nconst pick = (...k) => { for (const x of k) if (b[x]) return String(b[x]).trim(); return ''; };\nconst lead = {\n  id: 'L' + Date.now().toString(36),\n  name: pick('name', 'fullName', 'first_name'),\n  email: pick('email', 'mail').toLowerCase(),\n  phone: pick('phone', 'whatsapp', 'tel'),\n  company: pick('company', 'business', 'hotel'),\n  message: pick('message', 'msg', 'comment', 'text').slice(0, 1200),\n  source: pick('source', 'page', 'utm_source') || (req.headers && req.headers.referer) || 'site',\n  received_at: new Date().toISOString()\n};\nif (!lead.email && !lead.phone) return [];\n// score: B2B signals + budget words + contact completeness\nlet score = 0;\nconst t = (lead.message + ' ' + lead.company).toLowerCase();\nif (lead.company) score += 25;\nif (/hotel|villa|hostel|clinic|studio|отель|вилл|клиник|студи|бизнес|b2b/.test(t)) score += 25;\nif (/\\$|usd|eur|₽|бюджет|budget|sprint|спринт|автоматиз|automation|ai/.test(t)) score += 25;\nif (lead.email && lead.phone) score += 15;\nif (lead.message.length > 80) score += 10;\nlead.score = Math.min(100, score);\nlead.tier = lead.score >= 60 ? 'HOT' : lead.score >= 30 ? 'WARM' : 'COLD';\n// local store: workflow static data (persisted in n8n DB), last 1000 leads\nconst db = $getWorkflowStaticData('global');\ndb.leads = db.leads || [];\ndb.leads.unshift(lead);\ndb.leads = db.leads.slice(0, 1000);\nlead.total_leads = db.leads.length;\nreturn [{ json: lead }];"
      }
    },
    {
      "id": "ca9c4af5-cf37-5fdb-bc1a-9a61e534e82e",
      "name": "Compose Alert",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "jsCode": "const cfg = $('Config').first().json;\nconst l = $json;\nconst icon = { HOT: '🔥', WARM: '🟠', COLD: '⚪️' }[l.tier];\nconst text = [\n  `${icon} Новый лид · ${l.tier} (${l.score}/100)`,\n  l.name && `Имя: ${l.name}`, l.company && `Компания: ${l.company}`,\n  l.email && `Email: ${l.email}`, l.phone && `Тел: ${l.phone}`,\n  l.message && `Сообщение: ${l.message}`,\n  `Источник: ${l.source}`, `Всего лидов: ${l.total_leads}`,\n  l.tier === 'HOT' ? 'Следующий шаг: предложить стратегическую сессию.' : ''\n].filter(Boolean).join('\\n');\nreturn { json: { text, chat_id: cfg.TELEGRAM_CHAT_ID } };",
        "mode": "runOnceForEachItem"
      }
    },
    {
      "id": "9294561e-9715-5160-8096-d23a489213e0",
      "name": "Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        880,
        0
      ],
      "parameters": {
        "chatId": "={{ $json.chat_id }}",
        "text": "={{ $json.text }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Lead Webhook": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Score & Store Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score & Store Lead": {
      "main": [
        [
          {
            "node": "Compose Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compose Alert": {
      "main": [
        [
          {
            "node": "Telegram Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Makassar",
    "saveManualExecutions": true,
    "saveDataErrorExecution": "all"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}