-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
146 lines (107 loc) · 4.26 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
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
import os
import asyncio
from decouple import config
from telebot.async_telebot import AsyncTeleBot
import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
from utils.messages import Message as msg
from utils.downloader import downloader
from utils.yt_search import search as music_search
from utils.shazam_search import search as voice_search
TELEGRAM_TOKEN = config('TOKEN')
VOICE_DIR = config('VOICE_DIR')
AUDIO_DIR = config('AUDIO_DIR')
# stickers
sucess_st = "CAACAgIAAxkBAAIBFWKA-BbERtjxIIMk6WyBV5l8UOe6AAItAQACMNSdERCGBLkvnsTRJAQ"
error_st = "CAACAgIAAxkBAAIBGmKA-e1XoWNI3v9qMohWrXOfMyioAAIsAQACMNSdEbJU5DcKWA99JAQ"
search_st = "CAACAgIAAxkBAAIBHGKA-naJPWwqlEPnzCEq10MyaSXdAAIVAQACMNSdEW7uekmzNU5CJAQ"
bot = AsyncTeleBot(TELEGRAM_TOKEN, parse_mode=None)
# message sender
async def sender(chat_id, content, msg_id=None, parse_mode=None, f=None):
if msg_id:
return await bot.edit_message_text(text=content,
chat_id=chat_id,
message_id=msg_id,
parse_mode=parse_mode)
# send audio
if f:
return await bot.send_audio(chat_id, content)
return await bot.send_message(chat_id=chat_id,
text=content,
parse_mode=parse_mode)
# Send the welcome and help message
@bot.message_handler(commands=['start'])
async def send_welcome(message):
await bot.reply_to(message, msg.start.format(message.from_user.first_name))
# Send the welcome and help message
@bot.message_handler(commands=['help'])
async def send_welcome(message):
await bot.reply_to(message, msg.helpp)
# Handles all sent music title
@bot.message_handler(commands=['music'])
async def handle_music_search(message):
chat_id = message.chat.id
search = await sender(chat_id, msg.searching)
sticker = await bot.send_sticker(chat_id, search_st)
if message.text:
res = music_search(message.text)
if res[0] == False:
err = res[1]
await sender(chat_id, msg.err[err], search.message_id)
await bot.delete_message(chat_id, sticker.message_id)
sticker = await bot.send_sticker(chat_id, error_st)
else:
await bot.delete_message(chat_id, sticker.message_id)
text = f"{res[2]}\n{res[1]}"
await sender(chat_id, text, search.message_id)
sticker = await bot.send_sticker(chat_id, sucess_st)
download = await sender(chat_id, msg.downloading)
file_path = downloader(AUDIO_DIR, res[2], res[1])
if file_path:
with open(file_path, 'rb') as file:
await bot.delete_message(chat_id, sticker.message_id)
await bot.delete_message(chat_id, download.message_id)
await sender(message.chat.id, file, f=True)
# DELETE DOWLOANDED AUDIO FILE
os.remove(file_path)
else:
await sender(chat_id, msg.err['not_found'], search.message_id)
await bot.delete_message(chat_id, sticker.message_id)
sticker = await bot.send_sticker(chat_id, error_st)
# Handles all sent voice
@bot.message_handler(content_types=['voice'])
async def handle_voice(message):
chat_id = message.chat.id
search = await sender(chat_id, msg.searching)
sticker = await bot.send_sticker(chat_id, search_st)
file_info = await bot.get_file(message.voice.file_id)
file = await bot.download_file(file_info.file_path)
file_name = file_info.file_id + ".oga"
with open(VOICE_DIR+file_name, 'wb') as new_file:
new_file.write(file)
res = await voice_search(VOICE_DIR, file_name)
# REMOVE DOWNLOADED VOICE FILE
os.remove(VOICE_DIR+file_name)
if res[0] == False:
await bot.delete_message(chat_id, sticker.message_id)
err = res[1]
await sender(chat_id, msg.err[err], search.message_id)
sticker = await bot.send_sticker(chat_id, error_st)
else:
await bot.delete_message(chat_id, sticker.message_id)
text = f"{res[2]}\n{res[1]}"
await sender(chat_id, text, search.message_id)
sticker = await bot.send_sticker(chat_id, sucess_st)
download = await sender(chat_id, msg.downloading)
file_path = downloader(AUDIO_DIR, res[2], res[1])
if file_path:
with open(file_path, 'rb') as file:
await bot.delete_message(chat_id, sticker.message_id)
await bot.delete_message(chat_id, download.message_id)
await sender(message.chat.id, file, f=True)
# DELETE DOWLOANDED AUDIO FILE
os.remove(file_path)
def main():
asyncio.run(bot.infinity_polling())
if __name__ == "__main__":
main()