Browse Source

Updated NotifySlack payload & markdown handling (#412)

pull/419/head
Stefan Reimer 3 years ago committed by GitHub
parent
commit
49af85bfe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      apprise/plugins/NotifySlack.py

70
apprise/plugins/NotifySlack.py

@ -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,
) )
body = self._re_formatting_rules.sub( # pragma: no branch # Only for NONE markdown, otherwise eg links wont work
lambda x: self._re_formatting_map[x.group()], body, if self.notify_format != NotifyFormat.MARKDOWN:
) body = self._re_formatting_rules.sub( # pragma: no branch
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',
'text': {
'type': _slack_format,
'text': body
}
}],
'color': self.color(notify_type), 'color': self.color(notify_type),
# Time }]
'ts': time(),
}],
} }
# 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')
# Include the footer only if specified to do so
if self.include_footer: if self.include_footer:
# Include the footer only if specified to do so _footer = {
payload['attachments'][0]['footer'] = self.app_id '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

Loading…
Cancel
Save