/TelegramBot-YoutubeDownloder

This bot will download youtube video.

Primary LanguageJavaScript

TelegramBot-YoutubeDownloder

Descrition
This is a telegram bot to download youTube videos. This bot will provide a download link for the video. This bot is non profitable and free to use for personal use.


How to use it?
1. Go to the telegram
2. Search for @YouTubeVideoDownloder665Bot or visit https://t.me/YouTubeVideoDownloder665Bot
3. Start the bot
4. To download any youtube video send /url VideoLink. Example: /url https://www.youtube.com/watch?v=hwNWx1GTSKo
5. Select which formate you want to download.
6. Click the downloadable link and download the video.


Requirement


1. Nodejs
2. Telegram account
3. Server for Hosting.

Dependence


1. Express
2. node-telegram-bot-api
3. dotenv
4. ytdl-core
5. ejs

Project workflow description


Create the bot
1. Go the telegram
2. Search for BotFather
3. /start
4. /newbot - create a new bot
5. Choose a name for the bot. Example: YouTube Video Downloader
6. Choose a username for the bot. Example: YouTubeVideoDownloder665Bot
7. Copy the token to access the HTTP API. Example: 59726595.............
8. /setcommands
9. Select the @YouTubeVideoDownloder665Bot
10. Send the command description.
Example:
help - Help
format - Format
url - URL


Create the server

npm init -y
npm i node-telegram-bot-api express dotenv ytdl-core ejs

Create .env file and paste the token from BotFather.

TELEGRAM_BOT_TOKEN=59726595.............
PORT=3008
NTBA_FIX_319=1

Create the .gitignore file

/node_modules
.env

Create the server

require("dotenv").config();
const express = require("express");
const app = express();

const port = process.env.PORT || 3008;
app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

Create the telegram bot

require("dotenv").config();
const express = require("express");
const app = express();
const ytdl = require("ytdl-core");
const TelegramBot = require("node-telegram-bot-api");

const token = process.env.TELEGRAM_BOT_TOKEN;
const bot = new TelegramBot(token, { polling: true });

const port = process.env.PORT || 3007;
app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

Define diffrenet routes

bot.onText(/\/start/, (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, "Welcome to YouTube video downloder");
});
bot.onText(/\/help/, (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(
    chatId,
    "Use the correct format to get the video. Example: /url https://www.youtube.com/watch?v=PKKibX_baoo"
  );
});
bot.on("message", (msg) => {
  if (!msg.text.includes("/")) {
    bot.sendMessage(msg.chat.id, "Sorry I don't understand...");
  }
});

Define getVideoUrl function

const getVideoUrl = async (url, chatId) => {
  let videosName = [];
  let videos = [];
  try {
    const info = await ytdl.getInfo(url);
    info.formats.forEach((element) => {
      if (element.hasVideo && element.hasAudio) {
        videosName.push("/format " + element.qualityLabel);
        videos.push(element);
      } else if (
        element.hasAudio &&
        !element.hasVideo &&
        element.mimeType.includes("audio/mp4")
      ) {
        videosName.push("/format " + "mp3");
        videos.push(element);
      }
    });

    bot
      .sendMessage(chatId, "Choose video or audio format", {
        reply_markup: {
          keyboard: [videosName],
        },
      })
      .then((res) => {
        bot.onText(/\/format/, (msg) => {
          const format = msg.text.replace("/format ", "");

          videos.forEach((element) => {
            if (format === "mp3" && element.qualityLabel === null) {
              bot.sendMessage(chatId, element.url);
              videosName = [];
              videos = [];
            } else if (element.qualityLabel === format) {
              bot.sendMessage(chatId, element.url);
              videosName = [];
              videos = [];
            }
          });
        });
      });
  } catch (error) {
    bot.sendMessage(chatId, "Sorry no video found...");
    console.log(error.message);
  }
};

bot.onText(/\/url/, async (msg) => {
  const chatId = msg.chat.id;
  const url = msg.text.replace("/url ", "");

  await getVideoUrl(url, chatId);
});

Finally run the project

node index.js