{
  "id": "kpAcUnFPtxaqsGnZ",
  "name": "n8n Workflow: Daily News Aggregator & MongoDB Storage",
  "tags": [],
  "nodes": [
    {
      "id": "b00241ab-0355-4ada-acdb-56bea4f5d15a",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -80,
        64
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d4b2cdc4-39ec-4d3c-9b76-684cb485540e",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        778,
        -120
      ],
      "parameters": {
        "color": 3,
        "width": 508,
        "height": 200,
        "content": "Final Notification"
      },
      "typeVersion": 1
    },
    {
      "id": "caee7461-f2b4-46d6-ab86-110bd0d85fc4",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        78,
        8
      ],
      "parameters": {
        "color": 6,
        "width": 228,
        "height": 216,
        "content": "News Perplexity"
      },
      "typeVersion": 1
    },
    {
      "id": "1a0c7dc3-dc3c-498b-8567-ae92e9176245",
      "name": "Perplexity",
      "type": "n8n-nodes-base.perplexity",
      "position": [
        144,
        64
      ],
      "parameters": {
        "model": "sonar-pro",
        "options": {
          "searchRecency": "day"
        },
        "messages": {
          "message": [
            {
              "content": "Fetch the latest breaking news from the past few hours, focusing on major headlines, current events, and urgent updates. Prioritize high-impact global news (politics, economics, disasters, technology, conflicts) from reputable sources (e.g., Reuters, AP, BBC, CNN).\n\nReturn the results as a valid JSON array with each article structured as follows:\n\njson\n{\n  \"headline\": \"Concise headline (under 120 chars)\",  \n  \"timestamp\": \"ISO 8601 format (e.g., 2024-03-15T14:30:00Z)\",  \n  \"source\": \"Trusted source name (e.g., BBC News)\",  \n  \"summary\": \"1-2 paragraph neutral summary (avoid opinionated language)\",  \n  \"url\": \"Direct link to the article\",  \n  \"category\": \"Topical category (e.g., politics, technology)\",  \n  \"language\": \"en\",  \n  \"metadata\": {\n    \"priority\": \"high/medium/low\",  \n    \"location\": \"Geo-tag if relevant (e.g., 'Ukraine')\"  \n  }\n}\nRequirements:\n\nExclude paywalled/promotional content.\n\nEnsure timestamps are UTC.\n\nInclude at least 5-10 diverse news items.\n\n\nKey Improvements:\nSpecificity \u2013 Clarifies content focus (global, high-impact) and credible sources.\n\nData Quality \u2013 Explicitly excludes paywalled content and mandates neutral summaries.\n\nStructure \u2013 Uses nested metadata for extensibility (e.g., adding tags later)."
            }
          ]
        },
        "simplify": true,
        "requestOptions": {}
      },
      "credentials": {
        "perplexityApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "eceb97dd-a30a-4237-a992-ebd7e38f67b5",
      "name": "Response Perplexity",
      "type": "n8n-nodes-base.code",
      "position": [
        368,
        64
      ],
      "parameters": {
        "jsCode": "// Get the string with the news array\nlet raw = $json[\"message\"];\n\n// Remove backticks and the \u2018json\u2019 tag if they exist\nraw = raw.replace(/```json|```/g, '').trim();\n\n// Parse the clean string into an array of objects\nconst noticias = JSON.parse(raw);\n\n// Returns each news item as an individual item for the Loop\nreturn noticias.map(noticia => ({ json: noticia }));"
      },
      "typeVersion": 2
    },
    {
      "id": "a77e0c7f-3597-4c98-8730-8af03a3a017b",
      "name": "News",
      "type": "n8n-nodes-base.code",
      "position": [
        816,
        -80
      ],
      "parameters": {
        "jsCode": "\nreturn [\n  {\n    json: {\n      noticias: items.map(item => item.json)\n    }\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "49c51d23-2786-4005-8f99-3df1bd048dc3",
      "name": "Loop Over News",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        592,
        64
      ],
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "typeVersion": 3
    },
    {
      "id": "0051a6ef-16d8-489c-8ad9-b28191a88599",
      "name": "DB News",
      "type": "n8n-nodes-base.mongoDb",
      "notes": "Official Database",
      "position": [
        816,
        112
      ],
      "parameters": {
        "fields": "=headline,timestamp,source,summary,url,category,language,metadata",
        "options": {
          "dateFields": "={{ $json.headline }},{{ $json.timestamp }},{{ $json.source }},{{ $json.summary }},{{ $json.url }},{{ $json.category }},{{ $json.language }},{{ $json.metadata }}"
        },
        "operation": "insert",
        "collection": "news"
      },
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 1.2
    },
    {
      "id": "c3cf8752-d188-44c5-8d85-762620670fe9",
      "name": "Send Message",
      "type": "n8n-nodes-base.gmail",
      "notes": "News registration confirmation",
      "position": [
        1040,
        -80
      ],
      "parameters": {
        "sendTo": "<YOUR EMAIL>",
        "message": "=<!DOCTYPE html> <html lang=\"es\">  <head>     <meta charset=\"UTF-8\">     <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">     <title>News</title>     <style>         body {             font-family: Arial, sans-serif;             background-color: #f4f4f4;             margin: 0;             padding: 0;         }          .container {             background-color: #ffffff;             width: 90%;             max-width: 600px;             margin: 20px auto;             padding: 20px;             border-radius: 5px;             box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);         }          h1 {             color: #333333;             font-size: 22px;         }          p {             color: #555555;             font-size: 16px;             line-height: 1.5;         }          .button {             display: inline-block;             padding: 10px 20px;             background-color: #007bff;             color: #ffffff;             text-decoration: none;             border-radius: 4px;             margin-top: 10px;         }          .footer {             font-size: 12px;             color: #888888;             text-align: center;             margin-top: 20px;         }     </style> </head>  <body>     <div class=\"container\">         <h1>Hi!!</h1>         <p>             We are pleased to inform you that the registration of news in MongoDB was successful!         </p>         <p>Thanks.</p>         <div class=\"footer\">             \u00a9 2025 News World.         </div>     </div> </body>  </html> ",
        "options": {
          "appendAttribution": false
        },
        "subject": "=[ News ] Record of all news items"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 2.1,
      "alwaysOutputData": true
    },
    {
      "id": "da552b54-3fce-416e-bb6e-fcc5e5cacf4f",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1184,
        -464
      ],
      "parameters": {
        "width": 1008,
        "height": 1632,
        "content": "## n8n Workflow: Daily News Aggregator & MongoDB Storage\n\n### **Objective**  \n### Automatically fetch global news from Perplexity once per day, format the data, store it in MongoDB, and send a confirmation email via Gmail.\n\n---\n\n## **Workflow Steps**\n\n### 1. **Trigger: Cron Node**\n- **Frequency**: Runs once daily (e.g., `0 8 * * *` for 8:00 AM UTC).\n- **Action**: Initiates the workflow.\n\n### 2. **Perplexity Node**\n- **Input**: Prompt that specifies the type of news you want along with the JSON format you expect to save in MongoDB with the news information.\n- **Output**: A string from a JSON file with the news obtained. Response containing news articles (headlines, sources, timestamps).\n\n### 3. **Code Node: Data Formatting**\n- **Purpose**: Transforms String Data into a loop-compatible array.\n- **Example Code**:\n  ```javascript\n  // Get the string with the news array\n  let raw = $json[\"message\"];\n  // Remove backticks and the \u2018json\u2019 tag if they exist\n  raw = raw.replace(/```json|```/g, '').trim();\n  // Parse the clean string into an array of objects\n  const noticias = JSON.parse(raw);\n  // Returns each news item as an individual item for the Loop\n  return noticias.map(noticia => ({ json: noticia }));\n\n## 4. Loop Node: MongoDB Insertion\n**Action**: Iterates over each news item and inserts into MongoDB.\n\n**Configuration**:\n- **Collection**: `daily_news`\n- **Operation**: `Insert`  \n- **Fields**: \n  ```json\n  {\n    \"headline\": \"\",\n    \"timestamp\": \"\",\n    \"source\": \"\",\n    \"url\": \"\",\n    \"category\": \"\",\n    \"metadata\": {}\n  }\n\n## 5. Code Node: Aggregation & Notification Prep\n\n- **Purpose**: Wait for the news to be posted and then send a single notification email.\n\n## 6. Email Node (Gmail)\n\n- **Purpose**: Notification email\n\nWorkFlow\n    A[Cron Trigger] --> B[Perplexity API]\n    B --> C[Format Data]\n    C --> D[Loop: MongoDB Insert]\n    D --> E[Aggregate Results]\n    E --> F[Send Email Notification]"
      },
      "typeVersion": 1
    },
    {
      "id": "5f8fde92-09a5-44a2-9e8e-efebfcb84fd9",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        -256
      ],
      "parameters": {
        "color": 7,
        "width": 1504,
        "height": 672,
        "content": ""
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a765923b-a9ab-4567-a08f-132d54451d50",
  "connections": {
    "News": {
      "main": [
        [
          {
            "node": "Send Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DB News": {
      "main": [
        [
          {
            "node": "Loop Over News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Perplexity": {
      "main": [
        [
          {
            "node": "Response Perplexity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Message": {
      "main": [
        []
      ]
    },
    "Loop Over News": {
      "main": [
        [
          {
            "node": "News",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "DB News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Perplexity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Response Perplexity": {
      "main": [
        [
          {
            "node": "Loop Over News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}