From 2d97b66e9961070b9fcc3e260fd2f0922ab75ec2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 20 Apr 2021 02:25:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A0=20Add=20control=20characters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 6 +-- pyproject.toml | 2 +- royalnet_telethon/bullet/contents/__init__.py | 10 ++++- royalnet_telethon/formatting.py | 42 +++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 royalnet_telethon/formatting.py diff --git a/poetry.lock b/poetry.lock index f670a096..94814f21 100644 --- a/poetry.lock +++ b/poetry.lock @@ -284,7 +284,7 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"] [[package]] name = "royalnet" -version = "6.5.0" +version = "6.5.4" description = "A multipurpose bot framework" category = "main" optional = false @@ -747,8 +747,8 @@ requests = [ {file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"}, ] royalnet = [ - {file = "royalnet-6.5.0-py3-none-any.whl", hash = "sha256:1c1f0ddb4891d1951bd8fe2e9a4139cc5b3a039d9b2318c0cabd489dc2253ee3"}, - {file = "royalnet-6.5.0.tar.gz", hash = "sha256:dc4e5a284e8e2f36f259b5fe88e36a5947984c78600fd22e695e62277c787951"}, + {file = "royalnet-6.5.4-py3-none-any.whl", hash = "sha256:68d30fdd00653a5e79eab2b22a8c5bb16103e08e8674ec8f4b13ff1bdf1d33d2"}, + {file = "royalnet-6.5.4.tar.gz", hash = "sha256:fbab17b911c2ee200c0f7bfd5d2bcc4d1d3f9771b41ab64a848a08cdd464e3d8"}, ] rsa = [ {file = "rsa-4.7.2-py3-none-any.whl", hash = "sha256:78f9a9bf4e7be0c5ded4583326e7461e3a3c5aae24073648b4bdfa797d78c9d2"}, diff --git a/pyproject.toml b/pyproject.toml index 3d3dcee8..7366b159 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "royalnet-telethon" -version = "2.0.0" +version = "2.1.0" description = "A Telethon-based frontend for the royalnet.engineer module." authors = ["Stefano Pigozzi "] license = "AGPL-3.0-or-later" diff --git a/royalnet_telethon/bullet/contents/__init__.py b/royalnet_telethon/bullet/contents/__init__.py index 6dd23016..c8b44ee1 100644 --- a/royalnet_telethon/bullet/contents/__init__.py +++ b/royalnet_telethon/bullet/contents/__init__.py @@ -8,6 +8,7 @@ import telethon.tl.types as tlt import telethon.tl.custom as tlc import async_property as ap import datetime +from royalnet_telethon.formatting import tg_html_format class TelegramMessage(co.Message): @@ -39,7 +40,7 @@ class TelegramMessage(co.Message): async def reply(self, *, text: str = None, files: t.List[t.BinaryIO] = None) -> t.Optional[TelegramMessage]: - sent = await self._msg.reply(message=text, file=files) + sent = await self._msg.reply(message=tg_html_format(text), file=files, parse_mode="HTML") return TelegramMessage(msg=sent) @@ -59,7 +60,12 @@ class TelegramChannel(co.Channel): async def send_message(self, *, text: str = None, files: t.List[t.BinaryIO] = None) -> t.Optional[TelegramMessage]: - sent = await self._client.send_message(self._channel, message=text, file=files) + sent = await self._client.send_message( + self._channel, + message=tg_html_format(text), + file=files, + parse_mode="HTML" + ) return TelegramMessage(msg=sent) diff --git a/royalnet_telethon/formatting.py b/royalnet_telethon/formatting.py new file mode 100644 index 00000000..c5242662 --- /dev/null +++ b/royalnet_telethon/formatting.py @@ -0,0 +1,42 @@ +def tg_html_format(string: str) -> str: + """ + Format a string as Telegram HTML. + + The following characters will be considered control characters: + - ``\uE01B``: start bold + - ``\uE00B``: end bold + - ``\uE011``: start italic + - ``\uE001``: end italic + - ``\uE012``: start underline + - ``\uE002``: end underline + - ``\uE015``: start strike + - ``\uE005``: end strike + - ``\uE01C``: start single-line code + - ``\uE00C``: end single-line code + - ``\uE01D``: start multi-line code + - ``\uE00D``: end multi-line code + + :param string: The string to format. + :return: The formatted string. + + .. warning:: For now, this is a Telethon implementation detail. + + .. todo:: This may cause denial of service attacks from users! + """ + + string = string.replace("&", "&") + string = string.replace("<", "<") + string = string.replace(">", ">") + string = string.replace("\uE01B", "") + string = string.replace("\uE00B", "") + string = string.replace("\uE011", "") + string = string.replace("\uE001", "") + string = string.replace("\uE015", "") + string = string.replace("\uE005", "") + string = string.replace("\uE012", "") + string = string.replace("\uE002", "") + string = string.replace("\uE01C", "") + string = string.replace("\uE00C", "") + string = string.replace("\uE01D", "
")
+    string = string.replace("\uE00D", "
") + return string