|
|
@ -73,7 +73,6 @@ import re
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from json import dumps
|
|
|
|
from json import dumps
|
|
|
|
from json import loads
|
|
|
|
from json import loads
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from ..common import NotifyImageSize
|
|
|
|
from ..common import NotifyImageSize
|
|
|
@ -364,24 +363,40 @@ class NotifySlack(NotifyBase):
|
|
|
|
title = self._re_formatting_rules.sub( # pragma: no branch
|
|
|
|
title = self._re_formatting_rules.sub( # pragma: no branch
|
|
|
|
lambda x: self._re_formatting_map[x.group()], title,
|
|
|
|
lambda x: self._re_formatting_map[x.group()], title,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Only for NONE markdown, otherwise eg links wont work
|
|
|
|
|
|
|
|
if self.notify_format != NotifyFormat.MARKDOWN:
|
|
|
|
body = self._re_formatting_rules.sub( # pragma: no branch
|
|
|
|
body = self._re_formatting_rules.sub( # pragma: no branch
|
|
|
|
lambda x: self._re_formatting_map[x.group()], body,
|
|
|
|
lambda x: self._re_formatting_map[x.group()], body,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Prepare JSON Object (applicable to both WEBHOOK and BOT mode)
|
|
|
|
# Prepare JSON Object (applicable to both WEBHOOK and BOT mode)
|
|
|
|
|
|
|
|
_slack_format = 'mrkdwn' \
|
|
|
|
|
|
|
|
if self.notify_format == NotifyFormat.MARKDOWN else 'plain_text'
|
|
|
|
payload = {
|
|
|
|
payload = {
|
|
|
|
'username': self.user if self.user else self.app_id,
|
|
|
|
'username': self.user if self.user else self.app_id,
|
|
|
|
# Use Markdown language
|
|
|
|
|
|
|
|
'mrkdwn': (self.notify_format == NotifyFormat.MARKDOWN),
|
|
|
|
|
|
|
|
'attachments': [{
|
|
|
|
'attachments': [{
|
|
|
|
'title': title,
|
|
|
|
'blocks': [{
|
|
|
|
'text': body,
|
|
|
|
'type': 'section',
|
|
|
|
'color': self.color(notify_type),
|
|
|
|
'text': {
|
|
|
|
# Time
|
|
|
|
'type': _slack_format,
|
|
|
|
'ts': time(),
|
|
|
|
'text': body
|
|
|
|
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
|
|
|
|
'color': self.color(notify_type),
|
|
|
|
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Slack only accepts non-empty header sections
|
|
|
|
|
|
|
|
if title:
|
|
|
|
|
|
|
|
payload['attachments'][0]['blocks'].insert(0, {
|
|
|
|
|
|
|
|
'type': 'header',
|
|
|
|
|
|
|
|
'text': {
|
|
|
|
|
|
|
|
'type': 'plain_text',
|
|
|
|
|
|
|
|
'text': title,
|
|
|
|
|
|
|
|
'emoji': True
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
# Prepare our URL (depends on mode)
|
|
|
|
# Prepare our URL (depends on mode)
|
|
|
|
if self.mode is SlackMode.WEBHOOK:
|
|
|
|
if self.mode is SlackMode.WEBHOOK:
|
|
|
|
url = '{}/{}/{}/{}'.format(
|
|
|
|
url = '{}/{}/{}/{}'.format(
|
|
|
@ -394,9 +409,28 @@ class NotifySlack(NotifyBase):
|
|
|
|
else: # SlackMode.BOT
|
|
|
|
else: # SlackMode.BOT
|
|
|
|
url = self.api_url.format('chat.postMessage')
|
|
|
|
url = self.api_url.format('chat.postMessage')
|
|
|
|
|
|
|
|
|
|
|
|
if self.include_footer:
|
|
|
|
|
|
|
|
# Include the footer only if specified to do so
|
|
|
|
# Include the footer only if specified to do so
|
|
|
|
payload['attachments'][0]['footer'] = self.app_id
|
|
|
|
if self.include_footer:
|
|
|
|
|
|
|
|
_footer = {
|
|
|
|
|
|
|
|
'type': 'context',
|
|
|
|
|
|
|
|
'elements': [{
|
|
|
|
|
|
|
|
'type': _slack_format,
|
|
|
|
|
|
|
|
'text': self.app_id
|
|
|
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Acquire our to-be footer icon if configured to do so
|
|
|
|
|
|
|
|
image_url = None if not self.include_image \
|
|
|
|
|
|
|
|
else self.image_url(notify_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if image_url:
|
|
|
|
|
|
|
|
_footer['elements'].insert(0, {
|
|
|
|
|
|
|
|
'type': 'image',
|
|
|
|
|
|
|
|
'image_url': image_url,
|
|
|
|
|
|
|
|
'alt_text': notify_type
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
payload['attachments'][0]['blocks'].append(_footer)
|
|
|
|
|
|
|
|
|
|
|
|
if attach and self.mode is SlackMode.WEBHOOK:
|
|
|
|
if attach and self.mode is SlackMode.WEBHOOK:
|
|
|
|
# Be friendly; let the user know why they can't send their
|
|
|
|
# Be friendly; let the user know why they can't send their
|
|
|
@ -453,16 +487,6 @@ class NotifySlack(NotifyBase):
|
|
|
|
# slack. This list is used for sending attachments later.
|
|
|
|
# slack. This list is used for sending attachments later.
|
|
|
|
attach_channel_list.append(payload['channel'])
|
|
|
|
attach_channel_list.append(payload['channel'])
|
|
|
|
|
|
|
|
|
|
|
|
# Acquire our to-be footer icon if configured to do so
|
|
|
|
|
|
|
|
image_url = None if not self.include_image \
|
|
|
|
|
|
|
|
else self.image_url(notify_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if image_url:
|
|
|
|
|
|
|
|
payload['icon_url'] = image_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.include_footer:
|
|
|
|
|
|
|
|
payload['attachments'][0]['footer_icon'] = image_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = self._send(url, payload)
|
|
|
|
response = self._send(url, payload)
|
|
|
|
if not response:
|
|
|
|
if not response:
|
|
|
|
# Handle any error
|
|
|
|
# Handle any error
|
|
|
|