Telegram support for MarkdownV2 (#1101)

This commit is contained in:
Luckydonald 2024-04-11 03:20:16 +02:00 committed by Chris Caron
parent fa4a5a67bb
commit cced5ed7b5
2 changed files with 62 additions and 3 deletions

View File

@ -82,6 +82,34 @@ IS_CHAT_ID_RE = re.compile(
) )
class TelegramMarkdownVersion:
"""
Telegram Markdown Version
"""
# Classic (Original Telegram Markdown)
ONE = 'MARKDOWN'
# Supports strikethrough and many other items
TWO = 'MarkdownV2'
TELEGRAM_MARKDOWN_VERSION_MAP = {
# v1
"v1": TelegramMarkdownVersion.ONE,
"1": TelegramMarkdownVersion.ONE,
# v2
"v2": TelegramMarkdownVersion.TWO,
"2": TelegramMarkdownVersion.TWO,
"default": TelegramMarkdownVersion.TWO,
}
TELEGRAM_MARKDOWN_VERSIONS = {
# Note: This also acts as a reverse lookup mapping
TelegramMarkdownVersion.ONE: 'v1',
TelegramMarkdownVersion.TWO: 'v2',
}
class TelegramContentPlacement: class TelegramContentPlacement:
""" """
The Telegram Content Placement The Telegram Content Placement
@ -333,6 +361,12 @@ class NotifyTelegram(NotifyBase):
'name': _('Topic Thread ID'), 'name': _('Topic Thread ID'),
'type': 'int', 'type': 'int',
}, },
'mdv': {
'name': _('Markdown Version'),
'type': 'choice:string',
'values': ('v1', 'v2'),
'default': 'v2',
},
'to': { 'to': {
'alias_of': 'targets', 'alias_of': 'targets',
}, },
@ -346,7 +380,7 @@ class NotifyTelegram(NotifyBase):
def __init__(self, bot_token, targets, detect_owner=True, def __init__(self, bot_token, targets, detect_owner=True,
include_image=False, silent=None, preview=None, topic=None, include_image=False, silent=None, preview=None, topic=None,
content=None, **kwargs): content=None, mdv=None, **kwargs):
""" """
Initialize Telegram Object Initialize Telegram Object
""" """
@ -361,6 +395,17 @@ class NotifyTelegram(NotifyBase):
self.logger.warning(err) self.logger.warning(err)
raise TypeError(err) raise TypeError(err)
# Get our Markdown Version
self.markdown_ver = \
TELEGRAM_MARKDOWN_VERSION_MAP[NotifyTelegram.
template_args['mdv']['default']] \
if mdv is None else \
next((
v for k, v in TELEGRAM_MARKDOWN_VERSION_MAP.items()
if str(mdv).lower().startswith(k)),
TELEGRAM_MARKDOWN_VERSION_MAP[NotifyTelegram.
template_args['mdv']['default']])
# Define whether or not we should make audible alarms # Define whether or not we should make audible alarms
self.silent = self.template_args['silent']['default'] \ self.silent = self.template_args['silent']['default'] \
if silent is None else bool(silent) if silent is None else bool(silent)
@ -717,8 +762,7 @@ class NotifyTelegram(NotifyBase):
# Prepare Message Body # Prepare Message Body
if self.notify_format == NotifyFormat.MARKDOWN: if self.notify_format == NotifyFormat.MARKDOWN:
_payload['parse_mode'] = 'MARKDOWN' _payload['parse_mode'] = self.markdown_ver
_payload['text'] = body _payload['text'] = body
else: # HTML else: # HTML
@ -886,6 +930,7 @@ class NotifyTelegram(NotifyBase):
'silent': 'yes' if self.silent else 'no', 'silent': 'yes' if self.silent else 'no',
'preview': 'yes' if self.preview else 'no', 'preview': 'yes' if self.preview else 'no',
'content': self.content, 'content': self.content,
'mdv': TELEGRAM_MARKDOWN_VERSIONS[self.markdown_ver],
} }
if self.topic: if self.topic:
@ -990,6 +1035,10 @@ class NotifyTelegram(NotifyBase):
# Store our bot token # Store our bot token
results['bot_token'] = bot_token results['bot_token'] = bot_token
# Support Markdown Version
if 'mdv' in results['qsd'] and len(results['qsd']['mdv']):
results['mdv'] = results['qsd']['mdv']
# Support Thread Topic # Support Thread Topic
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']

View File

@ -122,6 +122,16 @@ apprise_url_tests = (
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown', { ('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown', {
'instance': NotifyTelegram, 'instance': NotifyTelegram,
}), }),
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown&mdv=v1', {
'instance': NotifyTelegram,
}),
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown&mdv=v2', {
'instance': NotifyTelegram,
}),
('tgram://123456789:abcdefg_hijklmnop/l2g/?format=markdown&mdv=bad', {
# Defaults to v2
'instance': NotifyTelegram,
}),
('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=html', { ('tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=html', {
'instance': NotifyTelegram, 'instance': NotifyTelegram,
}), }),