-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
403 lines (334 loc) · 16.2 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import vkapi
import pymongo
import sqlite3
import json
import random
client = pymongo.MongoClient()
users = client.bot.users
def message_processing(user_id, user_message):
if (users.find_one({"user_id":user_id})):
user = users.find_one({"user_id":user_id})
if user["current_action"] == "none":
if(user_message=="/recreate_me"):
recreate_user(user_id)
vkapi.send_message(user_id=user_id, message=f"Твой прогресс сброшен")
send_keyboard(user_id, "none")
elif(user_message=="/delete_me"):
delete_user(user_id)
vkapi.send_message(user_id, f"Ты удален из базы пользователей")
send_keyboard(user_id, "start")
elif user_message in ["учить слова", "учить слова💡"]:
change_action(user_id, "learning")
change_stage(user_id, 0)
vkapi.send_message(user_id,"Ты начал учить слова")
word_en, word_ru = get_word(user_id,(user['last_showed_word_num']+1), user['used_db'])
vkapi.send_message(user_id, f"{change_string_font(word_en, 'big')} \n Перевод: {word_ru}")
send_keyboard(user_id, "learning")
elif user_message in ["мои слова", "мои слова📒"]:
words1 = get_words_from_memory(user_id,"memory1")
words2 = get_words_from_memory(user_id,"memory2")
s_words1 =""
s_words2=""
for w in words1:
s_words1 += (f"{w} — {get_translation_of_word(w)}\n")
for w in words2:
s_words2 += (f"{w} — {get_translation_of_word(w)}\n")
vkapi.send_message(user_id,f"Ты учишь эти слова: \n Память 1:\n {s_words1} \n Память 2:\n {s_words2}")
send_keyboard(user_id, "none")
elif user_message in ["повторять слова", "повторять слова🔁"]:
words = get_words_from_memory(user_id, "memory1")
if len(words)>=3:
vkapi.send_message(user_id, f"Повторение слов")
change_action(user_id, "repeating")
change_stage(user_id, 0)
set_repeating_words(user_id)
words = get_repeating_words(user_id)
word = words[0]
set_current_repeating_word(user_id, word)
words.remove(word)
random.shuffle(words)
words = words[0:2]
words.append(word)
random.shuffle(words)
set_other_words(user_id, words)
words_ru = []
for w in words:
words_ru.append(get_translation_of_word(w))
vkapi.send_message(user_id, f"Слово {change_string_font(word, 'big')}")
send_special_keyboard(user_id, "Выберите перевод", words_ru)
else:
vkapi.send_message(user_id, f"Нужно учить минимум 3 слова для повторения")
else:
vkapi.send_message(user_id,"Извини, я тебя не понимаю")
send_keyboard(user_id, "none")
elif user["current_action"]=="learning":
if user_message in ["уже знаю", "уже знаю🤓"]:
if user["current_stage_in_action"]<9:
inc_last_showed_word_num(user_id)
word_en, word_ru = get_next_user_word(user_id)
inc_current_stage_in_action(user_id)
vkapi.send_message(user_id, f"{change_string_font(word_en, 'big')} \n Перевод: {word_ru}")
send_keyboard(user_id, "learning")
else:
vkapi.send_message(user_id, f"10 слов пройдено")
inc_last_showed_word_num(user_id)
send_keyboard(user_id, "none")
change_action(user_id, "none")
change_stage(user_id, 0)
elif user_message in ["учить слово", "учить слово🤔"] :
word_en, word_ru = get_next_user_word(user_id)
add_word_to_memory(user_id, "memory1", word_en)
inc_current_stage_in_action(user_id)
if user["current_stage_in_action"]<9:
inc_last_showed_word_num(user_id)
word_en, word_ru = get_next_user_word(user_id)
vkapi.send_message(user_id, f"{change_string_font(word_en, 'big')} \n Перевод: {word_ru}")
send_keyboard(user_id, "learning")
else:
inc_last_showed_word_num(user_id)
vkapi.send_message(user_id, f"10 слов пройдено")
send_keyboard(user_id, "none")
change_action(user_id, "none")
change_stage(user_id, 0)
elif user_message in ["стоп", "стоп 🛑"]:
vkapi.send_message(user_id,"Изучение слов окончено")
send_keyboard(user_id, "none")
change_action(user_id, "none")
change_stage(user_id, 0)
else:
vkapi.send_message(user_id, "Извини, я тебя не понимаю")
elif user['current_action']=="repeating":
if user_message in ["стоп", "стоп 🛑"]:
vkapi.send_message(user_id,"Повторение слов окончено")
send_keyboard(user_id, "none")
change_action(user_id, "none")
change_stage(user_id, 0)
else:
if user_message == get_translation_of_word(get_current_repeating_word(user_id)):
word=get_current_repeating_word(user_id)
vkapi.send_message(user_id, f"Всё, верно, следущее слово")
remove_word_from_memory(user_id, "memory1", word)
add_word_to_memory(user_id, "memory2", word)
words = get_words_from_memory(user_id, "memory1")
if len(words)>=3:
set_repeating_words(user_id)
words = get_repeating_words(user_id)
word = words[0]
set_current_repeating_word(user_id, word)
words.remove(word)
random.shuffle(words)
words = words[0:2]
words.append(word)
random.shuffle(words)
set_other_words(user_id, words)
words_ru = []
for w in words:
words_ru.append(get_translation_of_word(w))
vkapi.send_message(user_id, f"Слово {change_string_font(word, 'big')}")
send_special_keyboard(user_id, "Выбери перевод", words_ru)
else:
change_action(user_id,"none")
change_stage(user_id, 0)
vkapi.send_message(user_id, f"Изучи больше слов для повторения ")
change_stage(user_id, 0)
change_action(user_id, "none")
send_keyboard(user_id,"none")
else:
vkapi.send_message(user_id, f"Ты ошибся")
set_repeating_words(user_id)
words = get_repeating_words(user_id)
word = words[0]
set_current_repeating_word(user_id, word)
words.remove(word)
random.shuffle(words)
words = words[0:2]
words.append(word)
random.shuffle(words)
set_other_words(user_id, words)
words_ru = []
for w in words:
words_ru.append(get_translation_of_word(w))
vkapi.send_message(user_id, f"Слово {change_string_font(word, 'big')}")
send_special_keyboard(user_id, "Выберите перевод", words_ru)
else:
create_user(user_id)
vkapi.send_message(user_id=user_id, message=f"Привет, новый пользователь id'{user_id}', ты успешно зарегестрирован\n Время учить английский! ")
send_keyboard(user_id, "none")
def create_user(user_id):
users.insert_one({"user_id":user_id,"current_action":"none","current_stage_in_action":0, "used_db":"words1", "last_showed_word_num":1, "memory1":[], "memory2":[], "memory3":[], "memory4":[], "memory5":[], "repeating_words":[], "other_words":[], "current_repeating_word":""})
print(f"user {user_id} was created")
def delete_user(user_id):
print(f"user {user_id} was deleted")
users.delete_one({"user_id":user_id})
def recreate_user(user_id):
delete_user(user_id)
create_user(user_id)
def change_action(user_id, new_action):
users.update({"user_id":user_id},{"$set":{"current_action":new_action}})
def change_stage(user_id, new_stage):
users.update({"user_id":user_id},{"$set":{"current_stage_in_action":new_stage}})
def get_word(user_id, word_id, table):
conn = sqlite3.connect("./words/words.db")
cursor = conn.cursor()
cursor.execute(f"SELECT word_en FROM words2 WHERE rowid = {word_id}")
word_en = cursor.fetchone()
cursor.execute(f"SELECT word_ru FROM words2 WHERE rowid = {word_id}")
word_ru = cursor.fetchone()
if word_en:
return word_en[0], word_ru[0]
else:
user=users.find_one({"user_id":user_id})
inc_last_showed_word_num(user_id)
return get_word(user_id, user['last_showed_word_num'],table)
def get_next_user_word(user_id):
user = users.find_one({"user_id":user_id})
return get_word(user_id,(user['last_showed_word_num']+1), user['used_db'])
def inc_last_showed_word_num(user_id):
users.update({"user_id": user_id}, {"$inc":{"last_showed_word_num":1}})
def inc_current_stage_in_action(user_id):
users.update({"user_id": user_id}, {"$inc":{"current_stage_in_action":1}})
def add_word_to_memory(user_id,memory,word):
users.update({"user_id":user_id}, {"$push":{memory : word}})
print(f"Word {word} was added to {memory} of user {user_id}")
def get_words_from_memory(user_id, memory):
user = users.find_one({"user_id":user_id})
words = user[memory]
return words
def remove_word_from_memory(user_id, memory, word):
user = users.find_one({"user_id":user_id})
words = user[memory]
words.remove(word)
users.update({"user_id":user_id}, {"$set":{memory : words}})
def get_translation_of_word(word_en):
conn = sqlite3.connect("./words/words.db")
cursor = conn.cursor()
cursor.execute(f"SELECT word_ru FROM words2 WHERE word_en = '{word_en}'")
word_ru = cursor.fetchone()[0]
return word_ru
def set_repeating_words(user_id):
words = get_words_from_memory(user_id, "memory1")
random.shuffle(words)
users.update({"user_id":user_id}, {"$set":{"repeating_words" : words}})
def get_repeating_words(user_id):
user = users.find_one({"user_id":user_id})
words = user["repeating_words"]
return words
def set_other_words(user_id, words):
users.update({"user_id":user_id}, {"$set":{"other" : words}})
def get_other_words(user_id):
user = users.find_one({"user_id":user_id})
words = user["other_words"]
return words
def set_current_repeating_word(user_id, word):
users.update({"user_id":user_id}, {"$set":{"current_repeating_word" : word}})
def get_current_repeating_word(user_id):
user = users.find_one({"user_id":user_id})
return user['current_repeating_word']
def send_keyboard(user_id, keyboard_name):
vkapi.send_message(user_id=user_id, message="Твоё действие: ", keyboard=json.dumps(get_keyboard(keyboard_name)))
def change_string_font(s, font):
if font=="bold":
s1=""
for c in s:
s1+=chr(ord(c)+120205)
return s1
elif font=="big":
s1=""
for c in s:
s1+=chr(ord(c)+120283)
return s1
else:
return s
def get_keyboard(keyboard_name):
keyboard_learning={
"one_time": False,
"buttons": [
[{
"action": {
"type": "text",
"label": "Учить слово🤔"
},
"color": "default"
},
{
"action": {
"type": "text",
"label": "Уже знаю🤓"
},
"color": "default"
}
],
[
{
"action":{
"type": "text",
"label":"Стоп 🛑"
},
"color":"default"
}
]
]
}
keyboard_none={
"one_time": False,
"buttons": [
[{
"action": {
"type": "text",
"label": "Учить слова💡"
},
"color": "default"
},
{
"action": {
"type": "text",
"label": "Мои слова📒"
},
"color": "default"
}],[
{
"action":{
"type":"text",
"label":"Повторять слова🔁"
}
}
]
]
}
keyboard_start={
"one_time": False,
"buttons": [
[{
"action": {
"type": "text",
"label": "Начать🏎️"
},
"color": "positive"
}]
]
}
if keyboard_name=="learning":
return keyboard_learning
elif keyboard_name=="start":
return keyboard_start
elif keyboard_name=="none":
return keyboard_none
def send_special_keyboard(user_id, message, words):
keyboard={
"one_time": False,
"buttons": [
[],
[
{
"action":{
"type": "text",
"label":"Стоп 🛑"
},
"color":"default"
}
]
]
}
for word in words:
keyboard["buttons"][0].append({"action":{"type":"text","label":word}})
vkapi.send_message(user_id=user_id, message=message, keyboard=json.dumps(keyboard))