forked from python-telegram-bot/python-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path360
More file actions
38 lines (31 loc) · 1.31 KB
/
360
File metadata and controls
38 lines (31 loc) · 1.31 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
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from pytube import YouTube
import os
# Function to start the bot
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello! Send me a YouTube link, and I will download the video for you.')
# Function to handle messages
def handle_message(update: Update, context: CallbackContext) -> None:
url = update.message.text
try:
yt = YouTube(url)
stream = yt.streams.get_highest_resolution()
output_path = 'downloads/'
if not os.path.exists(output_path):
os.makedirs(output_path)
stream.download(output_path=output_path, filename=f'{yt.title}.mp4')
update.message.reply_text(f'Download completed: {yt.title}.mp4')
except Exception as e:
update.message.reply_text(f'Error: {str(e)}')
# Main function to run the bot
def main() -> None:
# Replace 'YOUR_TOKEN_HERE' with your actual bot token
updater = Updater("YOUR_TOKEN_HERE")
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()