{
  "name": "My workflow 2",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 6
            }
          ]
        }
      },
      "id": "d6fa69f4-f35b-49ae-ad34-6cc853455cc0",
      "name": "Schedule Trigger (Every 6 hrs)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1lH1NQPNAjw6xbsGkZ4eAT0BsD_urnur08huX2NLt_6Y",
          "mode": "list",
          "cachedResultName": "pakistan_tech_keywords",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lH1NQPNAjw6xbsGkZ4eAT0BsD_urnur08huX2NLt_6Y/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": 1685673177,
          "mode": "list",
          "cachedResultName": "All Keywords",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lH1NQPNAjw6xbsGkZ4eAT0BsD_urnur08huX2NLt_6Y/edit#gid=1685673177"
        },
        "options": {}
      },
      "id": "2cbb7619-7113-43b0-b800-dc6c671671ed",
      "name": "Read Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [224, 0],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "38CZsY1HaOCTjsod",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": false, "typeValidation": "strict" },
          "conditions": [
            {
              "id": "check-empty",
              "leftValue": "={{ $json['Content Link'] }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "equals" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "0f81834f-6571-43b4-b659-7eb59a9cb64f",
      "name": "Filter — Only Unprocessed Keywords",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2,
      "position": [448, 0]
    },
    {
      "parameters": {},
      "id": "663a23e1-a143-4e14-b7cf-7ee05ad3b7d9",
      "name": "Process 1 Keyword Per Run",
      "type": "n8n-nodes-base.limit",
      "typeVersion": 1,
      "position": [672, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "x-api-key", "value": "YOUR_ANTHROPIC_API_KEY" },
            { "name": "anthropic-version", "value": "2023-06-01" },
            { "name": "content-type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"claude-opus-4-5\",\n  \"max_tokens\": 4000,\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"You are an expert Pakistani tech journalist and SEO writer for TechBird.pk targeting Pakistani audiences. Write conversational, human, engaging content. Mention PKR prices, Daraz, PTA, Jazz, Zong, Telenor where relevant.\\n\\nWrite a comprehensive SEO-optimised blog article for the keyword: \\\"{{ $json['Keyword'] }}\\\"\\n\\nCategory: {{ $json['Category'] }}\\nContent Type: {{ $json['Content Type'] }}\\n\\nRespond ONLY in this exact structure with no extra text before or after:\\n\\nTITLE: [Compelling SEO title with keyword]\\n\\nMETA_DESCRIPTION: [155-160 character meta description]\\n\\nSLUG: [url-friendly-slug-with-hyphens]\\n\\nFOCUS_KEYPHRASE: [exact keyword phrase]\\n\\nARTICLE:\\n[Full article in clean HTML. Use h2, h3, p, ul, ol, strong, table tags only. No html/head/body tags. Minimum 1200 words. Must include: opening hook paragraph, 5-7 H2 sections with detail, one comparison or specs table, FAQ section with 4-5 H3 questions and answers, conclusion paragraph with CTA mentioning TechBird.pk. Place the keyword naturally 6-8 times. Reference Pakistani context throughout.]\"\n    }\n  ]\n}",
        "options": { "timeout": 90000 }
      },
      "id": "9c6d6dee-f986-4f36-ba5b-eebd484a0ac4",
      "name": "Claude — Generate SEO Article",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 0]
    },
    {
      "parameters": {
        "jsCode": "const response = $input.first().json;\n\nif (!response.content || !response.content[0]) {\n  throw new Error('Claude API error: ' + JSON.stringify(response));\n}\n\nconst raw = response.content[0].text;\n\nconst titleMatch = raw.match(/TITLE:\\s*(.+)/);\nconst title = titleMatch ? titleMatch[1].trim() : 'Untitled Article';\n\nconst metaMatch = raw.match(/META_DESCRIPTION:\\s*(.+)/);\nconst metaDescription = metaMatch ? metaMatch[1].trim() : '';\n\nconst slugMatch = raw.match(/SLUG:\\s*([\\w-]+)/);\nconst slug = slugMatch ? slugMatch[1].trim() : title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');\n\nconst keyphraseMatch = raw.match(/FOCUS_KEYPHRASE:\\s*(.+)/);\nconst focusKeyphrase = keyphraseMatch ? keyphraseMatch[1].trim() : '';\n\nconst articleMatch = raw.match(/ARTICLE:[^\\n]*\\n([\\s\\S]+)/);\nconst articleHtml = articleMatch ? articleMatch[1].trim() : raw;\n\nconst keywordData = $('Process 1 Keyword Per Run').first().json;\n\nreturn [{\n  json: {\n    title,\n    metaDescription,\n    slug,\n    focusKeyphrase,\n    articleHtml,\n    keyword: keywordData['Keyword'] || '',\n    category: keywordData['Category'] || '',\n    difficulty: keywordData['Difficulty'] || '',\n    rowNumber: keywordData['#'] || '',\n    originalRow: keywordData\n  }\n}];"
      },
      "id": "5756c8ff-614a-4bb5-ac31-f31b7e02066c",
      "name": "Parse Claude Output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1104, 0]
    },
    {
      "parameters": {
        "folderId": { "__rl": true, "value": "YOUR_GOOGLE_DRIVE_FOLDER_ID", "mode": "id" },
        "title": "={{ $json.title }}"
      },
      "id": "01ff6197-dc4a-4029-8fd8-e77e555a15fb",
      "name": "Create Google Doc",
      "type": "n8n-nodes-base.googleDocs",
      "typeVersion": 2,
      "position": [1328, 0],
      "credentials": {
        "googleDocsOAuth2Api": { "id": "hDgSdldSGCKwN7rm", "name": "Google Docs account" }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentURL": "={{ $('Create Google Doc').first().json.documentId }}",
        "actionsUi": {
          "actionFields": [
            {
              "action": "insert",
              "text": "={{ $('Parse Claude Output').first().json.title }}\n\n--- SEO METADATA ---\nMeta Description: {{ $('Parse Claude Output').first().json.metaDescription }}\nFocus Keyphrase: {{ $('Parse Claude Output').first().json.focusKeyphrase }}\nSlug: {{ $('Parse Claude Output').first().json.slug }}\nKeyword: {{ $('Parse Claude Output').first().json.keyword }}\nCategory: {{ $('Parse Claude Output').first().json.category }}\n--------------------\n\n{{ $('Parse Claude Output').first().json.articleHtml }}"
            }
          ]
        }
      },
      "id": "cc9f6015-7d40-4c8d-84a8-3bb9c4049709",
      "name": "Write Content to Google Doc",
      "type": "n8n-nodes-base.googleDocs",
      "typeVersion": 2,
      "position": [1552, 0],
      "credentials": {
        "googleDocsOAuth2Api": { "id": "hDgSdldSGCKwN7rm", "name": "Google Docs account" }
      }
    },
    {
      "parameters": {
        "jsCode": "const docId = $('Create Google Doc').first().json.documentId;\nconst docUrl = 'https://docs.google.com/document/d/' + docId + '/edit';\nconst parsed = $('Parse Claude Output').first().json;\nreturn [{ json: { ...parsed, docId, docUrl } }];"
      },
      "id": "d3a0d3ed-bd33-441b-897e-7666ebe9e7ef",
      "name": "Build Doc URL",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1760, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR-SITE.com/wp-json/wp/v2/posts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "Basic YOUR_BASE64_CREDENTIALS" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"title\": {{ JSON.stringify($json.title) }},\n  \"content\": {{ JSON.stringify($json.articleHtml) }},\n  \"status\": \"draft\",\n  \"slug\": {{ JSON.stringify($json.slug) }},\n  \"excerpt\": {{ JSON.stringify($json.metaDescription) }},\n  \"meta\": {\n    \"_yoast_wpseo_metadesc\": {{ JSON.stringify($json.metaDescription) }},\n    \"_yoast_wpseo_focuskw\": {{ JSON.stringify($json.focusKeyphrase) }}\n  }\n}",
        "options": { "timeout": 30000 }
      },
      "id": "e7ac2fe6-a93d-4bee-aa6b-863629a4ef9f",
      "name": "Create WordPress Draft Post",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1984, 0]
    },
    {
      "parameters": {
        "jsCode": "const wpResponse = $input.first().json;\nconst buildData = $('Build Doc URL').first().json;\n\nif (!wpResponse.id) {\n  throw new Error('WordPress failed: ' + JSON.stringify(wpResponse));\n}\n\nconst wpPostId = wpResponse.id;\nconst wpEditUrl = 'https://YOUR-SITE.com/wp-admin/post.php?post=' + wpPostId + '&action=edit';\n\nreturn [{ \n  json: { \n    ...buildData, \n    wpPostId, \n    wpEditUrl,\n    \"#\": buildData.rowNumber,\n    \"Content Link\": buildData.docUrl,\n    \"WP Draft Link\": wpEditUrl,\n    \"Status\": \"Done\",\n    \"Generated Title\": buildData.title\n  } \n}];"
      },
      "id": "8e1002fa-ad08-401d-8a6b-a3bca5826ad6",
      "name": "Build WordPress URLs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [2208, 0]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "All Keywords",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": ["#"],
          "schema": [
            { "id": "#", "displayName": "#", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Keyword", "displayName": "Keyword", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Category", "displayName": "Category", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Difficulty", "displayName": "Difficulty", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Content Type", "displayName": "Content Type", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Search Volume", "displayName": "Search Volume", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Content Link", "displayName": "Content Link", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "WP Draft Link", "displayName": "WP Draft Link", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Status", "displayName": "Status", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Generated Title", "displayName": "Generated Title", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "15c78321-39b0-47f8-ae60-91a2f88c0864",
      "name": "Update Google Sheet — Add Links",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [2432, 0],
      "credentials": {
        "googleSheetsOAuth2Api": { "id": "38CZsY1HaOCTjsod", "name": "Google Sheets account" }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Schedule Trigger (Every 6 hrs)": { "main": [[{ "node": "Read Google Sheet", "type": "main", "index": 0 }]] },
    "Read Google Sheet": { "main": [[{ "node": "Filter — Only Unprocessed Keywords", "type": "main", "index": 0 }]] },
    "Filter — Only Unprocessed Keywords": { "main": [[{ "node": "Process 1 Keyword Per Run", "type": "main", "index": 0 }]] },
    "Process 1 Keyword Per Run": { "main": [[{ "node": "Claude — Generate SEO Article", "type": "main", "index": 0 }]] },
    "Claude — Generate SEO Article": { "main": [[{ "node": "Parse Claude Output", "type": "main", "index": 0 }]] },
    "Parse Claude Output": { "main": [[{ "node": "Create Google Doc", "type": "main", "index": 0 }]] },
    "Create Google Doc": { "main": [[{ "node": "Write Content to Google Doc", "type": "main", "index": 0 }]] },
    "Write Content to Google Doc": { "main": [[{ "node": "Build Doc URL", "type": "main", "index": 0 }]] },
    "Build Doc URL": { "main": [[{ "node": "Create WordPress Draft Post", "type": "main", "index": 0 }]] },
    "Create WordPress Draft Post": { "main": [[{ "node": "Build WordPress URLs", "type": "main", "index": 0 }]] },
    "Build WordPress URLs": { "main": [[{ "node": "Update Google Sheet — Add Links", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": { "instanceId": "b661f62f645c398ae88d26195a0b922e5c846ac94a5a5e0bb6ab69cc7ad89cd9" },
  "id": "oGvzMIExHGcQyXDC",
  "tags": []
}
