mirror of
https://github.com/caronc/apprise.git
synced 2025-01-07 14:39:51 +01:00
Added Telegram Topic Thread ID Support (#854)
This commit is contained in:
parent
b327abf134
commit
e83cba66a6
@ -312,13 +312,18 @@ class NotifyTelegram(NotifyBase):
|
||||
'type': 'bool',
|
||||
'default': False,
|
||||
},
|
||||
'topic': {
|
||||
'name': _('Topic Thread ID'),
|
||||
'type': 'int',
|
||||
},
|
||||
'to': {
|
||||
'alias_of': 'targets',
|
||||
},
|
||||
})
|
||||
|
||||
def __init__(self, bot_token, targets, detect_owner=True,
|
||||
include_image=False, silent=None, preview=None, **kwargs):
|
||||
include_image=False, silent=None, preview=None, topic=None,
|
||||
**kwargs):
|
||||
"""
|
||||
Initialize Telegram Object
|
||||
"""
|
||||
@ -344,6 +349,20 @@ class NotifyTelegram(NotifyBase):
|
||||
self.preview = self.template_args['preview']['default'] \
|
||||
if preview is None else bool(preview)
|
||||
|
||||
if topic:
|
||||
try:
|
||||
self.topic = int(topic)
|
||||
|
||||
except (TypeError, ValueError):
|
||||
# Not a valid integer; ignore entry
|
||||
err = 'The Telegram Topic ID specified ({}) is invalid.'\
|
||||
.format(topic)
|
||||
self.logger.warning(err)
|
||||
raise TypeError(err)
|
||||
else:
|
||||
# No Topic Thread
|
||||
self.topic = None
|
||||
|
||||
# if detect_owner is set to True, we will attempt to determine who
|
||||
# the bot owner is based on the first person who messaged it. This
|
||||
# is not a fool proof way of doing things as over time Telegram removes
|
||||
@ -635,6 +654,9 @@ class NotifyTelegram(NotifyBase):
|
||||
'disable_web_page_preview': not self.preview,
|
||||
}
|
||||
|
||||
if self.topic:
|
||||
payload['message_thread_id'] = self.topic
|
||||
|
||||
# Prepare Message Body
|
||||
if self.notify_format == NotifyFormat.MARKDOWN:
|
||||
payload['parse_mode'] = 'MARKDOWN'
|
||||
@ -782,6 +804,9 @@ class NotifyTelegram(NotifyBase):
|
||||
'preview': 'yes' if self.preview else 'no',
|
||||
}
|
||||
|
||||
if self.topic:
|
||||
params['topic'] = self.topic
|
||||
|
||||
# Extend our parameters
|
||||
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
|
||||
|
||||
@ -863,6 +888,10 @@ class NotifyTelegram(NotifyBase):
|
||||
# Store our bot token
|
||||
results['bot_token'] = bot_token
|
||||
|
||||
# Support Thread Topic
|
||||
if 'topic' in results['qsd'] and len(results['qsd']['topic']):
|
||||
results['topic'] = results['qsd']['topic']
|
||||
|
||||
# Silent (Sends the message Silently); users will receive
|
||||
# notification with no sound.
|
||||
results['silent'] = \
|
||||
|
@ -98,6 +98,14 @@ apprise_url_tests = (
|
||||
('tgram://bottest@123456789:abcdefg_hijklmnop/lead2gold/', {
|
||||
'instance': NotifyTelegram,
|
||||
}),
|
||||
# Support Thread Topics
|
||||
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=12345', {
|
||||
'instance': NotifyTelegram,
|
||||
}),
|
||||
# Threads must be numeric
|
||||
('tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=invalid', {
|
||||
'instance': TypeError,
|
||||
}),
|
||||
# Testing image
|
||||
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes', {
|
||||
'instance': NotifyTelegram,
|
||||
|
Loading…
Reference in New Issue
Block a user