From 0e3d08ae7581a84749b4d2cb3d0a0858c43f5601 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 22 Jan 2018 00:26:43 +0100 Subject: [PATCH 1/5] Add HTML style parse mode --- pyrogram/client/client.py | 3 +- pyrogram/extensions/__init__.py | 1 + pyrogram/extensions/html.py | 103 ++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 pyrogram/extensions/html.py diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 131ce037..d325cc01 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -47,7 +47,7 @@ from pyrogram.api.types import ( InputPeerUser, InputPeerChat, InputPeerChannel ) from pyrogram.crypto import CTR -from pyrogram.extensions import Markdown +from pyrogram.extensions import Markdown, HTML from pyrogram.session import Auth, Session log = logging.getLogger(__name__) @@ -89,6 +89,7 @@ class Client: self.peers_by_username = {} self.markdown = Markdown(self.peers_by_id) + self.html = HTML(self.peers_by_id) self.config = None self.proxy = None diff --git a/pyrogram/extensions/__init__.py b/pyrogram/extensions/__init__.py index cd918a09..e60b4da1 100644 --- a/pyrogram/extensions/__init__.py +++ b/pyrogram/extensions/__init__.py @@ -16,4 +16,5 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .html import HTML from .markdown import Markdown diff --git a/pyrogram/extensions/html.py b/pyrogram/extensions/html.py new file mode 100644 index 00000000..99aa36ad --- /dev/null +++ b/pyrogram/extensions/html.py @@ -0,0 +1,103 @@ +import re +from struct import unpack + +from pyrogram.api.types import ( + MessageEntityBold as Bold, + MessageEntityItalic as Italic, + MessageEntityCode as Code, + MessageEntityTextUrl as Url, + MessageEntityPre as Pre, + InputMessageEntityMentionName as Mention +) + + +class HTML: + SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]") + + BOLD_RE = r"(?P(?P.*?))" + STRONG_RE = r"(?P(?P.*?))" + ITALIC_RE = r"(?P(?P.*?))" + EMPATHIZE_RE = r"(?P(?P.*?))" + CODE_RE = r"(?P(?P.*?))" + PRE_RE = r"(?P
(?P.*?)
)" + MENTION_RE = r"(?P\d+?)\">(?P.*?))" + URL_RE = r"(?P.*?)\">(?P.*?))" + + HTML_RE = re.compile("|".join([BOLD_RE, STRONG_RE, ITALIC_RE, EMPATHIZE_RE, CODE_RE, PRE_RE, MENTION_RE, URL_RE])) + + @classmethod + def add_surrogates(cls, text): + return cls.SMP_RE.sub( + lambda match: # Split SMP in two surrogates + "".join(chr(i) for i in unpack(" Date: Tue, 23 Jan 2018 14:43:12 +0100 Subject: [PATCH 2/5] Revamp HTML style parser --- pyrogram/extensions/html.py | 87 +++++++++++++------------------------ 1 file changed, 30 insertions(+), 57 deletions(-) diff --git a/pyrogram/extensions/html.py b/pyrogram/extensions/html.py index 99aa36ad..aa6c3fc2 100644 --- a/pyrogram/extensions/html.py +++ b/pyrogram/extensions/html.py @@ -7,23 +7,15 @@ from pyrogram.api.types import ( MessageEntityCode as Code, MessageEntityTextUrl as Url, MessageEntityPre as Pre, - InputMessageEntityMentionName as Mention + MessageEntityMentionName as MentionInvalid, + InputMessageEntityMentionName as Mention, ) class HTML: SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]") - - BOLD_RE = r"(?P(?P.*?))" - STRONG_RE = r"(?P(?P.*?))" - ITALIC_RE = r"(?P(?P.*?))" - EMPATHIZE_RE = r"(?P(?P.*?))" - CODE_RE = r"(?P(?P.*?))" - PRE_RE = r"(?P
(?P.*?)
)" - MENTION_RE = r"(?P\d+?)\">(?P.*?))" - URL_RE = r"(?P.*?)\">(?P.*?))" - - HTML_RE = re.compile("|".join([BOLD_RE, STRONG_RE, ITALIC_RE, EMPATHIZE_RE, CODE_RE, PRE_RE, MENTION_RE, URL_RE])) + HTML_RE = re.compile(r"<(\w+)(?: href=\"(.*)\")?>(.*)") + MENTION_RE = re.compile(r"tg://user\?id=(\d+)") @classmethod def add_surrogates(cls, text): @@ -45,57 +37,38 @@ class HTML: text = self.add_surrogates(text) offset = 0 - # TODO: Beautify ifs for match in self.HTML_RE.finditer(text): start = match.start() - offset + style, url, body = match.groups() - if match.group("b"): - pattern = match.group("b") - body = match.group("b_body") - entity = Bold(start, len(body)) - offset += 7 - elif match.group("strong"): - pattern = match.group("strong") - body = match.group("strong_body") - entity = Bold(start, len(body)) - offset += 17 - elif match.group("i"): - pattern = match.group("i") - body = match.group("i_body") - entity = Italic(start, len(body)) - offset += 7 - elif match.group("em"): - pattern = match.group("em") - body = match.group("em_body") - entity = Italic(start, len(body)) - offset += 9 - elif match.group("code"): - pattern = match.group("code") - body = match.group("code_body") - entity = Code(start, len(body)) - offset += 13 - elif match.group("pre"): - pattern = match.group("pre") - body = match.group("pre_body") - entity = Pre(start, len(body), "") - offset += 11 - elif match.group("mention"): - pattern = match.group("mention") - body = match.group("mention_text") - user_id = match.group("user_id") - entity = Mention(start, len(body), self.peers_by_id[int(user_id)]) - offset += len(user_id) + 28 - elif match.group("url"): - pattern = match.group("url") - body = match.group("url_text") - path = match.group("url_path") - entity = Url(start, len(body), path) - offset += len(path) + 15 + if url: + mention = self.MENTION_RE.match(url) + + if mention: + user_id = int(mention.group(1)) + input_user = self.peers_by_id.get(user_id, None) + + entity = ( + Mention(start, len(body), input_user) + if input_user else MentionInvalid(start, len(body), user_id) + ) + else: + entity = Url(start, len(body), url) else: - continue + if style == "b" or style == "strong": + entity = Bold(start, len(body)) + elif style == "i" or style == "em": + entity = Italic(start, len(body)) + elif style == "code": + entity = Code(start, len(body)) + elif style == "pre": + entity = Pre(start, len(body), "") + else: + continue entities.append(entity) - text = text.replace(pattern, body) + text = text.replace(match.group(), body) + offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0) return dict( message=self.remove_surrogates(text), From c39bf3043dfb02a57706fb73e7df49579ccc4009 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 23 Jan 2018 15:17:48 +0100 Subject: [PATCH 3/5] Move formatting classes inside the Client sub-package --- .../{extensions => client/style}/__init__.py | 0 pyrogram/{extensions => client/style}/html.py | 36 ++++++++++-------- .../{extensions => client/style}/markdown.py | 23 ++---------- pyrogram/client/style/utils.py | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 35 deletions(-) rename pyrogram/{extensions => client/style}/__init__.py (100%) rename pyrogram/{extensions => client/style}/html.py (68%) rename pyrogram/{extensions => client/style}/markdown.py (83%) create mode 100644 pyrogram/client/style/utils.py diff --git a/pyrogram/extensions/__init__.py b/pyrogram/client/style/__init__.py similarity index 100% rename from pyrogram/extensions/__init__.py rename to pyrogram/client/style/__init__.py diff --git a/pyrogram/extensions/html.py b/pyrogram/client/style/html.py similarity index 68% rename from pyrogram/extensions/html.py rename to pyrogram/client/style/html.py index aa6c3fc2..28091aa0 100644 --- a/pyrogram/extensions/html.py +++ b/pyrogram/client/style/html.py @@ -1,5 +1,22 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + import re -from struct import unpack from pyrogram.api.types import ( MessageEntityBold as Bold, @@ -10,6 +27,7 @@ from pyrogram.api.types import ( MessageEntityMentionName as MentionInvalid, InputMessageEntityMentionName as Mention, ) +from . import utils class HTML: @@ -17,24 +35,12 @@ class HTML: HTML_RE = re.compile(r"<(\w+)(?: href=\"(.*)\")?>(.*)") MENTION_RE = re.compile(r"tg://user\?id=(\d+)") - @classmethod - def add_surrogates(cls, text): - return cls.SMP_RE.sub( - lambda match: # Split SMP in two surrogates - "".join(chr(i) for i in unpack(". import re -from struct import unpack from pyrogram.api.types import ( MessageEntityBold as Bold, @@ -27,6 +26,7 @@ from pyrogram.api.types import ( MessageEntityPre as Pre, InputMessageEntityMentionName as Mention ) +from . import utils class Markdown: @@ -36,9 +36,6 @@ class Markdown: "`": Code } - # SMP = Supplementary Multilingual Plane: https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview - SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]") - # ``` python # for i in range(10): # print(i) @@ -69,26 +66,12 @@ class Markdown: MARKDOWN_RE = re.compile("|".join([PRE_RE, MENTION_RE, URL_RE, INLINE_RE])) - @classmethod - def add_surrogates(cls, text): - # Replace each SMP code point with a surrogate pair - return cls.SMP_RE.sub( - lambda match: # Split SMP in two surrogates - "".join(chr(i) for i in unpack(" +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import re +from struct import unpack + +# SMP = Supplementary Multilingual Plane: https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview +SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]") + + +def add_surrogates(text): + # Replace each SMP code point with a surrogate pair + return SMP_RE.sub( + lambda match: # Split SMP in two surrogates + "".join(chr(i) for i in unpack(" Date: Tue, 23 Jan 2018 15:18:52 +0100 Subject: [PATCH 4/5] Add optional parameter parse_mode --- pyrogram/client/client.py | 79 ++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 14 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index d325cc01..8aa8dd64 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -47,8 +47,8 @@ from pyrogram.api.types import ( InputPeerUser, InputPeerChat, InputPeerChannel ) from pyrogram.crypto import CTR -from pyrogram.extensions import Markdown, HTML from pyrogram.session import Auth, Session +from .style import Markdown, HTML log = logging.getLogger(__name__) @@ -472,6 +472,7 @@ class Client: def send_message(self, chat_id: int or str, text: str, + parse_mode: str = "", disable_web_page_preview: bool = None, disable_notification: bool = None, reply_to_msg_id: int = None): @@ -486,6 +487,10 @@ class Client: text (:obj:`str`): Text of the message to be sent. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your message. Defaults to "Markdown". + disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this message. @@ -502,6 +507,8 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown + return self.send( functions.messages.SendMessage( peer=self.resolve_peer(chat_id), @@ -509,7 +516,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_msg_id, random_id=self.rnd_id(), - **self.markdown.parse(text) + **style.parse(text) ) ) @@ -558,6 +565,7 @@ class Client: chat_id: int or str, photo: str, caption: str = "", + parse_mode: str = "", ttl_seconds: int = None, disable_notification: bool = None, reply_to_message_id: int = None): @@ -576,6 +584,10 @@ class Client: caption (:obj:`bool`, optional): Photo caption, 0-200 characters. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + ttl_seconds (:obj:`int`, optional): Self-Destruct Timer. If you set a timer, the photo will self-destruct in :obj:`ttl_seconds` @@ -594,6 +606,7 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown file = self.save_file(photo) while True: @@ -608,7 +621,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), - **self.markdown.parse(caption) + **style.parse(caption) ) ) except FilePartMissing as e: @@ -620,6 +633,7 @@ class Client: chat_id: int or str, audio: str, caption: str = "", + parse_mode: str = "", duration: int = 0, performer: str = None, title: str = None, @@ -642,6 +656,10 @@ class Client: caption (:obj:`str`, optional): Audio caption, 0-200 characters. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + duration (:obj:`int`, optional): Duration of the audio in seconds. @@ -664,6 +682,7 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown file = self.save_file(audio) while True: @@ -686,7 +705,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), - **self.markdown.parse(caption) + **style.parse(caption) ) ) except FilePartMissing as e: @@ -698,6 +717,7 @@ class Client: chat_id: int or str, document: str, caption: str = "", + parse_mode: str = "", disable_notification: bool = None, reply_to_message_id: int = None): """Use this method to send general files. @@ -715,6 +735,10 @@ class Client: caption (:obj:`str`, optional): Document caption, 0-200 characters. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + disable_notification (:obj:`bool`, optional): Sends the message silently. Users will receive a notification with no sound. @@ -728,6 +752,7 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown file = self.save_file(document) while True: @@ -745,7 +770,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), - **self.markdown.parse(caption) + **style.parse(caption) ) ) except FilePartMissing as e: @@ -756,10 +781,11 @@ class Client: def send_video(self, chat_id: int or str, video: str, + caption: str = "", + parse_mode: str = "", duration: int = 0, width: int = 0, height: int = 0, - caption: str = "", disable_notification: bool = None, reply_to_message_id: int = None): """Use this method to send video files. @@ -774,6 +800,13 @@ class Client: Video to send. Pass a file path as string to send a video that exists on your local machine. + caption (:obj:`str`, optional): + Video caption, 0-200 characters. + + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + duration (:obj:`int`, optional): Duration of sent video in seconds. @@ -783,9 +816,6 @@ class Client: height (:obj:`int`, optional): Video height. - caption (:obj:`str`, optional): - Video caption, 0-200 characters. - disable_notification (:obj:`bool`, optional): Sends the message silently. Users will receive a notification with no sound. @@ -799,6 +829,7 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown file = self.save_file(video) while True: @@ -820,7 +851,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), - **self.markdown.parse(caption) + **style.parse(caption) ) ) except FilePartMissing as e: @@ -832,6 +863,7 @@ class Client: chat_id: int or str, voice: str, caption: str = "", + parse_mode: str = "", duration: int = 0, disable_notification: bool = None, reply_to_message_id: int = None): @@ -850,6 +882,10 @@ class Client: caption (:obj:`str`, optional): Voice message caption, 0-200 characters. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + duration (:obj:`int`, optional): Duration of the voice message in seconds. @@ -866,6 +902,7 @@ class Client: Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown file = self.save_file(voice) while True: @@ -886,7 +923,7 @@ class Client: silent=disable_notification or None, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), - **self.markdown.parse(caption) + **style.parse(caption) ) ) except FilePartMissing as e: @@ -1193,6 +1230,7 @@ class Client: chat_id: int or str, message_id: int, text: str, + parse_mode: str = "", disable_web_page_preview: bool = None): """Use this method to edit text messages. @@ -1208,25 +1246,32 @@ class Client: text (:obj:`str`): New text of the message. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your message. Defaults to "Markdown". + disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this message. Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown + return self.send( functions.messages.EditMessage( peer=self.resolve_peer(chat_id), id=message_id, no_webpage=disable_web_page_preview or None, - **self.markdown.parse(text) + **style.parse(text) ) ) def edit_message_caption(self, chat_id: int or str, message_id: int, - caption: str): + caption: str, + parse_mode: str = ""): """Use this method to edit captions of messages. Args: @@ -1241,14 +1286,20 @@ class Client: caption (:obj:`str`): New caption of the message. + parse_mode (:obj:`str`): + Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or + inline URLs in your caption. Defaults to "Markdown". + Raises: :class:`pyrogram.Error` """ + style = self.html if parse_mode.lower() == "html" else self.markdown + return self.send( functions.messages.EditMessage( peer=self.resolve_peer(chat_id), id=message_id, - **self.markdown.parse(caption) + **style.parse(caption) ) ) From 84ae464b52364a787cb62b8c6d5cb5a65b79785a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 23 Jan 2018 15:34:36 +0100 Subject: [PATCH 5/5] Add ParseMode module --- pyrogram/__init__.py | 1 + pyrogram/client/__init__.py | 1 + pyrogram/client/client.py | 40 +++++++++++++++++++++-------------- pyrogram/client/parse_mode.py | 29 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 pyrogram/client/parse_mode.py diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index d6005bd8..a290ca0f 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -23,3 +23,4 @@ __version__ = "0.4.2" from .api.errors import Error from .client import ChatAction from .client import Client +from .client import ParseMode diff --git a/pyrogram/client/__init__.py b/pyrogram/client/__init__.py index 99c4337f..380f0cb4 100644 --- a/pyrogram/client/__init__.py +++ b/pyrogram/client/__init__.py @@ -18,3 +18,4 @@ from .chat_action import ChatAction from .client import Client +from .parse_mode import ParseMode diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 8aa8dd64..860b18d1 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -488,8 +488,9 @@ class Client: Text of the message to be sent. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your message. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your message. + Defaults to MARKDOWN. disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this message. @@ -585,8 +586,9 @@ class Client: Photo caption, 0-200 characters. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. ttl_seconds (:obj:`int`, optional): Self-Destruct Timer. @@ -657,8 +659,9 @@ class Client: Audio caption, 0-200 characters. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. duration (:obj:`int`, optional): Duration of the audio in seconds. @@ -736,8 +739,9 @@ class Client: Document caption, 0-200 characters. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. disable_notification (:obj:`bool`, optional): Sends the message silently. @@ -804,8 +808,9 @@ class Client: Video caption, 0-200 characters. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. duration (:obj:`int`, optional): Duration of sent video in seconds. @@ -883,8 +888,9 @@ class Client: Voice message caption, 0-200 characters. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. duration (:obj:`int`, optional): Duration of the voice message in seconds. @@ -1247,8 +1253,9 @@ class Client: New text of the message. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your message. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your message. + Defaults to MARKDOWN. disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this message. @@ -1287,8 +1294,9 @@ class Client: New caption of the message. parse_mode (:obj:`str`): - Use "Markdown" or "HTML" if you want Telegram apps to show bold, italic, fixed-width text or - inline URLs in your caption. Defaults to "Markdown". + Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps + to show bold, italic, fixed-width text or inline URLs in your caption. + Defaults to MARKDOWN. Raises: :class:`pyrogram.Error` diff --git a/pyrogram/client/parse_mode.py b/pyrogram/client/parse_mode.py new file mode 100644 index 00000000..668bb9c8 --- /dev/null +++ b/pyrogram/client/parse_mode.py @@ -0,0 +1,29 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2018 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + + +class ParseMode: + """This class provides a convenient access to parse modes. + It is intended to be used with any method that accepts the optional argument **parse_mode** + """ + + HTML = "html" + """Set the parse mode to HTML style""" + + MARKDOWN = "markdown" + """Set the parse mode to Markdown style"""