Replace most raw API usage with new location

This commit is contained in:
Lonami Exo
2021-09-12 12:16:02 +02:00
parent a901d43a6d
commit d48649602b
53 changed files with 918 additions and 984 deletions

View File

@@ -8,8 +8,7 @@ from io import BytesIO
from struct import unpack
from ..errors import TypeNotFoundError
from ..tl.alltlobjects import tlobjects
from ..tl.core import core_objects
from .. import _tl
_EPOCH_NAIVE = datetime(*time.gmtime(0)[:6])
_EPOCH = _EPOCH_NAIVE.replace(tzinfo=timezone.utc)
@@ -118,7 +117,7 @@ class BinaryReader:
def tgread_object(self):
"""Reads a Telegram object."""
constructor_id = self.read_int(signed=False)
clazz = tlobjects.get(constructor_id, None)
clazz = _tl.tlobjects.get(constructor_id, None)
if clazz is None:
# The class was None, but there's still a
# chance of it being a manually parsed value like bool!
@@ -130,7 +129,7 @@ class BinaryReader:
elif value == 0x1cb5c415: # Vector
return [self.tgread_object() for _ in range(self.read_int())]
clazz = core_objects.get(constructor_id, None)
clazz = _tl.core.get(constructor_id, None)
if clazz is None:
# If there was still no luck, give up
self.seek(-4) # Go back

View File

@@ -1,8 +1,7 @@
import inspect
import itertools
from . import utils
from .tl import types
from . import utils, _tl
# Which updates have the following fields?
_has_field = {
@@ -25,8 +24,8 @@ _has_field = {
def _fill():
for name in dir(types):
update = getattr(types, name)
for name in dir(_tl):
update = getattr(_tl, name)
if getattr(update, 'SUBCLASS_OF_ID', None) == 0x9f89304e:
cid = update.CONSTRUCTOR_ID
sig = inspect.signature(update.__init__)
@@ -84,7 +83,7 @@ class EntityCache:
except TypeError:
raise KeyError('Invalid key will not have entity') from None
for cls in (types.PeerUser, types.PeerChat, types.PeerChannel):
for cls in (_tl.PeerUser, _tl.PeerChat, _tl.PeerChannel):
result = self.__dict__.get(utils.get_peer_id(cls(item)))
if result:
return result
@@ -111,7 +110,7 @@ class EntityCache:
"""
# This method is called pretty often and we want it to have the lowest
# overhead possible. For that, we avoid `isinstance` and constantly
# getting attributes out of `types.` by "caching" the constructor IDs
# getting attributes out of `_tl.` by "caching" the constructor IDs
# in sets inside the arguments, and using local variables.
dct = self.__dict__
cid = update.CONSTRUCTOR_ID
@@ -120,11 +119,11 @@ class EntityCache:
return False
if cid in has_chat_id and \
utils.get_peer_id(types.PeerChat(update.chat_id)) not in dct:
utils.get_peer_id(_tl.PeerChat(update.chat_id)) not in dct:
return False
if cid in has_channel_id and \
utils.get_peer_id(types.PeerChannel(update.channel_id)) not in dct:
utils.get_peer_id(_tl.PeerChannel(update.channel_id)) not in dct:
return False
if cid in has_peer and \

View File

@@ -1,29 +1,28 @@
import datetime
import typing
from . import helpers
from .tl import types, custom
from . import helpers, _tl
Phone = str
Username = str
PeerID = int
Entity = typing.Union[types.User, types.Chat, types.Channel]
FullEntity = typing.Union[types.UserFull, types.messages.ChatFull, types.ChatFull, types.ChannelFull]
Entity = typing.Union[_tl.User, _tl.Chat, _tl.Channel]
FullEntity = typing.Union[_tl.UserFull, _tl.messages.ChatFull, _tl.ChatFull, _tl.ChannelFull]
EntityLike = typing.Union[
Phone,
Username,
PeerID,
types.TypePeer,
types.TypeInputPeer,
_tl.TypePeer,
_tl.TypeInputPeer,
Entity,
FullEntity
]
EntitiesLike = typing.Union[EntityLike, typing.Sequence[EntityLike]]
ButtonLike = typing.Union[types.TypeKeyboardButton, custom.Button]
ButtonLike = typing.Union[_tl.TypeKeyboardButton, custom.Button]
MarkupLike = typing.Union[
types.TypeReplyMarkup,
_tl.TypeReplyMarkup,
ButtonLike,
typing.Sequence[ButtonLike],
typing.Sequence[typing.Sequence[ButtonLike]]
@@ -42,9 +41,9 @@ FileLike = typing.Union[
BotFileID,
bytes,
typing.BinaryIO,
types.TypeMessageMedia,
types.TypeInputFile,
types.TypeInputFileLocation
_tl.TypeMessageMedia,
_tl.TypeInputFile,
_tl.TypeInputFileLocation
]
# Can't use `typing.Type` in Python 3.5.2
@@ -61,7 +60,7 @@ except TypeError:
typing.BinaryIO
]
MessageLike = typing.Union[str, types.Message]
MessageIDLike = typing.Union[int, types.Message, types.TypeInputMessage]
MessageLike = typing.Union[str, _tl.Message]
MessageIDLike = typing.Union[int, _tl.Message, _tl.TypeInputMessage]
ProgressCallback = typing.Callable[[int, int], None]

View File

@@ -7,14 +7,7 @@ from html import escape
from html.parser import HTMLParser
from typing import Iterable, Optional, Tuple, List
from .. import helpers
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityEmail, MessageEntityUrl,
MessageEntityTextUrl, MessageEntityMentionName,
MessageEntityUnderline, MessageEntityStrike, MessageEntityBlockquote,
TypeMessageEntity
)
from .. import helpers, _tl
# Helpers from markdown.py
@@ -46,15 +39,15 @@ class HTMLToTelegramParser(HTMLParser):
EntityType = None
args = {}
if tag == 'strong' or tag == 'b':
EntityType = MessageEntityBold
EntityType = _tl.MessageEntityBold
elif tag == 'em' or tag == 'i':
EntityType = MessageEntityItalic
EntityType = _tl.MessageEntityItalic
elif tag == 'u':
EntityType = MessageEntityUnderline
EntityType = _tl.MessageEntityUnderline
elif tag == 'del' or tag == 's':
EntityType = MessageEntityStrike
EntityType = _tl.MessageEntityStrike
elif tag == 'blockquote':
EntityType = MessageEntityBlockquote
EntityType = _tl.MessageEntityBlockquote
elif tag == 'code':
try:
# If we're in the middle of a <pre> tag, this <code> tag is
@@ -69,9 +62,9 @@ class HTMLToTelegramParser(HTMLParser):
except KeyError:
pass
except KeyError:
EntityType = MessageEntityCode
EntityType = _tl.MessageEntityCode
elif tag == 'pre':
EntityType = MessageEntityPre
EntityType = _tl.MessageEntityPre
args['language'] = ''
elif tag == 'a':
try:
@@ -80,12 +73,12 @@ class HTMLToTelegramParser(HTMLParser):
return
if url.startswith('mailto:'):
url = url[len('mailto:'):]
EntityType = MessageEntityEmail
EntityType = _tl.MessageEntityEmail
else:
if self.get_starttag_text() == url:
EntityType = MessageEntityUrl
EntityType = _tl.MessageEntityUrl
else:
EntityType = MessageEntityTextUrl
EntityType = _tl.MessageEntityTextUrl
args['url'] = url
url = None
self._open_tags_meta.popleft()
@@ -121,10 +114,10 @@ class HTMLToTelegramParser(HTMLParser):
self.entities.append(entity)
def parse(html: str) -> Tuple[str, List[TypeMessageEntity]]:
def parse(html: str) -> Tuple[str, List[_tl.TypeMessageEntity]]:
"""
Parses the given HTML message and returns its stripped representation
plus a list of the MessageEntity's that were found.
plus a list of the _tl.MessageEntity's that were found.
:param html: the message with HTML to be parsed.
:return: a tuple consisting of (clean message, [message entities]).
@@ -138,14 +131,14 @@ def parse(html: str) -> Tuple[str, List[TypeMessageEntity]]:
return _del_surrogate(text), parser.entities
def unparse(text: str, entities: Iterable[TypeMessageEntity], _offset: int = 0,
def unparse(text: str, entities: Iterable[_tl.TypeMessageEntity], _offset: int = 0,
_length: Optional[int] = None) -> str:
"""
Performs the reverse operation to .parse(), effectively returning HTML
given a normal text and its MessageEntity's.
given a normal text and its _tl.MessageEntity's.
:param text: the text to be reconverted into HTML.
:param entities: the MessageEntity's applied to the text.
:param entities: the _tl.MessageEntity's applied to the text.
:return: a HTML representation of the combination of both inputs.
"""
if not text:
@@ -185,19 +178,19 @@ def unparse(text: str, entities: Iterable[TypeMessageEntity], _offset: int = 0,
_offset=entity.offset, _length=length)
entity_type = type(entity)
if entity_type == MessageEntityBold:
if entity_type == _tl.MessageEntityBold:
html.append('<strong>{}</strong>'.format(entity_text))
elif entity_type == MessageEntityItalic:
elif entity_type == _tl.MessageEntityItalic:
html.append('<em>{}</em>'.format(entity_text))
elif entity_type == MessageEntityCode:
elif entity_type == _tl.MessageEntityCode:
html.append('<code>{}</code>'.format(entity_text))
elif entity_type == MessageEntityUnderline:
elif entity_type == _tl.MessageEntityUnderline:
html.append('<u>{}</u>'.format(entity_text))
elif entity_type == MessageEntityStrike:
elif entity_type == _tl.MessageEntityStrike:
html.append('<del>{}</del>'.format(entity_text))
elif entity_type == MessageEntityBlockquote:
elif entity_type == _tl.MessageEntityBlockquote:
html.append('<blockquote>{}</blockquote>'.format(entity_text))
elif entity_type == MessageEntityPre:
elif entity_type == _tl.MessageEntityPre:
if entity.language:
html.append(
"<pre>\n"
@@ -208,14 +201,14 @@ def unparse(text: str, entities: Iterable[TypeMessageEntity], _offset: int = 0,
else:
html.append('<pre><code>{}</code></pre>'
.format(entity_text))
elif entity_type == MessageEntityEmail:
elif entity_type == _tl.MessageEntityEmail:
html.append('<a href="mailto:{0}">{0}</a>'.format(entity_text))
elif entity_type == MessageEntityUrl:
elif entity_type == _tl.MessageEntityUrl:
html.append('<a href="{0}">{0}</a>'.format(entity_text))
elif entity_type == MessageEntityTextUrl:
elif entity_type == _tl.MessageEntityTextUrl:
html.append('<a href="{}">{}</a>'
.format(escape(entity.url), entity_text))
elif entity_type == MessageEntityMentionName:
elif entity_type == _tl.MessageEntityMentionName:
html.append('<a href="tg://user?id={}">{}</a>'
.format(entity.user_id, entity_text))
else:

View File

@@ -7,19 +7,14 @@ import re
import warnings
from ..helpers import add_surrogate, del_surrogate, within_surrogate, strip_text
from ..tl import TLObject
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityTextUrl, MessageEntityMentionName,
MessageEntityStrike
)
from .. import _tl
DEFAULT_DELIMITERS = {
'**': MessageEntityBold,
'__': MessageEntityItalic,
'~~': MessageEntityStrike,
'`': MessageEntityCode,
'```': MessageEntityPre
'**': _tl.MessageEntityBold,
'__': _tl.MessageEntityItalic,
'~~': _tl.MessageEntityStrike,
'`': _tl.MessageEntityCode,
'```': _tl.MessageEntityPre
}
DEFAULT_URL_RE = re.compile(r'\[([\S\s]+?)\]\((.+?)\)')
@@ -33,7 +28,7 @@ def overlap(a, b, x, y):
def parse(message, delimiters=None, url_re=None):
"""
Parses the given markdown message and returns its stripped representation
plus a list of the MessageEntity's that were found.
plus a list of the _tl.MessageEntity's that were found.
:param message: the message with markdown-like syntax to be parsed.
:param delimiters: the delimiters to be used, {delimiter: type}.
@@ -98,13 +93,13 @@ def parse(message, delimiters=None, url_re=None):
# Append the found entity
ent = delimiters[delim]
if ent == MessageEntityPre:
if ent == _tl.MessageEntityPre:
result.append(ent(i, end - i - len(delim), '')) # has 'lang'
else:
result.append(ent(i, end - i - len(delim)))
# No nested entities inside code blocks
if ent in (MessageEntityCode, MessageEntityPre):
if ent in (_tl.MessageEntityCode, _tl.MessageEntityPre):
i = end - len(delim)
continue
@@ -125,7 +120,7 @@ def parse(message, delimiters=None, url_re=None):
if ent.offset + ent.length > m.start():
ent.length -= delim_size
result.append(MessageEntityTextUrl(
result.append(_tl.MessageEntityTextUrl(
offset=m.start(), length=len(m.group(1)),
url=del_surrogate(m.group(2))
))
@@ -141,10 +136,10 @@ def parse(message, delimiters=None, url_re=None):
def unparse(text, entities, delimiters=None, url_fmt=None):
"""
Performs the reverse operation to .parse(), effectively returning
markdown-like syntax given a normal text and its MessageEntity's.
markdown-like syntax given a normal text and its _tl.MessageEntity's.
:param text: the text to be reconverted into markdown.
:param entities: the MessageEntity's applied to the text.
:param entities: the _tl.MessageEntity's applied to the text.
:return: a markdown-like text representing the combination of both inputs.
"""
if not text or not entities:
@@ -158,7 +153,7 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
if url_fmt is not None:
warnings.warn('url_fmt is deprecated') # since it complicates everything *a lot*
if isinstance(entities, TLObject):
if isinstance(entities, _tl.TLObject):
entities = (entities,)
text = add_surrogate(text)
@@ -173,9 +168,9 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
insert_at.append((e, delimiter))
else:
url = None
if isinstance(entity, MessageEntityTextUrl):
if isinstance(entity, _tl.MessageEntityTextUrl):
url = entity.url
elif isinstance(entity, MessageEntityMentionName):
elif isinstance(entity, _tl.MessageEntityMentionName):
url = 'tg://user?id={}'.format(entity.user_id)
if url:
insert_at.append((s, '['))

View File

@@ -2,7 +2,7 @@ import hashlib
import os
from .crypto import factorization
from .tl import types
from . import _tl
def check_prime_and_good_check(prime: int, g: int):
@@ -110,7 +110,7 @@ def pbkdf2sha512(password: bytes, salt: bytes, iterations: int):
return hashlib.pbkdf2_hmac('sha512', password, salt, iterations)
def compute_hash(algo: types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow,
def compute_hash(algo: _tl.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow,
password: str):
hash1 = sha256(algo.salt1, password.encode('utf-8'), algo.salt1)
hash2 = sha256(algo.salt2, hash1, algo.salt2)
@@ -118,7 +118,7 @@ def compute_hash(algo: types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter1000
return sha256(algo.salt2, hash3, algo.salt2)
def compute_digest(algo: types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow,
def compute_digest(algo: _tl.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow,
password: str):
try:
check_prime_and_good(algo.p, algo.g)
@@ -133,9 +133,9 @@ def compute_digest(algo: types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter10
# https://github.com/telegramdesktop/tdesktop/blob/18b74b90451a7db2379a9d753c9cbaf8734b4d5d/Telegram/SourceFiles/core/core_cloud_password.cpp
def compute_check(request: types.account.Password, password: str):
def compute_check(request: _tl.account.Password, password: str):
algo = request.current_algo
if not isinstance(algo, types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow):
if not isinstance(algo, _tl.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow):
raise ValueError('unsupported password algorithm {}'
.format(algo.__class__.__name__))
@@ -190,5 +190,5 @@ def compute_check(request: types.account.Password, password: str):
K
)
return types.InputCheckPasswordSRP(
return _tl.InputCheckPasswordSRP(
request.srp_id, bytes(a_for_hash), bytes(M1))

View File

@@ -1,6 +1,6 @@
import inspect
from .tl import types
from . import _tl
# Which updates have the following fields?
@@ -9,8 +9,8 @@ _has_channel_id = []
# TODO EntityCache does the same. Reuse?
def _fill():
for name in dir(types):
update = getattr(types, name)
for name in dir(_tl):
update = getattr(_tl, name)
if getattr(update, 'SUBCLASS_OF_ID', None) == 0x9f89304e:
cid = update.CONSTRUCTOR_ID
sig = inspect.signature(update.__init__)
@@ -51,41 +51,41 @@ class StateCache:
*,
channel_id=None,
has_pts=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateNewMessage,
types.UpdateDeleteMessages,
types.UpdateReadHistoryInbox,
types.UpdateReadHistoryOutbox,
types.UpdateWebPage,
types.UpdateReadMessagesContents,
types.UpdateEditMessage,
types.updates.State,
types.updates.DifferenceTooLong,
types.UpdateShortMessage,
types.UpdateShortChatMessage,
types.UpdateShortSentMessage
_tl.UpdateNewMessage,
_tl.UpdateDeleteMessages,
_tl.UpdateReadHistoryInbox,
_tl.UpdateReadHistoryOutbox,
_tl.UpdateWebPage,
_tl.UpdateReadMessagesContents,
_tl.UpdateEditMessage,
_tl.updates.State,
_tl.updates.DifferenceTooLong,
_tl.UpdateShortMessage,
_tl.UpdateShortChatMessage,
_tl.UpdateShortSentMessage
)),
has_date=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateUserPhoto,
types.UpdateEncryption,
types.UpdateEncryptedMessagesRead,
types.UpdateChatParticipantAdd,
types.updates.DifferenceEmpty,
types.UpdateShortMessage,
types.UpdateShortChatMessage,
types.UpdateShort,
types.UpdatesCombined,
types.Updates,
types.UpdateShortSentMessage,
_tl.UpdateUserPhoto,
_tl.UpdateEncryption,
_tl.UpdateEncryptedMessagesRead,
_tl.UpdateChatParticipantAdd,
_tl.updates.DifferenceEmpty,
_tl.UpdateShortMessage,
_tl.UpdateShortChatMessage,
_tl.UpdateShort,
_tl.UpdatesCombined,
_tl.Updates,
_tl.UpdateShortSentMessage,
)),
has_channel_pts=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateChannelTooLong,
types.UpdateNewChannelMessage,
types.UpdateDeleteChannelMessages,
types.UpdateEditChannelMessage,
types.UpdateChannelWebPage,
types.updates.ChannelDifferenceEmpty,
types.updates.ChannelDifferenceTooLong,
types.updates.ChannelDifference
_tl.UpdateChannelTooLong,
_tl.UpdateNewChannelMessage,
_tl.UpdateDeleteChannelMessages,
_tl.UpdateEditChannelMessage,
_tl.UpdateChannelWebPage,
_tl.updates.ChannelDifferenceEmpty,
_tl.updates.ChannelDifferenceTooLong,
_tl.updates.ChannelDifference
)),
check_only=False
):
@@ -120,8 +120,8 @@ class StateCache:
has_channel_id=frozenset(_has_channel_id),
# Hardcoded because only some with message are for channels
has_message=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateNewChannelMessage,
types.UpdateEditChannelMessage
_tl.UpdateNewChannelMessage,
_tl.UpdateEditChannelMessage
))
):
"""

View File

@@ -21,7 +21,7 @@ from types import GeneratorType
from .extensions import markdown, html
from .helpers import add_surrogate, del_surrogate, strip_text
from .tl import types
from . import _tl
try:
import hachoir
@@ -32,26 +32,26 @@ except ImportError:
# Register some of the most common mime-types to avoid any issues.
# See https://github.com/LonamiWebs/Telethon/issues/1096.
mimetypes.add_type('image/png', '.png')
mimetypes.add_type('image/jpeg', '.jpeg')
mimetypes.add_type('image/webp', '.webp')
mimetypes.add_type('image/gif', '.gif')
mimetypes.add_type('image/bmp', '.bmp')
mimetypes.add_type('image/x-tga', '.tga')
mimetypes.add_type('image/tiff', '.tiff')
mimetypes.add_type('image/vnd.adobe.photoshop', '.psd')
mime_tl.add_type('image/png', '.png')
mime_tl.add_type('image/jpeg', '.jpeg')
mime_tl.add_type('image/webp', '.webp')
mime_tl.add_type('image/gif', '.gif')
mime_tl.add_type('image/bmp', '.bmp')
mime_tl.add_type('image/x-tga', '.tga')
mime_tl.add_type('image/tiff', '.tiff')
mime_tl.add_type('image/vnd.adobe.photoshop', '.psd')
mimetypes.add_type('video/mp4', '.mp4')
mimetypes.add_type('video/quicktime', '.mov')
mimetypes.add_type('video/avi', '.avi')
mime_tl.add_type('video/mp4', '.mp4')
mime_tl.add_type('video/quicktime', '.mov')
mime_tl.add_type('video/avi', '.avi')
mimetypes.add_type('audio/mpeg', '.mp3')
mimetypes.add_type('audio/m4a', '.m4a')
mimetypes.add_type('audio/aac', '.aac')
mimetypes.add_type('audio/ogg', '.ogg')
mimetypes.add_type('audio/flac', '.flac')
mime_tl.add_type('audio/mpeg', '.mp3')
mime_tl.add_type('audio/m4a', '.m4a')
mime_tl.add_type('audio/aac', '.aac')
mime_tl.add_type('audio/ogg', '.ogg')
mime_tl.add_type('audio/flac', '.flac')
mimetypes.add_type('application/x-tgsticker', '.tgs')
mime_tl.add_type('application/x-tgsticker', '.tgs')
USERNAME_RE = re.compile(
r'@|(?:https?://)?(?:www\.)?(?:telegram\.(?:me|dog)|t\.me)/(@|joinchat/)?'
@@ -92,7 +92,7 @@ def get_display_name(entity):
Gets the display name for the given :tl:`User`,
:tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise.
"""
if isinstance(entity, types.User):
if isinstance(entity, _tl.User):
if entity.last_name and entity.first_name:
return '{} {}'.format(entity.first_name, entity.last_name)
elif entity.first_name:
@@ -102,7 +102,7 @@ def get_display_name(entity):
else:
return ''
elif isinstance(entity, (types.Chat, types.ChatForbidden, types.Channel)):
elif isinstance(entity, (_tl.Chat, _tl.ChatForbidden, _tl.Channel)):
return entity.title
return ''
@@ -117,14 +117,14 @@ def get_extension(media):
return '.jpg'
except TypeError:
# These cases are not handled by input photo because it can't
if isinstance(media, (types.UserProfilePhoto, types.ChatPhoto)):
if isinstance(media, (_tl.UserProfilePhoto, _tl.ChatPhoto)):
return '.jpg'
# Documents will come with a mime type
if isinstance(media, types.MessageMediaDocument):
if isinstance(media, _tl.MessageMediaDocument):
media = media.document
if isinstance(media, (
types.Document, types.WebDocument, types.WebDocumentNoProxy)):
_tl.Document, _tl.WebDocument, _tl.WebDocumentNoProxy)):
if media.mime_type == 'application/octet-stream':
# Octet stream are just bytes, which have no default extension
return ''
@@ -184,53 +184,53 @@ def get_input_peer(entity, allow_self=True, check_hash=True):
else:
_raise_cast_fail(entity, 'InputPeer')
if isinstance(entity, types.User):
if isinstance(entity, _tl.User):
if entity.is_self and allow_self:
return types.InputPeerSelf()
return _tl.InputPeerSelf()
elif (entity.access_hash is not None and not entity.min) or not check_hash:
return types.InputPeerUser(entity.id, entity.access_hash)
return _tl.InputPeerUser(entity.id, entity.access_hash)
else:
raise TypeError('User without access_hash or min info cannot be input')
if isinstance(entity, (types.Chat, types.ChatEmpty, types.ChatForbidden)):
return types.InputPeerChat(entity.id)
if isinstance(entity, (_tl.Chat, _tl.ChatEmpty, _tl.ChatForbidden)):
return _tl.InputPeerChat(entity.id)
if isinstance(entity, types.Channel):
if isinstance(entity, _tl.Channel):
if (entity.access_hash is not None and not entity.min) or not check_hash:
return types.InputPeerChannel(entity.id, entity.access_hash)
return _tl.InputPeerChannel(entity.id, entity.access_hash)
else:
raise TypeError('Channel without access_hash or min info cannot be input')
if isinstance(entity, types.ChannelForbidden):
if isinstance(entity, _tl.ChannelForbidden):
# "channelForbidden are never min", and since their hash is
# also not optional, we assume that this truly is the case.
return types.InputPeerChannel(entity.id, entity.access_hash)
return _tl.InputPeerChannel(entity.id, entity.access_hash)
if isinstance(entity, types.InputUser):
return types.InputPeerUser(entity.user_id, entity.access_hash)
if isinstance(entity, _tl.InputUser):
return _tl.InputPeerUser(entity.user_id, entity.access_hash)
if isinstance(entity, types.InputChannel):
return types.InputPeerChannel(entity.channel_id, entity.access_hash)
if isinstance(entity, _tl.InputChannel):
return _tl.InputPeerChannel(entity.channel_id, entity.access_hash)
if isinstance(entity, types.InputUserSelf):
return types.InputPeerSelf()
if isinstance(entity, _tl.InputUserSelf):
return _tl.InputPeerSelf()
if isinstance(entity, types.InputUserFromMessage):
return types.InputPeerUserFromMessage(entity.peer, entity.msg_id, entity.user_id)
if isinstance(entity, _tl.InputUserFromMessage):
return _tl.InputPeerUserFromMessage(entity.peer, entity.msg_id, entity.user_id)
if isinstance(entity, types.InputChannelFromMessage):
return types.InputPeerChannelFromMessage(entity.peer, entity.msg_id, entity.channel_id)
if isinstance(entity, _tl.InputChannelFromMessage):
return _tl.InputPeerChannelFromMessage(entity.peer, entity.msg_id, entity.channel_id)
if isinstance(entity, types.UserEmpty):
return types.InputPeerEmpty()
if isinstance(entity, _tl.UserEmpty):
return _tl.InputPeerEmpty()
if isinstance(entity, types.UserFull):
if isinstance(entity, _tl.UserFull):
return get_input_peer(entity.user)
if isinstance(entity, types.ChatFull):
return types.InputPeerChat(entity.id)
if isinstance(entity, _tl.ChatFull):
return _tl.InputPeerChat(entity.id)
if isinstance(entity, types.PeerChat):
return types.InputPeerChat(entity.chat_id)
if isinstance(entity, _tl.PeerChat):
return _tl.InputPeerChat(entity.chat_id)
_raise_cast_fail(entity, 'InputPeer')
@@ -251,14 +251,14 @@ def get_input_channel(entity):
except AttributeError:
_raise_cast_fail(entity, 'InputChannel')
if isinstance(entity, (types.Channel, types.ChannelForbidden)):
return types.InputChannel(entity.id, entity.access_hash or 0)
if isinstance(entity, (_tl.Channel, _tl.ChannelForbidden)):
return _tl.InputChannel(entity.id, entity.access_hash or 0)
if isinstance(entity, types.InputPeerChannel):
return types.InputChannel(entity.channel_id, entity.access_hash)
if isinstance(entity, _tl.InputPeerChannel):
return _tl.InputChannel(entity.channel_id, entity.access_hash)
if isinstance(entity, types.InputPeerChannelFromMessage):
return types.InputChannelFromMessage(entity.peer, entity.msg_id, entity.channel_id)
if isinstance(entity, _tl.InputPeerChannelFromMessage):
return _tl.InputChannelFromMessage(entity.peer, entity.msg_id, entity.channel_id)
_raise_cast_fail(entity, 'InputChannel')
@@ -279,26 +279,26 @@ def get_input_user(entity):
except AttributeError:
_raise_cast_fail(entity, 'InputUser')
if isinstance(entity, types.User):
if isinstance(entity, _tl.User):
if entity.is_self:
return types.InputUserSelf()
return _tl.InputUserSelf()
else:
return types.InputUser(entity.id, entity.access_hash or 0)
return _tl.InputUser(entity.id, entity.access_hash or 0)
if isinstance(entity, types.InputPeerSelf):
return types.InputUserSelf()
if isinstance(entity, _tl.InputPeerSelf):
return _tl.InputUserSelf()
if isinstance(entity, (types.UserEmpty, types.InputPeerEmpty)):
return types.InputUserEmpty()
if isinstance(entity, (_tl.UserEmpty, _tl.InputPeerEmpty)):
return _tl.InputUserEmpty()
if isinstance(entity, types.UserFull):
if isinstance(entity, _tl.UserFull):
return get_input_user(entity.user)
if isinstance(entity, types.InputPeerUser):
return types.InputUser(entity.user_id, entity.access_hash)
if isinstance(entity, _tl.InputPeerUser):
return _tl.InputUser(entity.user_id, entity.access_hash)
if isinstance(entity, types.InputPeerUserFromMessage):
return types.InputUserFromMessage(entity.peer, entity.msg_id, entity.user_id)
if isinstance(entity, _tl.InputPeerUserFromMessage):
return _tl.InputUserFromMessage(entity.peer, entity.msg_id, entity.user_id)
_raise_cast_fail(entity, 'InputUser')
@@ -309,12 +309,12 @@ def get_input_dialog(dialog):
if dialog.SUBCLASS_OF_ID == 0xa21c9795: # crc32(b'InputDialogPeer')
return dialog
if dialog.SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer')
return types.InputDialogPeer(dialog)
return _tl.InputDialogPeer(dialog)
except AttributeError:
_raise_cast_fail(dialog, 'InputDialogPeer')
try:
return types.InputDialogPeer(get_input_peer(dialog))
return _tl.InputDialogPeer(get_input_peer(dialog))
except TypeError:
pass
@@ -329,18 +329,18 @@ def get_input_document(document):
except AttributeError:
_raise_cast_fail(document, 'InputDocument')
if isinstance(document, types.Document):
return types.InputDocument(
if isinstance(document, _tl.Document):
return _tl.InputDocument(
id=document.id, access_hash=document.access_hash,
file_reference=document.file_reference)
if isinstance(document, types.DocumentEmpty):
return types.InputDocumentEmpty()
if isinstance(document, _tl.DocumentEmpty):
return _tl.InputDocumentEmpty()
if isinstance(document, types.MessageMediaDocument):
if isinstance(document, _tl.MessageMediaDocument):
return get_input_document(document.document)
if isinstance(document, types.Message):
if isinstance(document, _tl.Message):
return get_input_document(document.media)
_raise_cast_fail(document, 'InputDocument')
@@ -354,32 +354,32 @@ def get_input_photo(photo):
except AttributeError:
_raise_cast_fail(photo, 'InputPhoto')
if isinstance(photo, types.Message):
if isinstance(photo, _tl.Message):
photo = photo.media
if isinstance(photo, (types.photos.Photo, types.MessageMediaPhoto)):
if isinstance(photo, (_tl.photos.Photo, _tl.MessageMediaPhoto)):
photo = photo.photo
if isinstance(photo, types.Photo):
return types.InputPhoto(id=photo.id, access_hash=photo.access_hash,
if isinstance(photo, _tl.Photo):
return _tl.InputPhoto(id=photo.id, access_hash=photo.access_hash,
file_reference=photo.file_reference)
if isinstance(photo, types.PhotoEmpty):
return types.InputPhotoEmpty()
if isinstance(photo, _tl.PhotoEmpty):
return _tl.InputPhotoEmpty()
if isinstance(photo, types.messages.ChatFull):
if isinstance(photo, _tl.messages.ChatFull):
photo = photo.full_chat
if isinstance(photo, types.ChannelFull):
if isinstance(photo, _tl.ChannelFull):
return get_input_photo(photo.chat_photo)
elif isinstance(photo, types.UserFull):
elif isinstance(photo, _tl.UserFull):
return get_input_photo(photo.profile_photo)
elif isinstance(photo, (types.Channel, types.Chat, types.User)):
elif isinstance(photo, (_tl.Channel, _tl.Chat, _tl.User)):
return get_input_photo(photo.photo)
if isinstance(photo, (types.UserEmpty, types.ChatEmpty,
types.ChatForbidden, types.ChannelForbidden)):
return types.InputPhotoEmpty()
if isinstance(photo, (_tl.UserEmpty, _tl.ChatEmpty,
_tl.ChatForbidden, _tl.ChannelForbidden)):
return _tl.InputPhotoEmpty()
_raise_cast_fail(photo, 'InputPhoto')
@@ -390,15 +390,15 @@ def get_input_chat_photo(photo):
if photo.SUBCLASS_OF_ID == 0xd4eb2d74: # crc32(b'InputChatPhoto')
return photo
elif photo.SUBCLASS_OF_ID == 0xe7655f1f: # crc32(b'InputFile'):
return types.InputChatUploadedPhoto(photo)
return _tl.InputChatUploadedPhoto(photo)
except AttributeError:
_raise_cast_fail(photo, 'InputChatPhoto')
photo = get_input_photo(photo)
if isinstance(photo, types.InputPhoto):
return types.InputChatPhoto(photo)
elif isinstance(photo, types.InputPhotoEmpty):
return types.InputChatPhotoEmpty()
if isinstance(photo, _tl.InputPhoto):
return _tl.InputChatPhoto(photo)
elif isinstance(photo, _tl.InputPhotoEmpty):
return _tl.InputChatPhotoEmpty()
_raise_cast_fail(photo, 'InputChatPhoto')
@@ -411,16 +411,16 @@ def get_input_geo(geo):
except AttributeError:
_raise_cast_fail(geo, 'InputGeoPoint')
if isinstance(geo, types.GeoPoint):
return types.InputGeoPoint(lat=geo.lat, long=geo.long)
if isinstance(geo, _tl.GeoPoint):
return _tl.InputGeoPoint(lat=geo.lat, long=geo.long)
if isinstance(geo, types.GeoPointEmpty):
return types.InputGeoPointEmpty()
if isinstance(geo, _tl.GeoPointEmpty):
return _tl.InputGeoPointEmpty()
if isinstance(geo, types.MessageMediaGeo):
if isinstance(geo, _tl.MessageMediaGeo):
return get_input_geo(geo.geo)
if isinstance(geo, types.Message):
if isinstance(geo, _tl.Message):
return get_input_geo(geo.media)
_raise_cast_fail(geo, 'InputGeoPoint')
@@ -443,39 +443,39 @@ def get_input_media(
if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia')
return media
elif media.SUBCLASS_OF_ID == 0x846363e0: # crc32(b'InputPhoto')
return types.InputMediaPhoto(media, ttl_seconds=ttl)
return _tl.InputMediaPhoto(media, ttl_seconds=ttl)
elif media.SUBCLASS_OF_ID == 0xf33fdb68: # crc32(b'InputDocument')
return types.InputMediaDocument(media, ttl_seconds=ttl)
return _tl.InputMediaDocument(media, ttl_seconds=ttl)
except AttributeError:
_raise_cast_fail(media, 'InputMedia')
if isinstance(media, types.MessageMediaPhoto):
return types.InputMediaPhoto(
if isinstance(media, _tl.MessageMediaPhoto):
return _tl.InputMediaPhoto(
id=get_input_photo(media.photo),
ttl_seconds=ttl or media.ttl_seconds
)
if isinstance(media, (types.Photo, types.photos.Photo, types.PhotoEmpty)):
return types.InputMediaPhoto(
if isinstance(media, (_tl.Photo, _tl.photos.Photo, _tl.PhotoEmpty)):
return _tl.InputMediaPhoto(
id=get_input_photo(media),
ttl_seconds=ttl
)
if isinstance(media, types.MessageMediaDocument):
return types.InputMediaDocument(
if isinstance(media, _tl.MessageMediaDocument):
return _tl.InputMediaDocument(
id=get_input_document(media.document),
ttl_seconds=ttl or media.ttl_seconds
)
if isinstance(media, (types.Document, types.DocumentEmpty)):
return types.InputMediaDocument(
if isinstance(media, (_tl.Document, _tl.DocumentEmpty)):
return _tl.InputMediaDocument(
id=get_input_document(media),
ttl_seconds=ttl
)
if isinstance(media, (types.InputFile, types.InputFileBig)):
if isinstance(media, (_tl.InputFile, _tl.InputFileBig)):
if is_photo:
return types.InputMediaUploadedPhoto(file=media, ttl_seconds=ttl)
return _tl.InputMediaUploadedPhoto(file=media, ttl_seconds=ttl)
else:
attrs, mime = get_attributes(
media,
@@ -485,29 +485,29 @@ def get_input_media(
video_note=video_note,
supports_streaming=supports_streaming
)
return types.InputMediaUploadedDocument(
return _tl.InputMediaUploadedDocument(
file=media, mime_type=mime, attributes=attrs, force_file=force_document,
ttl_seconds=ttl)
if isinstance(media, types.MessageMediaGame):
return types.InputMediaGame(id=types.InputGameID(
if isinstance(media, _tl.MessageMediaGame):
return _tl.InputMediaGame(id=_tl.InputGameID(
id=media.game.id,
access_hash=media.game.access_hash
))
if isinstance(media, types.MessageMediaContact):
return types.InputMediaContact(
if isinstance(media, _tl.MessageMediaContact):
return _tl.InputMediaContact(
phone_number=media.phone_number,
first_name=media.first_name,
last_name=media.last_name,
vcard=''
)
if isinstance(media, types.MessageMediaGeo):
return types.InputMediaGeoPoint(geo_point=get_input_geo(media.geo))
if isinstance(media, _tl.MessageMediaGeo):
return _tl.InputMediaGeoPoint(geo_point=get_input_geo(media.geo))
if isinstance(media, types.MessageMediaVenue):
return types.InputMediaVenue(
if isinstance(media, _tl.MessageMediaVenue):
return _tl.InputMediaVenue(
geo_point=get_input_geo(media.geo),
title=media.title,
address=media.address,
@@ -516,19 +516,19 @@ def get_input_media(
venue_type=''
)
if isinstance(media, types.MessageMediaDice):
return types.InputMediaDice(media.emoticon)
if isinstance(media, _tl.MessageMediaDice):
return _tl.InputMediaDice(media.emoticon)
if isinstance(media, (
types.MessageMediaEmpty, types.MessageMediaUnsupported,
types.ChatPhotoEmpty, types.UserProfilePhotoEmpty,
types.ChatPhoto, types.UserProfilePhoto)):
return types.InputMediaEmpty()
_tl.MessageMediaEmpty, _tl.MessageMediaUnsupported,
_tl.ChatPhotoEmpty, _tl.UserProfilePhotoEmpty,
_tl.ChatPhoto, _tl.UserProfilePhoto)):
return _tl.InputMediaEmpty()
if isinstance(media, types.Message):
if isinstance(media, _tl.Message):
return get_input_media(media.media, is_photo=is_photo, ttl=ttl)
if isinstance(media, types.MessageMediaPoll):
if isinstance(media, _tl.MessageMediaPoll):
if media.poll.quiz:
if not media.results.results:
# A quiz has correct answers, which we don't know until answered.
@@ -539,15 +539,15 @@ def get_input_media(
else:
correct_answers = None
return types.InputMediaPoll(
return _tl.InputMediaPoll(
poll=media.poll,
correct_answers=correct_answers,
solution=media.results.solution,
solution_entities=media.results.solution_entities,
)
if isinstance(media, types.Poll):
return types.InputMediaPoll(media)
if isinstance(media, _tl.Poll):
return _tl.InputMediaPoll(media)
_raise_cast_fail(media, 'InputMedia')
@@ -556,11 +556,11 @@ def get_input_message(message):
"""Similar to :meth:`get_input_peer`, but for input messages."""
try:
if isinstance(message, int): # This case is really common too
return types.InputMessageID(message)
return _tl.InputMessageID(message)
elif message.SUBCLASS_OF_ID == 0x54b6bcc5: # crc32(b'InputMessage'):
return message
elif message.SUBCLASS_OF_ID == 0x790009e3: # crc32(b'Message'):
return types.InputMessageID(message.id)
return _tl.InputMessageID(message.id)
except AttributeError:
pass
@@ -573,7 +573,7 @@ def get_input_group_call(call):
if call.SUBCLASS_OF_ID == 0x58611ab1: # crc32(b'InputGroupCall')
return call
elif call.SUBCLASS_OF_ID == 0x20b4f320: # crc32(b'GroupCall')
return types.InputGroupCall(id=call.id, access_hash=call.access_hash)
return _tl.InputGroupCall(id=call.id, access_hash=call.access_hash)
except AttributeError:
_raise_cast_fail(call, 'InputGroupCall')
@@ -675,10 +675,10 @@ def get_attributes(file, *, attributes=None, mime_type=None,
# Note: ``file.name`` works for :tl:`InputFile` and some `IOBase` streams
name = file if isinstance(file, str) else getattr(file, 'name', 'unnamed')
if mime_type is None:
mime_type = mimetypes.guess_type(name)[0]
mime_type = mime_tl.guess_type(name)[0]
attr_dict = {types.DocumentAttributeFilename:
types.DocumentAttributeFilename(os.path.basename(name))}
attr_dict = {_tl.DocumentAttributeFilename:
_tl.DocumentAttributeFilename(os.path.basename(name))}
if is_audio(file):
m = _get_metadata(file)
@@ -690,8 +690,8 @@ def get_attributes(file, *, attributes=None, mime_type=None,
else:
performer = None
attr_dict[types.DocumentAttributeAudio] = \
types.DocumentAttributeAudio(
attr_dict[_tl.DocumentAttributeAudio] = \
_tl.DocumentAttributeAudio(
voice=voice_note,
title=m.get('title') if m.has('title') else None,
performer=performer,
@@ -702,7 +702,7 @@ def get_attributes(file, *, attributes=None, mime_type=None,
if not force_document and is_video(file):
m = _get_metadata(file)
if m:
doc = types.DocumentAttributeVideo(
doc = _tl.DocumentAttributeVideo(
round_message=video_note,
w=m.get('width') if m.has('width') else 1,
h=m.get('height') if m.has('height') else 1,
@@ -719,22 +719,22 @@ def get_attributes(file, *, attributes=None, mime_type=None,
if t_m and t_m.has("height"):
height = t_m.get("height")
doc = types.DocumentAttributeVideo(
doc = _tl.DocumentAttributeVideo(
0, width, height, round_message=video_note,
supports_streaming=supports_streaming)
else:
doc = types.DocumentAttributeVideo(
doc = _tl.DocumentAttributeVideo(
0, 1, 1, round_message=video_note,
supports_streaming=supports_streaming)
attr_dict[types.DocumentAttributeVideo] = doc
attr_dict[_tl.DocumentAttributeVideo] = doc
if voice_note:
if types.DocumentAttributeAudio in attr_dict:
attr_dict[types.DocumentAttributeAudio].voice = True
if _tl.DocumentAttributeAudio in attr_dict:
attr_dict[_tl.DocumentAttributeAudio].voice = True
else:
attr_dict[types.DocumentAttributeAudio] = \
types.DocumentAttributeAudio(0, voice=True)
attr_dict[_tl.DocumentAttributeAudio] = \
_tl.DocumentAttributeAudio(0, voice=True)
# Now override the attributes if any. As we have a dict of
# {cls: instance}, we can override any class with the list
@@ -803,23 +803,23 @@ def _get_file_info(location):
except AttributeError:
_raise_cast_fail(location, 'InputFileLocation')
if isinstance(location, types.Message):
if isinstance(location, _tl.Message):
location = location.media
if isinstance(location, types.MessageMediaDocument):
if isinstance(location, _tl.MessageMediaDocument):
location = location.document
elif isinstance(location, types.MessageMediaPhoto):
elif isinstance(location, _tl.MessageMediaPhoto):
location = location.photo
if isinstance(location, types.Document):
return _FileInfo(location.dc_id, types.InputDocumentFileLocation(
if isinstance(location, _tl.Document):
return _FileInfo(location.dc_id, _tl.InputDocumentFileLocation(
id=location.id,
access_hash=location.access_hash,
file_reference=location.file_reference,
thumb_size='' # Presumably to download one of its thumbnails
), location.size)
elif isinstance(location, types.Photo):
return _FileInfo(location.dc_id, types.InputPhotoFileLocation(
elif isinstance(location, _tl.Photo):
return _FileInfo(location.dc_id, _tl.InputPhotoFileLocation(
id=location.id,
access_hash=location.access_hash,
file_reference=location.file_reference,
@@ -860,7 +860,7 @@ def is_image(file):
if match:
return True
else:
return isinstance(resolve_bot_file_id(file), types.Photo)
return isinstance(resolve_bot_file_id(file), _tl.Photo)
def is_gif(file):
@@ -881,7 +881,7 @@ def is_audio(file):
return False
else:
file = 'a' + ext
return (mimetypes.guess_type(file)[0] or '').startswith('audio/')
return (mime_tl.guess_type(file)[0] or '').startswith('audio/')
def is_video(file):
@@ -895,7 +895,7 @@ def is_video(file):
return False
else:
file = 'a' + ext
return (mimetypes.guess_type(file)[0] or '').startswith('video/')
return (mime_tl.guess_type(file)[0] or '').startswith('video/')
def is_list_like(obj):
@@ -971,27 +971,27 @@ def get_peer(peer):
elif peer.SUBCLASS_OF_ID == 0x2d45687:
return peer
elif isinstance(peer, (
types.contacts.ResolvedPeer, types.InputNotifyPeer,
types.TopPeer, types.Dialog, types.DialogPeer)):
_tl.contacts.ResolvedPeer, _tl.InputNotifyPeer,
_tl.TopPeer, _tl.Dialog, _tl.DialogPeer)):
return peer.peer
elif isinstance(peer, types.ChannelFull):
return types.PeerChannel(peer.id)
elif isinstance(peer, types.UserEmpty):
return types.PeerUser(peer.id)
elif isinstance(peer, types.ChatEmpty):
return types.PeerChat(peer.id)
elif isinstance(peer, _tl.ChannelFull):
return _tl.PeerChannel(peer.id)
elif isinstance(peer, _tl.UserEmpty):
return _tl.PeerUser(peer.id)
elif isinstance(peer, _tl.ChatEmpty):
return _tl.PeerChat(peer.id)
if peer.SUBCLASS_OF_ID in (0x7d7c6f86, 0xd9c7fc18):
# ChatParticipant, ChannelParticipant
return types.PeerUser(peer.user_id)
return _tl.PeerUser(peer.user_id)
peer = get_input_peer(peer, allow_self=False, check_hash=False)
if isinstance(peer, (types.InputPeerUser, types.InputPeerUserFromMessage)):
return types.PeerUser(peer.user_id)
elif isinstance(peer, types.InputPeerChat):
return types.PeerChat(peer.chat_id)
elif isinstance(peer, (types.InputPeerChannel, types.InputPeerChannelFromMessage)):
return types.PeerChannel(peer.channel_id)
if isinstance(peer, (_tl.InputPeerUser, _tl.InputPeerUserFromMessage)):
return _tl.PeerUser(peer.user_id)
elif isinstance(peer, _tl.InputPeerChat):
return _tl.PeerChat(peer.chat_id)
elif isinstance(peer, (_tl.InputPeerChannel, _tl.InputPeerChannelFromMessage)):
return _tl.PeerChannel(peer.channel_id)
except (AttributeError, TypeError):
pass
_raise_cast_fail(peer, 'Peer')
@@ -1017,7 +1017,7 @@ def get_peer_id(peer, add_mark=True):
return peer if add_mark else resolve_id(peer)[0]
# Tell the user to use their client to resolve InputPeerSelf if we got one
if isinstance(peer, types.InputPeerSelf):
if isinstance(peer, _tl.InputPeerSelf):
_raise_cast_fail(peer, 'int (you might want to use client.get_peer_id)')
try:
@@ -1025,15 +1025,15 @@ def get_peer_id(peer, add_mark=True):
except TypeError:
_raise_cast_fail(peer, 'int')
if isinstance(peer, types.PeerUser):
if isinstance(peer, _tl.PeerUser):
return peer.user_id
elif isinstance(peer, types.PeerChat):
elif isinstance(peer, _tl.PeerChat):
# Check in case the user mixed things up to avoid blowing up
if not (0 < peer.chat_id <= 0x7fffffff):
peer.chat_id = resolve_id(peer.chat_id)[0]
return -peer.chat_id if add_mark else peer.chat_id
else: # if isinstance(peer, types.PeerChannel):
else: # if isinstance(peer, _tl.PeerChannel):
# Check in case the user mixed things up to avoid blowing up
if not (0 < peer.channel_id <= 0x7fffffff):
peer.channel_id = resolve_id(peer.channel_id)[0]
@@ -1048,14 +1048,14 @@ def get_peer_id(peer, add_mark=True):
def resolve_id(marked_id):
"""Given a marked ID, returns the original ID and its :tl:`Peer` type."""
if marked_id >= 0:
return marked_id, types.PeerUser
return marked_id, _tl.PeerUser
marked_id = -marked_id
if marked_id > 1000000000000:
marked_id -= 1000000000000
return marked_id, types.PeerChannel
return marked_id, _tl.PeerChannel
else:
return marked_id, types.PeerChat
return marked_id, _tl.PeerChat
def _rle_decode(data):
@@ -1159,12 +1159,12 @@ def resolve_bot_file_id(file_id):
attributes = []
if file_type == 3 or file_type == 9:
attributes.append(types.DocumentAttributeAudio(
attributes.append(_tl.DocumentAttributeAudio(
duration=0,
voice=file_type == 3
))
elif file_type == 4 or file_type == 13:
attributes.append(types.DocumentAttributeVideo(
attributes.append(_tl.DocumentAttributeVideo(
duration=0,
w=0,
h=0,
@@ -1172,14 +1172,14 @@ def resolve_bot_file_id(file_id):
))
# elif file_type == 5: # other, cannot know which
elif file_type == 8:
attributes.append(types.DocumentAttributeSticker(
attributes.append(_tl.DocumentAttributeSticker(
alt='',
stickerset=types.InputStickerSetEmpty()
stickerset=_tl.InputStickerSetEmpty()
))
elif file_type == 10:
attributes.append(types.DocumentAttributeAnimated())
attributes.append(_tl.DocumentAttributeAnimated())
return types.Document(
return _tl.Document(
id=media_id,
access_hash=access_hash,
date=None,
@@ -1210,12 +1210,12 @@ def resolve_bot_file_id(file_id):
# Thumbnails (small) always have ID 0; otherwise size 'x'
photo_size = 's' if media_id or access_hash else 'x'
return types.Photo(
return _tl.Photo(
id=media_id,
access_hash=access_hash,
file_reference=b'',
date=None,
sizes=[types.PhotoSize(
sizes=[_tl.PhotoSize(
type=photo_size,
w=0,
h=0,
@@ -1235,21 +1235,21 @@ def pack_bot_file_id(file):
If an invalid parameter is given, it will ``return None``.
"""
if isinstance(file, types.MessageMediaDocument):
if isinstance(file, _tl.MessageMediaDocument):
file = file.document
elif isinstance(file, types.MessageMediaPhoto):
elif isinstance(file, _tl.MessageMediaPhoto):
file = file.photo
if isinstance(file, types.Document):
if isinstance(file, _tl.Document):
file_type = 5
for attribute in file.attributes:
if isinstance(attribute, types.DocumentAttributeAudio):
if isinstance(attribute, _tl.DocumentAttributeAudio):
file_type = 3 if attribute.voice else 9
elif isinstance(attribute, types.DocumentAttributeVideo):
elif isinstance(attribute, _tl.DocumentAttributeVideo):
file_type = 13 if attribute.round_message else 4
elif isinstance(attribute, types.DocumentAttributeSticker):
elif isinstance(attribute, _tl.DocumentAttributeSticker):
file_type = 8
elif isinstance(attribute, types.DocumentAttributeAnimated):
elif isinstance(attribute, _tl.DocumentAttributeAnimated):
file_type = 10
else:
continue
@@ -1258,9 +1258,9 @@ def pack_bot_file_id(file):
return _encode_telegram_base64(_rle_encode(struct.pack(
'<iiqqb', file_type, file.dc_id, file.id, file.access_hash, 2)))
elif isinstance(file, types.Photo):
elif isinstance(file, _tl.Photo):
size = next((x for x in reversed(file.sizes) if isinstance(
x, (types.PhotoSize, types.PhotoCachedSize))), None)
x, (_tl.PhotoSize, _tl.PhotoCachedSize))), None)
if not size:
return None
@@ -1326,7 +1326,7 @@ def resolve_inline_message_id(inline_msg_id):
try:
dc_id, message_id, pid, access_hash = \
struct.unpack('<iiiq', _decode_telegram_base64(inline_msg_id))
peer = types.PeerChannel(-pid) if pid < 0 else types.PeerUser(pid)
peer = _tl.PeerChannel(-pid) if pid < 0 else _tl.PeerUser(pid)
return message_id, peer, dc_id, access_hash
except (struct.error, TypeError):
return None, None, None, None
@@ -1360,14 +1360,14 @@ def encode_waveform(waveform):
file = 'my.ogg'
# Send 'my.ogg' with a ascending-triangle waveform
await client.send_file(chat, file, attributes=[types.DocumentAttributeAudio(
await client.send_file(chat, file, attributes=[_tl.DocumentAttributeAudio(
duration=7,
voice=True,
waveform=utils.encode_waveform(bytes(range(2 ** 5)) # 2**5 because 5-bit
)]
# Send 'my.ogg' with a square waveform
await client.send_file(chat, file, attributes=[types.DocumentAttributeAudio(
await client.send_file(chat, file, attributes=[_tl.DocumentAttributeAudio(
duration=7,
voice=True,
waveform=utils.encode_waveform(bytes((31, 31, 15, 15, 15, 15, 31, 31)) * 4)
@@ -1542,18 +1542,18 @@ def stripped_photo_to_jpg(stripped):
def _photo_size_byte_count(size):
if isinstance(size, types.PhotoSize):
if isinstance(size, _tl.PhotoSize):
return size.size
elif isinstance(size, types.PhotoStrippedSize):
elif isinstance(size, _tl.PhotoStrippedSize):
if len(size.bytes) < 3 or size.bytes[0] != 1:
return len(size.bytes)
return len(size.bytes) + 622
elif isinstance(size, types.PhotoCachedSize):
elif isinstance(size, _tl.PhotoCachedSize):
return len(size.bytes)
elif isinstance(size, types.PhotoSizeEmpty):
elif isinstance(size, _tl.PhotoSizeEmpty):
return 0
elif isinstance(size, types.PhotoSizeProgressive):
elif isinstance(size, _tl.PhotoSizeProgressive):
return max(size.sizes)
else:
return None