【n8n超入門教學】n8n x AI Agent x LINE Messaging 打造個人行動助理 最終篇 - 每日新聞流程 & 總結

N8N 行動助理最終集:每日自動推播新聞+全功能 Demo
終於來到本系列的最後一集,今天要完成「每日自動推播新聞」的自動化流程,並 Demo 之前做過的所有功能。這一集會讓你看到 N8N 的彈性與威力!
每日自動推播新聞到 LINE
- 定時觸發
用「Schedule Trigger」節點,設定每天早上 9 點自動執行。 - 抓取新聞原始碼
用「HTTP Request」節點,連到 TVBS News 生活新聞頁,取得網頁原始碼。 - 解析新聞內容
用「Code」節點(支援 Python),寫程式解析原始碼,抓出新聞標題與連結。
(即使不會寫 Python,也能照著影片或範例程式碼複製貼上) - 推播到 LINE
複製之前的 LINE 節點,將 API URL 改成 broadcast(主動推播),把剛剛解析好的新聞內容(news_string)填入 Text 欄位。 - 測試與驗證
可以手動觸發流程測試,確認 LINE 對話窗能收到新聞,連結也能正確點擊。
全功能 Demo
- 查天氣:「幫我查台北天氣」→ AI 助理自動查詢 OpenWeatherMap,回覆天氣資訊。
- 自動記帳:「買咖啡100元」→ AI 助理自動記錄到 Google Sheets。
- 建立行事曆活動:「明天早上有重要會議九到十點」→ AI 助理自動建立 Google Calendar 活動。
- 每日新聞推播:每天早上自動收到 TVBS 生活新聞。
小結
這一集展示了 N8N 的彈性:
只要幾個節點,就能完成生活中常見的自動化需求。
你可以根據自己的 Daily Routine,設計更多自動化流程,讓 AI 幫你省下寶貴時間。
Code Node
import html.parser
import json
import re
class NewsHTMLParser(html.parser.HTMLParser):
def __init__(self):
super().__init__()
self.in_nu_news = False
self.in_list = False
self.in_ul = False
self.in_li = False
self.in_a = False
self.current_href = ""
self.current_title = ""
self.news_items = []
self.div_stack = []
def handle_starttag(self, tag, attrs):
attrs_dict = dict(attrs)
if tag == 'div':
self.div_stack.append(attrs_dict.get('class', ''))
if attrs_dict.get('class') == 'nu_news':
self.in_nu_news = True
elif self.in_nu_news and attrs_dict.get('class') == 'list':
self.in_list = True
elif tag == 'ul' and self.in_list:
self.in_ul = True
elif tag == 'li' and self.in_ul:
self.in_li = True
elif tag == 'a' and self.in_li:
self.in_a = True
self.current_href = attrs_dict.get('href', '')
self.current_title = ""
def handle_endtag(self, tag):
if tag == 'div':
if self.div_stack:
class_name = self.div_stack.pop()
if class_name == 'nu_news':
self.in_nu_news = False
elif class_name == 'list':
self.in_list = False
elif tag == 'ul':
self.in_ul = False
elif tag == 'li':
self.in_li = False
elif tag == 'a' and self.in_a:
self.in_a = False
if self.current_title.strip() and self.current_href:
self.news_items.append({
'title': self.current_title.strip(),
'href': self.current_href
})
def handle_data(self, data):
if self.in_a:
self.current_title += data
# 解析輸入的 HTML 內容
html_content = _input.first().json.data
# 創建解析器並解析 HTML
parser = NewsHTMLParser()
parser.feed(html_content)
# 初始化輸出行列表
output_lines = ["新聞列表:"]
# 處理解析結果
for i, item in enumerate(parser.news_items, 1):
title = item['title']
url = "https://news.tvbs.com.tw" + item['href']
# 加入格式化的新聞資訊
output_lines.append(f"{i}. {title}")
output_lines.append(f" 連結:{url}")
output_lines.append("") # 加入空行
# 合併所有行成為最終輸出字串
formatted_string = " ".join(output_lines)
# 返回結果
return [
{"news_string": formatted_string}
]
Code Node (Deprecated, DO NOT USE IT)
from bs4 import BeautifulSoup
import pandas as pd
import json
# 解析輸入的 HTML 內容
html = _input.first().json.data
soup = BeautifulSoup(html, 'html.parser')
# 初始化輸出行列表
output_lines = ["新聞列表:"]
# 找尋主要新聞容器 (class 為 "news_now2" 的 div)
news_now2 = soup.find('div', class_='news_now2')
if news_now2:
# 找尋新聞列表容器
list_container = news_now2.find('div', class_='list')
# 確認找到列表容器且包含 ul 元素
if list_container and list_container.find('ul'):
# 遍歷所有新聞項目,只取前10篇
for i, li in enumerate(list_container.find('ul').find_all('li'), 1):
if i > 10:
break
# 提取每則新聞的連結元素
link = li.find('a')
if link:
# 取得標題和網址
title = link.text.strip()
url = "https://news.tvbs.com.tw" + link.get('href', '')
# 加入格式化的新聞資訊
output_lines.append(f"{i}. {title}")
output_lines.append(f" 連結:{url}")
output_lines.append("") # 加入空行
# 合併所有行成為最終輸出字串
formatted_string = " ".join(output_lines)
# 返回結果
return [
{"news_string": formatted_string}
]
System Message
你是一位個人的行動助理,你的職責是協助使用者處理日常事務。必須使用以下工具來完成任務:
可用工具
* Open Weather Map: 用於查詢天氣資訊。當使用者需要了解特定地點的天氣狀況時,請調用此工具。
* Accounting Tool: 用於記錄使用者的收支。當使用者要求記帳時,必須調用此工具。使用者需要提供花費的描述和金額。日期時間會自動記錄為當前日期 (YYYY/MM/DD)。當前日期時間為 {{ $json.currentDate }}。
Accounting Tool 輸入範例:
日期時間: 2025/04/29
花費描述: 買酒
金額: 100
* Google Calendar: 用於建立行事曆事件。當使用者需要安排行程或建立提醒時,請調用此工具。事件的摘要 (Summary) 是必填欄位。當前日期時間為 {{ $json.currentDate }}。