2015-09-20 17:28:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 08:15:18 +01:00
|
|
|
# Copyright (C) 2015-2022
|
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
|
2020-10-31 16:33:34 +01:00
|
|
|
from typing import IO, TYPE_CHECKING, Any, Optional, Union
|
2017-06-23 00:48:24 +02:00
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
from telegram import TelegramObject
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._passport.credentials import decrypt
|
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-04-24 12:38:09 +02:00
|
|
|
from telegram._utils.types import FilePathInput, ODVInput
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot, FileCredentials
|
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
class File(TelegramObject):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
This object represents a file ready to be downloaded. The file can be downloaded with
|
|
|
|
:attr:`download`. It is guaranteed that the link will be valid for at least 1 hour. When the
|
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
|
|
|
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.
|
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
Note:
|
2021-10-19 18:28:19 +02:00
|
|
|
* Maximum file size to download is
|
|
|
|
: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-04-24 12:38:09 +02:00
|
|
|
then it will automatically be decrypted as it downloads when you call :meth:`download()`.
|
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.
|
2021-12-13 18:21:38 +01:00
|
|
|
file_size (:obj:`int`, optional): Optional. File size in bytes, if known.
|
2017-07-23 22:33:08 +02:00
|
|
|
file_path (:obj:`str`, optional): File path. Use :attr:`download` to get the file.
|
|
|
|
bot (:obj:`telegram.Bot`, optional): Bot to use with shortcut method.
|
|
|
|
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
|
|
|
file_id (:obj:`str`): Identifier for this file.
|
|
|
|
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.
|
2021-12-13 18:21:38 +01:00
|
|
|
file_size (:obj:`str`): Optional. File size in bytes.
|
2022-04-24 12:38:09 +02:00
|
|
|
file_path (:obj:`str`): Optional. File path. Use :meth:`download` to get the file.
|
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
|
|
|
|
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-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
2020-11-05 18:12:01 +01:00
|
|
|
self,
|
2020-10-09 17:22:07 +02:00
|
|
|
file_id: str,
|
|
|
|
file_unique_id: str,
|
|
|
|
bot: 'Bot' = None,
|
|
|
|
file_size: int = None,
|
|
|
|
file_path: str = None,
|
2020-11-05 18:12:01 +01:00
|
|
|
**_kwargs: Any,
|
2020-10-09 17:22:07 +02:00
|
|
|
):
|
2015-09-20 17:28:10 +02:00
|
|
|
# Required
|
|
|
|
self.file_id = str(file_id)
|
2020-03-28 16:37:26 +01:00
|
|
|
self.file_unique_id = str(file_unique_id)
|
2015-09-20 17:28:10 +02:00
|
|
|
# Optionals
|
2017-01-11 19:41:39 +01:00
|
|
|
self.file_size = file_size
|
2017-06-23 00:48:24 +02:00
|
|
|
self.file_path = file_path
|
2021-10-21 11:17:12 +02:00
|
|
|
self.set_bot(bot)
|
2020-10-06 19:28:40 +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-04-24 12:38:09 +02:00
|
|
|
async def download(
|
|
|
|
self,
|
|
|
|
custom_path: FilePathInput = None,
|
|
|
|
out: IO = None,
|
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
2021-10-05 19:50:11 +02:00
|
|
|
) -> Union[Path, IO]:
|
2015-09-20 17:28:10 +02:00
|
|
|
"""
|
2016-12-18 03:05:00 +01:00
|
|
|
Download this file. By default, the file is saved in the current working directory with its
|
2020-02-06 11:21:21 +01:00
|
|
|
original filename as reported by Telegram. If the file has no filename, it the file ID will
|
2022-02-09 17:30:16 +01:00
|
|
|
be used as filename. If a :paramref:`custom_path` is supplied, it will be saved to that
|
|
|
|
path instead. If :paramref:`out` is defined, the file contents will be saved to that object
|
|
|
|
using the ``out.write`` method.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Note:
|
2022-02-09 17:30:16 +01:00
|
|
|
* :paramref:`custom_path` and :paramref:`out` are mutually exclusive.
|
|
|
|
* If neither :paramref:`custom_path` nor :paramref:`out` is provided and
|
|
|
|
:attr:`file_path` is the path of a local file (which is the case when a Bot API
|
|
|
|
Server is running in local mode), this method will just return the path.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2021-10-05 19:50:11 +02:00
|
|
|
.. versionchanged:: 14.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.
|
2021-10-05 19:50:11 +02:00
|
|
|
|
2017-01-06 22:48:34 +01:00
|
|
|
Args:
|
2022-02-09 17:30:16 +01:00
|
|
|
custom_path (:class:`pathlib.Path` | :obj:`str`, optional): Custom path.
|
2018-03-01 09:10:04 +01:00
|
|
|
out (:obj:`io.BufferedWriter`, optional): A file-like object. Must be opened for
|
|
|
|
writing in binary mode, if applicable.
|
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`.
|
|
|
|
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`.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2018-03-01 09:10:04 +01:00
|
|
|
Returns:
|
2022-02-09 17:30:16 +01:00
|
|
|
:class:`pathlib.Path` | :obj:`io.BufferedWriter`: The same object as :paramref:`out` if
|
2021-10-19 18:28:19 +02:00
|
|
|
specified. Otherwise, returns the filename downloaded to or the file path of the
|
|
|
|
local file.
|
2018-03-01 09:10:04 +01:00
|
|
|
|
2016-12-18 03:05:00 +01:00
|
|
|
Raises:
|
2022-02-09 17:30:16 +01:00
|
|
|
ValueError: If both :paramref:`custom_path` and :paramref:`out` are passed.
|
2016-12-18 03:05:00 +01:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2016-12-18 03:05:00 +01:00
|
|
|
if custom_path is not None and out is not None:
|
2021-10-05 19:50:11 +02:00
|
|
|
raise ValueError('`custom_path` and `out` are mutually exclusive')
|
2016-12-18 03:05:00 +01: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()
|
|
|
|
path = Path(self.file_path) if local_file else None
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2016-12-18 03:05:00 +01:00
|
|
|
if out:
|
2020-11-29 16:20:46 +01:00
|
|
|
if local_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
buf = path.read_bytes()
|
2020-11-29 16:20:46 +01:00
|
|
|
else:
|
2022-04-24 12:38:09 +02:00
|
|
|
buf = await self.get_bot().request.retrieve(url)
|
2020-11-29 16:20:46 +01:00
|
|
|
if self._credentials:
|
|
|
|
buf = decrypt(
|
|
|
|
b64decode(self._credentials.secret), b64decode(self._credentials.hash), buf
|
|
|
|
)
|
2016-12-18 03:05:00 +01:00
|
|
|
out.write(buf)
|
2018-03-01 09:10:04 +01:00
|
|
|
return out
|
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:
|
|
|
|
buf = decrypt(
|
|
|
|
b64decode(self._credentials.secret), b64decode(self._credentials.hash), 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
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def _get_encoded_url(self) -> str:
|
2018-03-01 09:10:04 +01:00
|
|
|
"""Convert any UTF-8 char in :obj:`File.file_path` into a url encoded ASCII string."""
|
2021-10-05 19:50:11 +02:00
|
|
|
sres = urllib_parse.urlsplit(str(self.file_path))
|
2020-10-09 17:22:07 +02:00
|
|
|
return urllib_parse.urlunsplit(
|
|
|
|
urllib_parse.SplitResult(
|
|
|
|
sres.scheme, sres.netloc, urllib_parse.quote(sres.path), sres.query, sres.fragment
|
|
|
|
)
|
|
|
|
)
|
2018-03-01 09:10:04 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def download_as_bytearray(self, buf: bytearray = 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.
|
|
|
|
|
|
|
|
Returns:
|
2022-02-09 17:30:16 +01:00
|
|
|
:obj:`bytearray`: The same object as :paramref:`buf` if it was specified. Otherwise a
|
|
|
|
newly allocated :obj:`bytearray`.
|
2018-03-01 09:10:04 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
if buf is None:
|
|
|
|
buf = bytearray()
|
2020-11-29 16:20:46 +01:00
|
|
|
if is_local_file(self.file_path):
|
2021-10-05 19:50:11 +02:00
|
|
|
buf.extend(Path(self.file_path).read_bytes())
|
2020-11-29 16:20:46 +01:00
|
|
|
else:
|
2022-04-24 12:38:09 +02:00
|
|
|
buf.extend(await self.get_bot().request.retrieve(self._get_encoded_url()))
|
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
|