Build a Telegram bot that downloads YouTube videos
This guide builds a working Telegram bot that turns any YouTube link into a video or MP3 — using one API call instead of a self-hosted yt-dlp stack.
What you will build
A bot where a user sends a YouTube link and gets the video back. No yt-dlp to maintain, no proxies, and no self-hosted Bot API server — Tunelio returns a direct URL and Telegram fetches it from there.
Prerequisites
- A Telegram bot token from @BotFather.
- A Tunelio API key (register with Telegram or email — 100 free credits, no card).
- Python 3.10+ with python-telegram-bot and requests installed.
The bot
Set BOT_TOKEN and TUNELIO_KEY as environment variables, then run this. Every text message is treated as a URL, sent to GET /create, and the returned link is handed straight to Telegram:
import os, requests
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes
TUNELIO = {"Authorization": f"Bearer {os.environ['TUNELIO_KEY']}"}
async def handle(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
url = update.message.text.strip()
r = requests.get(
"https://tunelio.dev/create",
params={"url": url, "quality": "720p"},
headers=TUNELIO,
).json()
if r.get("status") != "ok":
await update.message.reply_text("Could not download that link.")
return
await update.message.reply_video(r["url"], caption=r["filename"])
app = ApplicationBuilder().token(os.environ["BOT_TOKEN"]).build()
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle))
app.run_polling()Send audio (MP3) instead
Want an MP3 bot? Ask for quality=mp3 and reply with audio — the rest is identical:
r = requests.get(
"https://tunelio.dev/create",
params={"url": url, "quality": "mp3"},
headers=TUNELIO,
).json()
await update.message.reply_audio(r["url"], title=r["filename"])Why this beats a yt-dlp bot
- No 50 MB wall: you send a URL, so you skip the self-hosted Bot API server most yt-dlp bots need for large files.
- No maintenance: proxies, PO tokens, and updates are handled server-side.
- It stays working: no nightly yt-dlp break to wake up to.
Next steps
That is a complete bot. Read the API reference for /info (sizes and formats up front) and error handling, then grab your free credits and ship it.
Frequently asked questions
Do I need a self-hosted Telegram Bot API server?
For sending a URL, no. reply_video with the direct link lets Telegram fetch the file, which sidesteps the 50 MB bot-upload cap for most videos.
Can the same bot do audio and video?
Yes. Switch quality between a resolution (e.g. 720p) and mp3, and reply with reply_video or reply_audio accordingly.
How many downloads do the free credits cover?
Each /create costs 10 credits, so 100 free credits is about 10 downloads to test the bot before choosing a plan.