{
 "name": "Serrana · leads de WhatsApp e Instagram",
 "nodes": [
  {
   "id": "c43a1f4c-3a44-4940-abff-d5ac5b624913",
   "name": "Nota e527",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -40,
    -380
   ],
   "parameters": {
    "content": "## Leads de WhatsApp e Instagram → CRM\nCada mensaje nuevo, venga de WhatsApp o de Instagram: la IA entiende qué quiere (precio, compra, cambio, mayorista), qué prenda y talle, y qué tan interesado está. Se carga en el CRM con vendedor y fecha de seguimiento, se le contesta en el mismo canal y, si es un lead caliente, el vendedor recibe el aviso por Telegram.",
    "width": 600,
    "height": 210,
    "color": 4
   }
  },
  {
   "id": "9bf879e0-530b-4947-b7ed-e8b60d6f2ed2",
   "name": "Mensaje de WhatsApp",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2.1,
   "position": [
    0,
    -60
   ],
   "parameters": {
    "httpMethod": "POST",
    "path": "whatsapp-serrana",
    "responseMode": "onReceived",
    "options": {}
   },
   "webhookId": "7301e445-b40f-444b-a23e-d57f19f9bed8"
  },
  {
   "id": "f722e14d-7312-4777-8967-64edc912845d",
   "name": "Mensaje de Instagram",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2.1,
   "position": [
    0,
    160
   ],
   "parameters": {
    "httpMethod": "POST",
    "path": "instagram-serrana",
    "responseMode": "onReceived",
    "options": {}
   },
   "webhookId": "a0d70965-0110-4184-b788-9c08a6a3dc94"
  },
  {
   "id": "a0137deb-5a69-4b26-ba22-cce82454ea7d",
   "name": "Unificar el mensaje",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    240,
    40
   ],
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "d4c63b0f-ee1c-4ed3-acdf-ba242884250f",
       "name": "canal",
       "value": "={{ $json.body.object === 'instagram' ? 'Instagram' : 'WhatsApp' }}",
       "type": "string"
      },
      {
       "id": "5e6a4dd5-410c-4eb9-a0cf-65c8e67fbd3a",
       "name": "contacto",
       "value": "={{ $json.body.object === 'instagram' ? $json.body.entry[0].messaging[0].sender.id : $json.body.entry[0].changes[0].value.messages[0].from }}",
       "type": "string"
      },
      {
       "id": "983ba87e-bea5-452c-a87d-48ce5ef2f51c",
       "name": "nombre",
       "value": "={{ $json.body.object === 'instagram' ? ($json.body.entry[0].messaging[0].sender.username || '') : ($json.body.entry[0].changes[0].value.contacts?.[0]?.profile?.name || '') }}",
       "type": "string"
      },
      {
       "id": "286f6ac4-58a9-4b12-bb2f-b14fffb7368e",
       "name": "texto",
       "value": "={{ $json.body.object === 'instagram' ? $json.body.entry[0].messaging[0].message.text : $json.body.entry[0].changes[0].value.messages[0].text.body }}",
       "type": "string"
      },
      {
       "id": "018b7520-3a82-44ea-8d25-ade35cfa3791",
       "name": "waApi",
       "value": "https://graph.facebook.com/v23.0",
       "type": "string"
      },
      {
       "id": "8b81c94a-69fb-47d9-b029-f5bc70603ca4",
       "name": "phoneNumberId",
       "value": "100000000000001",
       "type": "string"
      },
      {
       "id": "4a6705c1-88b0-4c24-80f0-65d798c6b3c5",
       "name": "igCuenta",
       "value": "17840000000000001",
       "type": "string"
      }
     ]
    },
    "includeOtherFields": true,
    "options": {}
   }
  },
  {
   "id": "3594c463-08bd-4402-81ee-0dc096529065",
   "name": "La IA entiende el mensaje",
   "type": "@n8n/n8n-nodes-langchain.informationExtractor",
   "typeVersion": 1.2,
   "position": [
    480,
    40
   ],
   "parameters": {
    "text": "={{ $json.texto }}",
    "schemaType": "fromAttributes",
    "attributes": {
     "attributes": [
      {
       "name": "intencion",
       "type": "string",
       "description": "Uno de: consulta de precio o stock, quiere comprar, cambio o reclamo, mayorista, otra",
       "required": true
      },
      {
       "name": "producto",
       "type": "string",
       "description": "Prenda que menciona, con el nombre del catálogo si coincide; vacío si no dice",
       "required": false
      },
      {
       "name": "talle",
       "type": "string",
       "description": "Talle que menciona; vacío si no dice",
       "required": false
      },
      {
       "name": "interes",
       "type": "number",
       "description": "Qué tan cerca está de comprar, de 1 (curiosea) a 5 (quiere pagar ya)",
       "required": true
      },
      {
       "name": "respuesta",
       "type": "string",
       "description": "Respuesta breve para el cliente, en español neutro y con los datos del catálogo; si pide algo que no está en el catálogo, dile que un vendedor le confirma. Sin inventar stock.",
       "required": true
      }
     ]
    },
    "options": {
     "systemPromptTemplate": "Atiendes los mensajes de Serrana, marca de ropa de lino y algodón. Catálogo: Camisa Tafí $ 64.000 · Camisa Traslasierra $ 72.000 · Camisa Alta Gracia (blanca) $ 69.000 · Blusa Nono $ 58.000 · Remera Básica $ 29.000 · Vestido Sauce $ 98.000 · Vestido Río $ 92.000 · Pantalón Nono $ 78.000 (salvia, terracota, mostaza, rosa, gris) · Saco Achala $ 128.000. Talles S a XL (pantalones 36 a 46). 6 cuotas sin interés, 10 % por transferencia, envío gratis desde $ 180.000. Mayoristas: mínimo 12 prendas."
    }
   }
  },
  {
   "id": "d6a953b7-ea92-42e7-9a8e-ab26e93988f8",
   "name": "Claude",
   "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
   "typeVersion": 1.6,
   "position": [
    500,
    260
   ],
   "parameters": {
    "model": {
     "__rl": true,
     "mode": "id",
     "value": "claude-sonnet-5-5"
    },
    "options": {
     "maxTokensToSample": 1200,
     "thinkingMode": "adaptive",
     "effort": "low"
    }
   },
   "credentials": {
    "anthropicApi": {
     "id": "",
     "name": "Claude (demos)"
    }
   }
  },
  {
   "id": "a8e53b58-98cb-43e6-a4bd-c2287e5dedb0",
   "name": "Vendedor y seguimiento",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    740,
    40
   ],
   "parameters": {
    "mode": "runOnceForEachItem",
    "jsCode": "\n// Quién lo atiende y cuándo se lo vuelve a contactar. Caliente = quiere comprar, es mayorista o la IA le da 4-5 de interés.\nconst m = $('Unificar el mensaje').item.json;\nconst d = $json.output || {};\nconst interes = Math.max(1, Math.min(5, Math.round(Number(d.interes) || 1)));\nconst VENDEDOR = { 'mayorista': 'Marcos (mayoristas)', 'cambio o reclamo': 'Atención al cliente' };\nconst caliente = d.intencion === 'quiere comprar' || d.intencion === 'mayorista' || interes >= 4;\nconst f = new Date(); f.setDate(f.getDate() + (caliente ? 0 : 2));\nreturn { ...m, ...d, interes, caliente, vendedor: VENDEDOR[d.intencion] || 'Julieta (ventas)', proximo_seguimiento: f.toISOString().slice(0, 10) };\n"
   }
  },
  {
   "id": "fdbc697d-4523-496d-9b19-39000095ca10",
   "name": "CRM · lead",
   "type": "n8n-nodes-base.dataTable",
   "typeVersion": 1.1,
   "position": [
    980,
    40
   ],
   "parameters": {
    "resource": "row",
    "operation": "upsert",
    "dataTableId": {
     "__rl": true,
     "mode": "id",
     "value": ""
    },
    "columns": {
     "mappingMode": "defineBelow",
     "value": {
      "canal": "={{ $json.canal }}",
      "contacto": "={{ $json.contacto }}",
      "nombre": "={{ $json.nombre }}",
      "intencion": "={{ $json.intencion }}",
      "producto": "={{ $json.producto }}",
      "talle": "={{ $json.talle }}",
      "interes": "={{ $json.interes }}",
      "vendedor": "={{ $json.vendedor }}",
      "proximo_seguimiento": "={{ $json.proximo_seguimiento }}",
      "ultimo_mensaje": "={{ $json.texto }}"
     },
     "matchingColumns": [],
     "schema": [
      {
       "id": "canal",
       "displayName": "canal",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "contacto",
       "displayName": "contacto",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "nombre",
       "displayName": "nombre",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "intencion",
       "displayName": "intencion",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "producto",
       "displayName": "producto",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "talle",
       "displayName": "talle",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "interes",
       "displayName": "interes",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "number",
       "canBeUsedToMatch": true
      },
      {
       "id": "vendedor",
       "displayName": "vendedor",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "proximo_seguimiento",
       "displayName": "proximo_seguimiento",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      },
      {
       "id": "ultimo_mensaje",
       "displayName": "ultimo_mensaje",
       "required": false,
       "defaultMatch": false,
       "display": true,
       "type": "string",
       "canBeUsedToMatch": true
      }
     ]
    },
    "options": {},
    "filters": {
     "conditions": [
      {
       "keyName": "contacto",
       "condition": "eq",
       "keyValue": "={{ $json.contacto }}"
      }
     ]
    }
   }
  },
  {
   "id": "e783d030-8601-4d67-bcd2-b541d2c0479d",
   "name": "¿Llegó por WhatsApp?",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.3,
   "position": [
    1220,
    -40
   ],
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "loose",
      "version": 2
     },
     "conditions": [
      {
       "id": "41a2ab99-0a76-4f1d-b4ca-a45f1b18f1d5",
       "leftValue": "={{ $('Vendedor y seguimiento').item.json.canal }}",
       "rightValue": "WhatsApp",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   }
  },
  {
   "id": "bf6d7357-121f-400b-be0c-89f69f4eee3b",
   "name": "Responder por WhatsApp",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1460,
    -140
   ],
   "parameters": {
    "method": "POST",
    "url": "={{ $('Vendedor y seguimiento').item.json.waApi }}/{{ $('Vendedor y seguimiento').item.json.phoneNumberId }}/messages",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ messaging_product: 'whatsapp', to: $('Vendedor y seguimiento').item.json.contacto, type: 'text', text: { body: $('Vendedor y seguimiento').item.json.respuesta } }) }}",
    "options": {},
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   }
  },
  {
   "id": "3ffda065-94a4-428d-a511-ae69a765523b",
   "name": "Responder por Instagram",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1460,
    40
   ],
   "parameters": {
    "method": "POST",
    "url": "={{ $('Vendedor y seguimiento').item.json.waApi }}/{{ $('Vendedor y seguimiento').item.json.igCuenta }}/messages",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ messaging_product: 'instagram', recipient: { id: $('Vendedor y seguimiento').item.json.contacto }, message: { text: $('Vendedor y seguimiento').item.json.respuesta } }) }}",
    "options": {},
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   }
  },
  {
   "id": "c8529f55-9945-41cf-8df0-0cdd42ad61b3",
   "name": "¿Es un lead caliente?",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.3,
   "position": [
    1220,
    200
   ],
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "loose",
      "version": 2
     },
     "conditions": [
      {
       "id": "7b2aefc9-5e62-418f-835d-a29ae136c7a7",
       "leftValue": "={{ $('Vendedor y seguimiento').item.json.caliente }}",
       "rightValue": "",
       "operator": {
        "type": "boolean",
        "operation": "true"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   }
  },
  {
   "id": "5626400f-3563-4b25-840f-9b7d7d3c1d0f",
   "name": "Aviso al vendedor por Telegram",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    1460,
    220
   ],
   "parameters": {
    "resource": "message",
    "operation": "sendMessage",
    "chatId": "5550300",
    "text": "={{ '<b>Lead caliente · ' + $('Vendedor y seguimiento').item.json.canal + '</b> → ' + $('Vendedor y seguimiento').item.json.vendedor + '\\n' + ($('Vendedor y seguimiento').item.json.nombre || $('Vendedor y seguimiento').item.json.contacto) + ': «' + $('Vendedor y seguimiento').item.json.texto + '»\\n' + $('Vendedor y seguimiento').item.json.intencion + ($('Vendedor y seguimiento').item.json.producto ? ' · ' + $('Vendedor y seguimiento').item.json.producto : '') + ($('Vendedor y seguimiento').item.json.talle ? ' · talle ' + $('Vendedor y seguimiento').item.json.talle : '') + ' · interés ' + $('Vendedor y seguimiento').item.json.interes + '/5' }}",
    "additionalFields": {
     "parse_mode": "HTML",
     "appendAttribution": false
    }
   },
   "credentials": {
    "telegramApi": {
     "id": "",
     "name": "Telegram (simulador)"
    }
   }
  }
 ],
 "connections": {
  "Mensaje de WhatsApp": {
   "main": [
    [
     {
      "node": "Unificar el mensaje",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Mensaje de Instagram": {
   "main": [
    [
     {
      "node": "Unificar el mensaje",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Unificar el mensaje": {
   "main": [
    [
     {
      "node": "La IA entiende el mensaje",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Claude": {
   "ai_languageModel": [
    [
     {
      "node": "La IA entiende el mensaje",
      "type": "ai_languageModel",
      "index": 0
     }
    ]
   ]
  },
  "La IA entiende el mensaje": {
   "main": [
    [
     {
      "node": "Vendedor y seguimiento",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Vendedor y seguimiento": {
   "main": [
    [
     {
      "node": "CRM · lead",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "CRM · lead": {
   "main": [
    [
     {
      "node": "¿Llegó por WhatsApp?",
      "type": "main",
      "index": 0
     },
     {
      "node": "¿Es un lead caliente?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "¿Llegó por WhatsApp?": {
   "main": [
    [
     {
      "node": "Responder por WhatsApp",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Responder por Instagram",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "¿Es un lead caliente?": {
   "main": [
    [
     {
      "node": "Aviso al vendedor por Telegram",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1",
  "timezone": "America/Argentina/Buenos_Aires",
  "saveManualExecutions": true
 }
}