yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.34k stars 1.52k forks source link

sendDocument issue #1149

Open xffc opened 11 months ago

xffc commented 11 months ago

I have read:

I am using the latest version of the library.

Expected Behavior

Send tester.log file.

Actual Behavior

It gives me error Unhandled rejection Error: ETELEGRAM: 400 Bad Request: invalid file HTTP URL specified: Wrong port number specified in the URL

Example

let name = "tester";
bot.sendDocument(chatId, fs.readFileSync(`${name}.log`, "utf-8"), {caption: "logs"});
klaus-10 commented 10 months ago

Here the problem is that the sendDocument accepts only "A file path, Stream or Buffer" while you are passing the contents of the file using fs.readFileSync, which reads the file and returns its content as a string and the sendDocument interpret it as a path. To pass the ".log" file you should use "fs.createReadStream("path_to_the_log_file");" or the directly the path of the file ".sendDocument(chatId, ${__dirname}/path_to_the_log_file)". There is still a problem using the Buffer because the fileType don't support the ".log" extention. For this reason you should enable manually the NTBA_FIX_350 inside env file as "NTBA_FIX_350=1". This would enable the library to auto-detect the file extension from the provided filename. So in your case you should be:

bot.sendDocument(chatId, fs.readFileSync(${name}.log), {caption: "logs"}, { filename: ${name}.log});

In this solution remember to specify the "filename" otherwise remove NTBA_FIX_350 from your env file.

ronak2898 commented 8 months ago

image

let name = "tester"; await bot.sendDocument(chatId, fs.readFileSync(${name}.log), {caption: "logs"});

Write await if you want to wait and remove encoding option "utf-8" from readFileSync then you good to go 🙌