hamsy
hamsy

Reputation: 369

Airflow <> Slack

I just started learning Airflow and I'm trying to create a small airflow script to send a notification to slack but somehow it fails to call for Slack API. First task works fine. Here is the code I use:

from airflow import DAG
from airflow.contrib.operators.bigquery_check_operator import BigQueryCheckOperator
from airflow.operators import BashOperator
from slackclient import SlackClient
from airflow.operators.slack_operator import SlackAPIPostOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'test',
    'depends_on_past': False,
    'start_date': datetime.today(),
    'email': ['test@test'],
    'email_on_failure': True,
    'email_on_retry': False,
    'retries': 4,
    'retry_delay': timedelta(minutes=5),
}

# Create the DAG
dag = DAG('slack_test',\
    default_args=default_args,\
    schedule_interval='15 * * * *')

final_check = BigQueryCheckOperator(
    task_id='bq_check_agg',
    sql='''
    #legacy sql
    SELECT
    max(date) as max_date
    FROM
    [test:test.test]
    WHERE date <= current_timestamp()
    ''',
    dag = dag)

    # slack notification
slack_notify = SlackAPIPostOperator(
    task_id='slack_notify',
    username  = 'Airflow',
    token='....',
    channel='#test-channel',
    text='ETL DONE!',
    dag=dag
)
slack_notify.set_upstream(final_check)

When I try to test slack_notify task, it fails. Here is the error I receive:

airflow.exceptions.AirflowException: Slack API call failed (%s)

Couldn't find any solutions for that online. Is something wrong with the slack authentication ?

Upvotes: 0

Views: 2648

Answers (2)

Eric
Eric

Reputation: 2729

I think you could invite your Airflow bot into your private channel first and then the slack bot can post message in that channel. Otherwise the slack bot cannot find a private channel and it will raise not_in_channel Slack API error.

With public slack channel you don't need to do that and you will receive messages from your Airflow bot.

Hope it helps.

Upvotes: 1

hamsy
hamsy

Reputation: 369

Solved. I was trying to post in a private slack channel. Worked fine with a public one

Upvotes: 0

Related Questions