我們要打造一個 LINE Bot,用戶傳訊息問天氣,機器人會:
接收用戶訊息,用 Webhook 觸發 n8n 工作流程
取得即時天氣數據(溫度、濕度、天氣狀況)
根據天氣數據,用自然語言回覆用戶
串接所有服務,全自動處理
如果你還沒建立 LINE Bot,請先到 LINE Developer 控制台建立:
輸入你的組織或個人名稱
選擇「Messaging API」類型
| 欄位 | 說明 |
|---|---|
| Channel Access Token | 從 LINE Developers 複製的長效權杖 |
| Channel Secret | 從 LINE Developers 複製的頻道密鑰 |
看到「Connection tested successfully」表示成功!
等測試成功後再開啟
| 欄位 | 設定值 |
|---|---|
| HTTP Method | POST |
| Path | line-webhook(自訂網址尾巴) |
(記得用 Production URL,不能有 -test)
看到 Success 表示設定成功!
在 Webhook 後面加一個 Code 節點,用來提取用戶傳來的訊息:
Gemini 沒有聯網功能,無法查即時天氣,所以我們要串接天氣 API!
使用 Email 註冊並驗證
新增一個 Code 節點,將中文城市名轉換成英文:
新增一個 HTTP Request 節點來呼叫天氣 API:
| 欄位 | 設定值 |
|---|---|
| Method | GET |
| URL | https://api.openweathermap.org/data/2.5/weather?q={{$json.cityName}},TW&appid=你的API_KEY&units=metric&lang=zh_tw |
| Authentication | None |
q={{$json.cityName}},TW - 城市名稱(動態)appid=你的API_KEY - 你的 API Keyunits=metric - 使用攝氏溫度lang=zh_tw - 回傳繁體中文新增一個 Code 節點(Format Weather Data),整理天氣資料給 Gemini:
新增一個 HTTP Request 節點來呼叫 Gemini:
| 欄位 | 設定值 |
|---|---|
| Method | POST |
| URL | https://generativelanguage.googleapis.com/v1/models/gemini-2.5-flash:generateContent?key=你的GEMINI_API_KEY |
| Send Headers | 開啟,Content-Type: application/json |
| Send Body | 開啟,Body Content Type: JSON |
| Specify Body | Using Fields Below |
| Body Parameter | contents = {{ $('Format Weather Data').item.json.contents }} |
新增一個 Code 節點,將 Gemini 的回覆整理成 LINE 的格式:
最後新增一個 HTTP Request 節點,把回覆傳回 LINE:
| 欄位 | 設定值 |
|---|---|
| Method | POST |
| URL | https://api.line.me/v2/bot/message/reply |
| Authentication | None |
| Name | Value |
|---|---|
| Content-Type | application/json |
| Authorization | Bearer 你的LINE_CHANNEL_ACCESS_TOKEN |
Send Body: 開啟 → Body Content Type: JSON → Specify Body: Using JSON
點擊 Webhook 節點,切換到 Production URL
例如:「台北市天氣」、「高雄天氣如何」
確認每個節點都有綠色勾勾表示成功!