简体   繁体   English

如何使用 Telegram Bot API 发送大文件?

[英]How to send large file with Telegram Bot API?

Telegram bot has a file size limit for sending in 50MB. Telegram bot 的文件大小限制为 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.我知道这个项目https://github.com/pwrtelegram/pwrtelegram但我无法让它工作。

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.有一个选项可以通过 Telegram API 实现文件上传,然后使用 bot 通过file_id发送。

I write a bot in Java using the library https://github.com/rubenlagus/TelegramBots我使用库https://github.com/rubenlagus/TelegramBots在 Java 中编写了一个机器人

UPDATE更新

For solve this problem i use telegram api, that has limit on 1.5 GB for big files.为了解决这个问题,我使用电报 api,它对大文件有 1.5 GB 的限制。

I prefer kotlogram - the perfect lib with good documentation https://github.com/badoualy/kotlogram我更喜欢 kotlogram - 具有良好文档的完美库https://github.com/badoualy/kotlogram

UPDATE 2更新 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.其中TelegramClient telegramClientTLInputPeerChannel tlInputPeerChannel您可以创建为写入文档。

DON'T COPY-PASTE, rewrite on your needs.不要复制粘贴,根据您的需要重写。

Withlocal Telegram Bot API server you are allowed to send InputStream with a 2000Mb file size limit, raised from 50Mb default.使用本地 Telegram Bot API 服务器,您可以发送具有 2000Mb 文件大小限制的 InputStream,默认为 50Mb。

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) InputStream (照片限制为10 MB ,其他文件限制为50 MB
  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.) 来自http url (Telegram将下载并发送文件。照片最大尺寸为5 MB ,其他类型内容最大为20 MB 。)
  3. Send cached files by their file_id s.(There are no limits for files sent this way) 通过file_id发送缓存文件。(以这种方式发送的文件没有限制

So, I recommend you to store file_ids beforehand and send files by these ids (this is recommended by api docs too). 因此,我建议您事先存储file_ids并通过这些ID发送文件(这也是api docs推荐的)。

Using a Local Bot API Server you can send a large file up to 2GB.使用本地 Bot API 服务器,您可以发送最大 2GB 的大文件。

GitHub Source Code: GitHub 源代码:

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

Official Documentation官方文档

https://core.telegram.org/bots/api#using-a-local-bot-api-server 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您可以按照此链接https://tdlib.github.io/telegram-bot-api/build.html上的说明build and install到您的服务器

basic setup:基本设置:

  1. Generate Telegram Applications id from https://my.telegram.org/appshttps://my.telegram.org/apps生成电报应用程序 ID
  2. Start the server ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20启动服务器./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.默认地址为http://127.0.0.1:8081/ ,端口为 8081。
  4. All the official APIs will work with this setup.所有官方 API 都适用于此设置。 Just change the address to http://127.0.0.1:8081/bot /METHOD_NAME reference: https://core.telegram.org/bots/api只需将地址更改为http://127.0.0.1:8081/bot /METHOD_NAME 参考: 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM