Hãy cùng tạo một workflow đơn giản: Mỗi ngày lấy thông tin thời tiết và gửi email báo cáo.
Bước 1: Tạo Trigger
- Bấm dấu
+màu cam để thêm node. - Tìm kiếm
Schedule. - Cấu hình: Chọn
Interval,Wait1Days. (Để test, bạn có thể chọn TriggerOn clicking 'execute').
Bước 2: Lấy dữ liệu (HTTP Request)
- Thêm node
HTTP Request. - Method:
GET. - URL: Sử dụng một API thời tiết miễn phí, ví dụ:
https://api.open-meteo.com/v1/forecast?latitude=21.02&longitude=105.83¤t_weather=true(Hà Nội). - Bấm
Execute Nodeđể xem dữ liệu trả về. Bạn sẽ thấy JSON chứa nhiệt độ, tốc độ gió...
Bước 3: Xử lý dữ liệu (Optional)
Nếu bạn muốn định dạng lại dữ liệu:
- Thêm node
Set. - Tạo field mới
message. - Value: Sử dụng Expression.
- Nhập:
Hôm nay nhiệt độ là: - Mở Expression Editor, chọn biến
temperaturetừ node HTTP Request. - Kết quả:
Hôm nay nhiệt độ là: 25 độ C.
- Nhập:
Bước 4: Gửi Email (Gmail Node)
Lưu ý: Cần cấu hình Credential cho Gmail (OAuth2 hoặc App Password) trước.
- Thêm node
Gmail. - Operation:
Send. - To Email: Nhập email của bạn.
- Subject: "Báo cáo thời tiết hàng ngày".
- Message: Dùng Expression lấy giá trị từ node
Set(hoặc trực tiếp từ HTTP Request).
Bước 5: Kích hoạt (Activate)
- Bấm nút
Activeở góc trên bên phải màn hình để workflow chạy tự động theo lịch (Schedule) bạn đã cài đặt. - Nhớ lưu (Save) workflow lại.
Code Workflow mẫu (JSON)
Bạn có thể copy đoạn JSON dưới đây và dán trực tiếp vào giao diện n8n (Ctrl+V) để import nhanh workflow này:
json:
{
"name": "Daily Weather Report",
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{
"field": "days",
"seconds": 0
}
]
}
},
"name": "Schedule",
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"url": "https://api.open-meteo.com/v1/forecast?latitude=21.02&longitude=105.83¤t_weather=true",
"options": {}
},
"name": "Get Weather",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [
450,
300
]
},
{
"parameters": {
"keepOnlySet": true,
"values": {
"string": [
{
"name": "message",
"value": "={{ 'Hôm nay nhiệt độ là: ' + $json.current_weather.temperature + ' độ C' }}"
},
{
"name": "subject",
"value": "Báo cáo thời tiết hôm nay"
}
]
},
"options": {}
},
"name": "Format Data",
"type": "n8n-nodes-base.set",
"typeVersion": 1,
"position": [
650,
300
]
},
{
"parameters": {
"resource": "message",
"subject": "={{ $json.subject }}",
"message": "={{ $json.message }}",
"toEmail": "user@example.com"
},
"name": "Gmail",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2,
"position": [
850,
300
],
"credentials": {
"gmailOAuth2": {
"id": "YOUR_CREDENTIAL_ID",
"name": "Gmail Account"
}
}
}
],
"connections": {
"Schedule": {
"main": [
[
{
"node": "Get Weather",
"type": "main",
"index": 0
}
]
]
},
"Get Weather": {
"main": [
[
{
"node": "Format Data",
"type": "main",
"index": 0
}
]
]
},
"Format Data": {
"main": [
[
{
"node": "Gmail",
"type": "main",
"index": 0
}
]
]
}
}
}