-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_bot.py
140 lines (114 loc) · 4.2 KB
/
main_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
from ast import parse
from setting import bot, sp, user_language
# import bot_en
# import bot_it
# Set the language for the bot
global start_txt #For /start
start_txt = ''
global top_track_txt #For /top
top_track_txt = ''
global change_lang_txt #When you use /start send a messager if you want change the language
change_lang_txt = ''
global info_album_txt
info_album_txt = ''
def check_and_set_language():
global start_txt
global top_track_txt
global change_lang_txt
global info_album_txt
if(user_language == "it"):
lan = "txt_it"
elif((user_language == "en") or (user_language.lower() == "none")):
lan = "txt_en"
f = open(f"{lan}/welcome.txt", "r")
start_txt = str(f.read())
f.close()
f = open(f"{lan}/top_track.txt", "r")
top_track_txt = str(f.read())
f.close()
f = open(f"{lan}/change_lang.txt", "r")
change_lang_txt = str(f.read())
f.close()
f = open(f"{lan}/info_album.txt", "r")
info_album_txt = str(f.read())
f.close()
@bot.message_handler(commands=['start'])
def bot_send_start(message):
check_and_set_language()
chat_id = message.chat.id
bot.send_message(chat_id, start_txt, parse_mode= "markdown")
bot.send_message(chat_id, change_lang_txt)
@bot.message_handler(commands=['top'])
def bot_top_tracks(message):
chat_id = message.chat.id
bot.send_message(chat_id, top_track_txt)
bot.register_next_step_handler(message, top_track)
@bot.message_handler(commands=['infoalbum'])
def bot_info_album_txt(message):
chat_id = message.chat.id
bot.send_message(chat_id, info_album_txt)
bot.register_next_step_handler(message, info_album)
# ================== TOP 5 TRACKS ==================
def top_track(message):
try:
chat_id_tmp = message.chat.id
input_text = str(message.text)
artist_id = search_artist_id(input_text)
results = sp.artist_top_tracks(artist_id)
except Exception as e:
bot.send_message(chat_id_tmp, "There was an error!")
raise e("Invalid text")
buffer = '' #Buffer for send the message
for idx, track in enumerate(results['tracks'][:5]):
buffer += str(idx + 1) + " - " + track['name'] + "\n"
bot.send_message(chat_id_tmp, buffer)
# ======================================================
# ================== INFO ALBUM ==================
def info_album(message):
try:
chat_id = message.chat.id
input_text = message.text
album_id = search_album_id(str(input_text))
results = sp.album(album_id)
except Exception as e:
bot.send_message(chat_id, "There was an error!")
raise e("Invalid text")
album_result = results
#Caption to send the message
name_album_txt = "Nome dell'album: " + str(album_result['name'])
total_tracks_txt = "Numero di tracce totali: " + str(album_result['total_tracks'])
release_data_txt = "Data di rilascio: " + str(album_result['release_date'])
caption = name_album_txt + "\n" + total_tracks_txt + "\n" + release_data_txt
image_url = album_result['images'][0]['url']
bot.send_photo(chat_id, image_url, caption)
# ======================================================
# Return the URI (a sort of ID) of the artist
def search_artist_id(artist_name) -> str:
results = sp.search(artist_name, limit= 1, type="artist")
items = results['artists']['items']
artist_item = items[0]
return(str(artist_item['uri']))
# Return the URI of the album
def search_album_id(nome_album) -> str:
results = sp.search(nome_album, limit= 1, type="album")
items = results['albums']['items']
album_item = items[0]
return(str(album_item['uri']))
# Bot commands for change the language
# Change the language to italian
@bot.message_handler(commands=['ita'])
def set_language(message):
# print("Set to italian")
bot.send_message(message.chat.id, "Lingua settata in italiano")
global user_language
user_language = 'it'
check_and_set_language()
# Change the language to english
@bot.message_handler(commands=['eng'])
def set_language(message):
#print("Set to english")
bot.send_message(message.chat.id, "Set english as language")
global user_language
user_language = 'en'
check_and_set_language()
bot.polling()