{
  "name": "Newsletter",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtHour": 9
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -592,
        -32
      ],
      "id": "78c17a2b-6458-42e5-acb6-74b350d17e4a",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "https://newsapi.org/v2/everything",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "from",
              "value": "={{ DateTime.fromISO($json.timestamp).minus({ days: 7 }) }}"
            },
            {
              "name": "q",
              "value": "={{ $json.topics.replaceAll(\",\", \" OR \") }}"
            },
            {
              "name": "sortBy",
              "value": "relevancy"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -144,
        -32
      ],
      "id": "41b2c6a0-7063-446e-9403-478e6c7b325b",
      "name": "Call NewsAPI",
      "alwaysOutputData": false,
      "credentials": {
        "httpQueryAuth": {
          "id": "bNnlBbWmYY7YIwcs",
          "name": "Query Auth account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "89066090",
        "text": "={{ \n  $json.output.map(article => {\n    const title = JSON.stringify(article.title).slice(1, -1);\n    const content = JSON.stringify(article.content).slice(1, -1);\n    const source = JSON.stringify(article.source).slice(1, -1);\n    const url = JSON.stringify(article.url).slice(1, -1);\n\n    return `*${title}*\\n${content}\\nSource: [${source}](${url})`;\n  }).join('\\n\\n')\n}}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1344,
        -32
      ],
      "id": "81bdbf07-dc4a-4fef-9e69-eef731f4bb14",
      "name": "Send a text message",
      "webhookId": "a8a903fe-d94b-4316-92c1-0806bd260745",
      "credentials": {
        "telegramApi": {
          "id": "WnxbBZsiprRTe5bc",
          "name": "Telegram Newsletter Bot"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "message.content.articles",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        432,
        -32
      ],
      "id": "a522827f-0470-452e-9b9a-1268dbc97ba2",
      "name": "Split Out"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-5.1",
          "mode": "list",
          "cachedResultName": "gpt-5.1"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        656,
        192
      ],
      "id": "9b3ce9c9-34d5-419a-a53a-20b4c08ca57f",
      "name": "GPT-5",
      "credentials": {
        "openAiApi": {
          "id": "1RHWZf4XkHAlv17I",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"title\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"content\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n        \"source\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n        \"url\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t}\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        912,
        192
      ],
      "id": "d34cda05-2f22-4358-8890-f53ec36600f9",
      "name": "Parser"
    },
    {
      "parameters": {
        "query": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Query', ``, 'string') }}",
        "options": {}
      },
      "type": "@tavily/n8n-nodes-tavily.tavilyTool",
      "typeVersion": 1,
      "position": [
        784,
        192
      ],
      "id": "d1fe8abe-76cf-4ce2-934d-5c78c4de8662",
      "name": "Tavily",
      "credentials": {
        "tavilyApi": {
          "id": "34pRTogSJhPTVJ79",
          "name": "Tavily account"
        }
      }
    },
    {
      "parameters": {
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "output"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1120,
        -32
      ],
      "id": "137623d3-5bd0-44a8-a915-36e9e0db1331",
      "name": "Aggregate"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "2e1993fe-ae4d-46e0-be9f-6b02a18cc610",
              "name": "topics",
              "value": "AI,n8n",
              "type": "string"
            },
            {
              "id": "2c6e2dad-d54a-4387-9bf5-4751037f99d9",
              "name": "language",
              "value": "English",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -368,
        -32
      ],
      "id": "d9fdf9e3-b954-43cb-98f7-503f6015e2f3",
      "name": "Set topics and language"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are a research writer that updates short news summaries into concise, factual articles.\n\n**Input:**\nTitle: {{ $json[\"title\"] }}\nSummary: {{ $json[\"summary\"] }}\nSource: {{ $json[\"source\"] }}\nOriginal URL: {{ $json[\"url\"] }}\nLanguage: {{ $('Set topics and language').item.json.language }}\n\n**Instructions:**\n\n1. Use **Tavily Search** to gather 2–3 reliable, recent, and relevant sources on this topic.\n2. Update the **title** if a more accurate or engaging one exists.\n3. Write **1–3 sentences** summarizing the topic, combining the original summary and information from the new sources.\n4. Return the original source name and url as well.\n\n**Output (JSON):**\n\n```json\n{\n  \"title\": \"final article title\",\n  \"content\": \"concise 1–3 sentence article content\",\n  \"source\": \"the name of the original source\",\n  \"url\": \"the url of the original source\"\n}\n```\n\n**Rules:**\n\n* Ensure the topic is relevant, informative, and timely.\n* Translate the article if necessary to comply with the desired language {{ $('Set topics and language').item.json.language }}.",
        "hasOutputParser": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        720,
        -32
      ],
      "id": "f96519e3-2626-4651-9157-30ab40ffbf60",
      "name": "Newsletter AI Agent"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-5.1",
          "mode": "list",
          "cachedResultName": "GPT-5.1"
        },
        "messages": {
          "values": [
            {
              "content": "=You are an assistant that selects the most relevant news articles for a user.\n\nInstructions:  \n1. Choose the **5 most relevant non-overlapping articles** based on the user topics.  \n2. For each article, provide:  \n   - title  \n   - short summary (1–2 sentences)  \n   - source name  \n   - url\n3. Output the results in the language specified by the user.\n\nOutput as a \"articles\" JSON array of objects, each with \"title\", \"summary\", \"source\" and \"url\".\n\nUser topics of interest: {{ $('Set topics and language').item.json.topics }}\nOutput language: {{ $('Set topics and language').item.json.language }}\n\nNewsAPI articles:\n\n{{ \n  $json.articles.map(\n    article => \n      `Title: ${article.title}\nDescription: ${article.description}\nContent: ${article.content}\nSource: ${article.source.name}\nURL: ${article.url}`\n  ).join('\\n---\\n')\n}}\n"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        80,
        -32
      ],
      "id": "699ff0a8-4412-45cb-b20d-ac4fa39c6077",
      "name": "AI Topic Selector",
      "credentials": {
        "openAiApi": {
          "id": "1RHWZf4XkHAlv17I",
          "name": "OpenAi account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Set topics and language",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call NewsAPI": {
      "main": [
        [
          {
            "node": "AI Topic Selector",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        []
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Newsletter AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT-5": {
      "ai_languageModel": [
        [
          {
            "node": "Newsletter AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Newsletter AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Tavily": {
      "ai_tool": [
        [
          {
            "node": "Newsletter AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set topics and language": {
      "main": [
        [
          {
            "node": "Call NewsAPI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Newsletter AI Agent": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Topic Selector": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v0",
    "errorWorkflow": "rylxy8YYWAdtXzot"
  },
  "versionId": "4ceb70e6-a94e-436a-b24a-37e463be64bd",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "33096ba7f58f02bb80c1ae17821e6d79073a6c7ab685cc827f3faacd5f65f9e5"
  },
  "id": "fCgv1sXrBIHpM9li",
  "tags": []
}