{ "name": "instra-reel-cache-new", "nodes": [ { "parameters": {}, "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, "position": [ -1488, 672 ], "id": "8c7dbb5f-85e3-4ae4-90ad-4bb5aa9893c2", "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": [ -1280, 672 ], "id": "f8854bba-7869-435c-800c-56b9b80d0053", "name": "Call FB to get the last posts", "credentials": { "httpHeaderAuth": { "id": "bU1D0djQYelYC4km", "name": "gitea-auth" }, "httpQueryAuth": { "id": "9poWzM5erRyAfM1e", "name": "Query Auth account" }, "httpBearerAuth": { "id": "zdT0ctdFDyJPrO8I", "name": "fb-auth" } } }, { "parameters": { "options": {} }, "type": "n8n-nodes-base.splitInBatches", "typeVersion": 3, "position": [ -416, 80 ], "id": "7e606a6c-87cf-405e-bc84-bc1691a96437", "name": "Loop Over Items" }, { "parameters": { "operation": "select", "table": { "__rl": true, "value": "pbs_recipes", "mode": "list", "cachedResultName": "pbs_recipes" }, "where": { "values": [ { "column": "post_id", "value": "={{ $json.wp_post_id}}" } ] }, "options": {} }, "type": "n8n-nodes-base.mySql", "typeVersion": 2.5, "position": [ -208, 176 ], "id": "62a933b5-1827-4519-b2d3-1b7e8c7b6a92", "name": "qry_pbs_recipe", "alwaysOutputData": true, "credentials": { "mySql": { "id": "4ETlVumrAq95ispJ", "name": "MySQL account" } } }, { "parameters": { "method": "POST", "url": "https://n8n.plantbasedsoutherner.com/webhook/staging-test", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "title", "value": "={{ \"done processing all records\" }}" }, { "name": "message", "value": "={{ \"Done\"}}" }, { "name": "timestamp", "value": "={{ $now.toFormat('MMM dd, HH:mm:ss') }}" } ] }, "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ -240, -272 ], "id": "46658896-024e-416b-83d9-1e67fc8f95d2", "name": "done processing message", "executeOnce": true }, { "parameters": { "inputSource": "passthrough" }, "type": "n8n-nodes-base.executeWorkflowTrigger", "typeVersion": 1.1, "position": [ -1488, 496 ], "id": "9fbc8d44-b972-4371-97a0-da0aa234a5ae", "name": "Enter_Cache_Update" }, { "parameters": { "assignments": { "assignments": [ { "id": "7452a3be-7224-49c1-9daa-2f2fecb1bd8d", "name": "data", "value": "= {{ $json.data }}", "type": "array" } ] }, "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ 368, 944 ], "id": "cf6c197e-65e7-44bf-9298-25328baf3d6c", "name": "Set_FB_Data" }, { "parameters": { "jsCode": "// This processes each post as a separate item\n// You have 5 items, each with a data array\nconst allPages = $input.all();\n\n// Flatten all data arrays into one big array\nconst allPosts = allPages.flatMap(page => {\n const posts = page.json.data || [];\n return posts;\n});\n\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": [ -880, 544 ], "id": "d9b6791d-d256-4a21-8ab8-a7abe829e681", "name": "JS_Post_Info" }, { "parameters": { "jsCode": "//set the static data for later\nconst staticData = $getWorkflowStaticData('global');\nstaticData.success = true;\nstaticData.reason = 'na';\n\n// Process each Instagram post\nconst outputItems = [];\n\nfor (const item of $input.all()) {\n const caption = item.json.caption || '';\n const postId = String(item.json.postID);\n \n // Extract keyword - handle smart quotes too\n const keywordMatch = caption.match(/comment\\s*[:\"'\"\"']?\\s*(\\w+)/i);\n const keyword = keywordMatch ? keywordMatch[1].toLowerCase() : null;\n \n // Extract WordPress post ID\n const wpPostIdMatch = caption.match(/ID\\s*:?\\s*(\\d+)/i);\n const wpPostId = wpPostIdMatch ? parseInt(wpPostIdMatch[1]) : null;\n \n outputItems.push({\n json: {\n ...item.json, // spread first\n instagram_post_id: postId, // then your explicit fields win\n instagram_caption: caption,\n keyword: keyword,\n wp_post_id: wpPostId,\n }\n });\n}\n\nreturn outputItems;" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -672, 544 ], "id": "60e7af66-0113-4699-9cec-f07715b36d98", "name": "JS_Code_and_keyword" }, { "parameters": { "operation": "executeQuery", "query": "INSERT INTO instagram_posts (reel_id, pbs_post_id, instagram_caption)\nVALUES (CAST($1 AS CHAR), $2, $3)\nON DUPLICATE KEY UPDATE\n reel_id = VALUES(reel_id),\n pbs_post_id = COALESCE(pbs_post_id, VALUES(pbs_post_id)),\n instagram_caption = VALUES(instagram_caption);\n", "options": { "queryReplacement": "={{ [$('JS_Code_and_keyword').item.json.postID + '',$('JS_Code_and_keyword').item.json.wp_post_id, $('JS_Code_and_keyword').item.json.caption] }}" } }, "type": "n8n-nodes-base.mySql", "typeVersion": 2.5, "position": [ 352, 752 ], "id": "309e1185-1954-47b3-8a71-ca876e1ff9e7", "name": "SQL_Up_Insta_Table", "credentials": { "mySql": { "id": "4ETlVumrAq95ispJ", "name": "MySQL account" } } }, { "parameters": { "jsCode": "const staticData = $getWorkflowStaticData('global');\nstaticData.success = false;\nstaticData.reason = 'Failed to get pbs_recipe data';\n\nreturn $input.all();" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 160, 304 ], "id": "8124c58d-e55e-435d-99e5-567fffafa8c4", "name": "JS_Set_Failure" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "bba2d400-e5c7-43f2-8a54-f9efe80e91a9", "leftValue": "={{ $('qry_pbs_recipe').first().json.id }}", "rightValue": "", "operator": { "type": "number", "operation": "exists", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ -48, 176 ], "id": "b3d35905-05f6-474c-865a-d3950866ae3b", "name": "Check_Data_Return" }, { "parameters": { "jsCode": "return [{\n json: { \n data : [\n {\n webhook_payload: $input.first().json.webhook_payload,\n id: String($input.first().json.resolved_media_id || $input.first().json.postID),\n caption: $input.first().json.caption || '',\n timestamp: $input.first().json.timestamp\n }\n ]\n }\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -1280, 496 ], "id": "dcf1617a-829a-40c8-931f-4db9bc5aad73", "name": "Set_Reply_Data" }, { "parameters": { "operation": "executeQuery", "query": "INSERT INTO platform_posts (platform_post_id, pbs_post_id, caption)\nVALUES ('{{ $('JS_Code_and_keyword').item.json.postID }}', $1, $2)\nON DUPLICATE KEY UPDATE\n platform_post_id = VALUES(platform_post_id),\n pbs_post_id = COALESCE(pbs_post_id, VALUES(pbs_post_id)),\n caption = VALUES(caption);", "options": { "queryReplacement": "={{ [ $('JS_Code_and_keyword').item.json.wp_post_id , $('JS_Code_and_keyword').item.json.caption ] }}" } }, "type": "n8n-nodes-base.mySql", "typeVersion": 2.5, "position": [ 1328, 192 ], "id": "85a47b95-f4a6-4034-bc79-18e338272b93", "name": "SQL_Up_Insta_Table1", "credentials": { "mySql": { "id": "4ETlVumrAq95ispJ", "name": "MySQL account" } } }, { "parameters": { "jsCode": "const staticData = $getWorkflowStaticData('global');\n\nreturn [{\n json: {\n success: staticData.success,\n reason: staticData.reason || ''\n }\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 208, -32 ], "id": "2122a727-fd72-48e4-af76-d56f277a6f77", "name": "Set_Return" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "cf2c90a9-1ce0-48b1-8e5b-c15aabd56a8d", "leftValue": "={{ $('Loop Over Items').item.json.wp_post_id }}", "rightValue": 0, "operator": { "type": "number", "operation": "empty", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 352, 304 ], "id": "1185f0f7-94c9-4a63-b22d-92b263371ac0", "name": "Check_Null_ID" }, { "parameters": { "method": "POST", "url": "https://n8n.staging.plantbasedsoutherner.com/webhook/staging-test", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "title", "value": "🚨Update Insta Data Error 🚨" }, { "name": "message", "value": "={{ \"This post contains an IMPROPER PBS_ID!\\n\\nReel ID: \" + $('Loop Over Items').item.json.postID + \"\\nKeyword: \" + $('Loop Over Items').item.json.keyword + \"\\nCaption: \" + $('Loop Over Items').item.json.caption }}" }, { "name": "timestamp", "value": "={{ $now.toFormat('MMM dd, HH:mm:ss') }}" } ] }, "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 672, 736 ], "id": "93a74a09-ecf4-4a32-a335-4f5855ba1d43", "name": "Send_FK_Error_Msg" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "14429cfd-1430-445e-a96a-f8ebeafe3059", "leftValue": "={{ $json.id }}", "rightValue": "", "operator": { "type": "number", "operation": "exists", "singleValue": true } } ], "combinator": "and" }, "looseTypeValidation": "={{ false }}", "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 752, 256 ], "id": "04e3c5f9-7ad3-4862-8287-7ec2ea728c80", "name": "Check_Insta_Exists" }, { "parameters": { "method": "POST", "url": "https://n8n.staging.plantbasedsoutherner.com/webhook/staging-test", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "title", "value": "Update Insta Data Problem ⚠️" }, { "name": "message", "value": "={{ \"This post did not contain a pbs_ID!\\n\\nReel ID: \" + $('Loop Over Items').item.json.postID + \"\\nKeyword: \" + $('Loop Over Items').item.json.keyword + \"\\nCaption: \" + $('Loop Over Items').item.json.caption }}" }, { "name": "timestamp", "value": "={{ $now.toFormat('MMM dd, HH:mm:ss') }}" } ] }, "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 656, 928 ], "id": "6ad9c0af-0d4a-40f5-b50b-a591c81ffbac", "name": "Send_No_ID_Msg" }, { "parameters": { "operation": "select", "table": { "__rl": true, "value": "platform_posts", "mode": "list", "cachedResultName": "platform_posts" }, "where": { "values": [ { "column": "platform_post_id", "value": "={{ $('Loop Over Items').item.json.postID }}" } ] }, "options": {} }, "type": "n8n-nodes-base.mySql", "typeVersion": 2.5, "position": [ 544, 256 ], "id": "9ff48ff9-58c9-4bb2-8912-554d4f6c24a1", "name": "qry_Insta_Data", "alwaysOutputData": true, "credentials": { "mySql": { "id": "4ETlVumrAq95ispJ", "name": "MySQL account" } } }, { "parameters": { "mode": "raw", "jsonOutput": "={ \n \"title\": \"Update Insta Data Problem ⚠️\",\n \"message\": \"{{ 'This post contains an IMPROPER PBS_ID!\\n\\nReel ID: ' + $('Loop Over Items').item.json.postID + '\\nKeyword: ' + $('Loop Over Items').item.json.keyword + '\\nCaption: ' + $('Loop Over Items').item.json.caption }}\",\n \"space\": \"automation\",\n \"timestamp\": \"{{ $now.toFormat('MMM dd, HH:mm:ss') }}\"\n}", "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ 688, 480 ], "id": "b0e95fe0-bf50-4bb2-8dbf-4aa9d04f57bf", "name": "set_Send_FK_Error_Msg" }, { "parameters": { "workflowId": { "__rl": true, "value": "P465jEMJyKNZGOHVppbv2", "mode": "list", "cachedResultUrl": "/workflow/P465jEMJyKNZGOHVppbv2", "cachedResultName": "SendPBSChat_New" }, "workflowInputs": { "mappingMode": "defineBelow", "value": {}, "matchingColumns": [], "schema": [], "attemptToConvertTypes": false, "convertFieldsToString": true }, "options": {} }, "type": "n8n-nodes-base.executeWorkflow", "typeVersion": 1.3, "position": [ 880, 480 ], "id": "dcb5a7fd-11e7-4efb-ac6b-6d0356c471fc", "name": "Call_Send_FK_Error_Msg" }, { "parameters": { "mode": "raw", "jsonOutput": "={ \n \"title\": \"Update Insta Data Problem ⚠️\",\n \"message\": \"{{\"This post did not contain a pbs_ID!\\n\\nReel ID: \" + $('Loop Over Items').item.json.postID + \"\\nKeyword: \" + $('Loop Over Items').item.json.keyword + \"\\nCaption: \" + $('Loop Over Items').item.json.caption }}\",\n \"space\": \"automation\",\n \"timestamp\": \"{{ $now.toFormat('MMM dd, HH:mm:ss') }}\"\n}", "includeOtherFields": "={{ false }}", "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ 960, 208 ], "id": "745e81ae-6c88-4069-a7c0-18767f055bbd", "name": "set_Send_No_ID_Msg" }, { "parameters": { "workflowId": { "__rl": true, "value": "P465jEMJyKNZGOHVppbv2", "mode": "list", "cachedResultUrl": "/workflow/P465jEMJyKNZGOHVppbv2", "cachedResultName": "SendPBSChat_New" }, "workflowInputs": { "mappingMode": "defineBelow", "value": {}, "matchingColumns": [], "schema": [], "attemptToConvertTypes": false, "convertFieldsToString": true }, "options": {} }, "type": "n8n-nodes-base.executeWorkflow", "typeVersion": 1.3, "position": [ 1152, 208 ], "id": "1ac17c7e-6105-4541-816c-df116212405c", "name": "Call_Send_No_ID_Msg" }, { "parameters": { "workflowId": { "__rl": true, "value": "P465jEMJyKNZGOHVppbv2", "mode": "list", "cachedResultUrl": "/workflow/P465jEMJyKNZGOHVppbv2", "cachedResultName": "SendPBSChat_New" }, "workflowInputs": { "mappingMode": "defineBelow", "value": {}, "matchingColumns": [], "schema": [], "attemptToConvertTypes": false, "convertFieldsToString": true }, "options": {} }, "type": "n8n-nodes-base.executeWorkflow", "typeVersion": 1.3, "position": [ -16, -32 ], "id": "56a2f297-371a-4e73-9ae9-5ae4b8cb3bf5", "name": "Call 'SendPBSChat_New'", "executeOnce": true }, { "parameters": { "mode": "raw", "jsonOutput": "={\n \"title\": \"Done processing all records ✅\",\n \"message\": \"Updated Hub Content with new instagram posts.\"\n \"timestamp\": \"{{ $now.toFormat('MMM dd, HH:mm:ss') }}\",\n \"space\": \"automation\"\n}", "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -192, -32 ], "id": "39857ac8-bbb0-40a4-88f0-1307ef13783b", "name": "set_pbs_update_success", "executeOnce": true } ], "pinData": { "Enter_Cache_Update": [ { "json": { "webhook_payload": { "headers": { "host": "n8n.plantbasedsoutherner.com", "user-agent": "Webhooks/1.0 (https://fb.me/webhooks)", "content-length": "305", "accept": "*/*", "content-type": "application/json", "origin": "https://n8n.plantbasedsoutherner.com", "x-forwarded-for": "173.252.95.57", "x-forwarded-host": "n8n.plantbasedsoutherner.com", "x-forwarded-port": "443", "x-forwarded-proto": "https", "x-forwarded-server": "874112b5743e", "x-hub-signature": "sha1=6e7fff130640ae3f922551d722fcaec4c13c7684", "x-hub-signature-256": "sha256=2aa0cbd4858e868b2ddc4a335f99dc1999d58c1fed5bfac81eb7b82ec7498f43", "x-real-ip": "173.252.95.57", "accept-encoding": "gzip" }, "params": {}, "query": {}, "body": { "entry": [ { "id": "17841470338696118", "time": 1775342848, "changes": [ { "value": { "from": { "id": "26460101473680406", "username": "samdoucet" }, "media": { "id": "18111441403805423", "media_product_type": "FEED" }, "id": "18081936362075987", "text": "Easter" }, "field": "comments" } ] } ], "object": "instagram" }, "webhookUrl": "https://n8n.plantbasedsoutherner.com/webhook/pi37K-qE4hpB-ncEGa5ph-BYoFD-fTBjmtc", "executionMode": "production" }, "resolved_media_id": "18111441403805423", "instagram_caption": "Easter just got a whole lot easier 💛\n\nIf you’re anything like me, you want a table that feels cozy, familiar, and full without spending the whole day in the kitchen.\n\nSo I pulled together a full plant-based Southern Easter menu.\n\nThe kind of dishes that feel like home, just a little lighter: banana pudding, carrot cake, herby pasta salad, bright spring sides, and yes… biscuits.\n\nEverything you need, all in one place.\n\n✨ 👉👉Want the recipes + grocery list?\n\nComment EASTER and I’ll send it your way 💛\n\nFrom my kitchen to yours—Happy Easter, y’all 🌸\n\n#plantbased #eastermenu #southerncooking #Easter #vegancomfortfood", "reel_keyword": "easter", "reel_wp_post_id": null, "postID": "18111441403805423", "caption": "Easter just got a whole lot easier 💛\n\nIf you’re anything like me, you want a table that feels cozy, familiar, and full without spending the whole day in the kitchen.\n\nSo I pulled together a full plant-based Southern Easter menu.\n\nThe kind of dishes that feel like home, just a little lighter: banana pudding, carrot cake, herby pasta salad, bright spring sides, and yes… biscuits.\n\nEverything you need, all in one place.\n\n✨ 👉👉Want the recipes + grocery list?\n\nComment EASTER and I’ll send it your way 💛\n\nFrom my kitchen to yours—Happy Easter, y’all 🌸\n\n#plantbased #eastermenu #southerncooking #Easter #vegancomfortfood", "timestamp": "2026-04-03T23:14:45+0000" }, "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_Post_Info", "type": "main", "index": 0 } ] ] }, "Loop Over Items": { "main": [ [ { "node": "set_pbs_update_success", "type": "main", "index": 0 } ], [ { "node": "qry_pbs_recipe", "type": "main", "index": 0 } ] ] }, "qry_pbs_recipe": { "main": [ [ { "node": "Check_Data_Return", "type": "main", "index": 0 } ] ] }, "done processing message": { "main": [ [] ] }, "Enter_Cache_Update": { "main": [ [ { "node": "Set_Reply_Data", "type": "main", "index": 0 } ] ] }, "JS_Post_Info": { "main": [ [ { "node": "JS_Code_and_keyword", "type": "main", "index": 0 } ] ] }, "JS_Code_and_keyword": { "main": [ [ { "node": "Loop Over Items", "type": "main", "index": 0 } ] ] }, "JS_Set_Failure": { "main": [ [ { "node": "Check_Null_ID", "type": "main", "index": 0 } ] ] }, "Check_Data_Return": { "main": [ [ { "node": "SQL_Up_Insta_Table1", "type": "main", "index": 0 } ], [ { "node": "JS_Set_Failure", "type": "main", "index": 0 } ] ] }, "Set_Reply_Data": { "main": [ [ { "node": "JS_Post_Info", "type": "main", "index": 0 } ] ] }, "SQL_Up_Insta_Table1": { "main": [ [ { "node": "Loop Over Items", "type": "main", "index": 0 } ] ] }, "Check_Null_ID": { "main": [ [ { "node": "qry_Insta_Data", "type": "main", "index": 0 } ], [ { "node": "set_Send_FK_Error_Msg", "type": "main", "index": 0 } ] ] }, "Check_Insta_Exists": { "main": [ [ { "node": "Loop Over Items", "type": "main", "index": 0 } ], [ { "node": "set_Send_No_ID_Msg", "type": "main", "index": 0 } ] ] }, "qry_Insta_Data": { "main": [ [ { "node": "Check_Insta_Exists", "type": "main", "index": 0 } ] ] }, "set_Send_FK_Error_Msg": { "main": [ [ { "node": "Call_Send_FK_Error_Msg", "type": "main", "index": 0 } ] ] }, "Call_Send_FK_Error_Msg": { "main": [ [ { "node": "Loop Over Items", "type": "main", "index": 0 } ] ] }, "set_Send_No_ID_Msg": { "main": [ [ { "node": "Call_Send_No_ID_Msg", "type": "main", "index": 0 } ] ] }, "Call_Send_No_ID_Msg": { "main": [ [ { "node": "SQL_Up_Insta_Table1", "type": "main", "index": 0 } ] ] }, "set_pbs_update_success": { "main": [ [ { "node": "Call 'SendPBSChat_New'", "type": "main", "index": 0 } ] ] }, "Call 'SendPBSChat_New'": { "main": [ [ { "node": "Set_Return", "type": "main", "index": 0 } ] ] } }, "active": true, "settings": { "executionOrder": "v1", "availableInMCP": false }, "versionId": "3a99224f-f7da-40ed-b1de-2cd787c42150", "meta": { "templateCredsSetupCompleted": true, "instanceId": "eeb18e777509f7207ee9abd45845e7fbf014989719160f3ed6ae633d872bf6c4" }, "id": "_bFiVQvN0skfuU6KlNtDO", "tags": [] }