Skip to content

Instantly share code, notes, and snippets.

@Aqendo
Created October 28, 2022 20:14
Show Gist options
  • Save Aqendo/ed4b831ab6c838d08dc8091f0262688c to your computer and use it in GitHub Desktop.
Save Aqendo/ed4b831ab6c838d08dc8091f0262688c to your computer and use it in GitHub Desktop.
vidakbot alternative
const Telegraf = require('telegraf');
const youtubedl = require('youtube-dl-exec')
const bot = new Telegraf.Telegraf(process.env.BOT_TOKEN);
const fs = require('fs');
const path = require('path');
const findMyFile = (fileName) => {
const extensions = ['mp4', 'mkv', 'webm', 'unknown_video'];
for(const extension of extensions) {
const path1 = path.resolve(process.cwd(), `${fileName}.${extension}`);
if (fs.existsSync(path1)) {
return {
path1, extension, fileName
}
}
}
throw Error('No such files');
}
const reg = /[A-Za-z0-9]/g;
bot.on('text', async (ctx) => {
//if (!(ctx.message.from.id == YOUT_ID)) return;
try {
await youtubedl(ctx.message.text, {
noCheckCertificates: true,
preferFreeFormats: true,
format: 'bestvideo[height<=1080]+bestaudio/best[height<=1080]',
addHeader: [
'referer:youtube.com',
],
output:`${[...ctx.message.text.matchAll(reg)].join("")}.%(ext)s`
})
let file = findMyFile([...ctx.message.text.matchAll(reg)].join(""))
await ctx.replyWithVideo({source:file.path1})
} catch (e) {
await ctx.reply("no video tonight, the bot is broken\n" + e)
console.log(e)
}
try {
await fs.unlink(file.path1, async (err)=>{
await ctx.reply(err.text)
})
} catch (e) {
console.log
}
});
bot.launch();
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
{
"name": "ytdlbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"telegraf": "^4.10.0",
"youtube-dl-exec": "^2.1.7"
},
"devDependencies": {
"@types/node": "^18.11.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment