{
  "nodes": [
    {
      "parameters": {
        "botToken": "=<YOUR_TELEGRAM_BOT_TOKEN>",
        "path": "webhook",
        "chatId": "={{ $json.message.chat.id }}",
        "text": "Hello! How can I help you today?"
      },
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        240,
        280
      ]
    },
    {
      "parameters": {
        "model": "gemini-pro",
        "prompt": "You are an eBook sales assistant. Your task is to understand the user's query about our books. Identify if they are asking for a specific book, a category, prices, or a general question. If searching for a book, extract the title or category. If it's a greeting, respond kindly. If it's a purchase question, indicate it. Provide your response in JSON format with 'intent' (e.g., 'search_book', 'greeting', 'purchase_question', 'generic') and 'detail' (e.g., 'fantasy', 'book title', 'specific question').\n\nUser query: {{ $json.message.text }}",
        "options": {}
      },
      "name": "Gemini: Process Query",
      "type": "n8n-nodes-base.googleGenerativeAi",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        460,
        280
      ]
    },
    {
      "parameters": {
        "conditions": [
          {
            "value1": "={{ $json.intent }}",
            "value2": "=search_book",
            "type": "string",
            "operator": "stringEqual"
          }
        ]
      },
      "name": "IF: Intent is 'search_book'",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        700,
        180
      ]
    },
    {
      "parameters": {
        "conditions": [
          {
            "value1": "={{ $json.intent }}",
            "value2": "=greeting",
            "type": "string",
            "operator": "stringEqual"
          }
        ]
      },
      "name": "IF: Intent is 'greeting'",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        700,
        340
      ]
    },
    {
      "parameters": {
        "conditions": [
          {
            "value1": "={{ $json.intent }}",
            "value2": "=purchase_question",
            "type": "string",
            "operator": "stringEqual"
          }
        ]
      },
      "name": "IF: Intent is 'purchase_question'",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        700,
        500
      ]
    },
    {
      "parameters": {
        "conditions": [
          {
            "value1": "={{ $json.intent }}",
            "value2": "=generic",
            "type": "string",
            "operator": "stringEqual"
          }
        ]
      },
      "name": "IF: Intent is 'generic'",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        700,
        660
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "spreadsheetId": "<YOUR_GOOGLE_SHEET_ID>",
        "sheetName": "<YOUR_SHEET_NAME>",
        "options": {}
      },
      "name": "Google Sheets: Get eBooks",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        920,
        180
      ],
      "disabled": false
    },
    {
      "parameters": {
        "functionCode": "const searchTerm = $json.detail.toLowerCase();\nconst ebooks = $input.item.json.data;\n\nlet responseMessage = 'Lo siento, no pude encontrar eBooks que coincidan con \"' + searchTerm + '\". \u00bfHay algo m\u00e1s en lo que pueda ayudarte?';\nlet foundEbooks = [];\n\nif (ebooks && Array.isArray(ebooks)) {\n  foundEbooks = ebooks.filter(book => \n    (book.Titulo && book.Titulo.toLowerCase().includes(searchTerm)) ||\n    (book.Categoria && book.Categoria.toLowerCase().includes(searchTerm))\n  );\n\n  if (foundEbooks.length > 0) {\n    responseMessage = 'Aqu\u00ed est\u00e1n los eBooks que encontr\u00e9:\\n';\n    foundEbooks.forEach(book => {\n      responseMessage += `\\n* ${book.Titulo} de ${book.Autor}`; \n      if (book.Precio) responseMessage += ` - Precio: ${book.Precio}`; \n      if (book.Descripcion) responseMessage += `\\n  Descripci\u00f3n: ${book.Descripcion.substring(0, 100)}...`;\n      if (book.EnlaceDescarga) responseMessage += `\\n  Enlace: ${book.EnlaceDescarga}`; \n      responseMessage += '\\n';\n    });\n  } else {\n    responseMessage = 'No encontr\u00e9 eBooks que coincidan con \"' + searchTerm + '\". \u00bfTe gustar\u00eda buscar por otra cosa?';\n  }\n}\n\nreturn [{ json: { text: responseMessage } }];",
        "options": {}
      },
      "name": "Code: Format eBook Response",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        1160,
        180
      ],
      "disabled": false
    },
    {
      "parameters": {
        "functionCode": "return [{ json: { text: '\u00a1Hola! Soy tu asistente de ventas de eBooks. \u00bfEn qu\u00e9 puedo ayudarte hoy?' } }];",
        "options": {}
      },
      "name": "Code: Greeting Message",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        920,
        340
      ],
      "disabled": false
    },
    {
      "parameters": {
        "functionCode": "return [{ json: { text: 'Para preguntas sobre compras o m\u00e9todos de pago, por favor, env\u00edanos un correo a ventas@tusebooks.com o visita nuestra secci\u00f3n de preguntas frecuentes.' } }];",
        "options": {}
      },
      "name": "Code: Purchase Question Message",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        920,
        500
      ],
      "disabled": false
    },
    {
      "parameters": {
        "functionCode": "return [{ json: { text: 'Lo siento, no entend\u00ed tu pregunta. \u00bfPodr\u00edas ser m\u00e1s espec\u00edfico sobre lo que buscas o qu\u00e9 eBook te interesa?' } }];",
        "options": {}
      },
      "name": "Code: Generic Message",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        920,
        660
      ],
      "disabled": false
    },
    {
      "parameters": {
        "chatId": "={{ $json.message.chat.id }}",
        "text": "={{ $json.text }}"
      },
      "name": "Telegram: Send Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "workflowId": null,
      "position": [
        1400,
        420
      ]
    }
  ],
  "connections": [
    {
      "from": "Telegram Trigger",
      "to": "Gemini: Process Query",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "Gemini: Process Query",
      "to": "IF: Intent is 'search_book'",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'search_book'",
      "to": "Google Sheets: Get eBooks",
      "fromHandle": "true",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'search_book'",
      "to": "IF: Intent is 'greeting'",
      "fromHandle": "false",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'greeting'",
      "to": "Code: Greeting Message",
      "fromHandle": "true",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'greeting'",
      "to": "IF: Intent is 'purchase_question'",
      "fromHandle": "false",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'purchase_question'",
      "to": "Code: Purchase Question Message",
      "fromHandle": "true",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'purchase_question'",
      "to": "IF: Intent is 'generic'",
      "fromHandle": "false",
      "toHandle": "main"
    },
    {
      "from": "IF: Intent is 'generic'",
      "to": "Code: Generic Message",
      "fromHandle": "true",
      "toHandle": "main"
    },
    {
      "from": "Google Sheets: Get eBooks",
      "to": "Code: Format eBook Response",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "Code: Format eBook Response",
      "to": "Telegram: Send Response",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "Code: Greeting Message",
      "to": "Telegram: Send Response",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "Code: Purchase Question Message",
      "to": "Telegram: Send Response",
      "fromHandle": "main",
      "toHandle": "main"
    },
    {
      "from": "Code: Generic Message",
      "to": "Telegram: Send Response",
      "fromHandle": "main",
      "toHandle": "main"
    }
  ]
}