{
  "name": "Telegram Newsletter Workflow",
  "nodes": [
    {
      "parameters": {
        "botToken": "YOUR_TELEGRAM_BOT_TOKEN",
        "updateType": [
          "message"
        ],
        "chatId": "@yourchanneloruser"
      },
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json['message']['text']}}",
              "operation": "contains",
              "value2": "newsletter:"
            }
          ]
        }
      },
      "name": "IF Newsletter",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const body = $json['message']['text'];\nconst topic = body.split(':')[1].trim();\nreturn { topic };"
      },
      "name": "Extract Topic",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const now = new Date();\nconst sevenDaysAgo = new Date();\nsevenDaysAgo.setDate(now.getDate() - 7);\nreturn { fromDate: sevenDaysAgo.toISOString(), toDate: now.toISOString() };"
      },
      "name": "Calculate Dates",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1
    },
    {
      "parameters": {
        "requestMethod": "GET",
        "url": "https://gnews.io/api/v4/search",
        "qsParametersJson": "={\"q\": $json['topic'], \"lang\": 'en', \"from\": $json['fromDate'], \"to\": $json['toDate'], \"max\": 15, \"sortby\": 'publishedAt', \"apikey\": 'YOUR_GNEWS_API_KEY'}"
      },
      "name": "Fetch Articles",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const topic = $json['topic'];\nconst articles = $json['articles'].slice(0, 15);\nlet items = articles.map(a => `\n  <h2><a href=\"${a.url}\" target=\"_blank\">${a.title}</a></h2>\n  <p>${a.description || ''} \u2014 ${a.source.name} (${new Date(a.publishedAt).toLocaleDateString()})</p>\n`).join('\\n');\nconst html = `<div style=\"font-family:Arial,sans-serif; max-width:700px; margin:auto;\"><h1>${topic}</h1>${items || '<p>No recent articles found.</p>'}<hr><p style=\"font-size:12px; color:#777;\">Generated ${new Date().toISOString().slice(0,10)}</p></div>`;\nreturn { html, topic };"
      },
      "name": "Build HTML Newsletter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1
    },
    {
      "parameters": {
        "fromEmail": "YOUR_EMAIL@gmail.com",
        "toEmail": "YOUR_PERSONAL_EMAIL@gmail.com",
        "subject": "Newsletter: {{$json['topic']}}",
        "textContent": "",
        "htmlContent": "={{$json['html']}}"
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "IF Newsletter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Newsletter": {
      "main": [
        [
          {
            "node": "Extract Topic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Topic": {
      "main": [
        [
          {
            "node": "Calculate Dates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Dates": {
      "main": [
        [
          {
            "node": "Fetch Articles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Articles": {
      "main": [
        [
          {
            "node": "Build HTML Newsletter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build HTML Newsletter": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}