<commit_before>
import os
import json
import requests
import logging

from alerta.app import app
from alerta.plugins import PluginBase

LOG = logging.getLogger('alerta.plugins.slack')

SLACK_WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL') or app.config['SLACK_WEBHOOK_URL']
SLACK_ATTACHMENTS = True if os.environ.get('SLACK_ATTACHMENTS', 'False') == 'True' else app.config.get('SLACK_ATTACHMENTS', False)
SLACK_CHANNEL = os.environ.get('SLACK_CHANNEL') or app.config.get('SLACK_CHANNEL', '')
ALERTA_USERNAME = os.environ.get('ALERTA_USERNAME') or app.config.get('ALERTA_USERNAME', 'alerta')

ICON_EMOJI = os.environ.get('ICON_EMOJI') or app.config.get('ICON_EMOJI', ':rocket:')
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')

class ServiceIntegration(PluginBase):

    def pre_receive(self, alert):
        return alert

    def post_receive(self, alert):

        if alert.repeat:
            return

        url = SLACK_WEBHOOK_URL

        summary = "*[%s] %s %s - _%s on %s_* <%s/#/alert/%s|%s>" % (
            alert.status.capitalize(), alert.environment, alert.severity.capitalize(), alert.event, alert.resource, ALERTA_UI_URL,
            alert.id, alert.get_id(short=True)
        )

        if alert.severity == 'security':
            color = "#000000"  # black
        if alert.severity == 'critical':
            color = "#FF0000"  # red
        elif alert.severity == 'major':
            color = "#FFA500"  # orange
        elif alert.severity == 'minor':
            color = "#FFFF00"  # yellow
        elif alert.severity == 'warning':
            color = "#1E90FF"  # blue
        elif alert.severity == 'informational':
            color = "#808080"  # gray
        elif alert.severity == 'debug':
            color = "#808080"  # gray
        elif alert.severity == 'trace':
            color = "#808080"  # gray
        else:
            color = "#00CC00"  # green

        text = "<%s/#/alert/%s|%s> %s - %s" % (ALERTA_UI_URL, alert.get_id(), alert.get_id(short=True), alert.event, alert.text)

        if not SLACK_ATTACHMENTS:

            payload = {
                "username": ALERTA_USERNAME,
                "channel": SLACK_CHANNEL,
                "text": summary,
                "icon_emoji": ICON_EMOJI
            }

        else:
            payload = {
                "username": ALERTA_USERNAME,
                "channel": SLACK_CHANNEL,
                "icon_emoji": ICON_EMOJI,
                "attachments": [{
                    "fallback": summary,
                    "color": color,
                    "pretext": text,
                    "fields": [
                        {"title": "Status", "value": alert.status.capitalize(), "short": True},
                        {"title": "Environment", "value": alert.environment, "short": True},
                        {"title": "Resource", "value": alert.resource, "short": True},
                        {"title": "Services", "value": ", ".join(alert.service), "short": True}
                    ]
                }]
            }

        LOG.debug('Slack payload: %s', payload)<commit_msg>use dashboard url instead of missing ALERTA_UI_URL<commit_after>
import os
import json
import requests
import logging

from alerta.app import app
from alerta.plugins import PluginBase

LOG = logging.getLogger('alerta.plugins.slack')

SLACK_WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL') or app.config['SLACK_WEBHOOK_URL']
SLACK_ATTACHMENTS = True if os.environ.get('SLACK_ATTACHMENTS', 'False') == 'True' else app.config.get('SLACK_ATTACHMENTS', False)
SLACK_CHANNEL = os.environ.get('SLACK_CHANNEL') or app.config.get('SLACK_CHANNEL', '')
ALERTA_USERNAME = os.environ.get('ALERTA_USERNAME') or app.config.get('ALERTA_USERNAME', 'alerta')

ICON_EMOJI = os.environ.get('ICON_EMOJI') or app.config.get('ICON_EMOJI', ':rocket:')
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')

class ServiceIntegration(PluginBase):

    def pre_receive(self, alert):
        return alert

    def post_receive(self, alert):

        if alert.repeat:
            return

        url = SLACK_WEBHOOK_URL

        summary = "*[%s] %s %s - _%s on %s_* <%s/#/alert/%s|%s>" % (
            alert.status.capitalize(), alert.environment, alert.severity.capitalize(), alert.event, alert.resource, DASHBOARD_URL,
            alert.id, alert.get_id(short=True)
        )

        if alert.severity == 'security':
            color = "#000000"  # black
        if alert.severity == 'critical':
            color = "#FF0000"  # red
        elif alert.severity == 'major':
            color = "#FFA500"  # orange
        elif alert.severity == 'minor':
            color = "#FFFF00"  # yellow
        elif alert.severity == 'warning':
            color = "#1E90FF"  # blue
        elif alert.severity == 'informational':
            color = "#808080"  # gray
        elif alert.severity == 'debug':
            color = "#808080"  # gray
        elif alert.severity == 'trace':
            color = "#808080"  # gray
        else:
            color = "#00CC00"  # green

        text = "<%s/#/alert/%s|%s> %s - %s" % (DASHBOARD_URL, alert.get_id(), alert.get_id(short=True), alert.event, alert.text)

        if not SLACK_ATTACHMENTS:

            payload = {
                "username": ALERTA_USERNAME,
                "channel": SLACK_CHANNEL,
                "text": summary,
                "icon_emoji": ICON_EMOJI
            }

        else:
            payload = {
                "username": ALERTA_USERNAME,
                "channel": SLACK_CHANNEL,
                "icon_emoji": ICON_EMOJI,
                "attachments": [{
                    "fallback": summary,
                    "color": color,
                    "pretext": text,
                    "fields": [
                        {"title": "Status", "value": alert.status.capitalize(), "short": True},
                        {"title": "Environment", "value": alert.environment, "short": True},
                        {"title": "Resource", "value": alert.resource, "short": True},
                        {"title": "Services", "value": ", ".join(alert.service), "short": True}
                    ]
                }]
            }

        LOG.debug('Slack payload: %s', payload)
{'fraction_non_alphanumeric': 0.14110916891784522, 'fraction_numerical': 0.012712537740346417, 'mean_word_length': 2.053857350800582, 'pattern_counts': {'":': 48, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1051796', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1915, 'n_words': 401}