-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (98 loc) · 4.15 KB
/
main.py
File metadata and controls
129 lines (98 loc) · 4.15 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
import random
from telebot import types, TeleBot, custom_filters
from telebot.storage import StateMemoryStorage
from telebot.handler_backends import State, StatesGroup
print('Start telegram bot...')
state_storage = StateMemoryStorage()
token_bot = ''
bot = TeleBot(token_bot, state_storage=state_storage)
known_users = []
userStep = {}
buttons = []
def show_hint(*lines):
return '\n'.join(lines)
def show_target(data):
return f"{data['target_word']} -> {data['translate_word']}"
class Command:
ADD_WORD = 'Добавить слово ➕'
DELETE_WORD = 'Удалить слово🔙'
NEXT = 'Дальше ⏭'
class MyStates(StatesGroup):
target_word = State()
translate_word = State()
another_words = State()
def get_user_step(uid):
if uid in userStep:
return userStep[uid]
else:
known_users.append(uid)
userStep[uid] = 0
print("New user detected, who hasn't used \"/start\" yet")
return 0
@bot.message_handler(commands=['cards', 'start'])
def create_cards(message):
cid = message.chat.id
if cid not in known_users:
known_users.append(cid)
userStep[cid] = 0
bot.send_message(cid, "Hello, stranger, let study English...")
markup = types.ReplyKeyboardMarkup(row_width=2)
global buttons
buttons = []
target_word = 'Peace' # брать из БД
translate = 'Мир' # брать из БД
target_word_btn = types.KeyboardButton(target_word)
buttons.append(target_word_btn)
others = ['Green', 'White', 'Hello', 'Car'] # брать из БД
other_words_btns = [types.KeyboardButton(word) for word in others]
buttons.extend(other_words_btns)
random.shuffle(buttons)
next_btn = types.KeyboardButton(Command.NEXT)
add_word_btn = types.KeyboardButton(Command.ADD_WORD)
delete_word_btn = types.KeyboardButton(Command.DELETE_WORD)
buttons.extend([next_btn, add_word_btn, delete_word_btn])
markup.add(*buttons)
greeting = f"Выбери перевод слова:\n🇷🇺 {translate}"
bot.send_message(message.chat.id, greeting, reply_markup=markup)
bot.set_state(message.from_user.id, MyStates.target_word, message.chat.id)
with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
data['target_word'] = target_word
data['translate_word'] = translate
data['other_words'] = others
@bot.message_handler(func=lambda message: message.text == Command.NEXT)
def next_cards(message):
create_cards(message)
@bot.message_handler(func=lambda message: message.text == Command.DELETE_WORD)
def delete_word(message):
with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
print(data['target_word']) # удалить из БД
@bot.message_handler(func=lambda message: message.text == Command.ADD_WORD)
def add_word(message):
cid = message.chat.id
userStep[cid] = 1
print(message.text) # сохранить в БД
@bot.message_handler(func=lambda message: True, content_types=['text'])
def message_reply(message):
text = message.text
markup = types.ReplyKeyboardMarkup(row_width=2)
with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
target_word = data['target_word']
if text == target_word:
hint = show_target(data)
hint_text = ["Отлично!❤", hint]
next_btn = types.KeyboardButton(Command.NEXT)
add_word_btn = types.KeyboardButton(Command.ADD_WORD)
delete_word_btn = types.KeyboardButton(Command.DELETE_WORD)
buttons.extend([next_btn, add_word_btn, delete_word_btn])
hint = show_hint(*hint_text)
else:
for btn in buttons:
if btn.text == text:
btn.text = text + '❌'
break
hint = show_hint("Допущена ошибка!",
f"Попробуй ещё раз вспомнить слово 🇷🇺{data['translate_word']}")
markup.add(*buttons)
bot.send_message(message.chat.id, hint, reply_markup=markup)
bot.add_custom_filter(custom_filters.StateFilter(bot))
bot.infinity_polling(skip_pending=True)