'How to import SlackWebHookOperator in Airflow?

I want to integrate Slack alert messages when a task fail in Airflow with SlackWebHookOperator. I have issue with the import of the operator.

In this tutorial (https://medium.com/datareply/integrating-slack-alerts-in-airflow-c9dcd155105) the author mentions there are 2 ways to achieve this. The first implementation is using slack legacy tokens which can be deprecated anytime and the second is using slack webhook which is the implementation I want. The author mentions to install slack dependencies, quoted by the author "Remember to install slack dependencies pip install apache-airflow[slack]" but this doesn't install SlackWebHookOperator as the import throws a “Cannot find reference slack_webhook_operator in init.py” error. Is there any other installation I am missing?

from airflow.contrib.operators.slack_webhook_operator import SlackWebHookOperator

I expect the SlackWebHookOperator to be able to be referenced.

I have tried installing slackclient dependency but it didn't installed SlackWebHookOperator as I cannot reference it. SlackWebHookOperator is not available



Solution 1:[1]

from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator

Solution 2:[2]

For Airflow 2.x

airflow.contrib.operators.slack_webhook_operator has been deprecated.


You'd first have to install apache-airflow-providers-slack:

pip install apache-airflow-providers-slack

and finally import SlackWebhookOperator:

from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josef
Solution 2 Giorgos Myrianthous