"""
Send a notification to slack.

NB: The webhook should be configured in the nextflow profile

"""

import os
import requests


def send_notification(title, message, plain=False):
    """
    Send a notification to the configured slack webhook.
    """
    SLACK_WEBHOOK = os.getenv('SLACK_WEBHOOK')
    if SLACK_WEBHOOK is None:
        raise SystemExit("SLACK_WEBHOOK environment variable not defined")

    if plain:
        slack_json = {
            "text" : title + ':  ' + message
        }
    else:
        slack_json = {
            "text" : title,
            "blocks" : [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": message
                    },
                },
            ]
        }
    try:
        response = requests.post(SLACK_WEBHOOK,
                    json=slack_json,
                    headers={'Content-Type':'application/json'}
                    )
        response.raise_for_status()
    except Exception as request_exception:
        raise SystemExit from request_exception

"""
Send a notification to slack.

NB: The webhook should be configured in the nextflow profile

"""

import os
import requests


def send_notification(title, message, plain=False):
    """
    Send a notification to the configured slack webhook.
    """
    SLACK_WEBHOOK = os.getenv('SLACK_WEBHOOK')
    if SLACK_WEBHOOK is None:
        try:
            from rnacentral_pipeline.secrets import SLACK_WEBHOOK
        except:
            raise SystemExit("SLACK_WEBHOOK environment variable not defined, and couldn't find a secrets file")

    if plain:
        slack_json = {
            "text" : title + ':  ' + message
        }
    else:
        slack_json = {
            "text" : title,
            "blocks" : [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": message
                    },
                },
            ]
        }
    try:
        response = requests.post(SLACK_WEBHOOK,
                    json=slack_json,
                    headers={'Content-Type':'application/json'}
                    )
        response.raise_for_status()
    except Exception as request_exception:
        raise SystemExit from request_exception

Add a secrets file in rnac notify
Add a secrets file in rnac notify

Nextflow doesn't propagate environment variables from the profile into 
the event handler closures. This is the simplest workaround for that. 
secrets.py should be on the cluster and symlinked into 
rnacentral_pipeline
