From bfdeb340f5cb063c3073e86fe754775b15b63faf Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Tue, 5 Feb 2019 23:11:45 -0500 Subject: [PATCH] updated discord to default to notify_text; refs #53 --- apprise/plugins/NotifyBase.py | 4 +++ apprise/plugins/NotifyDiscord.py | 48 ++++++++++++++++++-------------- test/test_rest_plugins.py | 48 +++++++++++++++++++------------- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/apprise/plugins/NotifyBase.py b/apprise/plugins/NotifyBase.py index 87b794ff..8bc18389 100644 --- a/apprise/plugins/NotifyBase.py +++ b/apprise/plugins/NotifyBase.py @@ -267,6 +267,10 @@ class NotifyBase(object): Takes html text as input and escapes it so that it won't conflict with any xml/html wrapping characters. """ + if not html: + # nothing more to do; return object as is + return html + escaped = _escape(html) if whitespace: diff --git a/apprise/plugins/NotifyDiscord.py b/apprise/plugins/NotifyDiscord.py index 405ef667..4d80f613 100644 --- a/apprise/plugins/NotifyDiscord.py +++ b/apprise/plugins/NotifyDiscord.py @@ -77,9 +77,6 @@ class NotifyDiscord(NotifyBase): # The maximum allowable characters allowed in the body per message body_maxlen = 2000 - # Default Notify Format - notify_format = NotifyFormat.MARKDOWN - def __init__(self, webhook_id, webhook_token, tts=False, avatar=True, footer=False, thumbnail=True, **kwargs): """ @@ -136,9 +133,15 @@ class NotifyDiscord(NotifyBase): 'wait': self.tts is False, # Our color associated with our notification - 'color': self.color(notify_type, int), + 'color': self.color(notify_type, int) + } - 'embeds': [{ + # Acquire image_url + image_url = self.image_url(notify_type) + + if self.notify_format == NotifyFormat.MARKDOWN: + # Use embeds for payload + payload['embeds'] = [{ 'provider': { 'name': self.app_id, 'url': self.app_url, @@ -147,9 +150,8 @@ class NotifyDiscord(NotifyBase): 'type': 'rich', 'description': body, }] - } - if self.notify_format == NotifyFormat.MARKDOWN: + # Break titles out so that we can sort them in embeds fields = self.extract_markdown_sections(body) if len(fields) > 0: @@ -160,25 +162,29 @@ class NotifyDiscord(NotifyBase): fields[0].get('name') + fields[0].get('value') payload['embeds'][0]['fields'] = fields[1:] - if self.footer: - logo_url = self.image_url(notify_type, logo=True) - payload['embeds'][0]['footer'] = { - 'text': self.app_desc, - } - if logo_url: - payload['embeds'][0]['footer']['icon_url'] = logo_url + if self.footer: + logo_url = self.image_url(notify_type, logo=True) + payload['embeds'][0]['footer'] = { + 'text': self.app_desc, + } - image_url = self.image_url(notify_type) - if image_url: - if self.thumbnail: + if logo_url: + payload['embeds'][0]['footer']['icon_url'] = logo_url + + if self.thumbnail and image_url: payload['embeds'][0]['thumbnail'] = { 'url': image_url, 'height': 256, 'width': 256, } - if self.avatar: - payload['avatar_url'] = image_url + else: + # not markdown + payload['content'] = body if not title \ + else "{}\r\n{}".format(title, body) + + if self.avatar and image_url: + payload['avatar_url'] = image_url if self.user: # Optionally override the default username of the webhook @@ -291,8 +297,8 @@ class NotifyDiscord(NotifyBase): """ regex = re.compile( - r'\s*#+\s*(?P[^#\n]+)([ \r\t\v#]*)' - r'(?P(.+?)(\n(?!\s#))|\s*$)', flags=re.S) + r'^\s*#+\s*(?P[^#\n]+)([ \r\t\v#])?' + r'(?P([^ \r\t\v#].+?)(\n(?!\s#))|\s*$)', flags=re.S|re.M) common = regex.finditer(markdown) fields = list() diff --git a/test/test_rest_plugins.py b/test/test_rest_plugins.py index 63b41df8..e9b20338 100644 --- a/test/test_rest_plugins.py +++ b/test/test_rest_plugins.py @@ -132,13 +132,15 @@ TEST_URLS = ( 'requests_response_code': requests.codes.no_content, }), # Enable other options - ('discord://%s/%s?footer=Yes&thumbnail=Yes' % ('i' * 24, 't' * 64), { - 'instance': plugins.NotifyDiscord, - 'requests_response_code': requests.codes.no_content, + ('discord://%s/%s?format=markdown&footer=Yes&thumbnail=Yes' % ( + 'i' * 24, 't' * 64), { + 'instance': plugins.NotifyDiscord, + 'requests_response_code': requests.codes.no_content, }), - ('discord://%s/%s?avatar=No&footer=No' % ('i' * 24, 't' * 64), { - 'instance': plugins.NotifyDiscord, - 'requests_response_code': requests.codes.no_content, + ('discord://%s/%s?format=markdown&avatar=No&footer=No' % ( + 'i' * 24, 't' * 64), { + 'instance': plugins.NotifyDiscord, + 'requests_response_code': requests.codes.no_content, }), # different format support ('discord://%s/%s?format=markdown' % ('i' * 24, 't' * 64), { @@ -1695,11 +1697,6 @@ def test_notify_discord_plugin(mock_post, mock_get): assert obj.notify(title='title', body='body', notify_type=NotifyType.INFO) is True - # Toggle our logo availability - obj.asset.image_url_logo = None - assert obj.notify(title='title', body='body', - notify_type=NotifyType.INFO) is True - # Test our header parsing test_markdown = "## Heading one\nbody body\n\n" + \ "# Heading 2 ##\n\nTest\n\n" + \ @@ -1719,19 +1716,30 @@ def test_notify_discord_plugin(mock_post, mock_get): assert obj.notify(title='title', body=test_markdown, notify_type=NotifyType.INFO) is True + # Create an apprise instance + a = Apprise() + # Our processing is slightly different when we aren't using markdown # as we do not pre-parse content during our notifications - obj = plugins.NotifyDiscord( - webhook_id=webhook_id, - webhook_token=webhook_token, - notify_format=NotifyFormat.TEXT) - - # Disable throttling to speed up unit tests - obj.throttle_attempt = 0 + assert a.add( + 'discord://{webhook_id}/{webhook_token}/' + '?format=markdown&footer=Yes'.format( + webhook_id=webhook_id, + webhook_token=webhook_token)) is True # This call includes an image with it's payload: - assert obj.notify(title='title', body='body', - notify_type=NotifyType.INFO) is True + assert a.notify(title='title', body=test_markdown, + notify_type=NotifyType.INFO, + body_format=NotifyFormat.TEXT) is True + + assert a.notify(title='title', body=test_markdown, + notify_type=NotifyType.INFO, + body_format=NotifyFormat.MARKDOWN) is True + + # Toggle our logo availability + a.asset.image_url_logo = None + assert a.notify(title='title', body='body', + notify_type=NotifyType.INFO) is True @mock.patch('requests.get')