Microsoft Teams notifications
This example shows how to set up Airflow notifications in a Microsoft Teams channel by using Airflow callbacks. Teams notifications about DAG runs and tasks let you quickly inform many team members about the status of your data pipelines.
Before you start
Before trying this example, make sure you have:
- Teams with a Business account supporting team channels.
- The Astro CLI.
- An Astro project running locally on your computer. See Getting started with the Astro CLI.
Send task failure notifications to MS Teams
Follow these steps to receive notifications in MS Teams for failed tasks in an example DAG. Refer to the Airflow callbacks section of our notifications guide to learn how to set up notifications for other types of events.
-
Open the folder containing your Astro Project. Copy the contents of the
include
folder in the project GitHub repository to your Astro projectinclude
folder.├── .astro
├── dags
└── include
├── hooks
│ └── ms_teams_webhook_hook.py
├── operators
│ └── ms_teams_webhook_operator.py
├── ms_teams_callback_functions.py
└── ms_teams_callback_functions_with_partial.py -
Create a Microsoft Teams Incoming Webhook for the channel where you want to receive notifications. Copy and save the webhook URL.
-
In the Airflow UI, create an Airflow connection by clicking on Admin and then Connections. Create a new connection with the following parameters. Note that you won't be able to test this connection from the Airflow UI.
- Connection Id:
ms_teams_callbacks
- Connection Type:
HTTP
- Host:
<your-organization>.office.com/webhook/<your-webhook-id>
- Schema:
https
- Connection Id:
Some corporate environments make use of outbound proxies. If you're behind an outbound proxy for internet access, put the proxy details in the Extra field when creating the HTTP Connection in the Airflow UI (For example, {"proxy":"http://my-proxy:3128"}
).
If the HTTP
connection type is not available, double check that the HTTP provider is installed in your Airflow environment.
-
Import the failure callback function at the top of your DAG file.
from include.ms_teams_callback_functions import failure_callback
-
Set the
on_failure_callback
keyword of your DAG'sdefault_args
parameter to the importedfailure_callback
function.@dag(
start_date=datetime(2023, 7, 1),
schedule="@daily",
default_args={
"on_failure_callback": failure_callback,
}
) -
Run your DAG. Any failed task will trigger the
failure_callback
function which sends a notification message to your Teams channel.
The include
folder of the project repository also contains callback functions for other triggers in addition to the failure callback shown here. You can modify any of the functions to customize the notification message. To learn more about all available callback parameters, see Airflow callbacks.