{
  "name": "2 - Chatbot Telegram SRI",
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.message.text }}",
        "options": {
          "systemMessage": "0. REGLA DE REINICIO: Si el mensaje del usuario es exactamente \"/start\" o \"/reiniciar\", IGNORA todo el historial de conversaci\u00f3n anterior y NO uses ninguna herramienta. Responde \u00fanicamente con un saludo cordial de bienvenida como asistente de Ferreter\u00eda Proferret y pregunta en qu\u00e9 puedes ayudarle hoy.\n\nREGLAS GENERALES Y DE FORMATO:\n\n1. HERRAMIENTAS: Al usar la herramienta \"buscar_factura\", ignora cualquier contenido largo, XML, PDF o texto en base64. Extrae \u00fanicamente: nombre del cliente, RUC/c\u00e9dula, fecha, categor\u00eda y valor total. Muestra un M\u00c1XIMO de 3 facturas por respuesta.\n\n2. FORMATO DE RESPUESTA: Presenta la informaci\u00f3n limpia y legible usando este esquema exacto (NO incluyas la clave de acceso de 49 d\u00edgitos en el resumen):\n\nSe encontraron las siguientes facturas:\n\n1. \ud83d\udcc4 Factura - [nombre]\n   \u2022 RUC/C\u00e9dula: [identificacion]\n   \u2022 Fecha: [AAAA-MM-DD]\n   \u2022 Categor\u00eda: [categoria]\n   \u2022 Total: $[valor]\n\n---\n(L\u00ednea de separaci\u00f3n entre cada factura)\n\n\u00bfDesea consultar la clave de acceso o enviar alguna por correo?",
          "maxIterations": 5
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        -64,
        -272
      ],
      "id": "b7dc45af-a009-4226-a921-fc1a78787077",
      "name": "AI Agent1"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}-v3",
        "contextWindowLength": 2
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [
        16,
        -112
      ],
      "id": "ab932397-5c5c-41be-b77b-0b2c68bdaa0e",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.3,
      "position": [
        -272,
        -272
      ],
      "id": "a6977616-f690-44c9-8d59-c006bf1aae1c",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "toolDescription": "\u00dasala para consultar la lista de comprobantes autorizados y buscar facturas por el nombre del cliente, RUC, c\u00e9dula, fecha o categor\u00eda. Retorna los datos clave y la clave de acceso de los comprobantes.",
        "url": "=http://localhost:4000/api/sri/comprobantes-ia",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "3"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequestTool",
      "typeVersion": 4.4,
      "position": [
        256,
        -80
      ],
      "id": "0b94cd10-c5e3-4776-8576-ea643926ff0b",
      "name": "buscar_factura"
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "\u00dasala para reenviar o enviar el correo electr\u00f3nico con los detalles o comprobante de la factura a la direcci\u00f3n indicada por el usuario. IMPORTANTE: \u00dasala SOLO UNA VEZ por solicitud. Una vez enviada, detente y confirma al usuario por Telegram.",
        "fromEmail": "kam.santana@yavirac.edu.ec",
        "toEmail": "={{ $fromAI(\"toEmail\", \"El correo electr\u00f3nico del cliente\") }}",
        "subject": "={{ $fromAI(\"subject\", \"El asunto del correo\") }}",
        "emailFormat": "text",
        "text": "={{ $fromAI(\"body\", \"Escribe aqu\u00ed el resumen detallado de la factura incluyendo obligatoriamente: Nombre del cliente, RUC/C\u00e9dula, Fecha, Categor\u00eda y Valor total extra\u00eddos del historial de la conversaci\u00f3n.\") }}",
        "options": {}
      },
      "type": "n8n-nodes-base.emailSendTool",
      "typeVersion": 2.1,
      "position": [
        128,
        -80
      ],
      "id": "4505fb9b-7e47-4958-bccf-bcd629b1b615",
      "name": "enviar_correo",
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot8943655662:AAEfl1dxsEOd3e2Qs0Y4hYZrMsY7XbUr-MQ/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"chat_id\": {{ $('Telegram Trigger').item.json.message.chat.id }},\n  \"text\": {{ JSON.stringify($json.output) }}\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        256,
        -272
      ],
      "id": "e36849d4-3443-433a-818a-cfe971c1b5fd",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        -176,
        -64
      ],
      "id": "2ebbcc9f-e52f-4c3f-8734-21d328db534c",
      "name": "Groq Chat Model",
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "AI Agent1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "AI Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "buscar_factura": {
      "ai_tool": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "enviar_correo": {
      "ai_tool": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        []
      ]
    },
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "a501c9e2-0927-45b0-84ad-17cc7ac92cac",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "mtjBeU1DCnasZBUS",
  "tags": []
}