Add some setters for custom.Message

This commit is contained in:
Lonami Exo
2018-06-03 11:53:18 +02:00
parent 8b16023566
commit 8d7c7a19c0
4 changed files with 79 additions and 49 deletions

View File

@@ -6,10 +6,12 @@ import math
import mimetypes
import os
import re
import struct
import types
from collections import UserList
from mimetypes import guess_extension
from .tl import TLObject
from .tl.types import (
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
@@ -500,6 +502,44 @@ def _fix_peer_id(peer_id):
return int(peer_id)
def add_surrogate(text):
return ''.join(
# SMP -> Surrogate Pairs (Telegram offsets are calculated with these).
# See https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview for more.
''.join(chr(y) for y in struct.unpack('<HH', x.encode('utf-16le')))
if (0x10000 <= ord(x) <= 0x10FFFF) else x for x in text
)
def del_surrogate(text):
return text.encode('utf-16', 'surrogatepass').decode('utf-16')
def get_inner_text(text, entity):
"""
Gets the inner text that's surrounded by the given entity or entities.
For instance: text = 'hey!', entity = MessageEntityBold(2, 2) -> 'y!'.
:param text: the original text.
:param entity: the entity or entities that must be matched.
:return: a single result or a list of the text surrounded by the entities.
"""
if isinstance(entity, TLObject):
entity = (entity,)
multiple = True
else:
multiple = False
text = add_surrogate(text)
result = []
for e in entity:
start = e.offset
end = e.offset + e.length
result.append(del_surrogate(text[start:end]))
return result if multiple else result[0]
def get_peer_id(peer):
"""
Finds the ID of the given peer, and converts it to the "bot api" format