This is a Telegram bot that downloads media and videos from YouTube, Instagram, Facebook, and Twitter/X. Users can send a link to the bot, and it will download the video from the specified platform and send it back to the user.
- Detects the media platform from the provided URL.
- Downloads videos from YouTube, Instagram, Facebook, and Twitter/X.
- Sends the downloaded video back to the user in the chat.
- Supports links from major social media platforms.
- Python 3.6 or later
telebot
libraryyt-dlp
librarypytube
libraryFFmpeg
library
-
Clone the repository or download the code.
-
Install the required libraries:
pip install pyTelegramBotAPI yt-dlp pytube
-
Replace the placeholder token with your actual bot token from BotFather:
TOKEN = 'YOUR_TOKEN_HERE'
-
Install ffmpeg
sudo apt install ffmpeg -y
-
Start the bot by running the script:
python bot.py
-
Open Telegram and start a chat with your bot.
-
Send the
/start
command to receive a welcome message. -
Send a valid URL from YouTube, Instagram, Facebook, or Twitter/X.
-
The bot will download the video and send it back to you.
The function detect_media_platform
takes a URL as input and returns the name of the platform (YouTube, Instagram, Facebook, Twitter/X) based on the URL pattern.
The function download_video
takes a URL and platform name as input, downloads the video, and returns the path to the downloaded video.
- For YouTube, it uses the
pytube
library. - For other platforms, it uses the
yt-dlp
library.
The bot is initialized with the provided token using the telebot
library. It has handlers for the /start
command and for processing any message containing a URL.
When a message is received, the bot:
- Detects the media platform from the URL.
- Sends a message indicating that the video is being downloaded.
- Downloads the video.
- Sends the downloaded video back to the user.
- Deletes the video file from the local storage to save space.
import telebot
import re
import os
import yt_dlp
from pytube import YouTube
def detect_media_platform(url: str) -> str:
youtube_pattern = re.compile(r'(https?://)?(www\.)?(youtube|youtu\.be)(\.com)?/.*')
instagram_pattern = re.compile(r'(https?://)?(www\.)?instagram\.com/.*')
facebook_pattern = re.compile(r'(https?://)?(www\.)?facebook\.com/.*')
twitter_pattern = re.compile(r'(https?://)?(www\.)?(twitter|x)\.com/.*')
if youtube_pattern.match(url):
return "YouTube"
elif instagram_pattern.match(url):
return "Instagram"
elif facebook_pattern.match(url):
return "Facebook"
elif twitter_pattern.match(url):
return "Twitter/X"
else:
return "Unknown"
def download_video(url: str, platform: str) -> str:
output_path = "downloads"
os.makedirs(output_path, exist_ok=True)
if platform == "YouTube":
yt = YouTube(url)
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
stream.download(output_path)
return os.path.join(output_path, stream.default_filename)
else:
ydl_opts = {'outtmpl': os.path.join(output_path, '%(title)s.%(ext)s')}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
video_title = info_dict.get('title', None)
video_ext = info_dict.get('ext', None)
return os.path.join(output_path, f"{video_title}.{video_ext}")
# Replace 'YOUR_TOKEN_HERE' with your actual bot token
TOKEN = 'YOUR_TOKEN_HERE'
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Hello! Please provide your link!")
@bot.message_handler(func=lambda message: True)
def handle_message(message):
url = message.text
platform = detect_media_platform(url)
if platform == "Unknown":
bot.reply_to(message, "Unsupported or unknown URL. Please provide a valid link from YouTube, Instagram, Facebook, or Twitter/X.")
else:
bot.reply_to(message, f"Downloading video from {platform}...")
video_path = download_video(url, platform)
with open(video_path, 'rb') as video:
bot.send_video(message.chat.id, video)
os.remove(video_path)
if __name__ == "__main__":
bot.polling()
- GIF Creation: If you need to create a GIF for your bot, you can use the images generated in the discussions and combine them using an online GIF maker or software like Photoshop or GIMP.
- BotPic: To set a profile picture for your bot, use BotFather in Telegram.
If you have any questions or need further assistance, feel free to reach out!