Compare commits

..

3 Commits

Author SHA1 Message Date
udf 3178eb5fba
Increase length limits when sending html 2019-08-03 17:46:28 +02:00
udf 59245a4e6d
Move constants to function parameters 2019-08-03 17:43:37 +02:00
udf 6dc069afd9
Send html file when message is too long 2019-08-03 17:41:48 +02:00
1 changed files with 24 additions and 10 deletions

View File

@ -4,13 +4,11 @@
from telethon import events
from telethon.utils import add_surrogate
from telethon.tl.types import MessageEntityPre
from telethon.tl.types import MessageEntityPre, DocumentAttributeFilename
from telethon.tl.tlobject import TLObject
from telethon.errors import MessageTooLongError
import datetime
STR_LEN_MAX = 256
BYTE_LEN_MAX = 64
def parse_pre(text):
text = text.strip()
@ -20,7 +18,7 @@ def parse_pre(text):
)
def yaml_format(obj, indent=0):
def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
"""
Pretty formats the given object as a YAML string which is returned.
(based on TLObject.pretty_format)
@ -55,8 +53,8 @@ def yaml_format(obj, indent=0):
indent -= 2
elif isinstance(obj, str):
# truncate long strings and display elipsis
result = repr(obj[:STR_LEN_MAX])
if len(obj) > STR_LEN_MAX:
result = repr(obj[:max_str_len])
if len(obj) > max_str_len:
result += ''
return result
elif isinstance(obj, bytes):
@ -64,7 +62,7 @@ def yaml_format(obj, indent=0):
if all(0x20 <= c < 0x7f for c in obj):
return repr(obj)
else:
return ('<…>' if len(obj) > BYTE_LEN_MAX else
return ('<…>' if len(obj) > max_byte_len else
' '.join(f'{b:02X}' for b in obj))
elif isinstance(obj, datetime.datetime):
# ISO-8601 without timezone offset (telethon dates are always UTC)
@ -74,7 +72,6 @@ def yaml_format(obj, indent=0):
result.append('\n')
indent += 2
for x in obj:
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
result.append('\n')
result.pop()
@ -91,4 +88,21 @@ async def _(event):
return
msg = await event.message.get_reply_message()
yaml_text = yaml_format(msg)
try:
await event.edit(yaml_text, parse_mode=parse_pre)
except MessageTooLongError:
await event.delete()
yaml_text = yaml_format(
msg,
max_str_len=9999,
max_byte_len=9999
)
await borg.send_file(
await event.get_input_chat(),
f'<pre>{yaml_text}</pre>'.encode('utf-8'),
reply_to=msg,
attributes=[
DocumentAttributeFilename('info.html')
],
allow_cache=False
)