Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send large file with Telegram Bot API?

Telegram bot has a file size limit for sending in 50MB.

I need to send large files. Is there any way around this?

I know about this project https://github.com/pwrtelegram/pwrtelegram but I couldn't make it work.

Maybe someone has already solved such a problem?

There is an option to implement the file upload via Telegram API and then send by file_id with bot.

I write a bot in Java using the library https://github.com/rubenlagus/TelegramBots

UPDATE

For solve this problem i use telegram api, that has limit on 1.5 GB for big files.

I prefer kotlogram - the perfect lib with good documentation https://github.com/badoualy/kotlogram

UPDATE 2

Example of something how i use this lib:

private void uploadToServer(TelegramClient telegramClient, TLInputPeerChannel tlInputPeerChannel, Path pathToFile, int partSize) {
    File file = pathToFile.toFile();
    long fileId = getRandomId();
    int totalParts = Math.toIntExact(file.length() / partSize + 1);
    int filePart = 0;
    int offset = filePart * partSize;
    try (InputStream is = new FileInputStream(file)) {

        byte[] buffer = new byte[partSize];
        int read;
        while ((read = is.read(buffer, offset, partSize)) != -1) {
            TLBytes bytes = new TLBytes(buffer, 0, read);
            TLBool tlBool = telegramClient.uploadSaveBigFilePart(fileId, filePart, totalParts, bytes);
            telegramClient.clearSentMessageList();
            filePart++;
        }
    } catch (Exception e) {
        log.error("Error uploading file to server", e);
    } finally {
        telegramClient.close();
    }
    sendToChannel(telegramClient, tlInputPeerChannel, "FILE_NAME.zip", fileId, totalParts)
}


private void sendToChannel(TelegramClient telegramClient, TLInputPeerChannel tlInputPeerChannel, String name, long fileId, int totalParts) {
    try {
        String mimeType = name.substring(name.indexOf(".") + 1);

        TLVector<TLAbsDocumentAttribute> attributes = new TLVector<>();
        attributes.add(new TLDocumentAttributeFilename(name));

        TLInputFileBig inputFileBig = new TLInputFileBig(fileId, totalParts, name);
        TLInputMediaUploadedDocument document = new TLInputMediaUploadedDocument(inputFileBig, mimeType, attributes, "", null);
        TLAbsUpdates tlAbsUpdates = telegramClient.messagesSendMedia(false, false, false,
                tlInputPeerChannel, null, document, getRandomId(), null);
    } catch (Exception e) {
        log.error("Error sending file by id into channel", e);
    } finally {
        telegramClient.close();
    }
}

where TelegramClient telegramClient and TLInputPeerChannel tlInputPeerChannel you can create as write in documentation.

DON'T COPY-PASTE, rewrite on your needs.

like image 751
bigspawn Avatar asked Sep 12 '18 05:09

bigspawn


People also ask

How can I send large files on Telegram?

There are a few different ways to do it. You can use a file splitter like HJSplit or 7-Zip. Or, you can use a file compression tool like WinRAR or WinZip to compress your files into smaller parts. Once you have split your files into multiple parts, you can send them through Telegram.

Can Telegram BOT send files?

Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future. See sendDocument official docs for a list of supported parameters and other info.

How many messages can a telegram BOT send?

If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results. Also note that your bot will not be able to send more than 20 messages per minute to the same group.


2 Answers

With local Telegram Bot API server you are allowed to send InputStream with a 2000Mb file size limit, raised from 50Mb default.

like image 134
Dmitry Verhoturov Avatar answered Sep 22 '22 23:09

Dmitry Verhoturov


IF you want to send file via telegram bot, you have three options:

  1. InputStream (10 MB limit for photos, 50 MB for other files)
  2. From http url (Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.)
  3. Send cached files by their file_ids.(There are no limits for files sent this way)

So, I recommend you to store file_ids beforehand and send files by these ids (this is recommended by api docs too).

like image 20
valijon Avatar answered Sep 21 '22 23:09

valijon