How to upload large files to telegram bot using aiogram 3.xx

Nowadays, Telegram has become one of the most popular platforms for communication and information exchange. In this regard, the development of bots for Telegram has also become a popular area. One of the important aspects of working with bots is the ability to download and process large files.

In this article, we will look at how to organize work with large files in Telegram bots using the aiogram 3.xx framework, and how you can deploy a local server for testing and developing a bot. I will also give an example of the simplest telegram bot.

Let's start with the fact that Telegram API has certain file size limitswhich can be sent or received through bots is 20 mb.

To bypass this limitation, you can use the telegram bot api to create a local server. It will already be possible to download and send files on it 2 GB.

First, you need to install telegram-bot-api. Universal instructions for installing it are available on the website: link

For Mac, it is more convenient to use the following instructions:

git clone https://github.com/tdlib/telegram-bot-api.git
cd telegram-bot-api
mkdir build
cd build
cmake ..
cmake --build .

Possible problems at this stage:

1)

build git:(master) cmake ..
zsh: command not found: cmake

Solution:

You need to install cmake, for Mac it is most convenient to install it via brew:

brew install cmake

For Ubuntu/Debian we use these commands:

sudo apt-get update
sudo apt-get install cmake

2)

build git:(master) cmake ..
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The CXX compiler identification is AppleClang 15.0.0.15000100
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:16 (add_subdirectory):
  The source directory

    /Users/aleksandrvolzanin/pet_project/more50/telegram-bot-api/td

  does not contain a CMakeLists.txt file.


CMake Error at CMakeLists.txt:40 (include):
  include could not find requested file:

    PreventInSourceBuild


CMake Error at CMakeLists.txt:41 (prevent_in_source_build):
  Unknown CMake command "prevent_in_source_build".


-- Configuring incomplete, errors occurred!

Solution:

You need to install the td submodule.

git submodule init
git submodule update

Next, to launch the local server, we need to go to the build folder and there we enter the command:

./telegram-bot-api --local --api-id=<API-ID> --api-hash=<API-HASH>

Here, instead of API-ID and API-Hash, you need to enter your data, which you can get on the website: link

If your terminal shows no errors, then congratulations, you have launched your local server for telegram bot.

Below is the code for the simplest telegram bot, which simply downloads audio to a folder on your computer:

import asyncio
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.client.telegram import TelegramAPIServer 


API_TOKEN = '7488158267:AAH1KIiUd-OOFi8HakIDKZg8UMzrLzlWPaE'
session = AiohttpSession(api=TelegramAPIServer.from_base("http://localhost:8081", is_local=True))
bot = Bot(token=API_TOKEN, session=session)
dp = Dispatcher()

@dp.message(F.audio)
async def send_audio(message: Message):

    audio = message.audio
    file_id = audio.file_id
    file = await message.bot.get_file(file_id)

    file_path = file.file_path
    download_path = "/Users/aleksandrvolzanin/pet_project/more50/audio/audio_message.mp3"

    await message.bot.download_file(file_path, download_path)

    await message.answer("Ваше аудиосообщение принято")


async def main():
    await dp.start_polling(bot)

if __name__ == "__main__":
    asyncio.run(main())

You will also need to create an audio folder in the project directory.

After that, the file, in my case it is a lecture recording, which weighs 100+ MB, will be downloaded to your computer, and you will see a message in the telegram bot:

Bot's answer

Bot's answer

Hierarchy of files in a project

Hierarchy of files in a project

If you can't, you can try logging out on the website: https://api.telegram.org/bot/logOut

you need to replace with your bot's token.

That's it! I hope you enjoyed the article, I decided to write it because I didn't see a simple and clear explanation of this feature of telegram bots.

You can also view the official git hub repository of the project: link

If you have any comments or additions, be sure to write them in the comments, I will try to give feedback and supplement the article!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *