Skip to content

Commit

Permalink
Added Telegram Topic Thread ID Support (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc authored May 12, 2023
1 parent b327abf commit e83cba6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
31 changes: 30 additions & 1 deletion apprise/plugins/NotifyTelegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -344,6 +349,20 @@ def __init__(self, bot_token, targets, detect_owner=True,
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
Expand Down Expand Up @@ -635,6 +654,9 @@ def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
'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'
Expand Down Expand Up @@ -782,6 +804,9 @@ def url(self, privacy=False, *args, **kwargs):
'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))

Expand Down Expand Up @@ -863,6 +888,10 @@ def parse_url(url):
# 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'] = \
Expand Down
8 changes: 8 additions & 0 deletions test/test_plugin_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
('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,
Expand Down

0 comments on commit e83cba6

Please sign in to comment.