-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
61 lines (39 loc) · 1.7 KB
/
bot.py
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
from typing import Final
from telegram import Update, InputFile
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from pytube import YouTube
TOKEN : Final = 'your_api_token_here'
BOT_USERNAME: Final = '@your-bot_username_here'
# Commands
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello! Welcome to my bot! Please send the link of the desired YouTube video to get started")
#Responses
def handle_response(link):
yt = YouTube(link)
audio = yt.streams.filter(only_audio=True, file_extension='mp4').order_by('abr').desc().first()
print(audio)
stream = audio.download()
return stream
# Logs incase if errors
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
message_type: str = update.message.chat.type
link = update.message.text
print(f'User ({update.message.chat.id}) in {message_type}: "{link}"')
response = handle_response(link)
print("Bot: ", response)
audio_file = InputFile(response)
await context.bot.send_audio(chat_id=update.message.chat.id, audio=response)
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f'Update {update} caused error {context.error}')
if __name__ == '__main__':
print('Starting bot...')
app = Application.builder().token(TOKEN).build()
#Commands
app.add_handler(CommandHandler('start', start_command))
#Messages
app.add_handler(MessageHandler(filters.TEXT, handle_message))
#Errors
app.add_error_handler(error)
#Polling (Checks for new messages from users)
print('Polling...')
app.run_polling(poll_interval=3)