iT邦幫忙

2025 iThome 鐵人賽

DAY 28
0
生成式 AI

從零開始 : 學習運用 n8n 與 Dify 的 AI 自動化實戰系列 第 28

Day 28 : 自動推送天氣 / 星座 / 新聞

  • 分享至 

  • xImage
  •  

今天目標

建立一個 每天自動推播 的流程,內容包含:

  • 今日天氣預報 ☁️
  • 星座運勢 🔮
  • 熱門新聞 📰

並且將結果 發送到 LINE 或信箱

一、建立流程概要

這次我們的流程架構如下:

[Schedule Trigger]
   ↓
[HTTP Request (天氣 API)]
   ↓
[HTTP Request (星座 API)]
   ↓
[HTTP Request (新聞 API)]
   ↓
[Function (整合三種資料)]
   ↓
[LINE Notify 或 Email 節點 (推送)]

二、建立觸發器(Schedule Trigger)

  1. 新增節點:Schedule Trigger
  2. 設定時間:
  • 重複頻率(Repeat Every):1 天
  • 時間(At Time):07:30
    ->意思是每天早上七點半自動執行。

三、天氣資訊(Weather API)

  1. 新增 HTTP Request 節點
  2. 設定:
  • Method:GET

  • URL

    https://wttr.in/Taipei?format=j1
    
  • Content Type:JSON

  1. 執行後會看到 JSON 結構裡包含:
{
  "current_condition": [
    {
      "temp_C": "27",
      "weatherDesc": [{"value": "Partly cloudy"}],
      "humidity": "79"
    }
  ]
}
  1. 在 Function 節點提取內容:
const data = $json["current_condition"][0];
return [{
  json: {
    weather: `🌤 台北現在 ${data.temp_C}°C,${data.weatherDesc[0].value},濕度 ${data.humidity}%`
  }
}];

四、星座運勢(Horoscope API)

  1. 新增 HTTP Request 節點
  2. URL 可用:
https://ohmanda.com/api/horoscope/aquarius/

(將 aquarius 改成你的星座英文,例如:aries, taurus, gemini...)

  1. Function 節點處理結果:
return [{
  json: {
    horoscope: `今日運勢:${$json["horoscope"]}`
  }
}];

五、新聞摘要(News API)

  1. 新增 HTTP Request 節點
  2. 若你想使用台灣新聞,可使用:
https://newsdata.io/api/1/news?apikey=你的API_KEY&country=tw&language=zh

(註冊免費帳號:https://newsdata.io

  1. 在 Function 節點整理三則新聞:
const articles = $json.results.slice(0, 3);
const news = articles.map((a, i) => `${i+1}. ${a.title}`).join("\n");
return [{
  json: {
    news: `📰 今日新聞摘要:\n${news}`
  }
}];

六、整合三種資料

  1. 新增 Merge 節點(Mode: Merge by Position)
     → 將天氣、星座、新聞三個輸出合併。

  2. 接著新增 Function 節點,將內容組成訊息:

const weather = $item(0).$json.weather;
const horoscope = $item(1).$json.horoscope;
const news = $item(2).$json.news;

return [{
  json: {
    message: `${weather}\n\n${horoscope}\n\n${news}`
  }
}];

七、推送到 LINE Notify(或信箱)

LINE Notify

  1. 新增 HTTP Request 節點
  2. 設定:
  • Method:POST

  • URL:https://notify-api.line.me/api/notify

  • Headers:

    Authorization: Bearer 你的LineNotifyToken
    Content-Type: application/x-www-form-urlencoded
    
  • Body:

    message={{$json["message"]}}
    

這樣每天早上七點半,LINE 就會自動收到天氣、星座和新聞更新!


上一篇
Day 27
系列文
從零開始 : 學習運用 n8n 與 Dify 的 AI 自動化實戰28
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言