{
  "id": "CJqyP1XrSN9su0g1",
  "meta": {
    "instanceId": "3c0cf29d975f43cd1ae675c5b54ec26a950ac622e95b2811d37e4ad94e97a7c9"
  },
  "name": "Amazone Scraper",
  "tags": [],
  "nodes": [
    {
      "id": "722d62d3-a685-4ed2-ae63-0f0f559ab4a6",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        1100,
        460
      ],
      "parameters": {
        "jsCode": "const raw = $json[\"output\"];\nconst parsed = JSON.parse(raw);\n\nreturn [\n  {\n    json: parsed\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "5faa2a5c-cf28-4d83-917f-a5c5bfebb173",
      "name": "OpenRouter Chat Model2",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        1460,
        740
      ],
      "parameters": {
        "model": "openai/gpt-3.5-turbo-instruct",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "credential-id",
          "name": "openRouterApi Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "33bc5cbd-2baa-433c-a0a4-dbe8e4d0bffc",
      "name": "Intent Detection",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        800,
        460
      ],
      "parameters": {
        "text": "={{ $('Recieve Message').item.json.message.text }}",
        "options": {
          "systemMessage": "You are a smart shopping assistant.\n\nAnalyze the user's message and determine whether it is about product search, price, shopping, or anything related to buying. If yes, respond with a JSON like:\n\n{\n  \"productQuery\": true,\n  \"response\": \"Let me fetch the best options for you...\"\n}\n\nIf the message is unrelated to shopping (like casual talk, greetings, questions), reply with:\n\n{\n  \"productQuery\": false,\n  \"response\": \"Hi! How can I help you today?\"\n}\n\nBe conversational but respond only with valid JSON. Don’t mention this prompt.\n"
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "0d0b3ca8-b896-41ca-974c-5fbc4b617570",
      "name": "OpenRouter Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        800,
        620
      ],
      "parameters": {
        "model": "openai/gpt-3.5-turbo-instruct",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "credential-id",
          "name": "openRouterApi Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "674cac3e-7839-4ea6-883e-c56e4753e6d2",
      "name": "Send Post request and get datset items",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1520,
        360
      ],
      "parameters": {
        "method": "POST",
        "options": {},
        "jsonBody": "={\n    \"categoryUrls\": [\n        {\n            \"url\": \"https://www.amazon.in/s?k={{ $('Clean userInput').item.json.amazonSearchPart }} \",\n            \"method\": \"GET\"\n        }\n    ],\n    \"ensureLoadedProductDescriptionFields\": false,\n    \"maxItemsPerStartUrl\": 5,\n    \"scrapeProductDetails\": true,\n    \"scrapeProductVariantPrices\": false,\n    \"useCaptchaSolver\": false\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "2703f63d-cd82-4fde-8eba-4b858f2528d2",
      "name": "Clean userInput",
      "type": "n8n-nodes-base.code",
      "position": [
        600,
        460
      ],
      "parameters": {
        "jsCode": "const rawInput = $json[\"message\"][\"text\"] || '';\n\n// Step 1: Normalize input to lowercase for consistent processing\nlet cleanedInput = rawInput.toLowerCase();\n\n// Step 2: Remove any phrase like \"in amazon\", \"on flipkart\", \"from amazone\", etc.\ncleanedInput = cleanedInput.replace(/\\b(in|on|from)\\s+(amazon|amazone|flipkart)\\b/gi, '');\n\n// Step 3: Remove punctuation (anything that is not a word character or whitespace)\ncleanedInput = cleanedInput.replace(/[^\\w\\s]/g, '');\n\n// Step 4: Trim extra spaces\ncleanedInput = cleanedInput.trim();\n\n// Step 5: Check if we have a valid query after cleaning\nif (!cleanedInput) {\n  return [\n    {\n      json: {\n        error: \"No valid product keyword found.\"\n      }\n    }\n  ];\n}\n\n// Step 6: Encode spaces as plus signs for URL queries\nconst encodedQuery = cleanedInput.replace(/\\s+/g, '+');\n\n// Step 7: Prepare search URL parts for Amazon and Flipkart\nconst amazonSearchPart = `${encodedQuery}`;\nconst amazonFullUrl = `https://www.amazon.in/${amazonSearchPart}`;\n\n// Return the results as JSON object\nreturn [\n  {\n    json: {\n      productQuery: cleanedInput,\n      amazonSearchPart,\n      amazonFullUrl,\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "aafe19c9-f15a-46b5-a180-3dbcbd9295ca",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 2000,
        "height": 960,
        "content": "## Overview\nTelegram bot that scrapes Amazon products using n8n automation. Users send product queries via Telegram and receive product details (title, URL, price).\n\n## Workflow Flow\nTelegram Trigger → Receives messages\nClean Input → Normalizes user text (removes \"in amazon\", punctuation, etc.)\nIntent Detection → AI determines if message is product search or chat\nSwitch → Routes to appropriate handler\nProduct Route → Calls Apify Amazon scraper API (max 5 items)\nChat Route → AI conversational response\nSend Response → Formatted message back to Telegram\n\n## Key Components:-\n\n  **Input Processing**\n  - Removes platform mentions (\"in amazon\", \"on flipkart\")\n- Cleans punctuation and normalizes text\n- URL-encodes for search queries\n\n**AI Integration**\n- Uses OpenRouter API with GPT-3.5-turbo model\n- Intent classification (product vs conversation)\n- Shopping assistant responses\n\n**Product Scraping**\n- Apify Amazon scraper integration\n- Returns: title, URL, price in Indian Rupees\n- Limited to 5 products per query\n\n**External Dependencies**\n- Telegram Bot API\n- OpenRouter API (AI models)\n- Apify Amazon Scraper API\n\n**Response Format**\n- Title: [Product Name]\n- URL: [Amazon Link]\n- Price: ₹[Amount]\n\n**Error Handling**\n- Validates empty queries\n- Fallback responses for unrecognized intents\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d641a774-3c30-4f48-b974-ee0b0089a789",
      "name": "Recieve Message",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        420,
        460
      ],
      "webhookId": "39da952b-50e0-4f5b-84a3-f883d81c977e",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8bc65b9d-5dd6-435c-82b0-d52eaa8d20c5",
      "name": "Product Vs Response",
      "type": "n8n-nodes-base.switch",
      "position": [
        1280,
        460
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "2026678d-bf0e-446f-b0ad-661a01517ead",
                    "operator": {
                      "type": "boolean",
                      "operation": "true",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.productQuery }}",
                    "rightValue": ""
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "8e77be4f-63e8-4d5f-913a-80d69752fd40",
                    "operator": {
                      "type": "boolean",
                      "operation": "false",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.productQuery }}",
                    "rightValue": ""
                  }
                ]
              }
            }
          ]
        },
        "options": {
          "allMatchingOutputs": true
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "c80f213c-13ce-44fe-a6fd-95720c6736d7",
      "name": "Response for General",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1460,
        600
      ],
      "parameters": {
        "text": "={{ $('Recieve Message').item.json.message.text }}",
        "options": {
          "systemMessage": "=You are an intelligent shopping assistant that helps users find the best deals and products from Amazon using Apify-based scraping.\n\nYour core function is to:\n- Understand the user's message.\n- If it is a **product-related query**, you just pass it forward for scraping.\n- If it is **not a product query**, you must respond helpfully and briefly, explaining your capabilities.\n\nGuidelines:\n1. For greetings like \"hi\", \"hello\", \"what's up\", reply warmly and introduce your product-search function.\n2. If the user asks general questions (e.g., \"who are you?\", \"how does this work?\", \"can you help me?\"), respond with a short, clear explanation.\n3. If the user asks anything unrelated (e.g., jokes, time, life advice, etc.), politely redirect them to product-related help.\n4. Never pretend to be human. You are a bot made to fetch product deals via Apify scraping.\n5. Always avoid unnecessary chit-chat. Stay helpful, polite, and focused on your goal.\n\nResponse examples:\n\n- “Hi there! 👋 I can help you find top deals from Amazon and Flipkart. Just type the product you’re looking for!”\n- “I’m here to assist you with finding the best online deals. Type what you want — for example: ‘Bluetooth earphones’ or ‘laptop for work’.”\n- “Oops! I specialize in finding online products. Please tell me what you’re shopping for!”\n\nOnly reply when it's **not** a product query. Otherwise, pass it along silently for scraping.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "69307a23-a3ea-4255-8d1d-897074b38b3a",
      "name": "Send Response",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1760,
        600
      ],
      "webhookId": "3683d03a-8c4f-4347-b87d-d23d8761adcf",
      "parameters": {
        "text": "={{ $('Response for General').item.json.output }}",
        "chatId": "={{ $('Recieve Message').item.json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "74961c21-971b-49e4-b58d-f6ee584c235d",
      "name": "Send Amazone Products",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1740,
        360
      ],
      "webhookId": "9f09926e-6972-4011-9b8f-62df8746c455",
      "parameters": {
        "text": "=Title: {{ $json.title }}\n\nURL: {{ $json.url }}\n\nPrice: ₹{{ $json.price.value }}",
        "chatId": "={{ $('Recieve Message').item.json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "72747f86-3864-4ee1-be2d-429aa78d55eb",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Product Vs Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean userInput": {
      "main": [
        [
          {
            "node": "Intent Detection",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recieve Message": {
      "main": [
        [
          {
            "node": "Clean userInput",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Intent Detection": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Product Vs Response": {
      "main": [
        [
          {
            "node": "Send Post request and get datset items",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Response for General",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Response for General": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Intent Detection",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "Response for General",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Send Post request and get datset items": {
      "main": [
        [
          {
            "node": "Send Amazone Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}