Skip to content

Commit

Permalink
Ntfy inline images are now correctly displayed (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Jul 1, 2023
1 parent b6e07dd commit 276da2e
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions apprise/plugins/NotifyNtfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,6 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
'User-Agent': self.app_id,
}

# Some default values for our request object to which we'll update
# depending on what our payload is
files = None

# See https://ntfy.sh/docs/publish/#publish-as-json
data = {}

Expand Down Expand Up @@ -494,11 +490,23 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
data['topic'] = topic
virt_payload = data

if self.attach:
virt_payload['attach'] = self.attach

if self.filename:
virt_payload['filename'] = self.filename

else:
# Point our payload to our parameters
virt_payload = params
notify_url += '/{topic}'.format(topic=topic)

# Prepare our Header
virt_payload['filename'] = attach.name

with open(attach.path, 'rb') as fp:
data = fp.read()

if image_url:
headers['X-Icon'] = image_url

Expand All @@ -523,18 +531,6 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
if self.__tags:
headers['X-Tags'] = ",".join(self.__tags)

if isinstance(attach, AttachBase):
# Prepare our Header
params['filename'] = attach.name

# prepare our files object
files = {'file': (attach.name, open(attach.path, 'rb'))}

elif self.attach is not None:
data['attach'] = self.attach
if self.filename is not None:
data['filename'] = self.filename

self.logger.debug('ntfy POST URL: %s (cert_verify=%r)' % (
notify_url, self.verify_certificate,
))
Expand All @@ -547,13 +543,18 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
# Default response type
response = None

if data:
data = data if attach else dumps(data)

else: # not data:
data = None

try:
r = requests.post(
notify_url,
params=params if params else None,
data=dumps(data) if data else None,
data=data,
headers=headers,
files=files,
auth=auth,
verify=self.verify_certificate,
timeout=self.request_timeout,
Expand Down Expand Up @@ -608,21 +609,15 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
notify_url) + 'notification.'
)
self.logger.debug('Socket Exception: %s' % str(e))
return False, response

except (OSError, IOError) as e:
self.logger.warning(
'An I/O error occurred while handling {}.'.format(
attach.name if isinstance(attach, AttachBase)
else virt_payload))
self.logger.debug('I/O Exception: %s' % str(e))
return False, response

finally:
# Close our file (if it's open) stored in the second element
# of our files tuple (index 1)
if files:
files['file'][1].close()
return False, response

def url(self, privacy=False, *args, **kwargs):
"""
Expand Down

0 comments on commit 276da2e

Please sign in to comment.