{
  "name": "Pipeline Avanzado de Leads por WhatsApp con IA",
  "flow": [
    {
      "id": 1,
      "module": "whatsapp-business:watchMessages",
      "version": 1,
      "parameters": {
        "phoneNumberId": "{{WHATSAPP_PHONE_ID}}",
        "limit": 10
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "phoneNumberId", "type": "text", "label": "Phone Number ID" }
        ]
      }
    },
    {
      "id": 2,
      "module": "openai:createChatCompletion",
      "version": 1,
      "parameters": {
        "apiKey": "{{OPENAI_API_KEY}}",
        "model": "gpt-4o-mini",
        "temperature": 0.3,
        "maxTokens": 500
      },
      "mapper": {
        "messages": [
          {
            "role": "system",
            "content": "Eres un asistente de ventas. Analiza el siguiente mensaje de un prospecto y devuelve SOLO un JSON con los campos: intencion_compra (alta/media/baja), urgencia (inmediata/esta_semana/sin_urgencia), categoria_producto (string), resumen (string de max 50 palabras), clasificacion (caliente/tibio/frio)."
          },
          {
            "role": "user",
            "content": "{{1.text}}"
          }
        ],
        "responseFormat": { "type": "json_object" }
      },
      "metadata": {
        "designer": { "x": 300, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "apiKey", "type": "text", "label": "API Key" },
          { "name": "model", "type": "select", "label": "Model" }
        ]
      }
    },
    {
      "id": 3,
      "module": "json:ParseJSON",
      "version": 1,
      "parameters": {},
      "mapper": {
        "json": "{{2.choices[0].message.content}}"
      },
      "metadata": {
        "designer": { "x": 600, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "json", "type": "text", "label": "JSON String" }
        ]
      }
    },
    {
      "id": 4,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "mapper": {},
      "routes": [
        {
          "label": "Lead Caliente",
          "filter": { "conditions": [{ "a": "{{3.clasificacion}}", "o": "text:equal", "b": "caliente" }] },
          "flow": [5, 7]
        },
        {
          "label": "Lead Tibio",
          "filter": { "conditions": [{ "a": "{{3.clasificacion}}", "o": "text:equal", "b": "tibio" }] },
          "flow": [6]
        },
        {
          "label": "Lead Frío",
          "flow": [6]
        }
      ],
      "metadata": {
        "designer": { "x": 900, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 5,
      "module": "airtable:createRecord",
      "version": 2,
      "parameters": {
        "baseId": "{{AIRTABLE_BASE_ID}}",
        "tableId": "Leads Calientes"
      },
      "mapper": {
        "fields": {
          "Nombre": "{{1.senderName}}",
          "Teléfono": "{{1.senderPhone}}",
          "Mensaje": "{{1.text}}",
          "Intención": "{{3.intencion_compra}}",
          "Urgencia": "{{3.urgencia}}",
          "Categoría": "{{3.categoria_producto}}",
          "Resumen IA": "{{3.resumen}}",
          "Clasificación": "Caliente",
          "Fecha": "{{formatDate(now; \"YYYY-MM-DD HH:mm:ss\")}}",
          "Estado": "Pendiente contacto"
        }
      },
      "metadata": {
        "designer": { "x": 1200, "y": -200 },
        "restore": {
          "baseId": { "label": "CRM Leads" },
          "tableId": { "label": "Leads Calientes" }
        },
        "expect": [
          { "name": "baseId", "type": "text", "label": "Base ID" },
          { "name": "tableId", "type": "text", "label": "Table" }
        ]
      }
    },
    {
      "id": 6,
      "module": "airtable:createRecord",
      "version": 2,
      "parameters": {
        "baseId": "{{AIRTABLE_BASE_ID}}",
        "tableId": "Leads General"
      },
      "mapper": {
        "fields": {
          "Nombre": "{{1.senderName}}",
          "Teléfono": "{{1.senderPhone}}",
          "Mensaje": "{{1.text}}",
          "Intención": "{{3.intencion_compra}}",
          "Urgencia": "{{3.urgencia}}",
          "Categoría": "{{3.categoria_producto}}",
          "Resumen IA": "{{3.resumen}}",
          "Clasificación": "{{3.clasificacion}}",
          "Fecha": "{{formatDate(now; \"YYYY-MM-DD HH:mm:ss\")}}"
        }
      },
      "metadata": {
        "designer": { "x": 1200, "y": 100 },
        "restore": {
          "baseId": { "label": "CRM Leads" },
          "tableId": { "label": "Leads General" }
        },
        "expect": [
          { "name": "baseId", "type": "text", "label": "Base ID" },
          { "name": "tableId", "type": "text", "label": "Table" }
        ]
      }
    },
    {
      "id": 7,
      "module": "whatsapp-business:sendMessage",
      "version": 1,
      "parameters": {
        "phoneNumberId": "{{WHATSAPP_PHONE_ID}}",
        "recipientPhone": "{{1.senderPhone}}"
      },
      "mapper": {
        "type": "text",
        "text": "¡Hola {{1.senderName}}! 👋 Gracias por contactarnos. He revisado tu consulta sobre {{3.categoria_producto}} y quiero ayudarte personalmente. Un asesor se comunicará contigo en los próximos minutos. ¿Hay algo específico que necesites mientras tanto?"
      },
      "metadata": {
        "designer": { "x": 1500, "y": -200 },
        "restore": {},
        "expect": [
          { "name": "phoneNumberId", "type": "text", "label": "Phone Number ID" },
          { "name": "recipientPhone", "type": "text", "label": "Recipient Phone" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}