🪄 前言:為什麼要改?

微軟在 2025 年底確定淘汰舊的 Microsoft Teams Incoming Webhook (O365 Connector),並將通知機制整合進 Power Automate / Teams Workflow。
這代表以前那條
https://company.webhook.office.com/webhookb2/...的 URL 將不再維護,你需要改成新的:https://environment.api.powerplatform.com/powerautomate/automations/direct/workflows/...幸好,轉換並不難。
目前通知機制的流程為: GCP Pub/Sub ⇒ Cloud Function(Python ) ⇒ Webhook(URL) ⇒ Teams
需要修改的部分為:
✔️ Cloud Function(Python ):調整腳本程式碼
✔️ Webhook(URL) : 改為 從 Teams 中的 Power Automate / Workflow產生的URL

⚙️ 教學步驟:完整版 Payload 實作(含顏色、粗體與連結樣式)
這邊實作完整版的方式有兩種,因為完整版涵蓋的功能有(含顏色、粗體與連結樣式)等,因此會
- Python 傳簡單資料、Flow 用 RichTextBlock 組樣式卡
- Python 傳簡單資料+RichTextBlock 組樣式卡、Flow 不用特別設定
我目前示範的是第1種
Step 1. 在 Python 端修改程式碼
這裡的核心是把舊 webhook 換成 Workflow URL
Workflow URL 要先到Teams Workflow設定才能取得, 詳見[Step 2. 建立 Teams Workflow]
並將原本 "text": "..." 的結構改為 "text": [ {...}, {...} ] 陣列格式。
import requests
# 🆕 新 Workflow Webhook URL
webhook_url = "https://environment.api.powerplatform.com/powerautomate/automations/direct/workflows/..."
headers = {'Content-Type': 'application/json'}
payload = {
#原本的text格式為以下內容
#"text": f"<b style='color:red;'>【{log_level}】</b> <b>[{project_id}]</b> - {service_name} <br><br> <b>錯誤訊息</b> : {error_msg}<br> <b>詳細資訊</b>:<a href='{url}' style='color:red;'>{url}</a>"
#修改text為以下的資料結構
"text": [
{
"log_level": log_level,
"project_id": project_id,
"service_name": service_name,
"error_msg": error_msg,
"url": url
}
]
}
response = requests.post(webhook_url, headers=headers, json=payload)
print(response.status_code, response.text)
💡 為什麼用陣列?
因為 Workflow 可以使用「Apply to each」自動對每筆資料重複貼出一張卡片。
這對 Pub/Sub 一次推多筆事件特別實用。
Step 2. 建立 Teams Workflow
下圖是Workflow的工作流程示意圖
┌──────────────────────────────────────┐
│ When a Teams webhook request received│
│ (接收 Python 發送的 JSON) │
└──────────────┬───────────────────────┘
│
┌──────────────▼──────────────┐
│ Apply to each │
│ Items = @triggerBody()?['text'] │
└──────────────┬──────────────┘
│
┌──────────────▼──────────────┐
│ Post adaptive card in Teams │
│ (使用 Flow bot 發送) │
└─────────────────────────────┘
1️⃣ 打開 Microsoft Teams → Workflows

2️⃣ 點選上方[建立]欄位後,點選[收到webhook要求時發布在頻道中(Post to a channel when a webhook request is received)]的範本

3️⃣填入該通知設定的流程名稱

4️⃣設定要通知的團隊(Teams)與頻道(Channel),這是階層的關係(團隊內的某個頻道)

5️⃣儲存後複製 HTTP POST URL → 貼回 Python 的 webhook_url

Step 3. Workflow 動作設定
1️⃣回到Microsoft Teams → Workflows → 首頁,選取剛建立的工作流程並編輯

2️⃣執行以下步驟
- [選取先前步驟的輸出] (標記1)中輸入:
@triggerBody()?['text'] - 在 Adaptive Card 欄位中(標記2)貼上
{
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5",
"body": [
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "【@{items('Send_each_adaptive_card')?['log_level']}】",
"color": "Attention",
"weight": "Bolder"
},
{
"type": "TextRun",
"text": " [@{items('Send_each_adaptive_card')?['project_id']}] - @{items('Send_each_adaptive_card')?['service_name']}",
"weight": "Bolder"
}
]
},
{
"type": "TextBlock",
"text": "",
"spacing": "Small"
},
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "錯誤訊息: ",
"weight": "Bolder"
},
{
"type": "TextRun",
"text": "@{items('Send_each_adaptive_card')?['error_msg']}",
"wrap": true
}
]
},
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "詳細資訊: ",
"weight": "Bolder"
},
{
"type": "TextRun",
"text": "@{items('Send_each_adaptive_card')?['url']}",
"color": "Attention",
"selectAction": {
"type": "Action.OpenUrl",
"url": "@{items('Send_each_adaptive_card')?['url']}"
}
}
]
}
]
}
📎 小技巧:
若拿到的程式碼中拿到預設的內容是items('Apply_to_each') 在存檔時應該會出現Flow 報錯 Apply_to_each 不存在,
這時請將items('Apply_to_each')把上面 JSON 改成items('Send_each_adaptive_card')。
3️⃣正確填完應該為以下結果,就可以按存檔完成流程建立

4️⃣測試後會在Teams指定的接收通知頻道收到以下結果

這樣就能同時:
- 維持原有 payload 架構(你不用改 Python 程式太多)
- 又能在 Teams 呈現紅字、粗體、超連結效果
💬 結語:適合誰用?
這個完整版本最適合:
- 想快速替換舊 webhook 的告警機制與通知流程的系統
- 需要自訂或維持原本卡片樣式
- 使用 Pub/Sub、CI/CD job、排程系統做提醒






















