Skip to content

Instantly share code, notes, and snippets.

@rainrisa
Created June 23, 2023 01:43
Show Gist options
  • Save rainrisa/7342bb6e1c35a3708633277a380519b0 to your computer and use it in GitHub Desktop.
Save rainrisa/7342bb6e1c35a3708633277a380519b0 to your computer and use it in GitHub Desktop.
Gramjs send file in chunk
import fs from "node:fs";
import { basename } from "node:path";
import bigInt from "big-integer";
import { Api } from "telegram";
import { generateRandomBytes, readBigIntFromBuffer } from "telegram/Helpers.js";
import { getAppropriatedPartSize } from "telegram/Utils.js";
import bot from "../bot.js";
type ProgressCallback = (chunkLength: number, uploaded: number, total: number) => void;
export default async function uploadFile(
filePath: string,
progressCallback?: ProgressCallback
): Promise<Api.InputFileBig> {
const fileId = readBigIntFromBuffer(generateRandomBytes(8), true, true);
const fileName = basename(filePath);
const fileSize = fs.statSync(filePath).size;
const defaultChunkSize = getAppropriatedPartSize(bigInt(fileSize)) * 1024;
const fileTotalParts = Math.ceil(fileSize / defaultChunkSize);
const file = fs.openSync(filePath, "r");
let partCount = 0;
let remainingSize = fileSize;
while (remainingSize > 0) {
const position = partCount * defaultChunkSize;
const chunkSize = remainingSize > defaultChunkSize ? defaultChunkSize : remainingSize;
const buffer = Buffer.alloc(chunkSize);
remainingSize -= chunkSize;
fs.readSync(file, buffer, { position, length: chunkSize });
await bot.invoke(
new Api.upload.SaveBigFilePart({
fileId,
filePart: partCount,
fileTotalParts,
bytes: buffer,
})
);
if (progressCallback) {
progressCallback(chunkSize, position, fileSize);
}
partCount++;
}
const uploadedFile = new Api.InputFileBig({
id: fileId,
parts: partCount,
name: fileName,
});
fs.closeSync(file);
return uploadedFile;
}
@rainrisa
Copy link
Author

Example of usage:

bot.addEventHandler(async (event) => {
  if (event.message.text.startsWith("/getmedia")) {
    const chatId = event.chatId!;
    const filePath = "./tmp/sample.mp4";
    const uploadedFile = await uploadFile(filePath, console.log);

    await bot.sendFile(chatId, { file: uploadedFile });
  }
}, new NewMessage({}));

@67373net
Copy link

67373net commented Feb 8, 2024

thanks for your code, it saved my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment