How to send large file with Telegram Bot API?
Asked Answered
J

4

12

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.

Jesuitism answered 12/9, 2018 at 5:47 Comment(3)
As you said you could send the files to your bot personally and then using their file IDs try to send the files to specific chat IDs (including people, groups and channels).Ideatum
I can use the project with Telegram API github.com/ex3ndr/telegram-api but it has not been updated for 5 years. I will try later. And send the file with Telegram API is not such a trivial task with where uploading algorithmJesuitism
Hmmm, I see. Then you could have a small download server and put the files there and try to share the link of them in Telegram.Ideatum
W
9

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

Warfare answered 15/7, 2021 at 6:1 Comment(0)
C
7

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).

Cushy answered 13/9, 2018 at 13:47 Comment(8)
Sending by file_id requires the file to be already on the server.Duplicate
@TimPotapov yes, you will send the file to the bot by telegram client(by hand or programmatically) and the bot will receive it as a message where you can access file_idCushy
Telegram client doesn't have file size limit (at least it must be a huge limit)Cushy
50MB video limitDuplicate
@TimPotapov you must be reading something older than 3-4 years tech.hindustantimes.com/tech/news/…Cushy
core.telegram.org/bots/api#sending-files search "50 mb"Duplicate
@TimPotapov I highly recommend to read difference between telegram client and bot. https://mcmap.net/q/706768/-telegram-api-vs-bot-api-closed As I already explained above you send the file from telegram client to telegram bot.Cushy
file_id is unique to a chat, so you'd have to send the same file with mproto api to every group / user once in order to use it with the bot apiCamus
S
3

Using a Local Bot API Server you can send a large file up to 2GB.

GitHub Source Code:

https://github.com/tdlib/telegram-bot-api

Official Documentation

https://core.telegram.org/bots/api#using-a-local-bot-api-server

You can build and install this to your server by following the instructions on this link https://tdlib.github.io/telegram-bot-api/build.html

basic setup :

  1. Generate Telegram Applications id from https://my.telegram.org/apps
  2. Start the server ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
  3. Default address is http://127.0.0.1:8081/ and the port is 8081.
  4. All the official APIs will work with this setup. Just change the address to http://127.0.0.1:8081/bot/METHOD_NAME reference: https://core.telegram.org/bots/api

Example Code:

    OkHttpClient client = new OkHttpClient().newBuilder()
      .build();
    MediaType mediaType = MediaType.parse("text/plain");
    RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
      .addFormDataPart("chat_id","your_chat_id_here")
      .addFormDataPart("video","file_location",
        RequestBody.create(MediaType.parse("application/octet-stream"),
        new File("file_location")))
      .addFormDataPart("supports_streaming","true")
      .build();
    // https://127.0.0.1:8081/bot<token>/METHOD_NAME 
    Request request = new Request.Builder()
      .url("http://127.0.0.1:8081/bot<token>/sendVideo")
      .method("POST", body)
      .build();
    Response response = client.newCall(request).execute();
Seise answered 4/1, 2023 at 22:38 Comment(0)
M
0

You can create a bot that will send the provided file to a channel and then you can get the message id. Create an endpoint that will accept the message it and stream the file directly from telegram.

Here is a blog that explains how you can do that. https://www.changeblogger.org/blog/creating-a-telegram-bot-for-large-file-downloads-using-gram-js

Meerschaum answered 13/12, 2023 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.