2015-09-20 17:28:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-20 17:28:10 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2016-10-17 00:22:40 +02:00
|
|
|
"""This module contains an object that represents a Telegram File."""
|
2020-11-29 16:20:46 +01:00
|
|
|
import shutil
|
2020-06-15 18:20:51 +02:00
|
|
|
import urllib.parse as urllib_parse
|
2020-10-31 16:33:34 +01:00
|
|
|
from base64 import b64decode
|
2021-10-05 19:50:11 +02:00
|
|
|
from pathlib import Path
|
2022-11-02 08:28:41 +01:00
|
|
|
from typing import TYPE_CHECKING, BinaryIO, Optional
|
2017-06-23 00:48:24 +02:00
|
|
|
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._passport.credentials import decrypt
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._telegramobject import TelegramObject
|
2022-04-24 12:38:09 +02:00
|
|
|
from telegram._utils.defaultvalue import DEFAULT_NONE
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.files import is_local_file
|
2022-10-07 11:51:53 +02:00
|
|
|
from telegram._utils.types import FilePathInput, JSONDict, ODVInput
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
2022-10-07 11:51:53 +02:00
|
|
|
from telegram import FileCredentials
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
class File(TelegramObject):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
2022-11-02 08:28:41 +01:00
|
|
|
This object represents a file ready to be downloaded. The file can be e.g. downloaded with
|
2022-11-24 12:09:51 +01:00
|
|
|
:attr:`download_to_drive`. It is guaranteed that the link will be valid for at least 1 hour.
|
2022-11-02 08:28:41 +01:00
|
|
|
When the link expires, a new one can be requested by calling :meth:`telegram.Bot.get_file`.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2020-07-14 21:33:56 +02:00
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
|
|
|
considered equal, if their :attr:`file_unique_id` is equal.
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. versionchanged:: 20.0
|
2022-11-24 12:09:51 +01:00
|
|
|
``download`` was split into :meth:`download_to_drive` and :meth:`download_to_memory`.
|
2022-11-02 08:28:41 +01:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
Note:
|
2021-10-19 18:28:19 +02:00
|
|
|
* Maximum file size to download is
|
2022-08-27 11:46:51 +02:00
|
|
|
:tg-const:`telegram.constants.FileSizeLimit.FILESIZE_DOWNLOAD`.
|
2020-12-30 15:59:50 +01:00
|
|
|
* If you obtain an instance of this class from :attr:`telegram.PassportFile.get_file`,
|
2022-11-02 08:28:41 +01:00
|
|
|
then it will automatically be decrypted as it downloads when you call e.g.
|
2022-11-24 12:09:51 +01:00
|
|
|
:meth:`download_to_drive`.
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
Args:
|
2020-03-28 16:37:26 +01:00
|
|
|
file_id (:obj:`str`): Identifier for this file, which can be used to download
|
|
|
|
or reuse the file.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
file_unique_id (:obj:`str`): Unique identifier for this file, which
|
|
|
|
is supposed to be the same over time and for different bots.
|
|
|
|
Can't be used to download or reuse the file.
|
2023-01-01 16:24:00 +01:00
|
|
|
file_size (:obj:`int`, optional): File size in bytes, if known.
|
2022-11-24 12:09:51 +01:00
|
|
|
file_path (:obj:`str`, optional): File path. Use e.g. :meth:`download_to_drive` to get the
|
2022-11-02 08:28:41 +01:00
|
|
|
file.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2023-01-01 16:24:00 +01:00
|
|
|
file_id (:obj:`str`): Identifier for this file, which can be used to download
|
|
|
|
or reuse the file.
|
2020-12-30 15:59:50 +01:00
|
|
|
file_unique_id (:obj:`str`): Unique identifier for this file, which
|
|
|
|
is supposed to be the same over time and for different bots.
|
|
|
|
Can't be used to download or reuse the file.
|
2023-01-01 16:24:00 +01:00
|
|
|
file_size (:obj:`int`): Optional. File size in bytes, if known.
|
|
|
|
file_path (:obj:`str`): Optional. File path. Use e.g. :meth:`download_to_drive` to get the
|
|
|
|
file.
|
2015-09-20 17:28:10 +02:00
|
|
|
"""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
|
|
|
__slots__ = (
|
|
|
|
"file_id",
|
|
|
|
"file_size",
|
|
|
|
"file_unique_id",
|
|
|
|
"file_path",
|
|
|
|
"_credentials",
|
|
|
|
)
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2020-03-28 16:37:26 +01:00
|
|
|
def __init__(
|
2020-11-05 18:12:01 +01:00
|
|
|
self,
|
2020-10-06 19:28:40 +02:00
|
|
|
file_id: str,
|
|
|
|
file_unique_id: str,
|
2023-05-18 07:57:59 +02:00
|
|
|
file_size: Optional[int] = None,
|
|
|
|
file_path: Optional[str] = None,
|
2022-10-07 11:51:53 +02:00
|
|
|
*,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
):
|
2022-10-07 11:51:53 +02:00
|
|
|
super().__init__(api_kwargs=api_kwargs)
|
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
# Required
|
2023-02-02 18:55:07 +01:00
|
|
|
self.file_id: str = str(file_id)
|
|
|
|
self.file_unique_id: str = str(file_unique_id)
|
2015-09-20 17:28:10 +02:00
|
|
|
# Optionals
|
2023-02-02 18:55:07 +01:00
|
|
|
self.file_size: Optional[int] = file_size
|
|
|
|
self.file_path: Optional[str] = file_path
|
2022-10-07 11:51:53 +02:00
|
|
|
|
2023-08-02 11:51:17 +02:00
|
|
|
self._credentials: Optional[FileCredentials] = None
|
2016-09-20 06:36:55 +02:00
|
|
|
|
2020-03-28 16:37:26 +01:00
|
|
|
self._id_attrs = (self.file_unique_id,)
|
2017-05-14 23:29:31 +02:00
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
self._freeze()
|
|
|
|
|
2022-11-02 08:28:41 +01:00
|
|
|
def _get_encoded_url(self) -> str:
|
|
|
|
"""Convert any UTF-8 char in :obj:`File.file_path` into a url encoded ASCII string."""
|
|
|
|
sres = urllib_parse.urlsplit(str(self.file_path))
|
|
|
|
return urllib_parse.urlunsplit(
|
|
|
|
urllib_parse.SplitResult(
|
|
|
|
sres.scheme, sres.netloc, urllib_parse.quote(sres.path), sres.query, sres.fragment
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _prepare_decrypt(self, buf: bytes) -> bytes:
|
|
|
|
return decrypt(b64decode(self._credentials.secret), b64decode(self._credentials.hash), buf)
|
|
|
|
|
2022-11-24 12:09:51 +01:00
|
|
|
async def download_to_drive(
|
2022-04-24 12:38:09 +02:00
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
custom_path: Optional[FilePathInput] = None,
|
2022-11-24 12:09:51 +01:00
|
|
|
*,
|
2022-04-24 12:38:09 +02:00
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
2022-11-02 08:28:41 +01:00
|
|
|
) -> Path:
|
2015-09-20 17:28:10 +02:00
|
|
|
"""
|
2023-02-05 18:09:55 +01:00
|
|
|
Download this file. By default, the file is saved in the current working directory with
|
|
|
|
:attr:`file_path` as file name. If the file has no filename, the file ID will be used as
|
|
|
|
filename. If :paramref:`custom_path` is supplied as a :obj:`str` or :obj:`pathlib.Path`,
|
|
|
|
it will be saved to that path.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Note:
|
2022-11-02 08:28:41 +01:00
|
|
|
If :paramref:`custom_path` isn't provided and :attr:`file_path` is the path of a
|
2022-11-24 12:09:51 +01:00
|
|
|
local file (which is the case when a Bot API Server is running in local mode), this
|
|
|
|
method will just return the path.
|
2022-11-02 08:28:41 +01:00
|
|
|
|
|
|
|
The only exception to this are encrypted files (e.g. a passport file). For these, a
|
2022-11-24 12:09:51 +01:00
|
|
|
file with the prefix `decrypted_` will be created in the same directory as the
|
|
|
|
original file in order to decrypt the file without changing the existing one
|
|
|
|
in-place.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-10-19 18:28:19 +02:00
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
* :paramref:`custom_path` parameter now also accepts :class:`pathlib.Path` as argument.
|
|
|
|
* Returns :class:`pathlib.Path` object in cases where previously a :obj:`str` was
|
2021-10-19 18:28:19 +02:00
|
|
|
returned.
|
2022-11-02 08:28:41 +01:00
|
|
|
* This method was previously called ``download``. It was split into
|
2022-11-24 12:09:51 +01:00
|
|
|
:meth:`download_to_drive` and :meth:`download_to_memory`.
|
2021-10-05 19:50:11 +02:00
|
|
|
|
2017-01-06 22:48:34 +01:00
|
|
|
Args:
|
2022-11-02 08:28:41 +01:00
|
|
|
custom_path (:class:`pathlib.Path` | :obj:`str` , optional): The path where the file
|
2023-02-05 18:09:55 +01:00
|
|
|
will be saved to. If not specified, will be saved in the current working directory
|
|
|
|
with :attr:`file_path` as file name or the :attr:`file_id` if :attr:`file_path`
|
|
|
|
is not set.
|
2022-11-24 12:09:51 +01:00
|
|
|
|
|
|
|
Keyword Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
2022-05-06 17:15:23 +02:00
|
|
|
write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
2022-04-24 12:38:09 +02:00
|
|
|
:paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
2022-05-06 17:15:23 +02:00
|
|
|
pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
2022-04-24 12:38:09 +02:00
|
|
|
:paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2018-03-01 09:10:04 +01:00
|
|
|
Returns:
|
2022-11-02 08:28:41 +01:00
|
|
|
:class:`pathlib.Path`: Returns the Path object the file was downloaded to.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-11-29 16:20:46 +01:00
|
|
|
local_file = is_local_file(self.file_path)
|
2021-10-05 19:50:11 +02:00
|
|
|
url = None if local_file else self._get_encoded_url()
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2022-11-02 08:28:41 +01:00
|
|
|
# if _credentials exists we want to decrypt the file
|
|
|
|
if local_file and self._credentials:
|
|
|
|
file_to_decrypt = Path(self.file_path)
|
|
|
|
buf = self._prepare_decrypt(file_to_decrypt.read_bytes())
|
|
|
|
if custom_path is not None:
|
|
|
|
path = Path(custom_path)
|
2020-11-29 16:20:46 +01:00
|
|
|
else:
|
2022-11-02 08:28:41 +01:00
|
|
|
path = Path(str(file_to_decrypt.parent) + "/decrypted_" + file_to_decrypt.name)
|
|
|
|
path.write_bytes(buf)
|
|
|
|
return path
|
2020-10-31 16:33:34 +01:00
|
|
|
|
2021-10-05 19:50:11 +02:00
|
|
|
if custom_path is not None and local_file:
|
|
|
|
shutil.copyfile(self.file_path, str(custom_path))
|
|
|
|
return Path(custom_path)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
if custom_path:
|
2021-10-05 19:50:11 +02:00
|
|
|
filename = Path(custom_path)
|
2020-11-29 16:20:46 +01:00
|
|
|
elif local_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
return Path(self.file_path)
|
2020-10-31 16:33:34 +01:00
|
|
|
elif self.file_path:
|
2021-10-05 19:50:11 +02:00
|
|
|
filename = Path(Path(self.file_path).name)
|
2015-09-20 17:28:10 +02:00
|
|
|
else:
|
2021-10-05 19:50:11 +02:00
|
|
|
filename = Path.cwd() / self.file_id
|
2020-10-31 16:33:34 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
buf = await self.get_bot().request.retrieve(
|
|
|
|
url,
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
|
|
|
)
|
2020-10-31 16:33:34 +01:00
|
|
|
if self._credentials:
|
2022-11-02 08:28:41 +01:00
|
|
|
buf = self._prepare_decrypt(buf)
|
2021-10-05 19:50:11 +02:00
|
|
|
filename.write_bytes(buf)
|
2020-10-31 16:33:34 +01:00
|
|
|
return filename
|
2018-03-01 09:10:04 +01:00
|
|
|
|
2022-11-24 12:09:51 +01:00
|
|
|
async def download_to_memory(
|
2022-11-02 08:28:41 +01:00
|
|
|
self,
|
|
|
|
out: BinaryIO,
|
2022-11-24 12:09:51 +01:00
|
|
|
*,
|
2022-11-02 08:28:41 +01:00
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Download this file into memory. :paramref:`out` needs to be supplied with a
|
|
|
|
:obj:`io.BufferedIOBase`, the file contents will be saved to that object using the
|
|
|
|
:obj:`out.write<io.BufferedIOBase.write>` method.
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Working with Files and Media <Working-with-Files-and-Media>`
|
|
|
|
|
2022-11-02 08:28:41 +01:00
|
|
|
.. versionadded:: 20.0
|
|
|
|
|
|
|
|
Args:
|
|
|
|
out (:obj:`io.BufferedIOBase`): A file-like object. Must be opened for writing in
|
|
|
|
binary mode.
|
2022-11-24 12:09:51 +01:00
|
|
|
|
|
|
|
Keyword Args:
|
2022-11-02 08:28:41 +01:00
|
|
|
read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
"""
|
|
|
|
local_file = is_local_file(self.file_path)
|
|
|
|
url = None if local_file else self._get_encoded_url()
|
|
|
|
path = Path(self.file_path) if local_file else None
|
|
|
|
if local_file:
|
|
|
|
buf = path.read_bytes()
|
|
|
|
else:
|
|
|
|
buf = await self.get_bot().request.retrieve(
|
|
|
|
url,
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
2018-03-01 09:10:04 +01:00
|
|
|
)
|
2022-11-02 08:28:41 +01:00
|
|
|
if self._credentials:
|
|
|
|
buf = self._prepare_decrypt(buf)
|
|
|
|
out.write(buf)
|
2018-03-01 09:10:04 +01:00
|
|
|
|
2022-11-24 12:09:51 +01:00
|
|
|
async def download_as_bytearray(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
buf: Optional[bytearray] = None,
|
2022-11-24 12:09:51 +01:00
|
|
|
*,
|
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
) -> bytearray:
|
2018-03-01 09:10:04 +01:00
|
|
|
"""Download this file and return it as a bytearray.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
buf (:obj:`bytearray`, optional): Extend the given bytearray with the downloaded data.
|
|
|
|
|
2022-11-24 12:09:51 +01:00
|
|
|
Keyword Args:
|
|
|
|
read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to
|
|
|
|
:paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to
|
|
|
|
:attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
|
2018-03-01 09:10:04 +01:00
|
|
|
Returns:
|
2022-02-09 17:30:16 +01:00
|
|
|
:obj:`bytearray`: The same object as :paramref:`buf` if it was specified. Otherwise a
|
2022-08-27 11:46:51 +02:00
|
|
|
newly allocated :obj:`bytearray`.
|
2018-03-01 09:10:04 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
if buf is None:
|
|
|
|
buf = bytearray()
|
2022-11-02 08:28:41 +01:00
|
|
|
|
2020-11-29 16:20:46 +01:00
|
|
|
if is_local_file(self.file_path):
|
2022-11-02 08:28:41 +01:00
|
|
|
bytes_data = Path(self.file_path).read_bytes()
|
|
|
|
else:
|
2022-11-24 12:09:51 +01:00
|
|
|
bytes_data = await self.get_bot().request.retrieve(
|
|
|
|
self._get_encoded_url(),
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
|
|
|
)
|
2022-11-02 08:28:41 +01:00
|
|
|
if self._credentials:
|
|
|
|
buf.extend(self._prepare_decrypt(bytes_data))
|
2020-11-29 16:20:46 +01:00
|
|
|
else:
|
2022-11-02 08:28:41 +01:00
|
|
|
buf.extend(bytes_data)
|
2018-03-01 09:10:04 +01:00
|
|
|
return buf
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def set_credentials(self, credentials: "FileCredentials") -> None:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""Sets the passport credentials for the file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
credentials (:class:`telegram.FileCredentials`): The credentials.
|
|
|
|
"""
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
self._credentials = credentials
|