This commit is contained in:
New-dev0 2021-06-10 17:40:28 +05:30
parent ea9c3004fc
commit 978c5874e2
6 changed files with 10 additions and 181 deletions

View File

@ -348,6 +348,4 @@ library a lot easier. Only the interesting ones will be listed here.
get_inner_text get_inner_text
get_peer_id get_peer_id
resolve_id resolve_id
pack_bot_file_id
resolve_bot_file_id
resolve_invite_link resolve_invite_link

View File

@ -1108,7 +1108,7 @@ class ChatMethods:
return await self(functions.channels.EditBannedRequest( return await self(functions.channels.EditBannedRequest(
channel=entity, channel=entity,
user_id=user, participant=user,
banned_rights=rights banned_rights=rights
)) ))
@ -1165,14 +1165,14 @@ class ChatMethods:
else: else:
resp = await self(functions.channels.EditBannedRequest( resp = await self(functions.channels.EditBannedRequest(
channel=entity, channel=entity,
user_id=user, participant=user,
banned_rights=types.ChatBannedRights( banned_rights=types.ChatBannedRights(
until_date=None, view_messages=True) until_date=None, view_messages=True)
)) ))
await asyncio.sleep(0.5) await asyncio.sleep(0.5)
await self(functions.channels.EditBannedRequest( await self(functions.channels.EditBannedRequest(
channel=entity, channel=entity,
user_id=user, participant=user,
banned_rights=types.ChatBannedRights(until_date=None) banned_rights=types.ChatBannedRights(until_date=None)
)) ))
else: else:

View File

@ -269,13 +269,11 @@ class DownloadMethods:
photo = entity.photo photo = entity.photo
if isinstance(photo, (types.UserProfilePhoto, types.ChatPhoto)): if isinstance(photo, (types.UserProfilePhoto, types.)):
dc_id = photo.dc_id dc_id = photo.dc_id
which = photo.photo_big if download_big else photo.photo_small
loc = types.InputPeerPhotoFileLocation( loc = types.InputPeerPhotoFileLocation(
peer=await self.get_input_entity(entity), peer=await self.get_input_entity(entity),
local_id=which.local_id, photo_id=photo.photo_id,
volume_id=which.volume_id,
big=download_big big=download_big
) )
else: else:
@ -397,9 +395,6 @@ class DownloadMethods:
date = datetime.datetime.now() date = datetime.datetime.now()
media = message media = message
if isinstance(media, str):
media = utils.resolve_bot_file_id(media)
if isinstance(media, types.MessageMediaWebPage): if isinstance(media, types.MessageMediaWebPage):
if isinstance(media.webpage, types.WebPage): if isinstance(media.webpage, types.WebPage):
media = media.webpage.document or media.webpage.photo media = media.webpage.document or media.webpage.photo

View File

@ -699,10 +699,6 @@ class UploadMethods:
media = types.InputMediaPhotoExternal(file) media = types.InputMediaPhotoExternal(file)
else: else:
media = types.InputMediaDocumentExternal(file) media = types.InputMediaDocumentExternal(file)
else:
bot_file = utils.resolve_bot_file_id(file)
if bot_file:
media = utils.get_input_media(bot_file)
if media: if media:
pass # Already have media, don't check the rest pass # Already have media, don't check the rest

View File

@ -256,6 +256,10 @@ class InlineBuilder:
It will be automatically set if ``mime_type`` is specified, It will be automatically set if ``mime_type`` is specified,
and default to ``'file'`` if no matching mime type is found. and default to ``'file'`` if no matching mime type is found.
attributes (`list`, optional):
Optional attributes that override the inferred ones, like
:tl:`DocumentAttributeFilename` and so on.
include_media (`bool`, optional): include_media (`bool`, optional):
Whether the document file used to display the result should be Whether the document file used to display the result should be
included in the message itself or not. By default, the document included in the message itself or not. By default, the document

View File

@ -518,8 +518,7 @@ def get_input_media(
if isinstance(media, ( if isinstance(media, (
types.MessageMediaEmpty, types.MessageMediaUnsupported, types.MessageMediaEmpty, types.MessageMediaUnsupported,
types.ChatPhotoEmpty, types.UserProfilePhotoEmpty, types.ChatPhotoEmpty, types.UserProfilePhotoEmpty,
types.ChatPhoto, types.UserProfilePhoto, types.ChatPhoto, types.UserProfilePhoto)):
types.FileLocationToBeDeprecated)):
return types.InputMediaEmpty() return types.InputMediaEmpty()
if isinstance(media, types.Message): if isinstance(media, types.Message):
@ -823,9 +822,6 @@ def _get_file_info(location):
thumb_size=location.sizes[-1].type thumb_size=location.sizes[-1].type
), _photo_size_byte_count(location.sizes[-1])) ), _photo_size_byte_count(location.sizes[-1]))
if isinstance(location, types.FileLocationToBeDeprecated):
raise TypeError('Unavailable location cannot be used as input')
_raise_cast_fail(location, 'InputFileLocation') _raise_cast_fail(location, 'InputFileLocation')
@ -859,8 +855,6 @@ def is_image(file):
match = re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) match = re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE)
if match: if match:
return True return True
else:
return isinstance(resolve_bot_file_id(file), types.Photo)
def is_gif(file): def is_gif(file):
@ -1119,164 +1113,6 @@ def _encode_telegram_base64(string):
return None # not valid base64, not valid ascii, not a string return None # not valid base64, not valid ascii, not a string
def resolve_bot_file_id(file_id):
"""
Given a Bot API-style `file_id <telethon.tl.custom.file.File.id>`,
returns the media it represents. If the `file_id <telethon.tl.custom.file.File.id>`
is not valid, `None` is returned instead.
Note that the `file_id <telethon.tl.custom.file.File.id>` does not have information
such as image dimensions or file size, so these will be zero if present.
For thumbnails, the photo ID and hash will always be zero.
"""
data = _rle_decode(_decode_telegram_base64(file_id))
if not data:
return None
# This isn't officially documented anywhere, but
# we assume the last byte is some kind of "version".
data, version = data[:-1], data[-1]
if version not in (2, 4):
return None
if (version == 2 and len(data) == 24) or (version == 4 and len(data) == 25):
if version == 2:
file_type, dc_id, media_id, access_hash = struct.unpack('<iiqq', data)
# elif version == 4:
else:
# TODO Figure out what the extra byte means
file_type, dc_id, media_id, access_hash, _ = struct.unpack('<iiqqb', data)
if not (1 <= dc_id <= 5):
# Valid `file_id`'s must have valid DC IDs. Since this method is
# called when sending a file and the user may have entered a path
# they believe is correct but the file doesn't exist, this method
# may detect a path as "valid" bot `file_id` even when it's not.
# By checking the `dc_id`, we greatly reduce the chances of this
# happening.
return None
attributes = []
if file_type == 3 or file_type == 9:
attributes.append(types.DocumentAttributeAudio(
duration=0,
voice=file_type == 3
))
elif file_type == 4 or file_type == 13:
attributes.append(types.DocumentAttributeVideo(
duration=0,
w=0,
h=0,
round_message=file_type == 13
))
# elif file_type == 5: # other, cannot know which
elif file_type == 8:
attributes.append(types.DocumentAttributeSticker(
alt='',
stickerset=types.InputStickerSetEmpty()
))
elif file_type == 10:
attributes.append(types.DocumentAttributeAnimated())
return types.Document(
id=media_id,
access_hash=access_hash,
date=None,
mime_type='',
size=0,
thumbs=None,
dc_id=dc_id,
attributes=attributes,
file_reference=b''
)
elif (version == 2 and len(data) == 44) or (version == 4 and len(data) in (49, 77)):
if version == 2:
(file_type, dc_id, media_id, access_hash,
volume_id, secret, local_id) = struct.unpack('<iiqqqqi', data)
# else version == 4:
elif len(data) == 49:
# TODO Figure out what the extra five bytes mean
(file_type, dc_id, media_id, access_hash,
volume_id, secret, local_id, _) = struct.unpack('<iiqqqqi5s', data)
elif len(data) == 77:
# See #1613.
(file_type, dc_id, _, media_id, access_hash, volume_id, _, local_id, _) = struct.unpack('<ii28sqqq12sib', data)
else:
return None
if not (1 <= dc_id <= 5):
return None
# Thumbnails (small) always have ID 0; otherwise size 'x'
photo_size = 's' if media_id or access_hash else 'x'
return types.Photo(
id=media_id,
access_hash=access_hash,
file_reference=b'',
date=None,
sizes=[types.PhotoSize(
type=photo_size,
location=types.FileLocationToBeDeprecated(
volume_id=volume_id,
local_id=local_id
),
w=0,
h=0,
size=0
)],
dc_id=dc_id,
has_stickers=None
)
def pack_bot_file_id(file):
"""
Inverse operation for `resolve_bot_file_id`.
The only parameters this method will accept are :tl:`Document` and
:tl:`Photo`, and it will return a variable-length ``file_id`` string.
If an invalid parameter is given, it will ``return None``.
"""
if isinstance(file, types.MessageMediaDocument):
file = file.document
elif isinstance(file, types.MessageMediaPhoto):
file = file.photo
if isinstance(file, types.Document):
file_type = 5
for attribute in file.attributes:
if isinstance(attribute, types.DocumentAttributeAudio):
file_type = 3 if attribute.voice else 9
elif isinstance(attribute, types.DocumentAttributeVideo):
file_type = 13 if attribute.round_message else 4
elif isinstance(attribute, types.DocumentAttributeSticker):
file_type = 8
elif isinstance(attribute, types.DocumentAttributeAnimated):
file_type = 10
else:
continue
break
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):
size = next((x for x in reversed(file.sizes) if isinstance(
x, (types.PhotoSize, types.PhotoCachedSize))), None)
if not size:
return None
size = size.location
return _encode_telegram_base64(_rle_encode(struct.pack(
'<iiqqqqib', 2, file.dc_id, file.id, file.access_hash,
size.volume_id, 0, size.local_id, 2 # 0 = old `secret`
)))
else:
return None
def resolve_invite_link(link): def resolve_invite_link(link):
""" """