如何使用 Telegram Bot API 发送大文件?
Telegram bot 的文件大小限制为 50MB.
Telegram bot has a file size limit for sending in 50MB.
我需要发送大文件.有没有办法解决这个问题?
I need to send large files. Is there any way around this?
我知道这个项目 https://github.com/pwrtelegram/pwrtelegram 但我不能让它发挥作用.
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?
可以选择通过 Telegram API 实现文件上传,然后通过 file_id 与 bot 发送.
There is an option to implement the file upload via Telegram API and then send by file_id with bot.
我使用库 https://github.com/rubenlagus/TelegramBots 用 Java 编写了一个机器人
I write a bot in Java using the library https://github.com/rubenlagus/TelegramBots
更新
为了解决这个问题,我使用了电报 api,它对大文件有 1.5 GB 的限制.
For solve this problem i use telegram api, that has limit on 1.5 GB for big files.
我更喜欢 kotlogram - 具有良好文档的完美库 https://github.com/badoualy/kotlogram
I prefer kotlogram - the perfect lib with good documentation https://github.com/badoualy/kotlogram
更新 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();
}
}
其中 TelegramClient telegramClient
和 TLInputPeerChannel tlInputPeerChannel
您可以在文档中创建.
where TelegramClient telegramClient
and TLInputPeerChannel tlInputPeerChannel
you can create as write in documentation.
不要复制粘贴,根据您的需要重写.
DON'T COPY-PASTE, rewrite on your needs.
如果你想通过电报机器人发送文件,你有 三个选项:
IF you want to send file via telegram bot, you have three options:
- InputStream(照片限制为 10 MB,其他文件限制为 50 MB)
- 来自http url(Telegram 将下载并发送文件.照片最大5 MB,其他类型20 MB最大内容.)
- 按file_ids 发送缓存文件.(以这种方式发送的文件没有限制)
- InputStream (10 MB limit for photos, 50 MB for other files)
- 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.)
- Send cached files by their file_ids.(There are no limits for files sent this way)
因此,我建议您预先存储 file_ids 并通过这些 id 发送文件(这是由 api 文档).
So, I recommend you to store file_ids beforehand and send files by these ids (this is recommended by api docs too).