updated discord to default to notify_text; refs #53

pull/58/head
Chris Caron 2019-02-05 23:11:45 -05:00
parent 042c5ed2da
commit bfdeb340f5
3 changed files with 59 additions and 41 deletions

View File

@ -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:

View File

@ -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:
@ -165,19 +167,23 @@ class NotifyDiscord(NotifyBase):
payload['embeds'][0]['footer'] = {
'text': self.app_desc,
}
if logo_url:
payload['embeds'][0]['footer']['icon_url'] = logo_url
image_url = self.image_url(notify_type)
if image_url:
if self.thumbnail:
if self.thumbnail and image_url:
payload['embeds'][0]['thumbnail'] = {
'url': image_url,
'height': 256,
'width': 256,
}
if self.avatar:
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:
@ -291,8 +297,8 @@ class NotifyDiscord(NotifyBase):
"""
regex = re.compile(
r'\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#]*)'
r'(?P<value>(.+?)(\n(?!\s#))|\s*$)', flags=re.S)
r'^\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#])?'
r'(?P<value>([^ \r\t\v#].+?)(\n(?!\s#))|\s*$)', flags=re.S|re.M)
common = regex.finditer(markdown)
fields = list()

View File

@ -132,11 +132,13 @@ TEST_URLS = (
'requests_response_code': requests.codes.no_content,
}),
# Enable other options
('discord://%s/%s?footer=Yes&thumbnail=Yes' % ('i' * 24, 't' * 64), {
('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), {
('discord://%s/%s?format=markdown&avatar=No&footer=No' % (
'i' * 24, 't' * 64), {
'instance': plugins.NotifyDiscord,
'requests_response_code': requests.codes.no_content,
}),
@ -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,18 +1716,29 @@ 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(
assert a.add(
'discord://{webhook_id}/{webhook_token}/'
'?format=markdown&footer=Yes'.format(
webhook_id=webhook_id,
webhook_token=webhook_token,
notify_format=NotifyFormat.TEXT)
# Disable throttling to speed up unit tests
obj.throttle_attempt = 0
webhook_token=webhook_token)) is True
# This call includes an image with it's payload:
assert obj.notify(title='title', body='body',
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