-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegrambot.py
More file actions
98 lines (78 loc) · 3.82 KB
/
telegrambot.py
File metadata and controls
98 lines (78 loc) · 3.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
import os
import re
from telegram import ForceReply, Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters, CallbackQueryHandler
import addfeed
import download
# Token del bot de Telegram
token = os.environ.get('TELEGRAM_TOKEN')
# Define a few command handlers. These usually take the two arguments update and context.
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
await update.message.reply_html(
rf"Hi {user.mention_html()}!",
reply_markup=ForceReply(selective=True),
)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
await update.message.reply_text("I need somebody!")
async def refresh_rss(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
result = addfeed.generate_rss()
await update.message.reply_html(
rf"{result}"
)
async def youtube(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
context.user_data['youtube_url'] = update.message.text
# El missatge conté "youtube.com"
if re.search(r"youtube\.com", update.message.text, re.IGNORECASE):
"""Sends a message with three inline buttons attached."""
keyboard = [
[
InlineKeyboardButton("Playlist", callback_data=f"playlist"),
InlineKeyboardButton("Single", callback_data=f"single"),
InlineKeyboardButton("Podcast", callback_data=f"podcast"),
InlineKeyboardButton("🚫", callback_data=f"res"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text("Què faig:", reply_markup=reply_markup)
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
await update.callback_query.answer()
url = context.user_data.get('youtube_url')
opcio = update.callback_query.data # Separar les dades
await update.callback_query.edit_message_text(text=f"Has escollit: {opcio}")
match opcio:
case "res":
pass
case "single":
path = 'music/single/%(title)s.%(ext)s'
result = await download.download_video(url, path)
case "playlist":
path = 'music/%(artist)s/%(album)s/%(playlist_index)s - %(title)s.%(ext)s'
result = await download.download_video(url, path)
case "podcast":
path = 'music/podcast/%(title)s.%(ext)s'
result = await download.download_video(url, path)
addfeed.generate_rss()
await context.bot.send_message(chat_id=update.callback_query.message.chat_id, text=f"{result}")
def main() -> None:
"""Start the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token(token).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("rss", refresh_rss))
application.add_handler(CallbackQueryHandler(button))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, youtube))
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()