How to integrate Apache Airflow with slack?

后端 未结 3 1773
梦毁少年i
梦毁少年i 2020-12-29 13:41

could someone please give me step by step manual on how to connect Apache Airflow to Slack workspace. I created webhook to my channel and what should I do with it next ?

3条回答
  •  悲哀的现实
    2020-12-29 13:48

    • Create a Slack Token from https://api.slack.com/custom-integrations/legacy-tokens
    • Use the SlackAPIPostOperator Operator in your DAG as below
    SlackAPIPostOperator(
          task_id='failure',
          token='YOUR_TOKEN',
          text=text_message,
          channel=SLACK_CHANNEL,
          username=SLACK_USER)
    

    The above is the simplest way you can use Airflow to send messages to Slack.

    However, if you want to configure Airflow to send messages to Slack on task failures, create a function and add on_failure_callback to your tasks with the name of the created slack function. An example is below:

    def slack_failed_task(contextDictionary, **kwargs):  
           failed_alert = SlackAPIPostOperator(
             task_id='slack_failed',
             channel="#datalabs",
             token="...",
             text = ':red_circle: DAG Failed',
             owner = '_owner',)
             return failed_alert.execute()
    
    
    task_with_failed_slack_alerts = PythonOperator(
        task_id='task0',
        python_callable=,
        on_failure_callback=slack_failed_task,
        provide_context=True,
        dag=dag)
    

    Using SlackWebHook (Works only for Airflow >= 1.10.0): If you want to use SlackWebHook use SlackWebhookOperator in a similar manner:

    https://github.com/apache/incubator-airflow/blob/master/airflow/contrib/operators/slack_webhook_operator.py#L25

提交回复
热议问题