Skip to content

Instantly share code, notes, and snippets.

@mehd-io
Last active February 25, 2019 10:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehd-io/08a2c43a6cf5ba37342ec9af866b601f to your computer and use it in GitHub Desktop.
Save mehd-io/08a2c43a6cf5ba37342ec9af866b601f to your computer and use it in GitHub Desktop.
Airflow - proper Slack notifications

Define the custom function in the dag :

sc = SlackClient(my_slack_token)

def slack_failed_task(contextDictionary, **kwargs):
        base_url = mybasedUrl
        task_instance = contextDictionary.get('task_instance')
        log_url = '{task_instance.log_url}'.format(**locals())
        failed_alert = sc.api_call('chat.postMessage',
        link_names=1,
        task_id='slack_failed',
        channel='#mychannel',
        icon_url='https://raw.githubusercontent.com/airbnb/airflow/master/airflow/www/static/pin_100.png';,
        username='Airflow',
        text = ':hankey: DAG Failed on dagid=*{task_instance.dag_id}* taskid=*{task_instance.task_id}* :page_facing_up: <{log_url}|see log> @mehdio :trollface: ? '.format(**locals()))
        return failed_alert.execute

Then use on each task, on_failure_callback=slack_failed_task

@mehd-io
Copy link
Author

mehd-io commented Feb 25, 2019

For last version of the python slack client, the tag name doesnt work anymore, you need to specify id of the user via <@user_id>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment