{
  "name": "Configurable Topic Newsletter",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "id": "1",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "topic",
              "value": "AI"
            }
          ]
        },
        "options": {}
      },
      "id": "2",
      "name": "Set Topic",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://newsapi.org/v2/everything",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "q",
              "value": "={{$json.topic}}"
            },
            {
              "name": "pageSize",
              "value": "15"
            },
            {
              "name": "from",
              "value": "={{$moment().subtract(7,'days').format('YYYY-MM-DD')}}"
            },
            {
              "name": "sortBy",
              "value": "publishedAt"
            },
            {
              "name": "apiKey",
              "value": "{{ $json.apiKey }}"
            }
          ]
        }
      },
      "id": "3",
      "name": "Fetch News",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const topic = $json.topic;\nconst articles = items[0].json.articles || [];\n\nlet html = `<h1 style=\\\"font-family:sans-serif;color:#333;\\\">\ud83d\udcf0 ${topic} Newsletter</h1>`;\n\narticles.forEach(a => {\n  html += `<h2 style=\\\"color:#0073e6;\\\">${a.title}</h2>`;\n  if (a.description) {\n    html += `<p style=\\\"font-size:14px;color:#555;\\\">${a.description}</p>`;\n  }\n  if (a.urlToImage) {\n    html += `<img src=\\\"${a.urlToImage}\\\" style=\\\"max-width:500px;border-radius:8px;margin:10px 0;\\\"/><br/>`;\n  }\n  html += `<a href=\\\"${a.url}\\\" target=\\\"_blank\\\" style=\\\"font-size:14px;\\\">Read more</a><hr/>`;\n});\n\nreturn [{ json: { html, topic } }];"
      },
      "id": "4",
      "name": "Format Newsletter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const html = $json.html;\n\nconst buffer = Buffer.from(html, \"utf-8\");\n\nreturn [{\n  binary: {\n    data: {\n      data: buffer.toString(\"base64\"),\n      mimeType: \"application/msword\",\n      fileName: \"Newsletter.doc\"\n    }\n  },\n  json: {\n    topic: $json.topic\n  }\n}];"
      },
      "id": "5",
      "name": "Convert to Word",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "youremail@gmail.com",
        "toEmail": "subscriber@example.com",
        "subject": "{{$json.topic}} Newsletter",
        "attachments": [
          "={{$binary.data}}"
        ],
        "content": "Attached is your latest {{$json.topic}} newsletter."
      },
      "id": "6",
      "name": "Send Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 1,
      "position": [
        1500,
        300
      ],
      "credentials": {
        "gmailOAuth2": "<your credential>"
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Set Topic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Topic": {
      "main": [
        [
          {
            "node": "Fetch News",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch News": {
      "main": [
        [
          {
            "node": "Format Newsletter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Newsletter": {
      "main": [
        [
          {
            "node": "Convert to Word",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to Word": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}