mirror of
https://github.com/caronc/apprise.git
synced 2024-11-24 17:14:00 +01:00
Fix Telegram Thread/Topic handling (#1177)
* Fix Telegram Thread/Topic handling * fixed test coverage
This commit is contained in:
parent
65f9caed87
commit
47540523e0
@ -361,6 +361,9 @@ class NotifyTelegram(NotifyBase):
|
|||||||
'name': _('Topic Thread ID'),
|
'name': _('Topic Thread ID'),
|
||||||
'type': 'int',
|
'type': 'int',
|
||||||
},
|
},
|
||||||
|
'thread': {
|
||||||
|
'alias_of': 'topic',
|
||||||
|
},
|
||||||
'mdv': {
|
'mdv': {
|
||||||
'name': _('Markdown Version'),
|
'name': _('Markdown Version'),
|
||||||
'type': 'choice:string',
|
'type': 'choice:string',
|
||||||
@ -460,14 +463,13 @@ class NotifyTelegram(NotifyBase):
|
|||||||
self.detect_owner = False
|
self.detect_owner = False
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
if results.group('topic'):
|
||||||
topic = int(
|
topic = int(
|
||||||
results.group('topic')
|
results.group('topic')
|
||||||
if results.group('topic') else self.topic)
|
if results.group('topic') else self.topic)
|
||||||
|
else:
|
||||||
except TypeError:
|
# Default (if one set)
|
||||||
# No worries
|
topic = self.topic
|
||||||
topic = None
|
|
||||||
|
|
||||||
if results.group('name') is not None:
|
if results.group('name') is not None:
|
||||||
# Name
|
# Name
|
||||||
@ -730,7 +732,7 @@ class NotifyTelegram(NotifyBase):
|
|||||||
_id = self.detect_bot_owner()
|
_id = self.detect_bot_owner()
|
||||||
if _id:
|
if _id:
|
||||||
# Permanently store our id in our target list for next time
|
# Permanently store our id in our target list for next time
|
||||||
self.targets.append((str(_id), None))
|
self.targets.append((str(_id), self.topic))
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'Update your Telegram Apprise URL to read: '
|
'Update your Telegram Apprise URL to read: '
|
||||||
'{}'.format(self.url(privacy=True)))
|
'{}'.format(self.url(privacy=True)))
|
||||||
@ -1043,6 +1045,9 @@ class NotifyTelegram(NotifyBase):
|
|||||||
if 'topic' in results['qsd'] and len(results['qsd']['topic']):
|
if 'topic' in results['qsd'] and len(results['qsd']['topic']):
|
||||||
results['topic'] = results['qsd']['topic']
|
results['topic'] = results['qsd']['topic']
|
||||||
|
|
||||||
|
elif 'thread' in results['qsd'] and len(results['qsd']['thread']):
|
||||||
|
results['topic'] = results['qsd']['thread']
|
||||||
|
|
||||||
# Silent (Sends the message Silently); users will receive
|
# Silent (Sends the message Silently); users will receive
|
||||||
# notification with no sound.
|
# notification with no sound.
|
||||||
results['silent'] = \
|
results['silent'] = \
|
||||||
|
@ -98,6 +98,10 @@ apprise_url_tests = (
|
|||||||
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=12345', {
|
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=12345', {
|
||||||
'instance': NotifyTelegram,
|
'instance': NotifyTelegram,
|
||||||
}),
|
}),
|
||||||
|
# Thread is just an alias of topic
|
||||||
|
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?thread=12345', {
|
||||||
|
'instance': NotifyTelegram,
|
||||||
|
}),
|
||||||
# Threads must be numeric
|
# Threads must be numeric
|
||||||
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=invalid', {
|
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=invalid', {
|
||||||
'instance': TypeError,
|
'instance': TypeError,
|
||||||
@ -106,6 +110,11 @@ apprise_url_tests = (
|
|||||||
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?content=invalid', {
|
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?content=invalid', {
|
||||||
'instance': TypeError,
|
'instance': TypeError,
|
||||||
}),
|
}),
|
||||||
|
('tgram://bottest@123456789:abcdefg_hijklmnop/id1:invalid/?thread=12345', {
|
||||||
|
'instance': NotifyTelegram,
|
||||||
|
# Notify will fail (bad target)
|
||||||
|
'response': False,
|
||||||
|
}),
|
||||||
# Testing image
|
# Testing image
|
||||||
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes', {
|
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes', {
|
||||||
'instance': NotifyTelegram,
|
'instance': NotifyTelegram,
|
||||||
@ -907,12 +916,9 @@ def test_plugin_telegram_html_formatting(mock_post):
|
|||||||
NotifyTelegram() HTML Formatting
|
NotifyTelegram() HTML Formatting
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# on't send anything other than <b>, <i>, <a>,<code> and <pre>
|
|
||||||
|
|
||||||
# Prepare Mock
|
# Prepare Mock
|
||||||
mock_post.return_value = requests.Request()
|
mock_post.return_value = requests.Request()
|
||||||
mock_post.return_value.status_code = requests.codes.ok
|
mock_post.return_value.status_code = requests.codes.ok
|
||||||
mock_post.return_value.content = '{}'
|
|
||||||
|
|
||||||
# Simple success response
|
# Simple success response
|
||||||
mock_post.return_value.content = dumps({
|
mock_post.return_value.content = dumps({
|
||||||
@ -942,7 +948,6 @@ def test_plugin_telegram_html_formatting(mock_post):
|
|||||||
}},
|
}},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
mock_post.return_value.status_code = requests.codes.ok
|
|
||||||
|
|
||||||
aobj = Apprise()
|
aobj = Apprise()
|
||||||
aobj.add('tgram://123456789:abcdefg_hijklmnop/')
|
aobj.add('tgram://123456789:abcdefg_hijklmnop/')
|
||||||
@ -1005,3 +1010,111 @@ def test_plugin_telegram_html_formatting(mock_post):
|
|||||||
'<b>Heading 3</b>\r\n<b>Heading 4</b>\r\n<b>Heading 5</b>\r\n' \
|
'<b>Heading 3</b>\r\n<b>Heading 4</b>\r\n<b>Heading 5</b>\r\n' \
|
||||||
'<b>Heading 6</b>\r\nA set of text\r\n' \
|
'<b>Heading 6</b>\r\nA set of text\r\n' \
|
||||||
'Another line after the set of text\r\nMore text\r\nlabel'
|
'Another line after the set of text\r\nMore text\r\nlabel'
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('requests.post')
|
||||||
|
def test_plugin_telegram_threads(mock_post):
|
||||||
|
"""
|
||||||
|
NotifyTelegram() Threads/Topics
|
||||||
|
|
||||||
|
"""
|
||||||
|
# Prepare Mock
|
||||||
|
mock_post.return_value = requests.Request()
|
||||||
|
mock_post.return_value.status_code = requests.codes.ok
|
||||||
|
|
||||||
|
# Simple success response
|
||||||
|
mock_post.return_value.content = dumps({
|
||||||
|
"ok": True,
|
||||||
|
"result": [{
|
||||||
|
"update_id": 645421321,
|
||||||
|
"message": {
|
||||||
|
"message_id": 2,
|
||||||
|
"from": {
|
||||||
|
"id": 532389719,
|
||||||
|
"is_bot": False,
|
||||||
|
"first_name": "Chris",
|
||||||
|
"language_code": "en-US"
|
||||||
|
},
|
||||||
|
"chat": {
|
||||||
|
"id": 532389719,
|
||||||
|
"first_name": "Chris",
|
||||||
|
"type": "private"
|
||||||
|
},
|
||||||
|
"date": 1519694394,
|
||||||
|
"text": "/start",
|
||||||
|
"entities": [{
|
||||||
|
"offset": 0,
|
||||||
|
"length": 6,
|
||||||
|
"type": "bot_command",
|
||||||
|
}],
|
||||||
|
}},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
aobj = Apprise()
|
||||||
|
aobj.add('tgram://123456789:abcdefg_hijklmnop/?thread=1234')
|
||||||
|
|
||||||
|
assert len(aobj) == 1
|
||||||
|
|
||||||
|
assert isinstance(aobj[0], NotifyTelegram)
|
||||||
|
|
||||||
|
body = 'my message'
|
||||||
|
|
||||||
|
assert aobj.notify(body=body)
|
||||||
|
|
||||||
|
# 1 call to look up bot owner, and second for notification
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
payload = loads(mock_post.call_args_list[1][1]['data'])
|
||||||
|
|
||||||
|
assert 'message_thread_id' in payload
|
||||||
|
assert payload['message_thread_id'] == 1234
|
||||||
|
|
||||||
|
mock_post.reset_mock()
|
||||||
|
|
||||||
|
aobj = Apprise()
|
||||||
|
aobj.add('tgram://123456789:abcdefg_hijklmnop/?topic=1234')
|
||||||
|
|
||||||
|
assert len(aobj) == 1
|
||||||
|
|
||||||
|
assert isinstance(aobj[0], NotifyTelegram)
|
||||||
|
|
||||||
|
body = 'my message'
|
||||||
|
|
||||||
|
assert aobj.notify(body=body)
|
||||||
|
|
||||||
|
# 1 call to look up bot owner, and second for notification
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
payload = loads(mock_post.call_args_list[1][1]['data'])
|
||||||
|
|
||||||
|
assert 'message_thread_id' in payload
|
||||||
|
assert payload['message_thread_id'] == 1234
|
||||||
|
|
||||||
|
mock_post.reset_mock()
|
||||||
|
|
||||||
|
aobj = Apprise()
|
||||||
|
aobj.add('tgram://123456789:abcdefg_hijklmnop/9876:1234/9876:1111')
|
||||||
|
|
||||||
|
assert len(aobj) == 1
|
||||||
|
|
||||||
|
assert isinstance(aobj[0], NotifyTelegram)
|
||||||
|
|
||||||
|
body = 'my message'
|
||||||
|
|
||||||
|
assert aobj.notify(body=body)
|
||||||
|
|
||||||
|
# 1 call to look up bot owner, and second for notification
|
||||||
|
assert mock_post.call_count == 2
|
||||||
|
|
||||||
|
payload = loads(mock_post.call_args_list[0][1]['data'])
|
||||||
|
|
||||||
|
assert 'message_thread_id' in payload
|
||||||
|
assert payload['message_thread_id'] == 1111
|
||||||
|
|
||||||
|
payload = loads(mock_post.call_args_list[1][1]['data'])
|
||||||
|
|
||||||
|
assert 'message_thread_id' in payload
|
||||||
|
assert payload['message_thread_id'] == 1234
|
||||||
|
|
||||||
|
mock_post.reset_mock()
|
||||||
|
Loading…
Reference in New Issue
Block a user