{ "name": "Insta Post Cache", "nodes": [ { "parameters": {}, "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, "position": [ -368, -16 ], "id": "028b7b07-d02a-4e96-8ee3-f1197b999c46", "name": "When clicking ‘Execute workflow’" }, { "parameters": { "url": "https://graph.facebook.com/v24.0/17841470338696118/media?fields=id,caption,timestamp,comments_count&limit=5", "authentication": "genericCredentialType", "genericAuthType": "httpBearerAuth", "options": { "pagination": { "pagination": { "paginationMode": "responseContainsNextURL", "nextURL": "={{ $response.body.paging?.next }}", "limitPagesFetched": true, "maxRequests": 6 } } } }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ -160, -16 ], "id": "dbff29da-5eae-4993-a3d9-8d41755c8a9d", "name": "Call FB to get the last posts", "credentials": { "httpHeaderAuth": { "id": "bU1D0djQYelYC4km", "name": "gitea-auth" }, "httpQueryAuth": { "id": "9poWzM5erRyAfM1e", "name": "Query Auth account" }, "httpBearerAuth": { "id": "iDvltYWG0ECfJA1r", "name": "Bearer Auth account" } } }, { "parameters": { "jsCode": "// Process each Instagram post\nconst outputItems = [];\n\nfor (const item of $input.all()) {\n const caption = item.json.caption || '';\n const postId = item.json.id;\n \n // Extract keyword - handle smart quotes too\n const keywordMatch = caption.match(/comment\\s+[\"'\"\"']?(\\w+)[\"'\"\"']?/i);\n const keyword = keywordMatch ? keywordMatch[1].toLowerCase() : null;\n \n // Extract WordPress post ID\n const wpPostIdMatch = caption.match(/ID:\\s*(\\d+)/i);\n const wpPostId = wpPostIdMatch ? parseInt(wpPostIdMatch[1]) : null;\n \n outputItems.push({\n json: {\n instagram_post_id: postId,\n instagram_caption: caption,\n keyword: keyword,\n wp_post_id: wpPostId,\n ...item.json\n }\n });\n}\n\nreturn outputItems;" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 208, -16 ], "id": "e73f63f0-316a-4a26-93b7-6e67cb355c00", "name": "Get the Code and keyword" }, { "parameters": { "jsCode": "// This processes each post as a separate item\nconst hoursAgo = 10;\nconst cutoffTime = new Date(Date.now() - (hoursAgo * 60 * 60 * 1000));\n// You have 5 items, each with a data array\nconst allPages = $input.all();\n\nconsole.log(`Received ${allPages.length} pages`);\n\n// Flatten all data arrays into one big array\nconst allPosts = allPages.flatMap(page => {\n const posts = page.json.data || [];\n console.log(`Page has ${posts.length} posts`);\n return posts;\n});\n\nconsole.log(`Total posts across all pages: ${allPosts.length}`);\n\n// Return as separate items\nreturn allPosts.map(post => ({\n json: {\n postID: post.id,\n caption: post.caption || '',\n timestamp: post.timestamp\n }\n}));" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 32, -16 ], "id": "6222089d-837a-4e01-b9e9-3a27e9a5b0ff", "name": "JS to get the post info" }, { "parameters": { "url": "=https://graph.facebook.com/v24.0/{{ $json.postId }}/comments?fields=id,text,username,timestamp&since={{ $now.minus({minutes: 5}).toISO() }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 240, 352 ], "id": "3b8c5f20-e068-4fa6-a446-1bbd800dd2b3", "name": "Now get the comments", "credentials": { "httpHeaderAuth": { "id": "bU1D0djQYelYC4km", "name": "gitea-auth" } } }, { "parameters": { "options": { "pagination": { "pagination": { "paginationMode": "responseContainsNextURL", "nextURL": "={{ $json.paging.next }}" } } } }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ -48, 320 ], "id": "8ee1772f-a9e8-4d4b-9ac3-ac3f53457087", "name": "HTTP Request" }, { "parameters": { "operation": "upsert", "dataTableId": { "__rl": true, "value": "czv8kqzBgSN7HiMt", "mode": "list", "cachedResultName": "Insta-Post-Data", "cachedResultUrl": "/projects/xJUdeIR7ChAsPEQN/datatables/czv8kqzBgSN7HiMt" }, "filters": { "conditions": [ { "keyName": "Postid", "keyValue": "={{ $json.postID }}" } ] }, "columns": { "mappingMode": "defineBelow", "value": { "Postid": "={{ $json.postID }}", "timestamp": "={{ $json.timestamp }}", "caption": "={{ $json.caption }}", "pbs_keyword": "={{ $json.keyword }}", "pbs_postid": "={{ $json.wp_post_id }}" }, "matchingColumns": [], "schema": [ { "id": "Postid", "displayName": "Postid", "required": false, "defaultMatch": false, "display": true, "type": "number", "readOnly": false, "removed": false }, { "id": "caption", "displayName": "caption", "required": false, "defaultMatch": false, "display": true, "type": "string", "readOnly": false, "removed": false }, { "id": "timestamp", "displayName": "timestamp", "required": false, "defaultMatch": false, "display": true, "type": "dateTime", "readOnly": false, "removed": false }, { "id": "pbs_keyword", "displayName": "pbs_keyword", "required": false, "defaultMatch": false, "display": true, "type": "string", "readOnly": false, "removed": false }, { "id": "pbs_postid", "displayName": "pbs_postid", "required": false, "defaultMatch": false, "display": true, "type": "number", "readOnly": false, "removed": false } ], "attemptToConvertTypes": false, "convertFieldsToString": false }, "options": {} }, "type": "n8n-nodes-base.dataTable", "typeVersion": 1.1, "position": [ 672, -224 ], "id": "8b80a33e-8bb0-414d-b8e8-74a7aa814ed3", "name": "Upsert row(s)1" }, { "parameters": { "operation": "rowNotExists", "dataTableId": { "__rl": true, "value": "bq738miYhEK37VaJ", "mode": "list", "cachedResultName": "Insta-Post-Data", "cachedResultUrl": "/projects/xJUdeIR7ChAsPEQN/datatables/bq738miYhEK37VaJ" }, "matchType": "allConditions", "filters": { "conditions": [ { "keyName": "Postid", "keyValue": "={{ $json.postID }}" } ] } }, "type": "n8n-nodes-base.dataTable", "typeVersion": 1.1, "position": [ 400, -16 ], "id": "52f357f5-9a8d-4d37-b768-6a926cabc750", "name": "If row does not exist" }, { "parameters": { "dataTableId": { "__rl": true, "value": "bq738miYhEK37VaJ", "mode": "list", "cachedResultName": "Insta-Post-Data", "cachedResultUrl": "/projects/xJUdeIR7ChAsPEQN/datatables/bq738miYhEK37VaJ" }, "columns": { "mappingMode": "defineBelow", "value": { "pbs_postid": "={{ $json.wp_post_id }}", "pbs_keyword": "={{ $json.keyword }}", "timestamp": "={{ $json.timestamp }}", "caption": "={{ $json.instagram_caption }}", "Postid": "={{ $json.postID }}" }, "matchingColumns": [], "schema": [ { "id": "id1", "displayName": "id1", "required": false, "defaultMatch": false, "display": true, "type": "number", "readOnly": false, "removed": true }, { "id": "Postid", "displayName": "Postid", "required": false, "defaultMatch": false, "display": true, "type": "string", "readOnly": false, "removed": false }, { "id": "caption", "displayName": "caption", "required": false, "defaultMatch": false, "display": true, "type": "string", "readOnly": false, "removed": false }, { "id": "timestamp", "displayName": "timestamp", "required": false, "defaultMatch": false, "display": true, "type": "dateTime", "readOnly": false, "removed": false }, { "id": "pbs_keyword", "displayName": "pbs_keyword", "required": false, "defaultMatch": false, "display": true, "type": "string", "readOnly": false, "removed": false }, { "id": "pbs_postid", "displayName": "pbs_postid", "required": false, "defaultMatch": false, "display": true, "type": "number", "readOnly": false, "removed": false }, { "id": "createdAt1", "displayName": "createdAt1", "required": false, "defaultMatch": false, "display": true, "type": "dateTime", "readOnly": false, "removed": true }, { "id": "updatedAt1", "displayName": "updatedAt1", "required": false, "defaultMatch": false, "display": true, "type": "dateTime", "readOnly": false, "removed": true } ], "attemptToConvertTypes": false, "convertFieldsToString": false }, "options": {} }, "type": "n8n-nodes-base.dataTable", "typeVersion": 1.1, "position": [ 576, -16 ], "id": "78be39f2-27e4-415f-b52b-448b5ec66289", "name": "Insert row" }, { "parameters": { "rule": { "interval": [ { "triggerAtHour": 18, "triggerAtMinute": 8 }, { "triggerAtMinute": 8 } ] } }, "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.3, "position": [ -384, -160 ], "id": "6b3aecf6-bd2c-49df-8e8e-63b1d6ae6728", "name": "Schedule Trigger" } ], "pinData": { "Schedule Trigger": [ { "json": { "timestamp": "2026-06-29T00:08:06.015-04:00", "Readable date": "June 29th 2026, 12:08:06 am", "Readable time": "12:08:06 am", "Day of week": "Monday", "Year": "2026", "Month": "June", "Day of month": "29", "Hour": "00", "Minute": "08", "Second": "06", "Timezone": "America/New_York (UTC-04:00)" }, "pairedItem": { "item": 0 } } ] }, "connections": { "When clicking ‘Execute workflow’": { "main": [ [ { "node": "Call FB to get the last posts", "type": "main", "index": 0 } ] ] }, "Call FB to get the last posts": { "main": [ [ { "node": "JS to get the post info", "type": "main", "index": 0 } ] ] }, "JS to get the post info": { "main": [ [ { "node": "Get the Code and keyword", "type": "main", "index": 0 } ] ] }, "Get the Code and keyword": { "main": [ [ { "node": "If row does not exist", "type": "main", "index": 0 } ] ] }, "Now get the comments": { "main": [ [] ] }, "If row does not exist": { "main": [ [ { "node": "Insert row", "type": "main", "index": 0 } ] ] }, "Schedule Trigger": { "main": [ [ { "node": "Call FB to get the last posts", "type": "main", "index": 0 } ] ] } }, "active": true, "settings": { "executionOrder": "v1", "availableInMCP": false }, "versionId": "cb67f90e-7f5a-4361-83f4-8efbb12f04e2", "meta": { "instanceId": "eeb18e777509f7207ee9abd45845e7fbf014989719160f3ed6ae633d872bf6c4" }, "id": "CNts_6nNU2F04pX6MFyw0", "tags": [] }