{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "fd5e8ae4-3345-41e4-b08b-9a12025d3b4e",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        0,
        -48
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "fd488b4b-33c4-4965-a7ce-0ef0469058c9",
      "name": "Set Context (Chat)",
      "type": "n8n-nodes-base.set",
      "position": [
        224,
        -48
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "bot_owner_name",
              "type": "string",
              "value": "Nguyen Thieu Toan (Jay Nguyen)"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3be6dea1-a1a0-4d7a-b96b-9e20bedb4524",
      "name": "Chat AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        464,
        -48
      ],
      "parameters": {
        "text": "={{ JSON.stringify($('Telegram Trigger').item.json.message.text) }}",
        "options": {
          "systemMessage": "=You are the AI Assistant for {{ $('Set Context (Chat)').item.json.bot_owner_name }}. Help answer specific questions concisely and effectively.\n\nOutput: Plain text only. No markdown, no HTML.\nStyle: Concise, efficient, straight to the point.\nCurrent time: {{ $now }}"
        },
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "c9cac707-a4fb-4785-a490-6cf2fda71f67",
      "name": "Send Telegram Reply",
      "type": "n8n-nodes-base.telegram",
      "position": [
        944,
        -48
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "037426c6-de79-45c3-88de-0ef2bc47f6a7",
      "name": "MongoDB Chat Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryMongoDbChat",
      "position": [
        480,
        176
      ],
      "parameters": {
        "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "databaseName": "n8n_memory_db",
        "sessionIdType": "customKey",
        "contextWindowLength": 15
      },
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "48a65a02-6c4e-4839-b2f8-c62e9eadabef",
      "name": "SerpAPI Search",
      "type": "n8n-nodes-serpapi.serpApiTool",
      "position": [
        608,
        176
      ],
      "parameters": {
        "q": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Search_Query__q_', `Search keyword`, 'string') }}",
        "operation": "google_ai_mode",
        "requestOptions": {},
        "descriptionType": "manual",
        "toolDescription": "Use when needing to deeply research or investigate a specific topic.",
        "additionalFields": {}
      },
      "credentials": {
        "serpApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5ced98fe-ad1d-443f-91fb-0961aa2720a0",
      "name": "Scrape Specific Page Tool",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        736,
        176
      ],
      "parameters": {
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "Smart Facebook News Monitor & AI Telegram Assistant",
          "cachedResultName": "Smart Facebook News Monitor & AI Telegram Assistant"
        },
        "description": "Use when the user requests to scrape news or updates from a specific Facebook page.",
        "workflowInputs": {
          "value": {},
          "schema": [],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "55e13f3d-d033-4326-b291-6d46b006f5d1",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        0,
        448
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 3
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "ffc1d605-0673-4e16-96d2-2d5b2ad8ecf7",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        0,
        640
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "url"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "670c5727-7532-4107-9995-964a7a5da167",
      "name": "Set Context (Scraper)",
      "type": "n8n-nodes-base.set",
      "position": [
        352,
        544
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "page_url",
              "type": "string",
              "value": "={{ $json.url || 'https://www.facebook.com/genstaff.net' }}"
            },
            {
              "id": "a2",
              "name": "page_name",
              "type": "string",
              "value": "GenStaff"
            },
            {
              "id": "a3",
              "name": "admin_chat_id",
              "type": "string",
              "value": "[YOUR_TELEGRAM_CHAT_ID]"
            },
            {
              "id": "a4",
              "name": "posts_limit",
              "type": "number",
              "value": 3
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a09768c6-f879-4b75-86ed-3e6d465d06ef",
      "name": "Apify Facebook Scraper",
      "type": "@apify/n8n-nodes-apify.apify",
      "position": [
        656,
        544
      ],
      "parameters": {
        "build": "latest",
        "memory": 4096,
        "actorId": {
          "__rl": true,
          "mode": "list",
          "value": "KoJrdxJCTtpon81KY",
          "cachedResultName": "Facebook Posts Scraper (apify/facebook-posts-scraper)"
        },
        "operation": "Run actor and get dataset",
        "customBody": "={{ JSON.stringify({ captionText: false, resultsLimit: $json.posts_limit, startUrls: [{ url: $json.page_url }] }) }}"
      },
      "credentials": {
        "apifyApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f8a822ae-e279-410e-9278-8144829cc0c7",
      "name": "Normalize Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        864,
        544
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "bcb5ed77-2508-412e-a0be-f4733a9148e3",
              "name": "postId",
              "type": "number",
              "value": "={{ $json.postId }}"
            },
            {
              "id": "b363794a-6d7e-415a-8028-f873d399a8ca",
              "name": "pageName",
              "type": "string",
              "value": "={{ $json.pageName }}"
            },
            {
              "id": "574d1402-ee74-4f8c-83a6-88f41f6cbbfb",
              "name": "url",
              "type": "string",
              "value": "={{ $json.url }}"
            },
            {
              "id": "d84c9c5d-cc71-41b3-a1e3-4d73b221aedf",
              "name": "text",
              "type": "string",
              "value": "={{ $json.text }}"
            },
            {
              "id": "ee0fb812-0eeb-4248-aa7c-e48e879e785f",
              "name": "likes",
              "type": "number",
              "value": "={{ $json.likes }}"
            },
            {
              "id": "bc697b26-7a60-4b4d-8087-1d7ebaccd4b6",
              "name": "comments",
              "type": "number",
              "value": "={{ $json.comments }}"
            },
            {
              "id": "de19c964-a2a8-4d9a-9455-38a5eb67bfd2",
              "name": "shares",
              "type": "number",
              "value": "={{ $json.shares }}"
            },
            {
              "id": "821a3016-8c0d-4151-9b55-a26bd9f8a8de",
              "name": "reactionLikeCount",
              "type": "number",
              "value": "={{ $json.reactionLikeCount }}"
            },
            {
              "id": "a5aff4bd-54c3-4650-9f6d-a0847840c770",
              "name": "reactionHahaCount",
              "type": "number",
              "value": "={{ $json.reactionHahaCount }}"
            },
            {
              "id": "55d84e1f-3156-4246-b296-faff8a509538",
              "name": "reactionLoveCount",
              "type": "number",
              "value": "={{ $json.reactionLoveCount }}"
            },
            {
              "id": "1ac72ba6-b9a7-4d13-9984-d09f4b0bbb66",
              "name": "image",
              "type": "string",
              "value": "={{ $json.media[0].thumbnail||\"\" }}"
            },
            {
              "id": "975cebf7-b9f2-4519-b5b6-59615d2e4967",
              "name": "reactionSadCount",
              "type": "string",
              "value": "={{ $json.reactionSadCount||\"0\" }}"
            },
            {
              "id": "b432170b-534b-4488-b761-4b0976e29b24",
              "name": "reactionAngryCount",
              "type": "string",
              "value": "={{ $json.reactionAngryCount||\"0\"  }}"
            },
            {
              "id": "fb3116d1-617c-413f-843f-bcc76f498987",
              "name": "reactionWowCount",
              "type": "string",
              "value": "={{ $json.reactionWowCount ||\"0\" }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "34803b69-b0fe-405a-be29-c0b9559dc99f",
      "name": "Data Table Upsert",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        1200,
        544
      ],
      "parameters": {
        "columns": {
          "value": {
            "postId": "={{ $json.postId }}"
          },
          "schema": [
            {
              "id": "postId",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "postId",
              "defaultMatch": false
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "postId"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "filters": {
          "conditions": [
            {
              "keyName": "=postId",
              "keyValue": "={{ $json.postId }}"
            }
          ]
        },
        "options": {},
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "dataTable_ID",
          "cachedResultName": "facebook_news_db"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "5a69477d-e865-40a7-8202-686bba2a418d",
      "name": "Format Post Content",
      "type": "n8n-nodes-base.set",
      "position": [
        1696,
        448
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "4babed47-e50b-476a-8594-5ecbbbf444b8",
              "name": "PostContent",
              "type": "string",
              "value": "=Content: {{ $('Normalize Fields').item.json.text }}\nReaction: {{ $('Normalize Fields').item.json.likes }} ({{ $('Normalize Fields').item.json.reactionLikeCount }} like, {{ $('Normalize Fields').item.json.reactionHahaCount }} Haha, {{ $('Normalize Fields').item.json.reactionLoveCount }} Love, {{ $('Normalize Fields').item.json.reactionSadCount }} Sad, {{ $('Normalize Fields').item.json.reactionWowCount }} Wow, {{ $('Normalize Fields').item.json.reactionAngryCount }} Angry)\nLink: {{ $('Normalize Fields').item.json.url }}\n"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "c50cdc56-067f-4432-ae5a-fd5b3acf55bc",
      "name": "Has Image?",
      "type": "n8n-nodes-base.filter",
      "position": [
        1472,
        640
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d75e464e-2f2d-4d6f-bdf3-aba2c891fa85",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $('Normalize Fields').item.json.image }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "765718e7-cec7-4169-a188-009cba790991",
      "name": "Download Image",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1696,
        640
      ],
      "parameters": {
        "url": "={{ $('Normalize Fields').item.json.image }}",
        "options": {}
      },
      "typeVersion": 4.4
    },
    {
      "id": "f91c8771-1ffc-4682-a490-06a6e672bc76",
      "name": "Aggregate Text",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1920,
        448
      ],
      "parameters": {
        "options": {},
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "PostContent"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4952bce8-2cd2-4cf7-8dc4-e80e17a93eca",
      "name": "Aggregate Images",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1920,
        640
      ],
      "parameters": {
        "options": {
          "includeBinaries": true
        },
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "data"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d53c31c3-1a60-4d84-ad99-f64d976b8c76",
      "name": "Merge Data",
      "type": "n8n-nodes-base.merge",
      "position": [
        2144,
        544
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "9732de78-ff30-418b-9b72-b67c216531e4",
      "name": "Final Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        2368,
        544
      ],
      "parameters": {
        "options": {
          "includeBinaries": true
        },
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "PostContent"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9af536c6-b5d8-48a9-b867-ee508a11dbca",
      "name": "Safety Analysis Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2656,
        544
      ],
      "parameters": {
        "text": "={{ JSON.stringify($json.PostContent) }}",
        "options": {
          "systemMessage": "=You are a Cybersecurity and Information Safety Analyst.\nYou will receive the latest posts and attached images from the page {{ $('Set Context (Scraper)').item.json.page_name }}. Evaluate the content, assess user reactions, and determine if the post is safe or contains toxic/offensive material.\n\nReturn structured JSON data as follows:\n{\"output\":\n[\n{\n\"title\": \"Post Title\",\n\"content\": \"Content Summary\",\n\"comment\": \"Evaluation of the post and user reactions\",\n\"link\":\"https://link-to-post\",\n\"review\": \"safe/unsafe\"\n}\n]\n}\n\nConstraints:\n\"title\": Short, clear title.\n\"content\": Brief summary of main points.\n\"comment\": Your professional assessment based on content and interaction count.\n\"link\": URL to the post.\n\"review\": Strict string value 'safe' or 'unsafe'.\n\nAll fields must be plain text, no markdown, no HTML.",
          "passthroughBinaryImages": true
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 3.1
    },
    {
      "id": "73c25c98-93a4-43ae-a7ea-d316de4485e4",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        2848,
        768
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\"output\":\n[\n{\n\"title\": \"Post Title\",\n\"content\": \"Content Summary\",\n\"comment\": \"Evaluation of the post and user reactions\",\n\"link\":\"https://link-to-post\",\n\"review\": \"safe/unsafe\"\n}\n]\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "d2f71691-7927-4f56-853f-0081662469a4",
      "name": "MongoDB Chat Memory 1",
      "type": "@n8n/n8n-nodes-langchain.memoryMongoDbChat",
      "position": [
        2672,
        784
      ],
      "parameters": {
        "sessionKey": "={{ $('Set Context (Scraper)').item.json.admin_chat_id }}",
        "databaseName": "n8n_memory_db",
        "sessionIdType": "customKey",
        "contextWindowLength": 15
      },
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "6faf6c56-c3df-4a91-b8ef-06d2427a587e",
      "name": "Format Telegram Message",
      "type": "n8n-nodes-base.code",
      "position": [
        3216,
        544
      ],
      "parameters": {
        "jsCode": "function escapeHtml(text) {\n  if (!text) return '';\n  return text\n    .replace(/&/g, \"&amp;\")\n    .replace(/</g, \"&lt;\")\n    .replace(/>/g, \"&gt;\");\n}\n\nconst inputData = $input.first().json;\nconst records = inputData?.output?.output || inputData?.output || [];\nconst safeRecords = Array.isArray(records) ? records : [records];\n\nlet message = \"<b>\ud83d\udcca POST MONITORING SUMMARY</b>\\n\\n\";\n\nsafeRecords.forEach((item, index) => {\n  const title = escapeHtml(item.title).toUpperCase();\n  const content = escapeHtml(item.content);\n  const comment = escapeHtml(item.comment);\n  const link = item.link;\n  \n  const isSafe = item.review && item.review.toLowerCase() === 'safe';\n  const reviewLabel = isSafe ? \"\u2705 Safe\" : \"\u26a0\ufe0f Flagged / Toxic\";\n\n  message += `<b>${index + 1}. ${title}</b>\\n`;\n  message += `\ud83d\udcdd <i>${content}</i>\\n`;\n  message += `\ud83d\udcac ${comment}\\n`;\n  message += `\ud83d\udee1\ufe0f Evaluation: <b>${reviewLabel}</b>\\n`;\n  message += `\ud83d\udd17 <a href=\"${link}\">View Post</a>\\n`;\n  message += `\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\\n\\n`;\n});\n\nreturn {\n  json: {\n    telegram_message: message.trim()\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "f3874b78-ab5b-4bee-b7f0-cccebc340e33",
      "name": "Send Notification",
      "type": "n8n-nodes-base.telegram",
      "position": [
        3440,
        544
      ],
      "parameters": {
        "text": "={{ $json.telegram_message }}",
        "chatId": "={{ $('Set Context (Scraper)').item.json.admin_chat_id }}",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "919c0c73-81c2-4528-a471-309f6f7b7ba3",
      "name": "Main Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        -160
      ],
      "parameters": {
        "width": 550,
        "height": 792,
        "content": "## Smart Facebook News Monitor & AI Telegram Assistant\n\nThis template provides a comprehensive dual-flow system connecting an automated Facebook Page safety monitor with a highly interactive AI Telegram chatbot.\n\n### How it works\n1. **Interactive Chatbot:** A Telegram trigger listens for user queries. The Gemini-powered AI Agent, equipped with long-term MongoDB memory, provides conversational responses and can execute specialized search tools.\n2. **Scheduled Scraper:** At defined intervals, an Apify Actor extracts the latest posts from a designated Facebook page.\n3. **Data Normalization:** Raw data is structured and upserted into an n8n Data Table to prevent processing duplicate posts.\n4. **Safety Analysis:** Post content and binary images are merged and passed to an AI Safety Agent. It forces structured JSON output to evaluate whether the post context and user reactions are safe or toxic.\n5. **Smart Notification:** The safety evaluation is compiled into a clean, emoji-rich HTML format and dispatched as a Telegram notification to the administrator.\n\n### Setup\n* [ ] Update variables in the **Set Context (Chat)** and **Set Context (Scraper)** nodes (e.g., your Telegram Admin ID and target Facebook URL).\n* [ ] Connect your **Telegram Bot API** credentials.\n* [ ] Connect your **Google Gemini** API key in the Language Model nodes.\n* [ ] Connect your **Apify** API credentials for the scraper.\n* [ ] Set up your **MongoDB** connection to enable persistent chatbot memory.\n* [ ] Ensure your Data Table is created and selected in the Upsert node.\n\n### LICENCE\nThis template is shared free of charge. Copyright belongs to Nguyen Thieu Toan. Any copying or modification must credit the author."
      },
      "typeVersion": 1
    },
    {
      "id": "1cd75386-bbdb-4f54-a363-539d47c7a53e",
      "name": "Section 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        -160
      ],
      "parameters": {
        "color": 7,
        "width": 1388,
        "height": 480,
        "content": "## Section 1: Interactive AI Telegram Chatbot\nHandles real-time user inquiries via Telegram. The AI Agent uses conversation memory to maintain context and invokes external tools (like SerpAPI) when deeper research is required."
      },
      "typeVersion": 1
    },
    {
      "id": "103aed3b-beb3-47f1-b7ed-3eceb250253a",
      "name": "Section 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        352
      ],
      "parameters": {
        "color": 7,
        "width": 1400,
        "height": 642,
        "content": "## Section 2: Scrape & Store Pipeline\nExecutes on a schedule (or triggered by another workflow) to scrape recent Facebook posts. Normalizes the JSON output and upserts the data into an n8n Data Table to ensure only new posts proceed."
      },
      "typeVersion": 1
    },
    {
      "id": "317745bb-ad12-42b7-ac38-d6f8912dd9ea",
      "name": "Section 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1376,
        352
      ],
      "parameters": {
        "color": 7,
        "width": 2276,
        "height": 650,
        "content": "## Section 3: AI Safety Analysis & Notification\nAggregates the post text and downloads associated binary images. The AI Safety Agent processes the multimedia context to determine the safety rating, formats a structured report, and sends it directly to the admin's Telegram."
      },
      "typeVersion": 1
    },
    {
      "id": "3306520d-0b17-46fc-bb98-5935c7990dfb",
      "name": "Edit Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        256,
        688
      ],
      "parameters": {
        "color": 3,
        "width": 280,
        "height": 164,
        "content": "## \u26a0\ufe0f Edit this node!\n\nOpen this node to set up your specific configurations:\n- Target Facebook Page URL\n- Telegram Admin ID\n- Scraper post limits"
      },
      "typeVersion": 1
    },
    {
      "id": "abfad2bd-ee1f-4266-bc68-429a238ca4b9",
      "name": "Author Message",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        672
      ],
      "parameters": {
        "color": 4,
        "width": 550,
        "height": 400,
        "content": "## Author Message\n\nHi! I am **Nguyen Thieu Toan (Jay Nguyen)** \u2014 an n8n Verified Creator and CEO/Founder of **GenStaff** (*AI Staff. Always On. Always Yours.*). Thank you for using this workflow template!\n\nThis template is shared to help you streamline your operations. If it optimizes your processes or saves you time, feel free to connect with me:\n\n* **Website:** [nguyenthieutoan.com](https://nguyenthieutoan.com)\n* **Email:** me@nguyenthieutoan.com\n* **Company:** GenStaff ([genstaff.net](https://genstaff.net))\n* **Socials (Facebook / X / LinkedIn):** @nguyenthieutoan\n\n*Discover more of my automation solutions:* **[n8n.io/creators/nguyenthieutoan](https://n8n.io/creators/nguyenthieutoan)**"
      },
      "typeVersion": 1
    },
    {
      "id": "f853d3ab-475a-4e00-817e-052d694976fc",
      "name": "If row does not exist",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        1040,
        544
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "=postId",
              "keyValue": "={{ $json.postId }}"
            }
          ]
        },
        "operation": "rowNotExists",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "ABoEQLUgMQzMfo4U",
          "cachedResultUrl": "/projects/mYtIzVb8cWfhFOhJ/datatables/ABoEQLUgMQzMfo4U",
          "cachedResultName": "bacninhNews_db"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "45e3694f-bbf8-4617-9d71-fe5c4195ebaa",
      "name": "Edit Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        48,
        112
      ],
      "parameters": {
        "color": 3,
        "width": 280,
        "height": 164,
        "content": "## \u26a0\ufe0f Edit this node!\n\nOpen this node to set up your specific configurations:\n- Target Facebook Page URL\n- Telegram Admin ID\n- Scraper post limits"
      },
      "typeVersion": 1
    },
    {
      "id": "4d53a30e-aa94-41f3-9872-1e673fdabe95",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        2480,
        800
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-3.1-flash-lite"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "e05bdb5d-200e-4122-b03e-3236b966019f",
      "name": "Google Gemini Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        368,
        176
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-3.1-flash-lite"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    }
  ],
  "connections": {
    "Has Image?": {
      "main": [
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Data": {
      "main": [
        [
          {
            "node": "Final Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chat AI Agent": {
      "main": [
        [
          {
            "node": "Send Telegram Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Text": {
      "main": [
        [
          {
            "node": "Merge Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Image": {
      "main": [
        [
          {
            "node": "Aggregate Images",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SerpAPI Search": {
      "ai_tool": [
        [
          {
            "node": "Chat AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Final Aggregate": {
      "main": [
        [
          {
            "node": "Safety Analysis Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Images": {
      "main": [
        [
          {
            "node": "Merge Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Normalize Fields": {
      "main": [
        [
          {
            "node": "If row does not exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Set Context (Scraper)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Set Context (Chat)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Data Table Upsert": {
      "main": [
        [
          {
            "node": "Format Post Content",
            "type": "main",
            "index": 0
          },
          {
            "node": "Has Image?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Context (Chat)": {
      "main": [
        [
          {
            "node": "Chat AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Post Content": {
      "main": [
        [
          {
            "node": "Aggregate Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "Chat AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "If row does not exist": {
      "main": [
        [
          {
            "node": "Data Table Upsert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB Chat Memory 1": {
      "ai_memory": [
        [
          {
            "node": "Safety Analysis Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Safety Analysis Agent": {
      "main": [
        [
          {
            "node": "Format Telegram Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Context (Scraper)": {
      "main": [
        [
          {
            "node": "Apify Facebook Scraper",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apify Facebook Scraper": {
      "main": [
        [
          {
            "node": "Normalize Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Telegram Message": {
      "main": [
        [
          {
            "node": "Send Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Safety Analysis Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Structured Output Parser",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Safety Analysis Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Chat AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Scrape Specific Page Tool": {
      "ai_tool": [
        [
          {
            "node": "Chat AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Set Context (Scraper)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}