def send_to_slack_func(execution_date, **context): slack_message = context['ti'].xcom_pull(task_ids='bq_fetch_data') slack_operator = SlackAPIPostOperator( task_id='slack_operator', text=slack_message, username="******", token=Variable.get("slack_access_token"), channel=Variable.get("slack_channel")) slack_operator.execute(context=context)
def slack_failed_task(context_dictionary, **kwargs): """ Connects to Slack and sends a failed message with the table, the timestamp the task failed and params :param context_dictionary: Airflow keyword arguments (see all here: https://airflow.apache.org/code.html#macros) :return: Returns the execution for the failed function """ try: from config.secret import token_slack except ModuleNotFoundError: logging.warning( "There is no secret.py file inside dags/config/ with token_auth defined! No messages will be send to Slack" ) return # unpack context s_dag = context_dictionary['dag'].dag_id timestamp = context_dictionary['ts'] params = context_dictionary["params"] s_task = context_dictionary["task"] failed_alert = SlackAPIPostOperator( task_id='Failure', channel="#airflow-test", token=token_slack, text=":red_circle: DAG '{}'\nTask: '{}'\n Timestamp: {}\nParams '{}'". format(s_dag, s_task, timestamp, params), username="******", owner='_owner', ) return failed_alert.execute()
def slack_succeeded_task(context_dictionary, **kwargs): """ Connects to Slack and sends a success message with the table, the timestamp the task failed and params :param context_dictionary: Airflow keyword arguments (see all here: https://airflow.apache.org/code.html#macros) :return: Returns the execution for the passed function """ try: from config.secret import token_slack except ModuleNotFoundError: logging.warning( "There is no secret.py file inside dags/config/ with token_auth defined! No messages will be send to Slack" ) return # unpack context s_dag = context_dictionary['dag'].dag_id s_task = context_dictionary["task"] succeed_msg = SlackAPIPostOperator( task_id='Success', channel="#airflow-test", token=token_slack, text=":green_heart: Completed task {} from DAG '{}'".format( s_task, s_dag), username="******", owner='_owner', ) return succeed_msg.execute()
def send_to_slack_func(execution_date, **context): operator = SlackAPIPostOperator( task_id='postTopCommitter', text=execution_date.strftime("%a")+": "+str(context['task_instance'].xcom_pull(task_ids='bq_fetch_data')), token=Variable.get('slack_token'), channel='general' ) return operator.execute(context=context)
def slack_success_task(context): success_alert = SlackAPIPostOperator( task_id='slack_success', channel="#data", token="MySlackApi", text=':red_circle: Airflow Task Success', username='******', ) return success_alert.execute(context=context)
def get_slack_notification(slack_notification_message): logging.info("> started executing get_slack_notification() ") try: print("*************** > ",slack_notification_message) requests.post(slack_token , json={"channel": slack_channel , "text": slack_notification_message }) slack_push_notification = SlackAPIPostOperator( task_id='slack_notification_records', channel= slack_channel, token= slack_token, username= slack_user , text="Hi Update from Airflow \n "+slack_notification_message, dag=dag ) slack_push_notification.execute() return True except Exception as e: logging.error('ERROR: some issue with Slack configuration'.format(local_file_path, str(e))) raise
def send_to_slack_func(**context): lista = context.get('ti').xcom_pull(key='return_value', task_ids='bq_fetch_data') lista = ['date: {}'.format(context.get('ds'))] + lista operator = SlackAPIPostOperator( task_id='slack_api', text=str(lista), token=Variable.get('token'), channel=Variable.get("slack_channel") ) return operator.execute(context=context)
def send_to_slack_func(**context): operator = SlackAPIPostOperator( task_id='send_to_slack', text=context.get('ds') + " >> " + str(context.get('ti').xcom_pull(key=None, task_ids='bq_fetch_data')), token=Variable.get('slack_key'), # todo: should be passed into a variable from Airflow channel="general", ) return operator.execute(context=context)
def slack_success_task(contextDictionary, **kwargs): success_alert = SlackAPIPostOperator( task_id='slack_success', channel="#{}".format(_slack_channel), token= "{}".format(_slack_token), text = """:sunny: [ DAG : {} ]\nSucceeded on: {} """.format(contextDictionary["dag"].dag_id, datetime.utcnow().isoformat()+"Z"), owner = 'Farah.C',) return success_alert.execute()
def slack_failed_task(contextDictionary, **kwargs): failed_alert = SlackAPIPostOperator( task_id = 'slack_failed', channel = "#{}".format(_slack_channel), token = "{}".format(_slack_token), text = """:rain_cloud: [ DAG : {} - Task : {} ]\nFailed on : {}\n------------- Exception -------------\n{}""".format( contextDictionary["dag"].dag_id, contextDictionary["task"].task_id, datetime.utcnow().isoformat()+"Z", contextDictionary["exception"]), owner = 'Farah.C',) return failed_alert.execute()
def send_to_slack_func(**context): task_id = 'top3_committers_airflow' text = context['task_instance'].xcom_pull(task_ids='bq_fetch_data') channel = "general" operator = SlackAPIPostOperator( task_id=task_id, token=Variable.get("secret_token"), channel=channel, username="******", text=text, icon_url="https://media.makeameme.org/created/i-am-your-5c16f0.jpg") return operator.execute(context=context)
def send_to_slack_func(execution_date, **context): ti = context['ti'] v1 = ti.xcom_pull(key=None, task_ids='bq_fetch_data') print(type(v1)) res = [] for x in v1: res.append("*" + x[0].encode('utf-8') + "*") execution = execution_date.to_date_string() execution_start = execution_date.subtract(days=7).to_date_string() print(execution) op = SlackAPIPostOperator( task_id="slack_post", text=", ".join(res) + " were _really_ active last week! " + str(execution_start) + " - " + str(execution), username="******", icon_url= "https://www.petmd.com/sites/default/files/Acute-Dog-Diarrhea-47066074.jpg", token=Variable.get("token"), dag=dag) op.execute(context=context)
def test_api_call_params_with_default_args(self, mock_hook): test_slack_conn_id = 'test_slack_conn_id' slack_api_post_operator = SlackAPIPostOperator( task_id='slack', username=self.test_username, slack_conn_id=test_slack_conn_id, ) slack_api_post_operator.execute() expected_api_params = { 'channel': "#general", 'username': self.test_username, 'text': 'No message has been set.\n' 'Here is a cat video instead\n' 'https://www.youtube.com/watch?v=J---aiyznGQ', 'icon_url': "https://raw.githubusercontent.com/apache/" "airflow/master/airflow/www/static/pin_100.png", 'attachments': '[]', 'blocks': '[]', } self.assertEqual(expected_api_params, slack_api_post_operator.api_params)
def task_fail_slack_alert(context): failed_alert = SlackAPIPostOperator( task_id='slack_failed', channel=slack_channel, token=slack_token, text=""" :red_circle: Task Failed. *Task*: {task} *Dag*: {dag} *Execution Time*: {exec_date} *Log Url*: {log_url} """.format( task=context.get('task_instance').task_id, dag=context.get('task_instance').dag_id, ti=context.get('task_instance'), exec_date=context.get('execution_date'), log_url=context.get('task_instance').log_url, )) return failed_alert.execute(context=context)
def failure_slack_message(context): slack_alert = SlackAPIPostOperator( task_id = 'task_failure_slack_message', channel = "#data_notifications", token = slack_token, username = slack_username text = ( ":heavy_exclamation_mark: *Daily Update* \n" "*Date*: %s \n" "*Alert*: `DAG: %s, Task: %s`" % ( yesterday , context.get('task_instance').dag_id , context.get('task_instance').task_id ) ) ) return(slack_alert.execute(context=context))
def task_fail_slack_alert(context): slack_msg = """ :red_circle: Task Failure *Task*: {task} *Dag*: {dag} *Execution Time*: {exec_date} *Log Url*: {log_url} """.format( task=context.get("task_instance").task_id, dag=context.get("task_instance").dag_id, ti=context.get("task_instance"), exec_date=context.get("execution_date"), log_url=context.get("task_instance").log_url, ) notification = SlackAPIPostOperator( task_id="successnotification", channel="#bidata", username="******", text=slack_msg, token=SLACK_TOKEN) return notification.execute()