Share via


從 Azure Data Factory 或 Synapse Analytics 管線將通知傳送至 Microsoft Teams 通道

在管線執行期間或之後,通常需要傳送通知。 通知會提供主動式警示,並減少需要回應式監視來探索問題。  您可以了解如何使用邏輯應用程式來傳送電子郵件通知,而資料處理站或 Synapse 管道可以叫用這些邏輯應用程式。 越來越多的企業也使用 Microsoft Teams 進行共同作業。 本文說明如何設定通知,從管線警示傳送至 Microsoft Teams。 

必要條件

在可以從管線將通知傳送至 Teams 之前,您必須先針對 Teams 通道建立傳入 Webhook。 如果您需要針對此目的建立新的 Teams 通道,請參閱 Teams 文件。  

  1. 開啟 Microsoft Teams 並移至 [應用程式] 索引標籤。搜尋「傳入 Webhook」,然後選取 [傳入 Webhook 連接器]。

    Shows the Incoming Webhook app under the Apps tab in Teams.

  2. 選取 [新增至小組] 按鈕,將連接器新增至您想要傳送通知的小組或小組通道名稱網站。

    Highlights the "Add to a team" button for the Incoming Webhook app.

  3. 輸入或選取您想要傳送通知的小組或小組通道名稱。

    Shows the team selection prompt on the Incoming Webhook app configuration dialog in Teams. Type the "Team or Team channel name"

  4. 選取 [設定連接器] 按鈕,為您在上一個步驟中選取的小組或小組通道名稱設定傳入 Webhook。

    Shows the team selection prompt on the Incoming Webhook app configuration dialog in Teams. Highlights the Team and the "Set up a connector" button

  5. 適當地命名 Webhook,並選擇性地上傳圖示以識別您的訊息。 之後,選取 [建立] 按鈕以建立傳入 Webhook。

    Highlights the name property, optional image upload, and "Create" button in the Incoming Webhook options page.  

  6. 複製建立時產生的 Webhook URL,並將其儲存在管線中以供稍後使用。 之後,選取 [完成] 按鈕以完成設定。

    Shows the new webhook URL on the Incoming Webhook options page after creation.

  7. 您可以在新增 Webhook 連接器的通道中看到通知。

    Shows the notification in the Teams channel where you added the webhook connector.

從管線傳送 Teams 通道通知的步驟:

  1. 從左側窗格中選取 [建立者] 索引標籤。

  2. 選取 + (加號) 按鈕,然後選取 [新增管線]

    Shows the "New pipeline" menu in the Azure Data Factory Studio.

  3. 在 [一般] 下的 [屬性] 窗格中,針對 [名稱] 指定 NotifiyTeamsChannelPipeline。 然後,按一下右上角的 [屬性] 圖示來摺疊面板。

    Shows the "Properties" panel.

    Shows the "Properties" panel hidden.

  4. 在 [設定] 窗格中,選取 [參數],然後選取 [+ 新增] 按鈕,為您的管線定義下列參數。

    名稱 類型​ 預設值
    訂用帳戶 String Specify subscription id for the pipeline
    resourceGroup String Specify resource group name for the pipeline
    runId String @activity('Specify name of the calling pipeline').output['pipelineRunId']
    NAME String @activity('Specify name of the calling pipeline').output['pipelineName']
    triggerTime String @activity('Specify name of the calling pipeline').ExecutionStartTime
    status String @activity('Specify name of the calling pipeline').Status
    message String @activity('Specify name of the calling pipeline').Error['message']
    executionEndTime String @activity('Specify name of the calling pipeline').ExecutionEndTime
    runDuration String @activity('Specify name of the calling pipeline').Duration
    teamWebhookUrl String Specify Team Webhook URL

    Shows the "Pipeline parameters".

    注意

    這些參數是用來建構監視 URL。 假設您未提供有效的訂用帳戶和資源群組 (管線所屬的相同資料處理站)。 在此情況下,通知不會包含有效的管線監視 URL,但訊息仍可運作。 此外,新增這些參數有助於防止一律從另一個管線傳遞這些值的需求。 如果想要透過中繼資料驅動方法來控制這些值,您應該據以修改它們。

    提示

    建議您新增目前的 Data Factory 訂用帳戶識別碼資源群組,以及 Teams Webhook URL (請參閱必要條件),以取得相關參數的預設值。

  5. 在 [設定] 窗格中,選取 [變數],然後選取 [+ 新增] 按鈕,為您的管線定義下列變數。

    名稱 類型​ 預設值
    messageCard String

    Shows the "Pipeline variables".

  6. 在管線 [活動] 窗格中搜尋 [設定變數],然後將 [設定變數] 活動拖曳至管線畫布。

  7. 在畫布上選取 [設定變數] 活動 (如果尚未選取的話),以及其 [一般] 索引標籤,來編輯其詳細資料。

  8. 在 [一般] 索引標籤中,針對 [設定變數] 活動的 [名稱] 指定 [設定 JSON 結構描述]

    Shows the "Set variable" activity general tab.

  9. 在 [變數] 索引標籤中,為 Name 屬性選取 messageCard 變數,並針對其 Value 屬性輸入下列 JSON

    {
        "@type": "MessageCard",
        "@context": "http://schema.org/extensions",
        "themeColor": "0076D7",
        "summary": "Pipeline status alert message​​​​",
        "sections": [
            {
                "activityTitle": "Pipeline execution alert​​​​",
                "facts": [
                    {
                        "name": "Subscription Id:",
                        "value": "@{pipeline().parameters.subscription}"
                    },
                    {
                        "name": "Resource Group:",
                        "value": "@{pipeline().parameters.resourceGroup}"
                    },
                    {
                        "name": "Data Factory Name:",
                        "value": "@{pipeline().DataFactory}"
                    },
                    {
                        "name": "Pipeline RunId:",
                        "value": "@{pipeline().parameters.runId}"
                    },
                    {
                        "name": "Pipline Name:",
                        "value": "@{pipeline().Pipeline}"
                    },
                    {
                        "name": "Pipeline Status:",
                        "value": "@{pipeline().parameters.status}"
                    },
                    {
                        "name": "Execution Start Time (UTC):",
                        "value": "@{pipeline().parameters.triggerTime}"
                    },
                    {
                        "name": "Execution Finish Time (UTC):",
                        "value": "@{pipeline().parameters.executionEndTime}"
                    },
                    {
                        "name": "Execution Duration (s):",
                        "value": "@{pipeline().parameters.runDuration}"
                    },
                    {
                        "name": "Message:",
                        "value": "@{pipeline().parameters.message}"
                    },
                    {
                        "name": "Notification Time (UTC):",
                        "value": "@{utcnow()}"
                    }
                ],
                "markdown": true
            }
        ],
        "potentialAction": [
            {
                "@type": "OpenUri",
                "name": "View pipeline run",
                "targets": [
                    {
                        "os": "default",
                        "uri": "@{concat('https://synapse.azure.com/monitoring/pipelineruns/',pipeline().parameters.runId,'?factory=/subscriptions/',pipeline().parameters.subscription,'/resourceGroups/',pipeline().parameters.resourceGroup,'/providers/Microsoft.DataFactory/factories/',pipeline().DataFactory)}"
                    }
                ]
            }
        ]
    }
    

    Shows the "Set variable" activity variables tab.

  10. 在管線 [活動] 窗格中搜尋 "Web",然後將 Web 活動拖曳至管線畫布。

  11. 建立 [Web] 活動的相依性條件,讓其只有在 [設定變數] 成功時才會執行。 若要建立此相依性,請選取 [設定變數] 活動右側的綠色控點、拖曳該控點,然後將其連線至 [Web] 活動。

  12. 在畫布上選取新的 [Web] 活動 (如果尚未選取的話),以及其 [一般] 索引標籤,來編輯其詳細資料。

  13. 在 [一般] 窗格中,針對 [Web] 活動的 [名稱] 指定 [叫用 Teams Webhook URL]

    Shows the "Web" activity general pane.

  14. 在 [設定] 窗格中,設定下列屬性,如下所示:

    屬性 value
    URL @pipeline().parameters.teamWebhookUrl
    方法 POST
    本文 @json(variables('messageCard'))

    Shows the "Web" activity settings pane.

  15. 全部已設定,現在您已準備好驗證、偵錯,然後發佈 NotifiyTeamsChannelPipeline 管線。

    • 若要驗證管線,請從工具列中選取 [驗證]
    • 若要偵錯管線,請選取工具列上的 [偵錯]。 您可以在視窗底部的 [輸出] 索引標籤中看到管線執行的狀態。
    • 當管線可成功執行後,請在頂端的工具列中選取 [全部發佈]。 此動作會將您已建立的實體發佈至 Data Factory。 請靜待 [發佈成功] 訊息顯示。

    Shows the "Validate, Debug, Publish" buttons to validate, debug, and then publish your pipeline.

範例用法

在此範例使用案例中,我們將建立具有三個 [執行管線] 活動的主要管線。 第一個 [執行管線] 活動會叫用我們的 ETL 管線,其餘兩個 [執行管線] 活動會叫用 "NotifiyTeamsChannelPipeline" 管線,將相關的成功或失敗通知傳送至 Teams 通道,取決於 ETL 管線的執行狀態。

  1. Data Factory 中的左窗格選取 [撰寫] 索引標籤,或從 Synapse Studio 中的左窗格選取 [整合] 索引標籤。 接下來,選取 + (加號) 按鈕,然後選取 [管線] 以建立新的管線。

  2. 在 [一般] 面板的 [屬性] 下,針對 [名稱] 指定 MasterPipeline。 然後,按一下右上角的 [屬性] 圖示來摺疊面板。

  3. 在管線 [活動] 窗格中搜尋管線,並將三個 [執行管線] 活動拖曳至管線畫布。

  4. 在畫布上選取第一個 [執行管線] 活動 (如果尚未選取的話),以及其 [一般] 窗格,以編輯其詳細資料。

    • 針對 [執行管線] 活動的 [名稱] 屬性,建議使用您叫用的 ETL 管線名稱,而您想要對此管線傳送通知。 例如,已針對 [執行管線] 活動的 [名稱] 使用 LoadDataPipeline,因為其是我們叫用的管線名稱。
    • 在 [設定] 窗格中,選取現有的管線,或針對 [叫用的管線] 屬性使用 [+ 新增] 按鈕,來建立新的管線。 例如,在我們的案例中,我們已針對 [叫用的管線] 屬性選取 LoadDataPipeline 管線。 選取其他選項,並視需要設定管線的任何參數,以完成您的設定。

    Shows the "Execute pipeline" activity general pane for "LoadDataPipeline" pipeline.

    Shows the "Execute pipeline" activity setting pane for "LoadDataPipeline" pipeline.

  5. 在畫布上選取第二個 [執行管線] 活動,以及其 [一般] 窗格,以編輯其詳細資料。

    • 針對 [執行管線] 活動的 [名稱] 指定 [OnSuccess 通知]

    • 在 [設定] 窗格中,針對 [叫用的管線] 屬性選取我們稍早建立的 NotifiyTeamsChannelPipeline 管線。 根據活動類型視需要自訂參數。 例如,我已自訂參數,如下所示:

      名稱
      訂用帳戶 11111111-0000-aaaa-bbbb-0000000000
      resourceGroup contosorg
      runId @activity('LoadDataPipeline').output['pipelineRunId']
      NAME @activity('LoadDataPipeline').output['pipelineName']
      triggerTime @activity('LoadDataPipeline').ExecutionStartTime
      status @activity('LoadDataPipeline').Status
      message Pipeline - LoadDataPipeline ran with success.
      executionEndTime @activity('LoadDataPipeline').ExecutionEndTime
      runDuration @activity('LoadDataPipeline').Duration
      teamWebhookUrl https://microsoft.webhook.office.com/webhookb2/1234abcd-1x11-2ff1-ab2c-1234d0699a9e@72f988bf-32b1-41af-91ab-2d7cd011db47/IncomingWebhook/8212f66ad80040ab83cf68b554d9232a/17d524d0-ed5c-44ed-98a0-35c12dd89a6d
    • 建立第二個 [執行管線] 活動的相依性條件,讓其只有在第一個 [執行管線] 活動成功時才會執行。 若要建立此相依性,請選取第一個 [執行管線] 活動右側的綠色控點、拖曳該控點,然後將其連線到第二個 [執行管線] 活動。

    Shows the second "Execute pipeline" activity "OnSuccess Notification" general pane for "NotifiyTeamsChannelPipeline" pipeline.

    Shows the second "Execute pipeline" activity "OnSuccess Notification" setting pane for "NotifiyTeamsChannelPipeline" pipeline.

  6. 在畫布上選取第三個 [執行管線] 活動,以及其 [一般] 窗格,以編輯其詳細資料。

    • 針對 [執行管線] 活動的 [名稱] 指定 [OnFailure 通知]

    • 在 [設定] 窗格中,針對 [叫用的管線] 屬性選取 NotifiyTeamsChannelPipeline 管線。 根據活動類型視需要自訂參數。 例如,目前我已自訂參數,如下所示:

      名稱
      訂用帳戶 11111111-0000-aaaa-bbbb-0000000000
      resourceGroup contosorg
      runId @activity('LoadDataPipeline').output['pipelineRunId']
      NAME @activity('LoadDataPipeline').output['pipelineName']
      triggerTime @activity('LoadDataPipeline').ExecutionStartTime
      status @activity('LoadDataPipeline').Status
      message @activity('LoadDataPipeline').Error['message']
      executionEndTime @activity('LoadDataPipeline').ExecutionEndTime
      runDuration @activity('LoadDataPipeline').Duration
      teamWebhookUrl https://microsoft.webhook.office.com/webhookb2/1234abcd-1x11-2ff1-ab2c-1234d0699a9e@72f988bf-32b1-41af-91ab-2d7cd011db47/IncomingWebhook/8212f66ad80040ab83cf68b554d9232a/17d524d0-ed5c-44ed-98a0-35c12dd89a6d
    • 建立第三個 [執行管線] 活動的相依性條件,讓其只有在第一個 [執行管線] 活動失敗時才會執行。 若要建立此相依性,請選取第一個 [執行管線] 活動右側的紅色控點、拖曳該控點,然後將其連線到第三個 [執行管線] 活動。

    • 驗證、偵錯,然後發佈 MasterPipeline 管線。

    Shows the third "Execute pipeline" activity "OnFailure Notification" general pane for "NotifiyTeamsChannelPipeline" pipeline.

    Shows the third "Execute pipeline" activity "OnFailure Notification" settings pane for "NotifiyTeamsChannelPipeline" pipeline.

  7. 執行管線以接收 Teams 中的通知。 例如,以下是範例通知、我的管線何時成功執行,以及何時失敗時。

    Shows on success pipeline notifications in a Teams channel.

    Shows on failure pipeline notifications in a Teams channel.

  8. 選取 [檢視管線執行] 按鈕以檢視管線執行。

使用系統變數和運算式新增動態訊息

您可以使用系統變數運算式,使訊息成為動態。 例如:

  • @activity("CopyData").output.errors[0].Message

  • @activity("DataFlow").error.Message

上述運算式會從失敗中傳回相關的錯誤訊息,這可在 Teams 通道上以通知形式送出。 如需本主題的詳細資訊,請參閱複製活動輸出屬性一文。

我們也鼓勵您檢閱 Microsoft Teams 支援的通知承載結構描述,並根據您的需求進一步自訂上述範本。

如何從管線傳送電子郵件