1

There are ≈ 2.5k archives of different size from 20kb to 2gb. They need to upload to telegram channel (not with links to other storages, clouds, etc.) Thought that it could be done through a bot (then I did not know about the 50mb limit).

Wrote the following code:

@dp.message_handler(commands=['send_files'])
async def send_files_handler(message: types.Message):
    files = os.listdir(DIRECTORY)
    for files in files:
        with open(os.path.join(DIRECTORY, file), 'rb') as f:
            try:
                # await bot.send_document(chat_id=CHAT_ID, document=f)
                await bot.send_document('CHENAL_ID', document=f)`

I write the command /send_files in the bot's PM, the bot starts sending all the files from the specified directory. I tried to run it, I saw after loading a few files errors. I wrote a sending block in try: and added a function to write in txt paths to files that failed to send

try:
                await bot.send_document('CHENAL_ID', document=f)
            except:
                r_f = open(r'D:\test\erorr.txt','a')
                r_f.write(str(f)+'\n')
                f.close()`

Wrote a script to move the files from erorr.txt to a separate folder to see what's wrong with them later. I realized that for some reason there were files up to 50mb and they are not in the channel. Repeated the algorithm again, but in a new directory and with a new file erorr.txt. Again, some of the files were downloaded and some were not. And I did it again many, many times.

I've seen article on habr where they use "User-bot", he sends files to the bot, and it in turn sends the file to the desired chat.

In general, I need a working method for auto uploading files to tg channel up to 2gb.

eshirvana
  • 23,227
  • 3
  • 22
  • 38
Caxa
  • 29
  • 4

1 Answers1

0

Tip, instead of moving those files etc, you could have checked the return value of bot.send_document, Telegram will let you know what you've done wrong.


The linked article seems to be using [github], a library to create your own Telegram Client using Telegram's MTProto Protocol.

Using Telethon's send_file() method, you could send files from your own Telegram account, so you will not be limited to the Bot limit of 50mb but (currently) 1.5GB.

You could even use the upload_file() method to just upload them to Telegram' s server, without sending the message. Save the returned id's somewhere and to instantly send the file to somebody.


Examples can also be found in the documentation:

# Normal files like photos
await client.send_file(chat, '/my/photos/me.jpg', caption="It's me!")

# Only documents
await client.send_file(chat, '/my/photos/photo.png', force_document=True)

# Photos as photo and document
file = await client.upload_file('photo.jpg')
await client.send_file(chat, file)                       # sends as photo
await client.send_file(chat, file, force_document=True)  # sends as document
0stone0
  • 34,288
  • 4
  • 39
  • 64