-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotbot.py
232 lines (196 loc) · 9.57 KB
/
spotbot.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import threading
import asyncio
import concurrent.futures
import pickle
from spotdl import Spotdl, Song
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
# File to store the allowed_ids and admin_id
pickle_file = 'spotbot_config.pkl'
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
# Initialize allowed_ids and admin_id from pickle if available, else start fresh
if os.path.exists(pickle_file):
with open(pickle_file, 'rb') as f:
data = pickle.load(f)
allowed_ids = data.get('allowed_ids', [])
admin_id = data.get('admin_id', None)
spotdl_client_id = data.get('spotdl_client_id', '5f573c9620494bae87890c0f08a60293')
spotdl_client_secret = data.get('spotdl_client_secret', '212476d9b0f3472eaa762d90b19b0ba8')
telegram_bot_token = data.get('telegram_bot_token', None)
else:
allowed_ids = []
admin_id = None
spotdl_client_id = '5f573c9620494bae87890c0f08a60293'
spotdl_client_secret = '212476d9b0f3472eaa762d90b19b0ba8'
telegram_bot_token = None
def save_allowed_ids():
data = {'allowed_ids': allowed_ids,
'admin_id': admin_id,
'spotdl_client_id': spotdl_client_id,
'spotdl_client_secret': spotdl_client_secret,
'telegram_bot_token': telegram_bot_token,
}
with open(pickle_file, 'wb') as f:
pickle.dump(data, f)
def start_spotdl():
spotdl_instance = Spotdl(
client_id=spotdl_client_id,
client_secret=spotdl_client_secret
)
return spotdl_instance
spotdl = None
def run_spotdl_operations(link, mode=''):
global spotdl
if not spotdl:
spotdl = start_spotdl()
songs = spotdl.search([link])
if mode == 'search':
return songs
else:
if songs:
results = spotdl.download_songs(songs)
return results
else:
return []
# Function to handle new users
def handle_new_user(update: Update, context: CallbackContext):
user_id = update.message.from_user.id
user_name = update.message.from_user.username
# Send a message to the admin to approve the user
admin_message = f"New user: @{user_name} (ID: {user_id}). Do you want to allow this user?"
keyboard = [
[InlineKeyboardButton("Yes", callback_data=f'approve_{user_id}'),
InlineKeyboardButton("No", callback_data=f'deny_{user_id}')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(chat_id=admin_id, text=admin_message, reply_markup=reply_markup)
update.message.reply_text("Your request to use this bot has been sent to the admin. Please wait.")
def start(update: Update, context: CallbackContext):
update.message.reply_text("Hello!\nSend a Spotify link \nor\nsearch 'Artist - Song' ")
def button_click(update: Update, context: CallbackContext):
query = update.callback_query
query.answer()
user_id = int(query.data.split('_')[-1])
if query.data.startswith('search_result_yes'):
link = query.data.split('_')[-2]
query.edit_message_text(text=f'Downloading the song...')
future = executor.submit(run_spotdl_operations, link)
songs = future.result()
if songs:
for item in songs:
song = item[0]
name = song.artist + ' - ' + song.name
mp3_file_path = item[1]
if os.path.exists(mp3_file_path):
with open(mp3_file_path, 'rb') as audio_file:
if len(songs) == 1:
context.bot.send_photo(user_id, song.cover_url, caption=name)
context.bot.send_audio(chat_id=user_id, audio=audio_file)
os.remove(mp3_file_path)
elif query.data.startswith('search_result_no'):
query.edit_message_text(text=f'Download cancelled.')
elif query.data.startswith('approve'):
allowed_ids.append(user_id)
save_allowed_ids()
context.bot.send_message(chat_id=user_id, text='Your request has been approved. You can now use the bot.')
query.edit_message_text(text=f'User {user_id} approved!')
elif query.data.startswith('deny'):
context.bot.send_message(chat_id=user_id, text='Your request to use this bot has been denied.')
query.edit_message_text(text=f'User {user_id} denied access.')
def search_and_confirm(update: Update, context: CallbackContext, artist, name, link):
user_id = update.message.from_user.id
search_result = f"{artist} - {name}"
keyboard = [
[InlineKeyboardButton("Yes", callback_data=f'search_result_yes_{link}_{user_id}'),
InlineKeyboardButton("No", callback_data=f'search_result_no_{user_id}')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(f"Search result:\n{search_result}.\n\n Is this correct?", reply_markup=reply_markup)
def handle_messages(update: Update, context: CallbackContext):
text = update.message.text
user_id = update.message.from_user.id
if user_id not in allowed_ids and user_id != admin_id:
handle_new_user(update, context)
return
if text.startswith('https://open.spotify.com/') or text.startswith('https://spotify.link/'):
try:
update.message.reply_text("Processing the link you sent.")
future = executor.submit(run_spotdl_operations, text)
songs = future.result()
if songs:
for item in songs:
song = item[0]
name = song.artist + ' - ' + song.name
mp3_file_path = item[1]
if os.path.exists(mp3_file_path):
with open(mp3_file_path, 'rb') as audio_file:
if len(songs) == 1:
context.bot.send_photo(user_id, song.cover_url, caption=name)
context.bot.send_audio(chat_id=user_id, audio=audio_file)
os.remove(mp3_file_path)
else:
update.message.reply_text("Unable to download songs from the Spotify link you sent.")
except:
update.message.reply_text("This song is not downloadable, sorry!")
elif '-' in text:
future = executor.submit(run_spotdl_operations, text, 'search')
song = future.result()[0]
search_and_confirm(update, context, song.artist, song.name, text)
else:
update.message.reply_text("Wrong link! This bot is only for downloading songs from Spotify!")
def list_allowed_users(update: Update, context: CallbackContext):
if update.message.from_user.id == admin_id:
allowed_users_info = ""
for user_id in allowed_ids:
user = context.bot.get_chat(user_id)
allowed_users_info += f"User ID: {user_id}, Username: @{user.username}\n" if user.username else f"User ID: {user_id}\n\n"
if allowed_users_info:
update.message.reply_text("List of allowed users:\n" + allowed_users_info)
else:
update.message.reply_text("No users are currently allowed.")
else:
update.message.reply_text("You are not authorized to perform this action.")
def delete_user(update: Update, context: CallbackContext):
if update.message.from_user.id == admin_id:
user_id = int(context.args[0])
if user_id in allowed_ids:
user = context.bot.get_chat(user_id)
allowed_ids.remove(user_id)
save_allowed_ids()
update.message.reply_text(f"User ID {user_id}, Username: @{user.username}, has been removed from the allowed list." if user.username else f"User ID {user_id} has been removed from the allowed list.")
else:
update.message.reply_text(f"User ID {user_id} is not in the allowed list.")
else:
update.message.reply_text("You are not authorized to perform this action.")
def send_message_to_users(update: Update, context: CallbackContext):
message = update.message.text[9:]
if update.message.from_user.id == admin_id and allowed_ids:
for user_id in allowed_ids:
context.bot.send_message(user_id, message)
update.message.reply_text(f'Message sent:\n{message}')
else:
update.message.reply_text("You are not authorized to perform this action.")
def main():
global allowed_ids, admin_id, spotdl_client_id, spotdl_client_secret, telegram_bot_token
if admin_id is None:
admin_id = int(input("Enter the admin's Telegram user ID: "))
telegram_bot_token = input("\nEnter Telegram Bot Token from @Botfather: ")
spotify_cred = input("\nDo you want to use your own spotify credentials (y/n): ")
if spotify_cred == 'y':
spotdl_client_id = input("\n Enter your Spotify client ID: ")
spotdl_client_secret = input("\n Enter your Spotify client secret: ")
save_allowed_ids()
updater = Updater(token=telegram_bot_token, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("listusers", list_allowed_users))
dispatcher.add_handler(CommandHandler("deleteuser", delete_user, pass_args=True))
dispatcher.add_handler(CommandHandler("message", send_message_to_users, pass_args=True))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_messages))
dispatcher.add_handler(CallbackQueryHandler(button_click))
print("Bot is running...")
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()