Last active
January 14, 2021 15:26
-
-
Save asolera/704b0e2c9b30665e4b12c5ad76428006 to your computer and use it in GitHub Desktop.
Airflow (2.0.0+) DAG example that sends notifications to MS Teams using Webhooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of sending MS Teams Notifications on DAG failure (Airflow 2.0.0+) | |
import requests | |
from airflow.decorators import dag, task | |
from airflow.operators.python import task, get_current_context | |
from airflow.utils.dates import days_ago | |
from airflow.models import DagRun | |
# from airflow.models import Variable | |
def on_failure(context): | |
exception = context.get("exception") | |
webhook = "https://outlook.office.com/webhook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/IncomingWebhook/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
# You can use Airflow variables instead (recommended) | |
# webhook = Variable.get("WEBHOOK_URL") | |
headers = {"Content-type": "application/json"} | |
data = {"text": "Hello from Airflow! Exception message: {}".format(exception)} | |
requests.post(webhook, headers=headers, json=data) | |
default_args = { | |
'owner': 'airflow', | |
'on_failure_callback': on_failure | |
} | |
@dag('MSTeamsNotification', default_args=default_args, schedule_interval=None, start_date=days_ago(1)) | |
def msteams_notification_dag(): | |
@task() | |
def example_task() -> None: | |
raise Exception("An example of MS Teams notification on Airflow DAG failure.") | |
example_task() | |
dag = msteams_notification_dag() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment