{
  "name": "Nodbot - Avito leads to CRM with dedupe key",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "avito-leads-to-crm",
        "responseMode": "responseNode"
      },
      "id": "avito-leads-to-crm-01",
      "name": "Webhook input",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -720,
        40
      ]
    },
    {
      "parameters": {
        "jsCode": "const src = $json.body ?? $json;\nconst phoneRaw = String(src.phone ?? src.contact_phone ?? '').trim();\nlet digits = phoneRaw.replace(/\\D/g, '');\nif (digits.length === 11 && digits.startsWith('8')) digits = `7${digits.slice(1)}`;\nif (digits.length === 10) digits = `7${digits}`;\nconst phone = /^7\\d{10}$/.test(digits) ? `+${digits}` : '';\nconst avitoId = String(src.lead_id ?? src.message_id ?? src.chat_id ?? '').trim();\nconst itemId = String(src.item_id ?? src.ad_id ?? '').trim();\nif (!avitoId && !phone) throw new Error('Need avito lead id or normalized phone');\nreturn [{ json: {\n  dedupe_key: avitoId ? `avito:${avitoId}` : `avito-phone:${digits}`,\n  phone_normalized: phone,\n  customer_name: src.name ?? src.user_name ?? 'Авито лид',\n  message: src.message ?? src.text ?? '',\n  item_id: itemId,\n  item_title: src.item_title ?? src.title ?? '',\n  source: 'avito',\n  received_at: new Date().toISOString()\n}}];"
      },
      "id": "avito-leads-to-crm-02",
      "name": "Normalize Avito lead context",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -440,
        40
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CRM_ENDPOINT/leads/upsert",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": []
        }
      },
      "id": "avito-leads-to-crm-03",
      "name": "CRM upsert lead",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -160,
        40
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_ALERT_WEBHOOK",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": []
        }
      },
      "id": "avito-leads-to-crm-04",
      "name": "Send alert on failed CRM response",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        120,
        40
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { ok: true, status: \"accepted\" } }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "avito-leads-to-crm-05",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        400,
        40
      ]
    }
  ],
  "connections": {
    "Webhook input": {
      "main": [
        [
          {
            "node": "Normalize Avito lead context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Avito lead context": {
      "main": [
        [
          {
            "node": "CRM upsert lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CRM upsert lead": {
      "main": [
        [
          {
            "node": "Send alert on failed CRM response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send alert on failed CRM response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "nodbot"
    },
    {
      "name": "avito"
    },
    {
      "name": "crm"
    },
    {
      "name": "dedupe"
    }
  ],
  "triggerCount": 0,
  "updatedAt": "2026-05-30T00:00:00.000Z",
  "versionId": "nodbot-avito-leads-to-crm-problem-solution-v1",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "notes": "Problem/Solution-мануал по передаче заявок Авито в CRM через n8n: webhook, item_id, chat/message context, нормализация телефона, dedupe key, upsert лида и production-контроль. Replace credentials, endpoints and custom field IDs before production."
}