{ "name": "Sub-instra-reply-handle-new", "nodes": [ { "parameters": { "inputSource": "passthrough" }, "id": "bc9bd3d4-2a46-42ff-bae7-9e430777b7bb", "typeVersion": 1.1, "name": "Enter_Workflow", "type": "n8n-nodes-base.executeWorkflowTrigger", "position": [ -768, 128 ] }, { "parameters": { "jsCode": "const value = $('Enter_Workflow').first().json.webhook_payload.body.entry[0].changes[0].value;\n\nconst resolvedMediaId = value.media?.original_media_id || value.media?.id;\n\nreturn [{\n json: {\n ...$input.first().json,\n resolved_media_id: resolvedMediaId\n }\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -544, 128 ], "id": "c692e1f0-4ee1-4c87-9ce2-a5330497d15c", "name": "Get_Media_ID" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "c4be9e7f-8c56-442e-8a0f-b31bc917f6e0", "leftValue": "={{ $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.text }}\n", "rightValue": "\\brecipe\\b", "operator": { "type": "string", "operation": "regex" } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 928, 32 ], "id": "e77f8544-1e55-473b-8934-0b0a596ffe27", "name": "Check_if_Recipe" }, { "parameters": { "jsCode": "// 1. Grab the comment from the incoming Webhook\nconst commentText = $('Enter_Workflow').first().json.webhook_payload.body.entry[0].changes[0].value.text\n\n\n// 3. Find the matching recipe\n // 1. Grab the keyword directly from the object\n const kw = $('Merge').first().json.reel_keyword || $('Merge').first().json.keyword || null;\n // 3. Run the match against the comment\n const regex = new RegExp(`\\\\b${kw}\\\\b`, 'i'); // Fixed: parentheses instead of backtick\n const match = regex.test(commentText);\n\n// 4. Return the result\nif (match) {\n return {\n found: true,\n };\n} else {\n return { found: false };\n}" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1136, 160 ], "id": "8bbdca4c-8ee4-4809-b10a-5559e6ed6562", "name": "Get_Msg_Keyword" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "7694f195-eabf-46ea-915f-fbfd8b49f30b", "leftValue": "={{ $json.found }}", "rightValue": true, "operator": { "type": "boolean", "operation": "equals" } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 1328, 160 ], "id": "68dfc980-fac5-4fa7-af2a-1b7ac2ec3851", "name": "If_Keyword_Used" }, { "parameters": { "method": "POST", "url": "https://n8n.staging.plantbasedsoutherner.com/webhook/staging-test", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "title", "value": "=Get Reel Data Error ⚠️" }, { "name": "message", "value": "={{ 'Did not find a record!\\nReel ID: ' + $('Get_Media_ID').item.json.resolved_media_id+ '\\nCommenter: @' + $('Enter_Workflow').first().json.webhook_payload.body.entry[0].changes[0].value.from.username + '\\ncomment: '+ $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.text}}\n" }, { "name": "timestamp", "value": "={{ $now.toFormat('MMM dd, HH:mm:ss') }}" } ] }, "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 1312, 848 ], "id": "581dd3e0-8544-4e78-9874-dc587e89774c", "name": "Send_Data_Error_Msg" }, { "parameters": { "method": "POST", "url": "https://n8n.staging.plantbasedsoutherner.com/webhook/staging-test", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "title", "value": "Send Message Problem ⚠️" }, { "name": "message", "value": "={{ \"This message did not trigger an auto recipe reply!\\n\\nReel ID: \" + $('Get_Media_ID').item.json.resolved_media_id + \"\\nCommenter: @\" + $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.from.username + \"\\nComment: \" + $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.text }}" }, { "name": "timestamp", "value": "={{ $now.toFormat('MMM dd, HH:mm:ss') }}" } ] }, "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 1520, 832 ], "id": "72f6c10b-1c08-494a-8a3c-2b97a0798248", "name": "Send_No_Reply_Msg" }, { "parameters": { "method": "POST", "url": "https://graph.instagram.com/v24.0/26121541700773885/messages", "authentication": "genericCredentialType", "genericAuthType": "httpBearerAuth", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"recipient\": {\n \"comment_id\": \"{{ $json.comment_id }}\"\n },\n \"message\": {\n \"text\": \"{{ $json.dm_message }}\"\n }\n}", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 1712, -144 ], "id": "6149e145-253b-451e-a426-d38a409b9504", "name": "FB_DM_Msg", "credentials": { "httpHeaderAuth": { "id": "bU1D0djQYelYC4km", "name": "gitea-auth" }, "httpBearerAuth": { "id": "y2nNsjj4Bi9JwGHX", "name": "insta-auth" } } }, { "parameters": { "jsCode": "// 1. Get the user who commented\nconst webhookData = $('Enter_Workflow').first().json.webhook_payload.body.entry[0].changes[0].value;\nconst recipientId = webhookData.from.id; // or from.self_ig_scoped_id\nconst commentId = webhookData.id;\nconst username = webhookData.from.username;\n\n// 2. Get the recipe data (from Merge for TRUE path, or from search keyword for FALSE path)\nconst recipeData = $('Merge').first().json;\nconst recipeUrl = recipeData.recipe_url;\nconst recipeTitle = recipeData.recipe_title;\n\n// Array of public reply messages (randomized)\nconst publicReplies = [\n `Sending you the recipe in your DMs! 🌻`,\n `🌻 Check your DMs for the recipe!`,\n `Recipe headed to your inbox! 🌻`,\n `🌻 DM sent with the recipe!`,\n `Recipe incoming! Check your messages 🌻`,\n `🌻 On its way to your DMs!`\n];\n\n// Pick a random reply\nconst randomReply = publicReplies[Math.floor(Math.random() * publicReplies.length)];\n\n// DM message stays consistent\nconst dmMessage = `Here's the recipe! 🌻 ${recipeUrl}`;\n\n// 4. Return data for the HTTP Request node\nreturn {\n json: {\n comment_id: commentId,\n username: username, \n public_reply_message: randomReply, // Random public comment\n dm_message: dmMessage, // Consistent DM message\n recipe_title: recipeTitle,\n recipe_url: recipeUrl\n \n }\n};\n" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1488, -80 ], "id": "21d50229-5490-4904-a9df-3b4db87190ac", "name": "Prep_FB_Msg" }, { "parameters": { "method": "POST", "url": "=https://graph.instagram.com/v24.0/{{ $json.comment_id }}/replies", "authentication": "genericCredentialType", "genericAuthType": "httpBearerAuth", "sendBody": true, "specifyBody": "json", "jsonBody": "={\n \"message\": \"@{{ $json.username }} {{ $json.public_reply_message }}\"\n}", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ 1712, 48 ], "id": "e1a60149-ad78-492f-833a-ef6f7b96c4ee", "name": "FB_Reel_Reply", "credentials": { "httpBearerAuth": { "id": "y2nNsjj4Bi9JwGHX", "name": "insta-auth" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "a552f373-88c2-4cca-aed4-771aad252155", "leftValue": "={{ $json.id }}", "rightValue": "={{0}}", "operator": { "type": "number", "operation": "exists", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 112, 176 ], "id": "b5ab3f2c-f518-474c-bd53-a7e67f0ce970", "name": "Check_Return_Data", "alwaysOutputData": false }, { "parameters": { "operation": "executeQuery", "query": "SELECT IP.*, PR.keyword, PR.recipe_url, PR.recipe_title \n FROM platform_posts AS IP \n LEFT JOIN pbs_recipes as PR on IP.pbs_post_id = PR.post_id \n WHERE IP.platform_post_id = $1", "options": { "queryReplacement": "={{ ($json.resolved_media_id)}}" } }, "type": "n8n-nodes-base.mySql", "typeVersion": 2.5, "position": [ -336, 144 ], "id": "dc2e570e-03e7-4984-8fb1-07212950e6f9", "name": "Get_Reel_Datas", "alwaysOutputData": true, "credentials": { "mySql": { "id": "4ETlVumrAq95ispJ", "name": "MySQL account" } } }, { "parameters": { "mode": "raw", "jsonOutput": "={\n \"data\": [\n {{$input.first().json}}\n ]\n}\n", "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -192, -144 ], "id": "2ea8eb67-7896-4439-a009-949950a5935d", "name": "Set_Reply_Data" }, { "parameters": { "url": "=https://graph.instagram.com/v25.0/{{ $json.resolved_media_id }}?fields=id,caption,timestamp,comments_count", "authentication": "genericCredentialType", "genericAuthType": "httpBearerAuth", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.3, "position": [ -368, -144 ], "id": "a35000ac-c014-4b8f-a7ac-ecb0195688c0", "name": "CallFB_Get_Post_Info", "credentials": { "httpHeaderAuth": { "id": "bU1D0djQYelYC4km", "name": "gitea-auth" }, "httpQueryAuth": { "id": "9poWzM5erRyAfM1e", "name": "Query Auth account" }, "httpBearerAuth": { "id": "y2nNsjj4Bi9JwGHX", "name": "insta-auth" } } }, { "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": [ -32, -144 ], "id": "d1190f3c-9e0c-4dbf-94fb-423cea08aa4a", "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 = item.json.id;\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 const keywordMatch = caption.match(/comment\\s*[\\u201C\\u201D\"'\"\"']?\\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 instagram_post_id: postId,\n instagram_caption: caption,\n reel_keyword: keyword,\n reel_wp_post_id: wpPostId,\n ...item.json\n }\n });\n}\n\nreturn outputItems;" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 128, -144 ], "id": "daa8e4c0-e8a6-4aec-a555-fa4e9d97412a", "name": "JS_Code_and_keyword" }, { "parameters": { "mode": "combine", "combineBy": "combineByPosition", "options": {} }, "type": "n8n-nodes-base.merge", "typeVersion": 3.2, "position": [ -80, 176 ], "id": "35cc2609-31da-4c59-8e82-3e1e16dc7c30", "name": "Merge" }, { "parameters": { "jsCode": "return [{\n json: {\n ...$('Get_Media_ID').first().json,\n ...$('Merge').first().json\n }\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 416, 352 ], "id": "d685d601-f747-479a-862d-9bc568fa0aa0", "name": "Code in JavaScript1" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "72d18483-cc91-4b53-9cc0-bff0ba2df30e", "leftValue": "={{ $json.recipe_url }}", "rightValue": "", "operator": { "type": "string", "operation": "exists", "singleValue": true } }, { "id": "89594b5f-5872-4004-b845-3f8189c94713", "leftValue": "={{ $json.recipe_url }}", "rightValue": "", "operator": { "type": "string", "operation": "notEmpty", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 320, 176 ], "id": "4e833080-8ed2-4c45-a299-33480cdec9ac", "name": "Check_URL" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "5e42803a-6d66-404b-ae45-b3421c1e6f2e", "leftValue": "={{ $json.success }}", "rightValue": false, "operator": { "type": "boolean", "operation": "true", "singleValue": true } } ], "combinator": "and" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ 800, 352 ], "id": "6c4d4448-fa48-4df7-83e2-2a61164953bc", "name": "Return_Sub_ReelCache" }, { "parameters": { "mode": "raw", "jsonOutput": "={ \n \"title\": \"Send Message Problem ⚠️\",\n \"message\": \"{{ \"This message did not trigger an auto recipe reply!\\n\\nReel ID: \" + $('Get_Media_ID').item.json.resolved_media_id + \"\\nCommenter: @\" + $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.from.username + \"\\nComment: \" + $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.text }}\",\n \"link_url\": \"{{ 'https://plantbasedsoutherner.com/admin/platform-posts/' + $('Get_Reel_Datas').first().json.id }}\",\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": [ 1552, 416 ], "id": "6ff7a17a-1263-49a4-baf8-eeaf2a4fc890", "name": "set_Send_No_Reply_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": [ 1888, 496 ], "id": "7917e735-40f5-4202-9ef7-f3a9902575cb", "name": "Call_PBS_Msg" }, { "parameters": { "mode": "raw", "jsonOutput": "={ \n \"title\": \"Get Reel Data Error ⚠️\",\n \"message\": \"{{ 'Did not find a record!\\nReel ID: ' + $('Get_Media_ID').item.json.resolved_media_id+ '\\nCommenter: @' + $('Enter_Workflow').first().json.webhook_payload.body.entry[0].changes[0].value.from.username + '\\ncomment: '+ $('Enter_Workflow').item.json.webhook_payload.body.entry[0].changes[0].value.text}}\",\n \"link_url\": \"{{ 'https://plantbasedsoutherner.com/admin/platform-posts/' + $('Get_Reel_Datas').first().json.id }}\",\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": [ 1280, 576 ], "id": "7d9d04bf-0f6a-4ef3-9ff8-93a59422046a", "name": "set_Send_Data_Error_Msg" }, { "parameters": { "workflowId": { "__rl": true, "value": "_bFiVQvN0skfuU6KlNtDO", "mode": "list", "cachedResultUrl": "/workflow/_bFiVQvN0skfuU6KlNtDO", "cachedResultName": "instra-reel-cache-new" }, "workflowInputs": { "mappingMode": "defineBelow", "value": {}, "matchingColumns": [], "schema": [], "attemptToConvertTypes": false, "convertFieldsToString": true }, "options": { "waitForSubWorkflow": true } }, "type": "n8n-nodes-base.executeWorkflow", "typeVersion": 1.3, "position": [ 624, 352 ], "id": "c032d658-0105-4aad-831d-0ca87127e50d", "name": "Call 'instra-reel-cache-new'" } ], "pinData": { "Enter_Workflow": [ { "json": { "webhook_payload": { "headers": { "host": "n8n.plantbasedsoutherner.com", "user-agent": "Webhooks/1.0 (https://fb.me/webhooks)", "content-length": "321", "accept": "*/*", "content-type": "application/json", "origin": "https://n8n.plantbasedsoutherner.com", "x-forwarded-for": "173.252.95.22", "x-forwarded-host": "n8n.plantbasedsoutherner.com", "x-forwarded-port": "443", "x-forwarded-proto": "https", "x-forwarded-server": "874112b5743e", "x-hub-signature": "sha1=c00e77402d3e1f402656d6d3f7993be3014e0614", "x-hub-signature-256": "sha256=a6262a348114ddf7086e37ea14533d194b79d63c6c9cf3aeac01dc84e9ccc02a", "x-real-ip": "173.252.95.22", "accept-encoding": "gzip" }, "params": {}, "query": {}, "body": { "entry": [ { "id": "17841470338696118", "time": 1780270502, "changes": [ { "value": { "from": { "id": "1383387096491187", "username": "tinyplanteaters" }, "media": { "id": "18008185346730575", "media_product_type": "REELS" }, "id": "17923963638346992", "text": "Yum 😋" }, "field": "comments" } ] } ], "object": "instagram" }, "webhookUrl": "https://n8n.plantbasedsoutherner.com/webhook/pi37K-qE4hpB-ncEGa5ph-BYoFD-fTBjmtc", "executionMode": "production" } }, "pairedItem": { "item": 0 } } ] }, "connections": { "Enter_Workflow": { "main": [ [ { "node": "Get_Media_ID", "type": "main", "index": 0 } ] ] }, "Get_Media_ID": { "main": [ [ { "node": "Get_Reel_Datas", "type": "main", "index": 0 }, { "node": "CallFB_Get_Post_Info", "type": "main", "index": 0 } ] ] }, "Check_if_Recipe": { "main": [ [ { "node": "Prep_FB_Msg", "type": "main", "index": 0 } ], [ { "node": "Get_Msg_Keyword", "type": "main", "index": 0 } ] ] }, "Get_Msg_Keyword": { "main": [ [ { "node": "If_Keyword_Used", "type": "main", "index": 0 } ] ] }, "If_Keyword_Used": { "main": [ [ { "node": "Prep_FB_Msg", "type": "main", "index": 0 } ], [ { "node": "set_Send_No_Reply_Msg", "type": "main", "index": 0 } ] ] }, "Prep_FB_Msg": { "main": [ [ { "node": "FB_DM_Msg", "type": "main", "index": 0 }, { "node": "FB_Reel_Reply", "type": "main", "index": 0 } ] ] }, "Check_Return_Data": { "main": [ [ { "node": "Check_URL", "type": "main", "index": 0 } ], [ { "node": "Code in JavaScript1", "type": "main", "index": 0 } ] ] }, "Get_Reel_Datas": { "main": [ [ { "node": "Merge", "type": "main", "index": 0 } ] ] }, "Set_Reply_Data": { "main": [ [ { "node": "JS_Post_Info", "type": "main", "index": 0 } ] ] }, "CallFB_Get_Post_Info": { "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": "Merge", "type": "main", "index": 1 } ] ] }, "Merge": { "main": [ [ { "node": "Check_Return_Data", "type": "main", "index": 0 } ] ] }, "Code in JavaScript1": { "main": [ [ { "node": "Call 'instra-reel-cache-new'", "type": "main", "index": 0 } ] ] }, "Check_URL": { "main": [ [ { "node": "Check_if_Recipe", "type": "main", "index": 0 } ], [ { "node": "set_Send_Data_Error_Msg", "type": "main", "index": 0 } ] ] }, "Return_Sub_ReelCache": { "main": [ [ { "node": "Check_if_Recipe", "type": "main", "index": 0 } ], [ { "node": "set_Send_Data_Error_Msg", "type": "main", "index": 0 } ] ] }, "set_Send_No_Reply_Msg": { "main": [ [ { "node": "Call_PBS_Msg", "type": "main", "index": 0 } ] ] }, "set_Send_Data_Error_Msg": { "main": [ [ { "node": "Call_PBS_Msg", "type": "main", "index": 0 } ] ] }, "Call 'instra-reel-cache-new'": { "main": [ [ { "node": "Return_Sub_ReelCache", "type": "main", "index": 0 } ] ] } }, "active": true, "settings": { "executionOrder": "v1", "availableInMCP": false }, "versionId": "0aa99e07-3a91-4005-923d-2c88acb71fae", "meta": { "templateCredsSetupCompleted": true, "instanceId": "eeb18e777509f7207ee9abd45845e7fbf014989719160f3ed6ae633d872bf6c4" }, "id": "gtosD7EYCQQ8JoqOaLtUA", "tags": [] }