Trigger Types
Triggers quyết định khi nào workflow chạy. Bài này cover tất cả trigger types trong n8n.
🎯 Mục tiêu bài học
🎯 Mục tiêu
- Hiểu các loại triggers
- Setup Schedule triggers
- Tạo Webhook endpoints
- Sử dụng App triggers (Gmail, Slack, etc.)
Checkpoint
Trigger trong n8n có vai trò gì? Kể ra ít nhất 4 loại trigger.
📋 Trigger Overview
1. Trigger Overview
1.1 Trigger Categories
| Category | Examples | Use Case |
|---|---|---|
| Manual | Manual Trigger | Testing, on-demand |
| Schedule | Cron, Interval | Daily reports, backups |
| Webhook | Webhook node | External events, form submissions |
| App | Gmail, Slack, Sheets | New email, message, row |
| Error | Error Trigger | Monitor failed workflows |
1.2 Khi nào dùng trigger nào?
1📅 Schedule: Báo cáo hàng ngày, backup data, sync data2🔗 Webhook: Form submit, payment notification, CI/CD3📧 App Trigger: New email → process, new Slack message → respond4🖱️ Manual: Testing, one-time tasks5⚠️ Error: Alert khi workflow failsCheckpoint
Trigger nào phù hợp cho báo cáo hàng ngày? Trigger nào dùng khi nhận form submission?
⏰ Schedule Trigger
2. Schedule Trigger
2.1 Cron Expressions
1Cron format: [second] [minute] [hour] [day-of-month] [month] [day-of-week]2 3Ví dụ:40 9 * * 1-5 → Thứ 2-6, 9:00 AM50 0 * * * → Mỗi ngày, 12:00 AM (midnight)6*/30 * * * * → Mỗi 30 phút70 8 1 * * → Ngày 1 mỗi tháng, 8:00 AM80 9,18 * * * → 9 AM và 6 PM mỗi ngày2.2 Schedule Node Setup
- Add node Schedule Trigger
- Chọn trigger mode:
- Every X minutes/hours — Đơn giản
- Cron Expression — Linh hoạt
- Specific dates — Ngày cụ thể
2.3 Use Cases
Daily Sales Report:
1Schedule (9 AM) → Google Sheets (get data) → AI Summarize → Slack (post)Weekly Backup:
1Schedule (Sunday 2 AM) → Database Query → Google Drive (upload)Hourly Monitoring:
1Schedule (every 1h) → HTTP Request (health check) → IF failed → Email alertCheckpoint
Cron expression 0 9 * * 1-5 có nghĩa là gì? Viết cron expression cho "mỗi 30 phút".
🔗 Webhook Trigger
3. Webhook Trigger
3.1 What is a Webhook?
1Webhook = URL mà external services gọi khi event xảy ra2 3Ví dụ:4- Stripe gọi webhook khi payment thành công5- GitHub gọi webhook khi có push6- Form submit gọi webhook với form data3.2 Setup Webhook
- Add node Webhook
- n8n tạo URL:
https://your-n8n.com/webhook/abc123 - Chọn HTTP Method: GET, POST, PUT, DELETE
- Copy URL → paste vào external service
3.3 Webhook Configuration
| Setting | Description |
|---|---|
| HTTP Method | GET (simple), POST (with data) |
| Path | Custom path: /contact-form |
| Authentication | None, Basic Auth, Header Auth |
| Response Code | 200, 201, etc. |
| Response Data | Custom JSON response |
3.4 Example: Contact Form
1Webhook (POST /contact) 2→ Validate data (IF)3→ Google Sheets (save)4→ Email notification (send)5→ Respond "Thank you!"Webhook receives:
1{2 "name": "Nguyễn Văn A",3 "email": "a@email.com",4 "message": "Tôi muốn tư vấn sản phẩm XYZ"5}3.5 Test Webhooks
1# Test locally with curl2curl -X POST http://localhost:5678/webhook/abc123 \3 -H "Content-Type: application/json" \4 -d '{"name": "Test", "email": "test@email.com"}'5 6# Online testing: webhook.site, requestbin.com3.6 Security
1⚠️ Webhook URL là public — ai cũng có thể gọi!2 3Bảo mật:41. Basic Auth: Username + Password52. Header Auth: Secret token trong header63. IP Whitelist: Chỉ cho phép IPs cụ thể74. HMAC Validation: Verify request signatureCheckpoint
Webhook URL là public, vậy cần bảo mật bằng cách nào? Kể ra ít nhất 3 phương pháp.
📱 App Triggers
4. App Triggers
4.1 Gmail Trigger
1Trigger: New email received2Filters: From specific sender, subject contains "urgent"3Output: sender, subject, body, attachmentsSetup:
- Add Gmail Trigger node
- Connect Google account
- Set filters (optional)
- Set poll interval (every 1 min - 1 hour)
4.2 Slack Trigger
1Trigger: New message in channel2Output: message text, user, channel, timestamp4.3 Google Sheets Trigger
1Trigger: New row added / Row updated2Output: Row data as JSON4.4 Common App Trigger Patterns
| Trigger Source | → Action |
|---|---|
| Gmail: new email | → Parse → Categorize → Forward/Reply |
| Slack: mention | → AI process → Reply in thread |
| Sheets: new row | → Validate → Process → Update status |
| GitHub: new issue | → Categorize → Assign → Notify Slack |
| Typeform: submission | → Save to DB → Send confirmation email |
Checkpoint
Gmail Trigger có thể filter theo những tiêu chí nào? Kể ra ít nhất 3 tiêu chí.
⚠️ Error Trigger
5. Error Trigger
5.1 Setup Error Monitoring
1Error Trigger → Format error details → Send alertError Trigger fires when ANY workflow in your n8n instance fails.
5.2 Error Data
1{2 "execution": {3 "id": "123",4 "url": "https://n8n.example.com/execution/123",5 "error": {6 "message": "Cannot read property 'email' of undefined",7 "node": "Send Email"8 }9 },10 "workflow": {11 "id": "45",12 "name": "Daily Report"13 }14}5.3 Alert Workflow
1Error Trigger 2→ Format message: "❌ Workflow '{workflow.name}' failed: {error.message}"3→ Slack (post to #alerts)4→ Email (send to admin)Checkpoint
Error Trigger fires khi nào? Nó cung cấp những thông tin gì về lỗi?
🔀 Advanced: Combining Triggers
6. Advanced: Combining Triggers
6.1 Multi-Trigger Workflow
Một workflow chỉ có 1 trigger, nhưng bạn có thể:
1Approach 1: Webhook gọi workflow khác2Webhook → Execute Workflow (trigger Daily Report)3 4Approach 2: Merge workflows5Workflow A (Schedule) → Webhook to Workflow C6Workflow B (Slack trigger) → Webhook to Workflow C7Workflow C (Webhook) → Process → Output6.2 Trigger + Filter
1Gmail Trigger (all emails)2→ IF (subject contains "invoice")3 → TRUE: Process invoice4 → FALSE: Archive7. Hands-on Lab
Lab 1: Morning Briefing
Tạo workflow chạy 8 AM hàng ngày:
- Schedule Trigger (8:00 AM, Mon-Fri)
- HTTP Request — Get weather forecast
- Google Calendar — Get today's events
- AI — Summarize into briefing
- Slack — Post to personal channel
Lab 2: Lead Capture Webhook
- Webhook (POST /new-lead)
- Validate — Check required fields
- Google Sheets — Save lead data
- Email — Send confirmation to lead
- Slack — Notify sales team
Lab 3: Error Alert System
- Error Trigger
- Format error details
- Slack — Post to #n8n-alerts
- Email — Send to admin (if critical)
📝 Quiz
-
Webhook trigger khác Schedule trigger ở điểm nào?
- Webhook chạy khi nhận HTTP request, Schedule chạy theo thời gian
- Webhook miễn phí, Schedule trả phí
- Webhook nhanh hơn
- Không khác nhau
-
Cron expression
0 9 * * 1-5nghĩa là?- Mỗi 9 phút
- 9 AM thứ 2 đến thứ 6
- Ngày 9 hàng tháng
- 9 PM mỗi ngày
-
Error Trigger fires khi?
- User nhấn button
- Scheduled time
- Bất kỳ workflow nào fail
- New email
🎯 Key Takeaways
- Schedule — Cho recurring tasks (daily reports, backups)
- Webhook — Cho external events (form submissions, payments)
- App Triggers — Cho app-specific events (new email, Slack message)
- Error Trigger — Giám sát tất cả workflows
- Security — Luôn bảo mật webhook URLs
Checkpoint
Một workflow n8n có thể có bao nhiêu triggers? Làm sao để kết hợp nhiều triggers?
🚀 Bài tiếp theo
Notion & Airtable Integration — Kết nối với Notion và Airtable!
