Kodflux
Kodflux
Crear cuenta gratisIniciar Sesión

JSON / Basico / 4 min

n8n Workflow: Webhook a Slack con IA

Workflow n8n que recibe un webhook, procesa texto con GPT-4o y publica el resultado formateado en Slack.

Este workflow conecta cualquier evento externo con un análisis de IA y una notificación en Slack.

Un caso típico: un cliente llena un formulario, n8n recibe el webhook, GPT-4o califica la solicitud y Slack recibe un resumen con prioridad, categoría y score.

Guía de Implementación Paso a Paso

  1. Instala n8n self-hosted con Docker.
BASH
docker volume create n8n_data
docker run -it --rm --name n8n \
  -p 5678:5678 \
  -v n8n_data:/home/node/.n8n \
  docker.n8n.io/n8nio/n8n
  1. Entra a http://localhost:5678.
  2. Importa el workflow desde Workflows > Import from JSON.
  3. Configura credenciales de OpenAI y Slack.
  4. Activa el workflow.
  5. Prueba el webhook con un POST.
BASH
curl -X POST https://tu-n8n.com/webhook/analizar \
  -H "Content-Type: application/json" \
  -d '{"mensaje": "Necesito integrar WhatsApp con mi CRM urgente"}'
JSON
{
  "name": "Webhook → OpenAI → Slack",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "analizar",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [250, 300]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "model": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "Eres un asistente de calificación de leads. Analiza el mensaje recibido y responde SOLO con un JSON con estos campos: {\"resumen\":\"máx 50 palabras\",\"prioridad\":\"ALTA|MEDIA|BAJA\",\"categoria\":\"categoria del pedido\",\"score\":número del 1 al 10,\"accion_recomendada\":\"qué hacer\"}. Sin explicaciones, solo JSON."
            },
            {
              "role": "user",
              "content": "={{ $json.body.mensaje }}"
            }
          ]
        },
        "options": {
          "temperature": 0.3,
          "maxTokens": 300
        }
      },
      "id": "openai-analyze",
      "name": "Analizar con GPT-4o",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.4,
      "position": [500, 300],
      "credentials": {
        "openAiApi": {
          "id": "REEMPLAZA_CON_ID_CREDENCIAL",
          "name": "OpenAI API"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json.choices[0].message.content;\nlet analysis;\ntry {\n  analysis = JSON.parse(raw);\n} catch(e) {\n  analysis = { resumen: raw, prioridad: 'MEDIA', score: 5, categoria: 'General', accion_recomendada: 'Revisar manualmente' };\n}\n\nconst emoji = { ALTA: '🔴', MEDIA: '🟡', BAJA: '🟢' }[analysis.prioridad] || '⚪';\n\nreturn [{\n  json: {\n    ...analysis,\n    emoji,\n    mensaje_original: $('Webhook').first().json.body.mensaje,\n    timestamp: new Date().toISOString()\n  }\n}];"
      },
      "id": "format-data",
      "name": "Formatear datos",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [750, 300]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "REEMPLAZA_CON_ID_CANAL",
          "mode": "id"
        },
        "messageType": "block",
        "blocksUi": {
          "blocksValues": [
            {
              "type": "header",
              "text": {
                "type": "plain_text",
                "text": "={{ $json.emoji + ' Nuevo mensaje calificado — Score: ' + $json.score + '/10' }}"
              }
            },
            {
              "type": "section",
              "fields": [
                {
                  "type": "mrkdwn",
                  "text": "*Prioridad:*\n={{ $json.prioridad }}"
                },
                {
                  "type": "mrkdwn",
                  "text": "*Categoría:*\n={{ $json.categoria }}"
                }
              ]
            },
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": "*Resumen:*\n={{ $json.resumen }}"
              }
            },
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": "*Acción recomendada:*\n={{ $json.accion_recomendada }}"
              }
            }
          ]
        },
        "otherOptions": {}
      },
      "id": "slack-notify",
      "name": "Notificar en Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [1000, 300],
      "credentials": {
        "slackApi": {
          "id": "REEMPLAZA_CON_ID_CREDENCIAL_SLACK",
          "name": "Slack API"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\"status\": \"ok\", \"score\": {{ $('Formatear datos').first().json.score }}, \"prioridad\": \"{{ $('Formatear datos').first().json.prioridad }}\"}"
      },
      "id": "respond-webhook",
      "name": "Responder Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1000, 480]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [[{ "node": "Analizar con GPT-4o", "type": "main", "index": 0 }]]
    },
    "Analizar con GPT-4o": {
      "main": [[{ "node": "Formatear datos", "type": "main", "index": 0 }]]
    },
    "Formatear datos": {
      "main": [
        [{ "node": "Notificar en Slack", "type": "main", "index": 0 }],
        [{ "node": "Responder Webhook", "type": "main", "index": 0 }]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}

Prompt para Codex

TEXT
Genera un workflow JSON de n8n que conecte Webhook → OpenAI GPT-4o → Slack.

Nodos requeridos:
1. Webhook (POST /analizar) — recibe { "mensaje": "texto" }
2. OpenAI Chat Completion — modelo gpt-4o, temperature 0.3, max_tokens 300
3. Code node (JavaScript) — parsea el JSON de OpenAI, asigna emoji por prioridad, agrega timestamp
4. Slack — envía Block Kit con header, fields para prioridad y categoría, sections para resumen y acción
5. Respond to Webhook — responde con { status: ok, score, prioridad }

Formato: JSON válido de n8n importable directamente. Usa typeVersion 2 para Webhook. Incluye placeholders REEMPLAZA_CON_ID_CREDENCIAL donde se necesitan credenciales.

Buscar en Kodflux