{
  "name": "Google Maps Lead Gen - Apify to GSheet",
  "nodes": [
    {
      "parameters": {},
      "id": "start",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "apifyToken",
              "value": "YOUR_APIFY_TOKEN_HERE"
            },
            {
              "name": "actorId",
              "value": "compass/crawler-google-places"
            },
            {
              "name": "keywords",
              "value": "spa Ho Chi Minh City, gym Hanoi, cafe Da Nang"
            },
            {
              "name": "maxPlaces",
              "value": "50"
            }
          ]
        }
      },
      "id": "config",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.apify.com/v2/acts/{{$node.Config.json.actorId}}/runs?token={{$node.Config.json.apifyToken}}",
        "options": {},
        "bodyParametersJson": "={\n  \"startUrls\": [\n    {{ $node.Config.json.keywords.split(',').map(k => `{\"url\": \"https://www.google.com/maps/search/${k.trim().replace(/ /g, '+')}\"}` ).join(',') }}\n  ],\n  \"maxCrawledPlaces\": {{$node.Config.json.maxPlaces}}\n}"
      },
      "id": "apify-run",
      "name": "Apify - Start Actor",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "amount": 30
      },
      "id": "wait",
      "name": "Wait 30s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.apify.com/v2/acts/{{$node.Config.json.actorId}}/runs/{{$node[\"Apify - Start Actor\"].json.data.id}}/dataset/items?token={{$node.Config.json.apifyToken}}"
      },
      "id": "apify-get",
      "name": "Apify - Get Dataset",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst qualified = [];\n\nfor (const item of items) {\n  const data = item.json;\n  \n  let score = 0;\n  const country = data.countryCode || '';\n  const rating = parseFloat(data.totalScore || 0);\n  const reviews = parseInt(data.reviewsCount || 0);\n  const phone = data.phone || '';\n  const website = data.website || '';\n  const category = (data.categoryName || '').toLowerCase();\n  const city = (data.city || '').toLowerCase();\n  \n  if (country !== 'VN') continue;\n  if (rating < 4.0) continue;\n  if (reviews < 10) continue;\n  if (!phone && !website) continue;\n  \n  if (rating >= 4.5) score += 20;\n  if (reviews >= 50) score += 15;\n  if (website) score += 10;\n  \n  const priorityCategories = ['spa', 'gym', 'cafe', 'restaurant', 'beauty', 'salon', 'marketing', 'software'];\n  if (priorityCategories.some(cat => category.includes(cat))) score += 10;\n  \n  const priorityCities = ['ho chi minh', 'hanoi', 'da nang', 'hai phong', 'can tho'];\n  if (priorityCities.some(c => city.includes(c))) score += 10;\n  \n  if (score >= 50) {\n    qualified.push({\n      json: {\n        name: data.title,\n        address: data.address,\n        city: data.city,\n        phone: phone,\n        website: website,\n        rating: rating,\n        reviews: reviews,\n        category: data.categoryName,\n        score: score,\n        mapsUrl: data.url,\n        scrapedAt: new Date().toISOString()\n      }\n    });\n  }\n}\n\nreturn qualified;"
      },
      "id": "filter-score",
      "name": "Filter & Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": "YOUR_GOOGLE_SHEET_ID",
        "sheetName": "Leads",
        "columns": {
          "mappingMode": "autoMapInputData"
        }
      },
      "id": "gsheet",
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 3,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "=\ud83c\udfaf New Leads: {{$node[\"Filter & Score\"].json.length}} qualified leads\n\nTop 3:\n{{$node[\"Filter & Score\"].json.slice(0,3).map(l => `\u2022 ${l.name} (Score: ${l.score}) - ${l.city}`).join('\\n')}}"
      },
      "id": "telegram",
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1560,
        480
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Apify - Start Actor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apify - Start Actor": {
      "main": [
        [
          {
            "node": "Wait 30s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 30s": {
      "main": [
        [
          {
            "node": "Apify - Get Dataset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apify - Get Dataset": {
      "main": [
        [
          {
            "node": "Filter & Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter & Score": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          },
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}