Skip to content

Commit 3151877

Browse files
authored
Ntfy.sh Internationalized URL support added for click= (#1312)
1 parent 689fc07 commit 3151877

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

apprise/plugins/ntfy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from json import loads
4141
from json import dumps
4242
from os.path import basename
43-
43+
from urllib.parse import quote
4444
from .base import NotifyBase
4545
from ..common import NotifyFormat
4646
from ..common import NotifyType
@@ -347,7 +347,10 @@ def __init__(self, targets=None, attach=None, filename=None, click=None,
347347
self.filename = filename
348348

349349
# A clickthrough option for notifications
350-
self.click = click
350+
# Support Internationalized URLs
351+
self.click = None if not isinstance(click, str) else (
352+
click if not any(ord(char) > 127 for char in click)
353+
else quote(click, safe=':/?&=[]'))
351354

352355
# Time delay for notifications (various string formats)
353356
self.delay = delay
@@ -539,7 +542,7 @@ def _send(self, topic, body=None, title=None, attach=None, image_url=None,
539542
headers['X-Delay'] = self.delay
540543

541544
if self.click is not None:
542-
headers['X-Click'] = self.click
545+
headers['X-Click'] = quote(self.click, safe=':/?@&=#')
543546

544547
if self.email is not None:
545548
headers['X-Email'] = self.email

test/test_plugin_ntfy.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,46 @@ def test_plugin_ntfy_config_files(mock_post, mock_get):
583583
assert obj.url_id() == obj2.url_id()
584584

585585

586+
@mock.patch('requests.post')
587+
def test_plugin_ntfy_internationalized_urls(mock_post):
588+
"""
589+
NotifyNtfy() Internationalized URL Support
590+
591+
"""
592+
593+
# Prepare Mock return object
594+
response = mock.Mock()
595+
response.content = GOOD_RESPONSE_TEXT
596+
response.status_code = requests.codes.ok
597+
mock_post.return_value = response
598+
599+
# Our input
600+
title = 'My Title'
601+
body = 'My Body'
602+
603+
# Google Translate promised me this just says 'Apprise Example' (I hope
604+
# this is the case 🙏). Below is a URL requiring encoding so that it
605+
# can be correctly passed into an http header:
606+
click = 'https://通知の例'
607+
608+
# Prepare our object
609+
obj = apprise.Apprise.instantiate(f'ntfy://ntfy.sh/topic1?click={click}')
610+
611+
# Send our notification
612+
assert obj.notify(title=title, body=body)
613+
assert mock_post.call_count == 1
614+
615+
assert mock_post.call_args_list[0][0][0] == 'http://ntfy.sh'
616+
617+
# Verify that our International URL was correctly escaped
618+
assert 'https://%25E9%2580%259A%25E7%259F' \
619+
'%25A5%25E3%2581%25AE%25E4%25BE%258B' in \
620+
mock_post.call_args_list[0][1]['headers']['X-Click']
621+
622+
# Validate that we did not obstruct our URL in anyway
623+
assert apprise.Apprise.instantiate(obj.url()).url() == obj.url()
624+
625+
586626
@mock.patch('requests.post')
587627
def test_plugin_ntfy_message_to_attach(mock_post):
588628
"""

0 commit comments

Comments
 (0)