mirror of https://github.com/caronc/apprise
updated discord to default to notify_text; refs #53
parent
042c5ed2da
commit
bfdeb340f5
|
@ -267,6 +267,10 @@ class NotifyBase(object):
|
||||||
Takes html text as input and escapes it so that it won't
|
Takes html text as input and escapes it so that it won't
|
||||||
conflict with any xml/html wrapping characters.
|
conflict with any xml/html wrapping characters.
|
||||||
"""
|
"""
|
||||||
|
if not html:
|
||||||
|
# nothing more to do; return object as is
|
||||||
|
return html
|
||||||
|
|
||||||
escaped = _escape(html)
|
escaped = _escape(html)
|
||||||
|
|
||||||
if whitespace:
|
if whitespace:
|
||||||
|
|
|
@ -77,9 +77,6 @@ class NotifyDiscord(NotifyBase):
|
||||||
# The maximum allowable characters allowed in the body per message
|
# The maximum allowable characters allowed in the body per message
|
||||||
body_maxlen = 2000
|
body_maxlen = 2000
|
||||||
|
|
||||||
# Default Notify Format
|
|
||||||
notify_format = NotifyFormat.MARKDOWN
|
|
||||||
|
|
||||||
def __init__(self, webhook_id, webhook_token, tts=False, avatar=True,
|
def __init__(self, webhook_id, webhook_token, tts=False, avatar=True,
|
||||||
footer=False, thumbnail=True, **kwargs):
|
footer=False, thumbnail=True, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -136,9 +133,15 @@ class NotifyDiscord(NotifyBase):
|
||||||
'wait': self.tts is False,
|
'wait': self.tts is False,
|
||||||
|
|
||||||
# Our color associated with our notification
|
# 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': {
|
'provider': {
|
||||||
'name': self.app_id,
|
'name': self.app_id,
|
||||||
'url': self.app_url,
|
'url': self.app_url,
|
||||||
|
@ -147,9 +150,8 @@ class NotifyDiscord(NotifyBase):
|
||||||
'type': 'rich',
|
'type': 'rich',
|
||||||
'description': body,
|
'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)
|
fields = self.extract_markdown_sections(body)
|
||||||
|
|
||||||
if len(fields) > 0:
|
if len(fields) > 0:
|
||||||
|
@ -165,19 +167,23 @@ class NotifyDiscord(NotifyBase):
|
||||||
payload['embeds'][0]['footer'] = {
|
payload['embeds'][0]['footer'] = {
|
||||||
'text': self.app_desc,
|
'text': self.app_desc,
|
||||||
}
|
}
|
||||||
|
|
||||||
if logo_url:
|
if logo_url:
|
||||||
payload['embeds'][0]['footer']['icon_url'] = logo_url
|
payload['embeds'][0]['footer']['icon_url'] = logo_url
|
||||||
|
|
||||||
image_url = self.image_url(notify_type)
|
if self.thumbnail and image_url:
|
||||||
if image_url:
|
|
||||||
if self.thumbnail:
|
|
||||||
payload['embeds'][0]['thumbnail'] = {
|
payload['embeds'][0]['thumbnail'] = {
|
||||||
'url': image_url,
|
'url': image_url,
|
||||||
'height': 256,
|
'height': 256,
|
||||||
'width': 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
|
payload['avatar_url'] = image_url
|
||||||
|
|
||||||
if self.user:
|
if self.user:
|
||||||
|
@ -291,8 +297,8 @@ class NotifyDiscord(NotifyBase):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
regex = re.compile(
|
regex = re.compile(
|
||||||
r'\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#]*)'
|
r'^\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#])?'
|
||||||
r'(?P<value>(.+?)(\n(?!\s#))|\s*$)', flags=re.S)
|
r'(?P<value>([^ \r\t\v#].+?)(\n(?!\s#))|\s*$)', flags=re.S|re.M)
|
||||||
|
|
||||||
common = regex.finditer(markdown)
|
common = regex.finditer(markdown)
|
||||||
fields = list()
|
fields = list()
|
||||||
|
|
|
@ -132,11 +132,13 @@ TEST_URLS = (
|
||||||
'requests_response_code': requests.codes.no_content,
|
'requests_response_code': requests.codes.no_content,
|
||||||
}),
|
}),
|
||||||
# Enable other options
|
# 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,
|
'instance': plugins.NotifyDiscord,
|
||||||
'requests_response_code': requests.codes.no_content,
|
'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,
|
'instance': plugins.NotifyDiscord,
|
||||||
'requests_response_code': requests.codes.no_content,
|
'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',
|
assert obj.notify(title='title', body='body',
|
||||||
notify_type=NotifyType.INFO) is True
|
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 our header parsing
|
||||||
test_markdown = "## Heading one\nbody body\n\n" + \
|
test_markdown = "## Heading one\nbody body\n\n" + \
|
||||||
"# Heading 2 ##\n\nTest\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,
|
assert obj.notify(title='title', body=test_markdown,
|
||||||
notify_type=NotifyType.INFO) is True
|
notify_type=NotifyType.INFO) is True
|
||||||
|
|
||||||
|
# Create an apprise instance
|
||||||
|
a = Apprise()
|
||||||
|
|
||||||
# Our processing is slightly different when we aren't using markdown
|
# Our processing is slightly different when we aren't using markdown
|
||||||
# as we do not pre-parse content during our notifications
|
# 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_id=webhook_id,
|
||||||
webhook_token=webhook_token,
|
webhook_token=webhook_token)) is True
|
||||||
notify_format=NotifyFormat.TEXT)
|
|
||||||
|
|
||||||
# Disable throttling to speed up unit tests
|
|
||||||
obj.throttle_attempt = 0
|
|
||||||
|
|
||||||
# This call includes an image with it's payload:
|
# 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
|
notify_type=NotifyType.INFO) is True
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue