Reputation: 75
I am writing a telegram bot to download a video from YouTube, I use the youtube-dl api, and when I try to get a video as response, but the video simply downloads to the root directory, but the script doesnt send it to the user, it gives me an error.
Error type:
Test video sending
[youtube] En8go1kP3rg: Downloading webpage
[download] Destination: DJ SMASH feat Po�t - (REMIX 2020) TOP Xit-En8go1kP3rg.mp4
[download] 100% of 7.47MiB in 00:01
(node:10996) UnhandledPromiseRejectionWarning: ReferenceError: output is not defined
at TelegramBot.<anonymous> (C:\Users\MR.ROBOT\Desktop\TM-Bot\index.js:29:31)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10996) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10996) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
There is my code:
const telegramApi = require("node-telegram-bot-api");
const token = "myApi";
const fs = require("fs");
const youtubedl = require("youtube-dl-exec");
const bot = new telegramApi(token, { polling: true });
const start = () => {
bot.on("message", async (msg) => {
const text = msg.text;
const chatId = msg.chat.id;
const mode = "youtube";
if (text === "/start") {
await bot.sendSticker(
chatId,
"https://tlgrm.ru/_/stickers/b50/063/b5006369-8faa-44d7-9f02-1ca97d82cd49/1.webp"
);
await bot.sendMessage(chatId, "Пошла жара...");
}
if (mode === "youtube") {
console.log("Test video sending");
const video = await youtubedl(text, {
noWarnings: true,
preferFreeFormats: true,
}).then((output) => console.log(output));
bot.sendMessage(chatId, output);
}
});
};
start();
What am I doing wrong?
P.S. Ignore the mode variable, this is in case I want to add other services besides youtube.
UPD.: If i write bot.sendMessage(chatId, output)
It gives a response in telegram:
[youtube] 7rlRet5t5pU: Downloading webpage
[download] Destination: miyagi & - (slowed + reverb)-7rlRet5t5pU.mp4
[download] 0.0% of 8.63MiB at 139.74KiB/s ETA 01:03[download] 0.0% of 8.63MiB at 367.33KiB/s ETA 00:24[download] 0.1% of 8.63MiB at 857.10KiB/s ETA 00:10[download] 0.2% of 8.63MiB at 1.79MiB/s ETA 00:04 [download] 0.4% of 8.63MiB at 1.90MiB/s ETA 00:04 [download] 0.7% of 8.63MiB at 2.15MiB/s ETA 00:03 [download] 1.4% of 8.63MiB at 3.14MiB/s ETA 00:02 [download] 2.9% of 8.63MiB at 4.31MiB/s ETA 00:01 [download] 5.8% of 8.63MiB at 5.62MiB/s ETA 00:01 [download] 11.6% of 8.63MiB at 7.51MiB/s ETA 00:01 [download] 23.2% of 8.63MiB at 9.09MiB/s ETA 00:00 [download] 46.3% of 8.63MiB at 10.05MiB/s ETA 00:00 [download] 92.6% of 8.63MiB at 10.44MiB/s ETA 00:00 [download] 100.0% of 8.63MiB at 10.55MiB/s ETA 00:00 [download] 100% of 8.63MiB in 00:01
If i write: bot.sendVideo(chatId, output)
Error in terminal:
Test video sending
[youtube] 7rlRet5t5pU: Downloading webpage
[download] Destination: miyagi & - (slowed + reverb)-7rlRet5t5pU.mp4
[download] 100% of 8.63MiB in 00:00
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: invalid file HTTP URL specified: Wrong URL host
at C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\node-telegram-bot-api\src\telegram.js:291:15
at tryCatcher (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:729:18)
at _drainQueueStep (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:15:14)
at processImmediate (internal/timers.js:461:21)
Upvotes: 2
Views: 1318
Reputation: 156
Did you try to use sendVideo
method? It support mp4 file format - see more here https://github.com/yagop/node-telegram-bot-api/blob/release/doc/api.md#TelegramBot+sendVideo
Also, the output
variable is undefined casue it is in different scope. You should put bot.sendX
call in the callback where you have a console.log
funciton, so:
}).then((output) => {
console.log(output)
bot.sendMessage(chatId, output); // or sendVideo
});
Upvotes: 1