MinAI - Về trang chủ
Hướng dẫn
5/1335 phút
Đang tải...

Trigger Types

Manual, Schedule, Webhook, và App triggers trong n8n

Trigger Types

Triggers quyết định khi nào workflow chạy. Bài này cover tất cả trigger types trong n8n.

0

🎯 Mục tiêu bài học

TB5 min

🎯 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.

1

📋 Trigger Overview

TB5 min

1. Trigger Overview

1.1 Trigger Categories

CategoryExamplesUse Case
ManualManual TriggerTesting, on-demand
ScheduleCron, IntervalDaily reports, backups
WebhookWebhook nodeExternal events, form submissions
AppGmail, Slack, SheetsNew email, message, row
ErrorError TriggerMonitor failed workflows

1.2 Khi nào dùng trigger nào?

Ví dụ
1📅 Schedule: Báo cáo hàng ngày, backup data, sync data
2🔗 Webhook: Form submit, payment notification, CI/CD
3📧 App Trigger: New email → process, new Slack message → respond
4🖱️ Manual: Testing, one-time tasks
5⚠️ Error: Alert khi workflow fails

Checkpoint

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?

2

⏰ Schedule Trigger

TB5 min

2. Schedule Trigger

2.1 Cron Expressions

Ví dụ
1Cron format: [second] [minute] [hour] [day-of-month] [month] [day-of-week]
2
3Ví dụ:
40 9 * * 1-5 → Thứ 2-6, 9:00 AM
50 0 * * * → Mỗi ngày, 12:00 AM (midnight)
6*/30 * * * * → Mỗi 30 phút
70 8 1 * * → Ngày 1 mỗi tháng, 8:00 AM
80 9,18 * * * → 9 AM và 6 PM mỗi ngày

2.2 Schedule Node Setup

  1. Add node Schedule Trigger
  2. 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:

Ví dụ
1Schedule (9 AM) → Google Sheets (get data) → AI Summarize → Slack (post)

Weekly Backup:

Ví dụ
1Schedule (Sunday 2 AM) → Database Query → Google Drive (upload)

Hourly Monitoring:

Ví dụ
1Schedule (every 1h) → HTTP Request (health check) → IF failed → Email alert

Checkpoint

Cron expression 0 9 * * 1-5 có nghĩa là gì? Viết cron expression cho "mỗi 30 phút".

3

🔗 Webhook Trigger

TB5 min

3. Webhook Trigger

3.1 What is a Webhook?

Ví dụ
1Webhook = URL mà external services gọi khi event xảy ra
2
3Ví dụ:
4- Stripe gọi webhook khi payment thành công
5- GitHub gọi webhook khi có push
6- Form submit gọi webhook với form data

3.2 Setup Webhook

  1. Add node Webhook
  2. n8n tạo URL: https://your-n8n.com/webhook/abc123
  3. Chọn HTTP Method: GET, POST, PUT, DELETE
  4. Copy URL → paste vào external service

3.3 Webhook Configuration

SettingDescription
HTTP MethodGET (simple), POST (with data)
PathCustom path: /contact-form
AuthenticationNone, Basic Auth, Header Auth
Response Code200, 201, etc.
Response DataCustom JSON response

3.4 Example: Contact Form

Ví dụ
1Webhook (POST /contact)
2→ Validate data (IF)
3→ Google Sheets (save)
4→ Email notification (send)
5→ Respond "Thank you!"

Webhook receives:

JSON
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

Bash
1# Test locally with curl
2curl -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.com

3.6 Security

Ví dụ
1⚠️ Webhook URL là public — ai cũng có thể gọi!
2
3Bảo mật:
41. Basic Auth: Username + Password
52. Header Auth: Secret token trong header
63. IP Whitelist: Chỉ cho phép IPs cụ thể
74. HMAC Validation: Verify request signature

Checkpoint

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.

4

📱 App Triggers

TB5 min

4. App Triggers

4.1 Gmail Trigger

Ví dụ
1Trigger: New email received
2Filters: From specific sender, subject contains "urgent"
3Output: sender, subject, body, attachments

Setup:

  1. Add Gmail Trigger node
  2. Connect Google account
  3. Set filters (optional)
  4. Set poll interval (every 1 min - 1 hour)

4.2 Slack Trigger

Ví dụ
1Trigger: New message in channel
2Output: message text, user, channel, timestamp

4.3 Google Sheets Trigger

Ví dụ
1Trigger: New row added / Row updated
2Output: Row data as JSON

4.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í.

5

⚠️ Error Trigger

TB5 min

5. Error Trigger

5.1 Setup Error Monitoring

Ví dụ
1Error Trigger → Format error details → Send alert

Error Trigger fires when ANY workflow in your n8n instance fails.

5.2 Error Data

JSON
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

Ví dụ
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?

6

🔀 Advanced: Combining Triggers

TB5 min

6. Advanced: Combining Triggers

6.1 Multi-Trigger Workflow

Một workflow chỉ có 1 trigger, nhưng bạn có thể:

Ví dụ
1Approach 1: Webhook gọi workflow khác
2Webhook → Execute Workflow (trigger Daily Report)
3
4Approach 2: Merge workflows
5Workflow A (Schedule) → Webhook to Workflow C
6Workflow B (Slack trigger) → Webhook to Workflow C
7Workflow C (Webhook) → Process → Output

6.2 Trigger + Filter

Ví dụ
1Gmail Trigger (all emails)
2→ IF (subject contains "invoice")
3 → TRUE: Process invoice
4 → FALSE: Archive

7. Hands-on Lab

Lab 1: Morning Briefing

Tạo workflow chạy 8 AM hàng ngày:

  1. Schedule Trigger (8:00 AM, Mon-Fri)
  2. HTTP Request — Get weather forecast
  3. Google Calendar — Get today's events
  4. AI — Summarize into briefing
  5. Slack — Post to personal channel

Lab 2: Lead Capture Webhook

  1. Webhook (POST /new-lead)
  2. Validate — Check required fields
  3. Google Sheets — Save lead data
  4. Email — Send confirmation to lead
  5. Slack — Notify sales team

Lab 3: Error Alert System

  1. Error Trigger
  2. Format error details
  3. Slack — Post to #n8n-alerts
  4. Email — Send to admin (if critical)

📝 Quiz

  1. 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
  2. Cron expression 0 9 * * 1-5 nghĩ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
  3. Error Trigger fires khi?

    • User nhấn button
    • Scheduled time
    • Bất kỳ workflow nào fail
    • New email

🎯 Key Takeaways

  1. Schedule — Cho recurring tasks (daily reports, backups)
  2. Webhook — Cho external events (form submissions, payments)
  3. App Triggers — Cho app-specific events (new email, Slack message)
  4. Error Trigger — Giám sát tất cả workflows
  5. 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!