2021-01-26 21:20:52 +02:00
.. _sending-files:
###################
How to upload file?
###################
As says `official Telegram Bot API documentation <https://core.telegram.org/bots/api#sending-files> `_
there are three ways to send files (photos, stickers, audio, media, etc.):
If the file is already stored somewhere on the Telegram servers or file is available by the URL,
you don't need to reupload it.
2023-04-08 17:54:08 +03:00
But if you need to upload a new file just use subclasses of `InputFile <types/input_file.html> `__ .
2021-01-26 21:20:52 +02:00
2023-02-25 18:51:54 +03:00
Here are the three different available builtin types of input file:
2021-01-26 21:20:52 +02:00
- :class: `aiogram.types.input_file.FSInputFile` - `uploading from file system <#upload-from-file-system> `__
- :class: `aiogram.types.input_file.BufferedInputFile` - `uploading from buffer <#upload-from-buffer> `__
- :class: `aiogram.types.input_file.URLInputFile` - `uploading from URL <#upload-from-url> `__
.. warning ::
2024-06-17 00:57:28 +03:00
**Be respectful to Telegram**
2021-01-26 21:20:52 +02:00
2023-02-25 18:51:54 +03:00
Instances of `InputFile` are reusable.
2024-06-17 00:57:28 +03:00
That means you can create an instance of InputFile and send it multiple times. However, Telegram does not recommend doing this. Instead, once you upload a file, save its `file_id` and reuse that later.
2021-01-26 21:20:52 +02:00
Upload from file system
=======================
By first step you will need to import InputFile wrapper:
.. code-block ::
from aiogram.types import FSInputFile
Then you can use it:
.. code-block ::
cat = FSInputFile("cat.png")
agenda = FSInputFile("my-document.pdf", filename="agenda-2019-11-19.pdf")
.. autoclass :: aiogram.types.input_file.FSInputFile
2022-10-02 18:24:26 +03:00
:members: __init__
2021-01-26 21:20:52 +02:00
Upload from buffer
==================
Files can be also passed from buffer
(For example you generate image using `Pillow <https://pillow.readthedocs.io/en/stable/> `_
2023-02-25 18:51:54 +03:00
and you want to send it to Telegram):
2021-01-26 21:20:52 +02:00
Import wrapper:
.. code-block ::
from aiogram.types import BufferedInputFile
And then you can use it:
.. code-block ::
text_file = BufferedInputFile(b"Hello, world!", filename="file.txt")
.. autoclass :: aiogram.types.input_file.BufferedInputFile
2022-10-02 18:24:26 +03:00
:members: __init__
2021-01-26 21:20:52 +02:00
Upload from url
===============
If you need to upload a file from another server, but the direct link is bound to your server's IP,
or you want to bypass native `upload limits <https://core.telegram.org/bots/api#sending-files> `_
by URL, you can use :obj: `aiogram.types.input_file.URLInputFile` .
Import wrapper:
.. code-block ::
from aiogram.types import URLInputFile
And then you can use it:
.. code-block ::
image = URLInputFile(
"https://www.python.org/static/community_logos/python-powered-h-140x182.png",
filename="python-logo.png"
)
.. autoclass :: aiogram.types.input_file.URLInputFile
:members: