Cấu hình Incoming Webhook gửi alert vào Microsoft Teams
🎯 Mục tiêu: Gửi message JSON từ terminal vào Teams channel qua Incoming Webhook — mô phỏng cách monitoring system gửi alert.
🧰 Công cụ / nền tảng: Microsoft Teams (free/work account), PowerShell hoặc curl (WSL2/Linux).
📦 Chuẩn bị: Có Teams workspace; tạo channel #ops-alerts.
▶️ Các bước:
# BƯỚC 1: Tạo Incoming Webhook trong Teams
# Teams → channel #ops-alerts → (...) → Connectors → Incoming Webhook
# Đặt tên: "OpsAlert-Bot", upload icon → Create → Copy URL
# URL dạng: https://xxx.webhook.office.com/webhookb2/...
# BƯỚC 2: Lưu URL vào biến môi trường (PowerShell)
$WEBHOOK_URL = "https://xxx.webhook.office.com/webhookb2/PASTE_YOUR_URL_HERE"
# BƯỚC 3: Gửi test message (PowerShell)
$body = @{
"@type" = "MessageCard"
"@context" = "http://schema.org/extensions"
"summary" = "Test Alert"
"themeColor" = "FF0000"
"title" = "🚨 [CRITICAL] CPU > 90% — app-api-prod"
"sections" = @(@{
"facts" = @(
@{ "name" = "Service"; "value" = "app-api" }
@{ "name" = "Host"; "value" = "vm-prod-01" }
@{ "name" = "Value"; "value" = "CPU 94%" }
@{ "name" = "Severity"; "value" = "critical" }
@{ "name" = "Runbook"; "value" = "https://wiki/runbook/cpu-high" }
)
})
} | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri $WEBHOOK_URL -Method Post -Body $body -ContentType "application/json"
# BƯỚC 4 (Linux/WSL2 — curl tương đương):
# curl -H "Content-Type: application/json" \
# -d '{"@type":"MessageCard","themeColor":"FF0000","title":"🚨 CPU > 90%","text":"app-api-prod | CPU 94%"}' \
# "$WEBHOOK_URL"
✅ Kết quả mong đợi: Teams channel #ops-alerts hiển thị card màu đỏ với đầy đủ thông tin service, host, value, severity, link runbook. Terminal trả về 1 (success).
🧹 Cleanup: Vào Teams Connectors → xóa Webhook để tránh URL bị lộ. Không commit $WEBHOOK_URL vào git.