This repository was archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
273 lines (254 loc) · 11.1 KB
/
main.py
File metadata and controls
273 lines (254 loc) · 11.1 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Leo da Vinci
#
# Volpe Stefano (5Bsa)
# 16/02/2020
# Python 3.8.1
import json
import os.path
import sys
import telebot
from telebot import types
token_filename = 'token.txt'
states_filename = 'states.dat'
topics_filename = '5Bsa.dat'
def check_topic(topic):
if not ('name' in topic and 'tags' in topic and 'subtopics' in topic):
print("Errore:")
print(topic)
return False
for x in topic['subtopics']:
if not check_topic(x):
return False
return True
def update_states(states):
with open(states_filename, 'w') as states_file:
states_file.write(json.dumps(states))
def get_node(topics, state):
topics_copy = topics
for x in state:
for y in topics_copy["subtopics"]:
if y["name"] == x:
topics_copy = y
break
else:
print("Topics: ", topics_copy, "\nState: ", x)
return None
return topics_copy
def get_links(topics, tags):
res = []
found = False
for x in topics["tags"]:
for y in tags:
if x == y:
res.append(topics["name"])
found = True
break
if found:
break
for x in topics["subtopics"]:
res.extend(get_links(x, tags))
return res
def node_to_string(node):
res = node["name"]
if not len(node["subtopics"]):
return res + " (nessun sottoargomento)"
for x in node["subtopics"]:
res += "\n - " + x["name"]
return res
def bot_send_subtopics(bot, message, topics, states):
bot.send_message(message.chat.id, node_to_string(get_node(topics, states[str(message.from_user.id)])))
def main():
# Token
if not os.path.isfile(token_filename):
print('File token "' + token_filename + '" non trovato.')
return
with open(token_filename) as token_file:
leo = telebot.TeleBot(token_file.readline())
# States
states = {}
if os.path.isfile(states_filename):
with open(states_filename) as states_file:
states = json.loads(states_file.read())
# Topics
topics = {}
if os.path.isfile(topics_filename):
with open(topics_filename) as topics_file:
topics = json.loads(topics_file.read())
if not check_topic(topics):
print('File argomenti "' + topics_filename + '" non valido.')
return
else:
print('File argomenti "' + topics_filename + '" non trovato.')
return
print('File argomenti "' + topics_filename + '" valido.')
@leo.message_handler(commands = ['start'])
def leo_reply_start(message):
sticker_filename = 'stickers/greetings.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
states[str(message.from_user.id)] = []
update_states(states)
leo.send_message(message.chat.id, 'Salve, suddito.')
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['help'])
def leo_reply_help(message):
sticker_filename = 'stickers/confused.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id,
'Mi reputo un sovrano illuminato, suddito.'
)
leo.send_message(message.chat.id,
'Digita "/" senza inviare il messaggio: vedrai un elenco di '
'comandi suggeriti.\n\nPrima di sceglierne uno, ricorda:\n'
' - gli elementi delle liste che ti mostro sono "argomenti"\n'
' - i titoli delle liste che ti mostro sono "argomenti attuali"\n'
' - (+ argomento) ti chiede di specificare il nome di un '
'argomento dalla lista al quale applicare il comando\n'
' - le parole chiave di un argomento sono "temi"\n'
' - due argomenti sono collegabili se e solo se hanno almeno '
'un tema in comune\n'
' - (+ tema) ti chiede di specificare un tema qualsiasi'
)
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['close'])
def leo_reply_close(message):
if len(states[str(message.from_user.id)]):
states[str(message.from_user.id)] = states[str(message.from_user.id)][:-1]
update_states(states)
else:
sticker_filename = 'stickers/dunno.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Proprio no, suddito...')
leo.send_message(message.chat.id,
'"' + topics['name'] + '", in quanto argomento-radice, non ha alcun '
'argomento che lo contenga.'
)
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['open'])
def leo_reply_open(message):
command = "/open " # terminating space is so important
argument = message.text[message.text.find(command) + len(command):]
node = get_node(topics, states[str(message.from_user.id)])
for x in node["subtopics"]:
if x["name"] == argument:
states[str(message.from_user.id)].append(argument)
update_states(states)
break
else:
sticker_filename = 'stickers/suspicious.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Cosa vai dicendo, suddito?')
leo.send_message(message.chat.id,
'Non ho trovato alcun argomento "' +
argument +
'" dentro l\'argomento attuale.' if len(argument) else
'Devi specificare un argomento quando usi questo comando.'
)
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['check'])
def leo_reply_check(message):
command = "/check " # terminating space is so important
argument = message.text[message.text.find(command) + len(command):]
node = get_node(topics, states[str(message.from_user.id)])
for x in node["subtopics"]:
if x["name"] == argument:
if len(x["tags"]):
leo.send_message(message.chat.id,
'Temi inerenti all\'argomento "' +
argument +
'":\n' +
str(x["tags"])
)
else:
leo.send_message(message.chat.id,
'Temi inerenti all\'\nargomento "' +
argument +
'":\nnessuno'
)
break
else:
sticker_filename = 'stickers/suspicious.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Cosa vai dicendo, suddito?')
leo.send_message(message.chat.id,
'Non ho trovato alcun argomento "' +
argument +
'" dentro l\'argomento attuale.' if len(argument) else
'Devi specificare un argomento quando usi questo comando.'
)
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['link'])
def leo_reply_link(message):
command = "/link " # terminating space is so important
argument = message.text[message.text.find(command) + len(command):]
node = get_node(topics, states[str(message.from_user.id)])
for x in node["subtopics"]:
if x["name"] == argument:
sticker_filename = 'stickers/fanfare.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Per te, suddito. Fanne buon uso!')
res = 'Collegamenti con l\'argomento "' + argument + '":'
links = get_links(topics, x["tags"])
if links:
for y in links:
if y != x["name"]:
res += '\n - ' + y
else:
res += '\nnessuno'
leo.send_message(message.chat.id, res)
break
else:
sticker_filename = 'stickers/suspicious.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Cosa vai dicendo, suddito?')
leo.send_message(message.chat.id,
'Non ho trovato alcun argomento "' +
argument +
'" dentro l\'argomento attuale.' if len(argument) else
'Devi specificare un argomento quando usi questo comando.'
)
bot_send_subtopics(leo, message, topics, states)
@leo.message_handler(commands = ['list'])
def leo_reply_list(message):
command = "/list " # terminating space is so important
argument = message.text[message.text.find(command) + len(command):]
node = get_node(topics, states[str(message.from_user.id)])
if argument:
sticker_filename = 'stickers/idea.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'In quanto re, sono sempre un passo avanti.')
res = 'Inerenti al tema "' + argument + '":'
links = get_links(topics, [argument])
if links:
for x in links:
res += '\n - ' + x
else:
res += '\nnessuno'
leo.send_message(message.chat.id, res)
else:
sticker_filename = 'stickers/suspicious.webp'
if os.path.isfile(sticker_filename):
with open(sticker_filename, 'rb') as sticker:
leo.send_sticker(message.chat.id, sticker)
leo.send_message(message.chat.id, 'Cosa vai dicendo, suddito?')
leo.send_message(message.chat.id, 'Devi specificare un argomento quando usi questo comando.')
bot_send_subtopics(leo, message, topics, states)
# TODO upload custom JSON
leo.polling()
if __name__ == '__main__':
main()