1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

⚠ Add control characters

This commit is contained in:
Steffo 2021-04-20 02:25:25 +02:00
parent bb23a58733
commit 2d97b66e99
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 54 additions and 6 deletions

6
poetry.lock generated
View file

@ -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"},

View file

@ -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 <me@steffo.eu>"]
license = "AGPL-3.0-or-later"

View file

@ -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)

View file

@ -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("&", "&amp;")
string = string.replace("<", "&lt;")
string = string.replace(">", "&gt;")
string = string.replace("\uE01B", "<b>")
string = string.replace("\uE00B", "</b>")
string = string.replace("\uE011", "<i>")
string = string.replace("\uE001", "</i>")
string = string.replace("\uE015", "<s>")
string = string.replace("\uE005", "</s>")
string = string.replace("\uE012", "<u>")
string = string.replace("\uE002", "</u>")
string = string.replace("\uE01C", "<code>")
string = string.replace("\uE00C", "</code>")
string = string.replace("\uE01D", "<pre>")
string = string.replace("\uE00D", "</pre>")
return string