# 用 n8n + OpenAI + Notion 打造自動產文系統(提供設定檔下載)
這篇教你從 0 建立一條自動化內容流程:
- AI 自動產生文章
- 自動寫入 Notion
- 建立自己的內容池
整個流程不需要後端開發,只用 n8n 即可完成。
---
## 一、系統架構
流程如下:
```mermaid
flowchart TD
A[Manual Trigger] --> B[Message Model]
B --> C[Code 解析 JSON]
C --> D[Notion 寫入資料庫]
```
---
## 二、建立 Notion 內容池
### 1️⃣ 建立資料庫
在 Notion 建立:
- Table – Full Page
- 命名為:文章內容池


### 2️⃣ 建立欄位
| 欄位名稱 | 類型 |
|------------|--------|
| title | Title |
| content | Text |
| created_at | Date |
完成後,即為最小可用內容資料庫。
---
## 三、建立 Notion Integration
1. 前往
https://www.notion.so/my-integrations
2. 建立
Internal Integration
3. 複製
Internal Integration Secret

---
### 授權資料庫
進入 Integration 的「Content access」頁面,
將「文章內容池」加入授權清單。
完成後 n8n 才能讀寫該資料庫。
---
## 四、n8n 設定流程
### Step 1:新增 Trigger
新增:
Manual Trigger

---
### Step 2:新增 Message Model

Model 建議選:
gpt-4o-mini

▲紅框要去 OpenAI 申請一組 API Key
---
#### System 設定
你是台灣繁體中文寫作助理。
口吻中立資訊型。

---
#### User 設定
產生一篇 社群 貼文。
主題請自行決定,範圍包含:
貸款、信用、理財觀念、負債管理、金融迷思。
200~300 字。
分段清楚。
不要推銷。
結尾加三個 hashtag。
請只輸出 JSON:
{"title":"...","content":"..."}

---
### Step 3:新增 Code 節點(解析 JSON)

將以下程式碼貼入 Code 節點:
```javascript
const text = $json.output?.[0]?.content?.[0]?.text ?? JSON.stringify($json);
const cleaned = String(text)
.replace(/^```json\s*/i, "")
.replace(/^```\s*/i, "")
.replace(/\s*```$/i, "")
.trim();
const obj = JSON.parse(cleaned);
return [{
json: {
title: obj.title,
content: obj.content
}
}];
```
▼ 輸出(左)結果,並準備將其寫回 Notion:

---
### Step 4:新增 Notion 節點

1. 設定好在 integrations/internal 取得的 KEY(Internal Integration Secret)
2. DataBase 抓之前在 Notion 定義好的 Database
3. Resource 設為:Database Page
4. Operation 設為:Create(寫入)
欄位對應如下:
- title 對應 `{{$json.title}}`
- content 對應 `{{$json.content}}`
- created_at 對應 `{{$now}}`

---
## 五、測試流程
點擊:
Execute workflow

成功後:
- Notion 會新增一筆資料
- 自動生成標題
- 自動填入內容
- 自動寫入時間

---
## 六、延伸玩法
你可以進一步:
- 改成 Cron(每日自動產文)
- 加入圖片生成
- 增加主題輪動
- 避免標題重複
- 串接 API 自動發佈
---
完成結果

## 七、總結
這條流程本質是一個內容引擎:
- AI 負責創作
- n8n 負責自動化
- Notion 負責儲存與管理
當架構搭好後,後續優化只是在上面疊加功能。
___
提供完整 n8n Workflow 設定檔(JSON)下載。
此檔案不包含任何 API Key、Token 或憑證資訊。
匯入後請自行設定:
OpenAI API Credential
Notion Integration Token
Notion Database ID
設定完成後即可正常執行。
<details>
<summary>📦 點我展開 n8n Workflow JSON</summary>
```json
{
"name": "My workflow",
"nodes": [
{
"parameters": {},
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
-752,
-272
],
"id": "c44cd9fe-1ca8-4b33-bac4-e828a19daf51",
"name": "When clicking ‘Execute workflow’"
},
{
"parameters": {
"modelId": {
"__rl": true,
"value": "gpt-4o-mini",
"mode": "list",
"cachedResultName": "GPT-4O-MINI"
},
"responses": {
"values": [
{
"content": "你是台灣繁體中文的寫作助理,擅長用中立資訊型口吻寫短文。"
},
{
"content": "產生一篇可發在 Threads 的貼文。主題:貸款常見迷思。\n限制:180~260 字、繁體中文、分段清楚、不要推銷、不要提到任何品牌、結尾加 3 個 hashtag(例如 #貸款 #信用 #理財)。\n請輸出 JSON,格式:{\"title\":\"...\",\"content\":\"...\"},不要輸出多餘文字。"
}
]
},
"builtInTools": {},
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.openAi",
"typeVersion": 2.1,
"position": [
-544,
-272
],
"id": "8d13f3f9-9e17-4adf-a47c-69e4c13074ac",
"name": "Message a model",
"credentials": {
"openAiApi": {
"id": "m46SLUj1sLZ9v6YV",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"jsCode": "\nconst text = $json.output[0].content[0].text;\n\nconst cleaned = text\n .replace(/^```json\\s*/, \"\")\n .replace(/\\s*```$/, \"\");\n\nconst obj = JSON.parse(cleaned);\n\nreturn [{\n json: {\n title: obj.title,\n content: obj.content\n }\n}];"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-192,
-272
],
"id": "f4677712-462b-42a6-9542-332473a5b301",
"name": "Code in JavaScript"
},
{
"parameters": {
"resource": "databasePage",
"databaseId": {
"__rl": true,
"value": "YOUR_NOTION_DATABASE_ID",
"mode": "list",
"cachedResultName": "你的DB",
"cachedResultUrl": "https://www.notion.so/312d98a5912d803ba14bc737e153ccbb"
},
"propertiesUi": {
"propertyValues": [
{
"key": "title|title",
"title": "={{ $json.title }}"
},
{
"key": "content|rich_text",
"textContent": "={{ $json.content }}"
},
{
"key": "created_at|date",
"date": "={{$now}}"
}
]
},
"blockUi": {
"blockValues": [
{}
]
},
"options": {}
},
"type": "n8n-nodes-base.notion",
"typeVersion": 2.2,
"position": [
16,
-272
],
"id": "58dcc065-f936-46e8-92f6-a92800c25123",
"name": "Create a database page",
"credentials": {
"notionApi": {
"id": "RhOoR28DwPCCEhhf",
"name": "Notion account"
}
}
}
],
"pinData": {},
"connections": {
"When clicking ‘Execute workflow’": {
"main": [
[
{
"node": "Message a model",
"type": "main",
"index": 0
}
]
]
},
"Message a model": {
"main": [
[
{
"node": "Code in JavaScript",
"type": "main",
"index": 0
}
]
]
},
"Code in JavaScript": {
"main": [
[
{
"node": "Create a database page",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1",
"binaryMode": "separate",
"availableInMCP": false
},
"versionId": "bc095bbc-ecfd-4f10-b817-0c3e0beb3e03",
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "343f2f704e7828be4e21cb92253659e2f14b635f53a670eb2fa4e6a6edab79f7"
},
"id": "TLsexfLcACljNTty",
"tags": []
}