Creating a YouTube downloader Telegram bot requires careful adherence to the Telegram API rules and respective library documentation. To get started, you'll need a Telegram bot token (which you receive after creating a bot with the BotFather on Telegram), Python 3.6+, pyrogram, and yt-dlp libraries installed. Below you will find an example code to create a basic bot for this purpose.
Please be aware that running such a bot may violate YouTube's Terms of Service and copyright regulations, so proceed with caution and at your own risk.
First, install the necessary libraries using pip:
pip install pyrogram yt-dlp
Then, you can create your bot following this example code:
youtube_downloader_bot.py
from pyrogram import Client, filters, types
import yt_dlp
# Insert your own API ID, hash, and bot token from Telegram here.
api_id = "YOUR_API_ID"
api_hash = "YOUR_API_HASH"
bot_token = "YOUR_BOT_TOKEN"
app = Client("youtube_downloader_bot", api_id, api_hash, bot_token=bot_token)
# Command handler to start the bot
@app.on_message(filters.command("start"))
async def start(client, message):
await message.reply_text("Hello! Send me a YouTube URL to download.")
# Function to extract available formats
async def extract_formats(url):
ydl_opts = {
'listformats': True,
'quiet': True,
'no_warnings': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
return info_dict
# Command handler to process the YouTube URL
@app.on_message(filters.regex(r'(https?://[^\s]+)'))
async def youtube_url_handler(client, message):
url = message.matches[0].group(0)
info_dict = await extract_formats(url)
formats = info_dict.get("formats", [info_dict])
# Generate an inline keyboard with available formats
keyboard = []
for fmt in formats:
format_id = fmt.get('format_id', '')
format_note = fmt.get('format_note', '')
if format_note:
keyboard.append([types.InlineKeyboardButton(text=f"{format_note} - {format_id}", callback_data=f"{format_id}")])
reply_markup = types.InlineKeyboardMarkup(keyboard)
await message.reply_text("Please choose a quality:", reply_markup=reply_markup)
# Callback query handler to process the user's choice
@app.on_callback_query()
async def callback_query_handler(client, callback_query):
format_id = callback_query.data
message = callback_query.message.reply_to_message
if not message:
return
url = message.text
ydl_opts = {
'format': format_id,
'quiet': True,
'no_warnings': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# simulate the download to get the file name
info_dict = ydl.extract_info(url, download=False)
filename = ydl.prepare_filename(info_dict)
ydl.download([url])
await callback_query.edit_message_text("Download started...")
# Send the video file
with open(filename, 'rb') as video:
await client.send_video(chat_id=callback_query.from_user.id, video=video)
await callback_query.edit_message_text("Download completed!")
app.run()
This code initializes a simple pyrogram client with a YouTube downloader bot. Here's the flow:
- Users start the bot and send a YouTube URL.
- The bot extracts the available formats (qualities) for the given URL.
- An inline keyboard is provided with the available qualities so the user can choose.
- Once the user chooses a quality, the bot uses
yt-dlp to download the video and sends it to the user.
Make sure to replace YOUR_API_ID, YOUR_API_HASH, and YOUR_BOT_TOKEN with your actual values. You can obtain the API ID and hash from https://my.telegram.org and the bot token from the BotFather.
Note: This script may need further tuning to handle edge cases and proper error management for a production bot. This code is for educational purposes, and you should bear in mind API rate limits, error handling, and proper file management in your final implementation.