-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbot.py
More file actions
69 lines (46 loc) · 1.67 KB
/
bot.py
File metadata and controls
69 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os, re
import threading
import telebot
from dotenv import load_dotenv
load_dotenv()
from modules.checker import youtube_regex
from modules.downloader import download_video
TOKEN = os.getenv("BOT_API_KEY")
bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
# '/start' command reply
@bot.message_handler(commands=["start"])
def send_welcome(message):
bot.reply_to(
message,
"Hello, I'm a <b>Simple Youtube Downloader!👋</b>\n\nTo get started, just type the /help command.",
)
# '/help' command reply
@bot.message_handler(commands=["help"])
def send_help(message):
bot.reply_to(
message,
"""
<b>Just send me your video link.</b> ▶️
<i>Source: <a href="https://github.com/hansanaD/TelegramYTDLBot">TelegramYTDLBot</a> by <a href="https://github.com/DevHanza/">DevHanza</a></i>
""",
disable_web_page_preview=True,
)
# Youtube Link Listener
@bot.message_handler(func=lambda m: True)
def on_yt_link(message):
# run download in background so bot stays responsive
threading.Thread(target=yt_link_handler, args=(message,)).start()
def yt_link_handler(message):
matches = re.findall(youtube_regex, message.text)
if matches:
url = matches[0]
status_msg = bot.reply_to(message, f"Starting to download..")
try:
file_path = download_video(url, bot, message.chat.id, status_msg.message_id)
with open(file_path, "rb") as file:
bot.send_video(message.chat.id, file)
os.remove(file_path)
except Exception as e:
bot.reply_to(message, f"❌ Error: {e}")
print("TelegramYTDLBot is running..\n")
bot.infinity_polling()