How to upload file?#

As says official Telegram Bot API documentation 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.

But if you need to upload new file just use subclasses of InputFile.

Here is available three different builtin types of input file:

Warning

Be respectful with Telegram

Instances of InputFile is reusable. That’s mean you can create instance of InputFile and sent this file multiple times but Telegram is not recommend to do that and when you upload file once just save their file_id and use it in next times.

Upload from file system#

By first step you will need to import InputFile wrapper:

from aiogram.types import FSInputFile

Then you can use it:

cat = FSInputFile("cat.png")
agenda = FSInputFile("my-document.pdf", filename="agenda-2019-11-19.pdf")
class aiogram.types.input_file.FSInputFile(path: Union[str, Path], filename: Optional[str] = None, chunk_size: int = 65536)[source]#
__init__(path: Union[str, Path], filename: Optional[str] = None, chunk_size: int = 65536)[source]#

Represents object for uploading files from filesystem

Parameters:
  • path – Path to file

  • filename – Filename to be propagated to telegram. By default, will be parsed from path

  • chunk_size – Uploading chunk size

Upload from buffer#

Files can be also passed from buffer (For example you generate image using Pillow and the want’s to sent it to the Telegram):

Import wrapper:

from aiogram.types import BufferedInputFile

And then you can use it:

text_file = BufferedInputFile(b"Hello, world!", filename="file.txt")
class aiogram.types.input_file.BufferedInputFile(file: bytes, filename: str, chunk_size: int = 65536)[source]#
__init__(file: bytes, filename: str, chunk_size: int = 65536)[source]#

Represents object for uploading files from filesystem

Parameters:
  • file – Bytes

  • filename – Filename to be propagated to telegram.

  • chunk_size – Uploading chunk size

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 by URL, you can use aiogram.types.input_file.URLInputFile.

Import wrapper:

from aiogram.types import URLInputFile

And then you can use it:

image = URLInputFile(
    "https://www.python.org/static/community_logos/python-powered-h-140x182.png",
    filename="python-logo.png"
)
class aiogram.types.input_file.URLInputFile(url: str, filename: Optional[str] = None, chunk_size: int = 65536, timeout: int = 30)[source]#