-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
247 lines (216 loc) · 8.11 KB
/
main.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
from core.DB import Queries as Query
import core.config as c
from core.helper import *
from core.content import messages as msg, stickers as stk, desc
import telebot
bot = telebot.TeleBot(c.TOKEN)
@bot.message_handler(commands=['init'])
def handle_init(message):
"""Add new user to db.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
user = query.get_user_by_id(chat_id)
name = user['nickname']
bot.send_message(chat_id, msg['mes2'][user['lang']].format(name=name))
else:
bot.send_message(chat_id, "Начинаю сбор данных...")
lang = extract_arg(message.text)
if not lang:
bot.send_message(chat_id, "Выберите оин из предложеных языков\
переводов и установите как первый параметр после функции\
`/init UA` или `/init RU`.")
else:
if lang[0].upper() in c.languages:
language = lang[0].upper()
name = message.from_user.first_name + ' ' \
+ message.from_user.last_name
query.add_user(
{
"nickname": name,
"id": chat_id,
"lang": lang[0].upper(),
}
)
bot.send_message(chat_id,
msg["mes3"][language].format(name=name))
bot.send_message(chat_id, msg["mes1"][language])
else:
bot.send_message(chat_id, "К сожелению введенный вами\
язык не поддерщивается")
@bot.message_handler(commands=['start'])
def send_welcome(message):
"""Send welcome message after add bot t0 chat.
:param message: telebot message object
:return: void
"""
bot.send_message(message.from_user.id, "Hi, I'm very glad to see you!!!")
bot.send_message(message.from_user.id, "Please set your language: \
/init UA or /init RU")
bot.send_message(message.from_user.id, desc, parse_mode="HTML")
@bot.message_handler(commands=['help'])
def send_help(message):
"""Send message with command list and short guideline.
:param message: telebot message object
:return: void
"""
bot.send_message(message.from_user.id, desc, parse_mode="HTML")
@bot.message_handler(commands=['changelanguage'])
def change_language(message):
"""Change messages language UA or RU.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
lang = extract_arg(message.text)
if not lang:
bot.send_message(chat_id, "Выберите оин из предложеных языков переводов \
и установите как первый параметр после функции `/changelanguage \
UA` или `/changelanguage RU`.")
else:
if lang[0].upper() in c.languages:
language = lang[0].upper()
query.change_user_lang(chat_id, language)
bot.send_message(chat_id, msg["mes10"][language])
@bot.message_handler(commands=['freeze'])
def freeze(message):
"""Disable auto sending words for translation.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
user = query.get_user_by_id(chat_id)
query.change_user_status(chat_id, 0)
bot.send_message(chat_id, msg["mes8"][user['lang']])
@bot.message_handler(commands=['unfreeze'])
def unfreeze(message):
"""Enable auto sending words for translation.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
user = query.get_user_by_id(chat_id)
query.change_user_status(chat_id, 1)
query.change_carma(chat_id, True)
bot.send_message(chat_id, msg["mes9"][user['lang']])
@bot.message_handler(commands=['next'])
def handle_next(message):
"""Trigger sending words for translation.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
user = query.get_user_by_id(chat_id)
if user['w_id'] is not None:
query.update_user_word(
user['w_id'],
chat_id,
False
)
query.set_word_id(
{
"w_id": None,
"id": chat_id,
"time": user['send_time']
}
)
result = get_new_exercise(chat_id)
if result is False:
bot.send_message(chat_id, msg["mes6"][user['lang']])
else:
translations = str_to_list(result['t_' + user['lang'].lower()])
bot.send_message(chat_id, get_random(
msg["mes5"][user['lang']]).format(
word=get_random(translations)), parse_mode="HTML")
hint = result['hint_' + user['lang'].lower()]
if hint is not None:
bot.send_message(chat_id, get_random(
msg["mes11"][user['lang']]).format(
hint=hint), parse_mode="HTML")
@bot.message_handler(commands=['score'])
def handle_score(message):
"""Return score table.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
result = query.get_score()
table = "<b> SCORE LIST: </b> \n"
for i in result:
table += "<b>" + i['nickname'] + "</b>" + " : "\
+ "<i>" + str(i['score']) + "</i>" + "\n"
bot.send_message(chat_id, table, parse_mode="HTML")
else:
pass
@bot.message_handler(content_types=['text'])
def handle_message(message):
"""Check get words and send answer.
:param message: telebot message object
:return: void
"""
chat_id = message.from_user.id
query = Query()
if query.check_user(chat_id):
user = query.get_user_by_id(chat_id)
result = query.get_answer(chat_id)
if result['answer'] is None:
bot.reply_to(message, get_random(msg["mes4"][user['lang']]))
else:
get_answer = extract_answer(message.text)
if not get_answer:
pass
else:
if get_answer.lower() == result['answer'].lower():
query.update_user_word(
user['w_id'],
chat_id,
True
)
query.set_word_id(
{
"w_id": None,
"id": chat_id,
"time": user['send_time']
}
)
bot.send_sticker(
message.from_user.id, get_random(stk["YES"]))
query.change_carma(chat_id, True)
else:
query.update_user_word(
user['w_id'],
chat_id,
False
)
query.set_word_id(
{
"w_id": None,
"id": chat_id,
"time": user['send_time']
}
)
bot.send_sticker(
message.from_user.id, get_random(stk["NO"]))
bot.send_message(chat_id, msg["mes7"][user['lang']].format(
word=result['answer']), parse_mode="HTML")
else:
pass
"""
@bot.message_handler(content_types=['sticker'])
def handle_sticers(message):
print(message.sticker.file_id)
"""
bot.polling(none_stop=True, timeout=60)