在之前的實作中,有學習到如何讀取檔案到n8n並且使用gemini節點來分析檔案,這次就要把這些技巧實際應用到日常學習情境,做出一個小專案—— 每日自動推播多益單字。這次流程是:每天系統會隨機挑選 10 個單字,並透過 Telegram 自動推送到手機上,讓我在通勤、休息或零碎時間,都能隨手學習。除此之外,當單字學習完成後,還能把學習日期自動寫入到 Notion 資料庫中,形成專屬的學習紀錄。
並且將背過的單字與學習日期存進Notion。希望藉由這個方式就能把單字學習融入到日常生活裡,不需要再特地打開資料庫查詢,也能確保每天都有新的輸入,避免偷懶。
讀取檔案(黃色部分)
你是一個英文單字整理助理,現在我會提供一份多益單字PDF文件。
請你將裡面的內容轉換成結構化表格,格式如下:
- 單字(word)
- 中文解釋 (meaning)
- 詞性(pos) —— 如果原文件沒有,請幫我判斷是v.、n.、adj.、adv.還是phr.
- 例句(sentence) —— 如果原文件沒有,請幫我生成一個自然、生活化的英文例句
輸出請用 JSON 陣列的形式,範例如下:
[
{
"word": "abundant",
"meaning": "豐富的",
"pos":"adj.",
"sentence": "The forest is abundant with wildlife.",
},
{
"word": "fragile",
"meaning": "易碎的",
"Part of Speech":"adj.",
"sentence": "Be careful, the vase is fragile.",
}
]請完整解析整份文件,並確保每個單字都有被輸出。
建立code節點
// n8n Code Node (JavaScript)
const input = $json;
// 取出 Gemini 回傳的文字內容
let rawText = input.content?.parts?.[0]?.text || "";
// 嘗試抓取 ```json ... ``` 包起來的內容
let match = rawText.match(/```json([\s\S]*?)```/);
let cleanJson;
if (match) {
// 有找到 JSON block
try {
cleanJson = JSON.parse(match[1].trim());
} catch (e) {
throw new Error("JSON parse error: " + e.message);
}
} else {
// 沒有找到 json block,直接輸出原始文字
cleanJson = { rawText };
}
return cleanJson;
建立code節點,隨機抽出10個單字,包含它的詞性、中文、例句、學習時間
單字推播與存入Notion(紅色部分)
建立code節點,將前一節點抽取的十個單字(10個items)整理成一個item且為可以傳給telegram的格式
const items = $input.all();
// 隨機洗牌
const shuffled = items.sort(() => 0.5 - Math.random());
// 抽出前 10 筆
const selected = shuffled.slice(0, 10);
// 取得今天日期
const today = new Date().toISOString().split("T")[0];
// 組成 Telegram 訊息
let message = `📘 今日單字學習(${today})\n\n`;
selected.forEach((item, index) => {
const word = item.json.name || ""; // 單字
const pos = item.json.pos || ""; // 詞性
const meaning = item.json.meaning || ""; // 中文意思
const sentence = item.json.sentence || ""; // 例句
message += `${index + 1}. ${word} (${pos}) ${meaning}\n Example: ${sentence}\n\n`;
});
// 輸出給 Telegram Node
return [{ json: { text: message } }];
建立telegram節點,功能選擇send a message
將背過的單字存進Notion,並填入學習日期 (藍色部分)
在這次的實作過程中,其實也遇到了一些挑戰。最大的困難是 Notion 資料庫輸出後的格式,常常需要透過 Function Node 來重新整理或挑選欄位,才能正確抓取單字、詞性、中文意思與例句。如果資料表設計不夠清楚,就更容易在流程中出錯。但也正因為這些挑戰,讓我更熟悉如何在 n8n 內透過程式碼去整理與轉換資料,並思考在建立 Notion Database 時,應該如何設計欄位,才能在自動化流程裡更好用。
https://gec.hwai.edu.tw/p/405-1044-36651,c639.php?Lang=zh-tw