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,6 +679,7 @@ class TelegramClient(TelegramBareClient):
if not parse_mode:
return message, []
if isinstance(parse_mode, str):
parse_mode = parse_mode.lower()
if parse_mode in {'md', 'markdown'}:
message, msg_entities = markdown.parse(message)
@ -686,6 +687,10 @@ class TelegramClient(TelegramBareClient):
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:
raise TypeError('Invalid parsing mode type: {}'.format(parse_mode))
for i, e in enumerate(msg_entities):
if isinstance(e, MessageEntityTextUrl):