在前面幾天,我們已經學會了如何用 n8n 做「課程提醒」、「自動寄信」等功能。
今天,我要帶大家一起做一個更生活化的應用──每天自動查天氣,寄信提醒自己要不要帶傘或穿外套!
讓系統每天固定時間(例如早上 7 點、晚上 6 點):
這樣一來,就能做到「AI 天氣小秘書」的功能,每天主動通知你天氣狀況!
這次整個工作流程的架構如下:
Schedule Trigger → HTTP Request(OpenWeatherMap) → HTTP Request(Dify) → Gmail
1(每天一次)07:00 和 18:00。補充:
Method: GET
URL:
https://api.openweathermap.org/data/2.5/weather?q=Taipei&appid=你的API_KEY
{
"weather": [
{ "description": "clear sky" }
],
"main": {
"temp": 298.15
},
"name": "Taipei"
}
--> 溫度是「開爾文(Kelvin)」,要轉成攝氏溫度要用:
{{ $json["main"]["temp"] - 273.15 }}
HTTP Request - Dify)https://api.dify.ai/v1/chat-messages
Authorization
Bearer 你的_Dify_API_Key
{
"inputs": {
"city_name": "={{$json["name"]}}",
"temperature_celsius": "={{$json["main"]["temp"] - 273.15}}",
"weather_condition": "={{$json["weather"][0]["description"]}}"
},
"query": "請根據以下資訊生成一段自然語氣的天氣播報,內容要簡短活潑:城市是 {{ $json['name'] }},氣溫是 {{ $json['main']['temp'] - 273.15 }} 度,天氣是 {{ $json['weather'][0]['description'] }}。",
"response_mode": "blocking",
"user": "weather-user"
}
city_name
temperature_celsius
weather_condition
From Email: 你的 Gmail 帳號
To Email: 收件者(可以是自己)
Subject: 「今日天氣提醒」
Text:
今日天氣播報:
{{$json["answer"]}}
-->這裡的 $json["answer"] 會自動帶入 Dify 生成的播報內容。