{
  "name": "Zenius Telegram Assistant",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {
          "useWebhook": true
        }
      },
      "id": "Telegram Trigger",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Normalizar entrada de Telegram\nconst upd = items[0].json;\nconst msg = upd.message || {};\nconst chatId = msg.chat?.id;\nconst text = (msg.text || '').trim().slice(0, 2000);\nconst name = msg.from?.first_name || '';\n\n// Comandos b\u00e1sicos\nif (text === '/start') {\n  return [{ json: { kind: 'command', reply: `\u00a1Hola ${name}! Soy el asistente de Zenius Automations. Preg\u00fantame sobre automatizaci\u00f3n, agentes de IA o integraci\u00f3n con WhatsApp. Escribe /ayuda para ver opciones.`, chatId } }];\n}\nif (text === '/ayuda') {\n  return [{ json: { kind: 'command', reply: 'Comandos:\\n/start \u2013 iniciar\\n/ayuda \u2013 ver ayuda\\n/demo \u2013 solicitar una demo\\n/contacto \u2013 dejar tus datos', chatId } }];\n}\nif (text === '/demo') {\n  return [{ json: { kind: 'command', reply: 'Genial. P\u00e1same en un solo mensaje: Nombre, email y empresa. Ejemplo:\\nNombre: Ana P\u00e9rez\\nEmail: ana@empresa.com\\nEmpresa: ACME S.L.', chatId } }];\n}\nif (text === '/contacto') {\n  return [{ json: { kind: 'command', reply: 'D\u00e9jame tus datos de contacto en un solo mensaje: Nombre, email y empresa. Te escribimos pronto.', chatId } }];\n}\n\n// Anti-spam sencillo (1 msg/2s por chat)\nconst now = Date.now();\nconst key = `rate_${chatId}`;\nconst last = $flow.get(key) || 0;\nif (now - last < 2000) {\n  return [{ json: { kind: 'rate', reply: 'Voy un poco lento, dame un segundo\u2026', chatId } }];\n}\n$flow.set(key, now);\n\nreturn [{ json: { kind: 'chat', chatId, userText: text, name } }];"
      },
      "name": "Prepare Input",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.kind}}",
              "operation": "equals",
              "value2": "chat"
            }
          ]
        }
      },
      "name": "If Chat",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        280
      ]
    },
    {
      "parameters": {
        "functionCode": "// Prompt del sistema + usuario\nconst sys = `Eres el asistente de Zenius Automations. Ayudas en espa\u00f1ol de Espa\u00f1a, tono claro y cercano. Si detectas intenci\u00f3n de compra, pide en una sola pregunta: Nombre, email y empresa. Ofrece demo breve si procede.`;\nconst user = $json.userText || '';\nreturn [{ json: { messages: [ {role: 'system', content: sys}, {role: 'user', content: user} ], chatId: $json.chatId } }];"
      },
      "name": "Build Prompt",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        940,
        220
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "model": "gpt-4o-mini",
        "messages": "={{$json.messages}}",
        "responseFormat": "{\"mode\":\"text\"}"
      },
      "name": "OpenAI Chat",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 4,
      "position": [
        1180,
        220
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Extraer texto del LLM\nconst text = $json.choices?.[0]?.message?.content || 'No he podido generar respuesta.';\nreturn [{ json: { reply: text, chatId: $json.chatId } }];"
      },
      "name": "Format Reply",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1420,
        220
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.chatId}}",
        "text": "={{$json.reply}}"
      },
      "name": "Telegram Reply (LLM)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1660,
        220
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.kind}}",
              "operation": "notEqual",
              "value2": "chat"
            }
          ]
        }
      },
      "name": "If Command/Rate",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        940,
        420
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.chatId}}",
        "text": "={{$json.reply}}"
      },
      "name": "Telegram Reply (Cmd)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1180,
        520
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Prepare Input": {
      "main": [
        [
          {
            "node": "If Chat",
            "type": "main",
            "index": 0
          },
          {
            "node": "If Command/Rate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Chat": {
      "main": [
        [
          {
            "node": "Build Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Prompt": {
      "main": [
        [
          {
            "node": "OpenAI Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat": {
      "main": [
        [
          {
            "node": "Format Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Reply": {
      "main": [
        [
          {
            "node": "Telegram Reply (LLM)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Command/Rate": {
      "main": [
        [
          {
            "node": "Telegram Reply (Cmd)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}