背景
シンプルにAirflowのDAGでSlack投稿したい
方法
SlackWebhookOperatorを使って実装
import airflow
from datetime import timedelta
from airflow import DAG
from airflow.contrib.operators.slack_webhook_operator import SlackWebhookOperator
default_args = {
"owner": "airflow",
"start_date": airflow.utils.dates.days_ago(1),
"retries": 3,
"retry_delay": timedelta(minutes=5)
}
dag = DAG("slack_dag", default_args=default_args, catchup=False, schedule_interval="@daily")
slack = SlackWebhookOperator(
task_id='slack_test',
webhook_token="https://hooks.slack.com/services/xxxxxx",
channel="your_channel",
message="message",
icon_url="https://some-image/xxx",
attachments= [{
"title": "title",
"text": "text",
"color": "good"
}],
username="airflow_slack",
dag=dag
)