{
  "id": "Y7rM4PEAHjh3zefB",
  "name": "Dialogflow Intent Exporter",
  "tags": [],
  "nodes": [
    {
      "id": "2ee1d52e-e143-47e1-b3bc-22d0fd17528e",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        64,
        128
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "4e8fe8a7-13e3-4cd9-9670-6e5a720da072",
      "name": "A\u00f1adir fila en la hoja",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1600,
        176
      ],
      "parameters": {
        "columns": {
          "value": {
            "Nombre": "={{ $json.Nombre }}",
            "Prioridad": "={{ $json.textoPrioridad }}",
            "Hora de registro": "={{$now.format('HH:mm:ss') }}",
            "Fecha de registro": "={{$now.format('dd/MM/yyyy') }}",
            "Color de prioridad": "={{ $json.colorPrioridad }}",
            "Valor de prioridad": "={{ $json.prioridad }}"
          },
          "schema": [
            {
              "id": "Nombre",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Nombre",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Prioridad",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Prioridad",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Color de prioridad",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Color de prioridad",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Valor de prioridad",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Valor de prioridad",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Fecha de registro",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Fecha de registro",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Hora de registro",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Hora de registro",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1v9VHfyEQSWYZxKCMEal0uT5_z6gV1Qx4EIwgr6ShN6E/edit#gid=0",
          "cachedResultName": "intents-nombres"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1v9VHfyEQSWYZxKCMEal0uT5_z6gV1Qx4EIwgr6ShN6E",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1v9VHfyEQSWYZxKCMEal0uT5_z6gV1Qx4EIwgr6ShN6E/edit?usp=drivesdk",
          "cachedResultName": "Listado-intents"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "63227264-c9f0-4c5a-aea1-a22e99ece365",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 896,
        "height": 480,
        "content": "## Se valida el usuario y comando enviado"
      },
      "typeVersion": 1
    },
    {
      "id": "66dfded1-7af6-45f9-98ae-7fdedd75a644",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        944,
        80
      ],
      "parameters": {
        "width": 496,
        "height": 272,
        "content": "## Se obtiene la informaci\u00f3n de los intents y se procesa"
      },
      "typeVersion": 1
    },
    {
      "id": "4ebfb516-ff02-4c53-9fee-82676ff3b3eb",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1488,
        80
      ],
      "parameters": {
        "color": 4,
        "width": 576,
        "height": 272,
        "content": "## Registro en Sheets y confirmaci\u00f3n por Telegram"
      },
      "typeVersion": 1
    },
    {
      "id": "72b44433-2b77-4bc3-a31b-cbc04f3ecfd5",
      "name": "Validaci\u00f3n de usuario por ID",
      "type": "n8n-nodes-base.if",
      "position": [
        288,
        128
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3f4c5e75-e48d-450a-896b-4bd396851417",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.message.from.id }}",
              "rightValue": 7894561234
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ca22c2d9-821a-4f15-8011-2e9f02361cc6",
      "name": "Validaci\u00f3n del comando",
      "type": "n8n-nodes-base.if",
      "position": [
        480,
        32
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "30b2659a-c708-4cb4-8576-913182ec1e65",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "backup"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a383d91d-134c-43c1-b48b-e5d8024ff831",
      "name": "Mensaje de validaci\u00f3n de comando",
      "type": "n8n-nodes-base.telegram",
      "position": [
        624,
        160
      ],
      "parameters": {
        "text": "Palabra inv\u00e1lida",
        "chatId": "={{ $json.message.from.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "778342fd-e973-4c30-b1d6-6fbd79187c5d",
      "name": "Obtiene datos de los intents",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1040,
        176
      ],
      "parameters": {
        "url": "https://dialogflow.googleapis.com/v2/projects/TU_PROJECT_ID/agent/intents",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleApi"
      },
      "typeVersion": 4.2
    },
    {
      "id": "730f58b8-bb78-471b-bde0-ba1a10ee8b24",
      "name": "Mensaje de confirmaci\u00f3n",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1824,
        176
      ],
      "parameters": {
        "text": "=\u00a0Se registraron {{$items(\"A\u00f1adir fila en la hoja\").length}} intents en Google Sheets.",
        "chatId": "={{ $('Telegram Trigger').item.json.message.from.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "executeOnce": true,
      "typeVersion": 1.2
    },
    {
      "id": "7d18b6fa-39c1-46c4-8578-f90b357f3f36",
      "name": "Mensaje de usuario inv\u00e1lido",
      "type": "n8n-nodes-base.telegram",
      "position": [
        448,
        288
      ],
      "parameters": {
        "text": "Usuario inv\u00e1lido",
        "chatId": "={{ $('Telegram Trigger').item.json.message.from.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d925126b-f8c9-4c68-836b-bfe769c70c57",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        -272
      ],
      "parameters": {
        "color": 6,
        "width": 352,
        "height": 1008,
        "content": "## \ud83d\udd04 Flujo explicado\n\n### \ud83c\udfaf Trigger (Telegram)\n- **Se activa autom\u00e1ticamente** al recibir *cualquier mensaje* en el bot de Telegram.\n\n### \ud83d\udee1\ufe0f Validaci\u00f3n de Usuario y Comando\n- **Se valida el ID** del usuario remitente. (Reemplaza el nro de 10 d\u00edgitos por un id real)\n- **Se verifica** que el mensaje recibido sea exactamente **\"backup\"**.\n- \u274c En caso de error, **se env\u00eda un mensaje indicando que el usuario o el comando es inv\u00e1lido**.\n\n### \ud83c\udf10 Solicitud HTTP a Dialogflow\n- **Se consulta la API de Dialogflow** para obtener todos los intents configurados en el agente.\n\n### \ud83e\udde0 Nodo de C\u00f3digo (JavaScript)\n- **Se transforma el JSON** recibido desde Dialogflow.\n- **Se extraen** los valores `displayName` y `priority` de cada intent.\n- **Se asigna un emoji y texto descriptivo** dependiendo del nivel de prioridad:\n  - \ud83d\udd34 Highest\n  - \ud83d\udfe0 High\n  - \ud83d\udd35 Normal\n  - \ud83d\udfe2 Low\n  - \ud83d\udeab Ignore\n\n### \ud83d\udcc4 Registro en Google Sheets\n- **Se agrega cada intent** como una fila en la hoja de c\u00e1lculo.\n- **Se registran tambi\u00e9n la fecha y hora** de procesamiento.\n\n### \u2705 Confirmaci\u00f3n por Telegram\n- **Se env\u00eda un mensaje de confirmaci\u00f3n** con el total de intents registrados.\n- Este mensaje se env\u00eda **una sola vez** usando la opci\u00f3n `Execute Once`.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "ac3bf08d-e601-445d-9e48-13bb6a676f36",
      "name": "Mapear intents con su prioridad",
      "type": "n8n-nodes-base.code",
      "position": [
        1264,
        176
      ],
      "parameters": {
        "jsCode": "// 1\ufe0f\u20e3 Obtenemos el array de intents del primer item recibido por este nodo.\n// Si la propiedad \"intents\" no existe, devolvemos un array vac\u00edo para evitar errores.\nconst data = $input.first().json.intents || [];\n\n// 2\ufe0f\u20e3 Funci\u00f3n que eval\u00faa la prioridad num\u00e9rica de un intent\n// y devuelve un objeto con:\n//   - emoji: Representaci\u00f3n visual del nivel de prioridad\n//   - texto: Nombre descriptivo del nivel de prioridad\nfunction obtenerPrioridad(priority) {\n  if (priority >= 1000000) return { emoji: '\ud83d\udd34', texto: 'Highest' }; // Prioridad m\u00e1xima\n  if (priority >= 750000)  return { emoji: '\ud83d\udfe0', texto: 'High' };    // Prioridad alta\n  if (priority >= 500000)  return { emoji: '\ud83d\udd35', texto: 'Normal' };  // Prioridad normal\n  if (priority >= 250000)  return { emoji: '\ud83d\udfe2', texto: 'Low' };     // Prioridad baja\n  return { emoji: '\ud83d\udeab', texto: 'Ignore' };                           // Ignorado o desactivado\n}\n\n// 3\ufe0f\u20e3 Recorremos cada intent y lo convertimos en un item compatible con n8n.\n// Cada item incluir\u00e1:\n//   - Nombre: El displayName del intent\n//   - prioridad: Valor num\u00e9rico de la prioridad del intent\n//   - colorPrioridad: Emoji que representa la prioridad\n//   - textoPrioridad: Descripci\u00f3n textual de la prioridad\nconst intents = data.map(intent => {\n  \n  // Extrae la informaci\u00f3n de prioridad del intent actual.\n  // Si la propiedad \"priority\" no existe, usamos 0 como valor por defecto para evitar errores.\n  const prioridadInfo = obtenerPrioridad(intent.priority || 0);\n  \n  // Construye el item final que n8n espera: { json: {...} }\n  return {\n    json: {\n      Nombre: intent.displayName,\n      prioridad: intent.priority,\n      colorPrioridad: prioridadInfo.emoji,\n      textoPrioridad: prioridadInfo.texto\n    }\n  };\n});\n\n// 4\ufe0f\u20e3 Devolvemos el array de items para que n8n los procese.\n// Cada item ser\u00e1 una fila independiente si lo conectas, por ejemplo, a Google Sheets.\nreturn intents;\n"
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "5b987344-93a5-44ed-a9d7-77206db9ee97",
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Validaci\u00f3n de usuario por ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "A\u00f1adir fila en la hoja": {
      "main": [
        [
          {
            "node": "Mensaje de confirmaci\u00f3n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validaci\u00f3n del comando": {
      "main": [
        [
          {
            "node": "Obtiene datos de los intents",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mensaje de validaci\u00f3n de comando",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Obtiene datos de los intents": {
      "main": [
        [
          {
            "node": "Mapear intents con su prioridad",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validaci\u00f3n de usuario por ID": {
      "main": [
        [
          {
            "node": "Validaci\u00f3n del comando",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mensaje de usuario inv\u00e1lido",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mapear intents con su prioridad": {
      "main": [
        [
          {
            "node": "A\u00f1adir fila en la hoja",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}