Support callable parse_mode's

This allows to more easily plug in custom parse methods.
This commit is contained in:
Lonami Exo 2018-04-03 14:23:05 +02:00
parent eabaa3854a
commit 07a8a73e3e

View File

@ -679,13 +679,18 @@ class TelegramClient(TelegramBareClient):
if not parse_mode: if not parse_mode:
return message, [] return message, []
parse_mode = parse_mode.lower() if isinstance(parse_mode, str):
if parse_mode in {'md', 'markdown'}: parse_mode = parse_mode.lower()
message, msg_entities = markdown.parse(message) if parse_mode in {'md', 'markdown'}:
elif parse_mode.startswith('htm'): message, msg_entities = markdown.parse(message)
message, msg_entities = html.parse(message) elif parse_mode.startswith('htm'):
message, msg_entities = html.parse(message)
else:
raise ValueError('Unknown parsing mode: {}'.format(parse_mode))
elif callable(parse_mode):
message, msg_entities = parse_mode(message)
else: else:
raise ValueError('Unknown parsing mode: {}'.format(parse_mode)) raise TypeError('Invalid parsing mode type: {}'.format(parse_mode))
for i, e in enumerate(msg_entities): for i, e in enumerate(msg_entities):
if isinstance(e, MessageEntityTextUrl): if isinstance(e, MessageEntityTextUrl):