-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
executable file
·321 lines (286 loc) · 9.18 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
import os
import logging
import telebot
import models
import views
BOT_TOKEN = os.environ.get('BOT_TOKEN')
ADMIN_TOKEN = os.environ.get('ADMIN_TOKEN')
DB_NAME = os.environ.get('DB_NAME')
DB_USER = os.environ.get('DB_USER')
DB_PASSWORD = os.environ.get('DB_PASSWORD')
DB_PORT = os.environ.get('DB_PORT', '5432')
DB_HOST = os.environ.get('DB_HOST', 'postgres_db')
LOGLEVEL = os.environ.get('LOGLEVEL')
# init bot
bot = telebot.TeleBot(BOT_TOKEN)
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
# init database
logger.info('Initializing database...')
models.db.bind(provider='postgres', user=DB_USER, password=DB_PASSWORD,
host=DB_HOST, port=DB_PORT, database=DB_NAME)
models.db.generate_mapping(create_tables=True)
# Bot helpers
def send(incoming_message: object, outcoming_message: str):
"""
Send message (without Telegram reply wrapper).
* incoming_message: the original message object
came to bot from user
* outcoming_message: answer the bot should send to
the author of incoming_message
"""
bot.send_message(
incoming_message.from_user.id,
outcoming_message,
parse_mode='HTML'
)
def reply(to_message: object, with_message: str):
"""
Reply to given incoming message with outcoming message
(with Telegram reply wrapper).
* to_message: the original message object
came to bot from user
* with_message: answer the bot should send to
the author of incoming_message
"""
bot.reply_to(
to_message,
with_message,
parse_mode='HTML'
)
# Message handlers
@bot.message_handler(commands=['start', 'help'])
def answer_start(message):
"""
Bot sends general help page and basic bot info
"""
send(message, views.hello(message.from_user.username))
@bot.message_handler(commands=['answer'])
@models.db_session
def get_answer(message):
"""
Bot saves new answer to puzzle
"""
user_id = message.from_user.id
user_name = message.from_user.username
command: list = message.text[7:].strip().split()
if not len(command) >= 2:
err_txt = 'Please check your command, bad syntax!'
reply(message, views.command_help(
command='answer', error_text=err_txt)
)
return
# puzzle checks
puzzle_id: str = command[0]
if not puzzle_id.isdigit():
err_txt = (
'Please check your command, bad syntax! '
f'Puzzle ID should be a number, not {puzzle_id}'
)
reply(message, views.command_help(
command='answer', error_text=err_txt)
)
return
puzzle: models.Puzzle = models.Puzzle.exists(puzzle_id)
if not puzzle:
err_txt = (
'Please check your command, bad syntax! '
f'Puzzle ID should be a number, not {puzzle_id}'
)
reply(message, views.error(
error_text=f'No puzzle with ID {puzzle_id}'
)
)
return
# user checks
user_answer: models.Answer | None = (
models.Puzzle.user_has_answer(user_id, puzzle_id)
)
if user_answer:
err_txt = (
f'You have already answered this puzzle #{puzzle_id}: '
f'({user_answer.registered})!'
)
reply(message, views.error(error_text=err_txt))
return
# user answer checks
user_answer: str = ' '.join(command[1:])
if len(user_answer) < 6:
err_txt = (
f'Your answer seems too short: {user_answer}'
)
reply(message, views.command_help(
command='answer', error_text=err_txt)
)
return
elif len(user_answer) > 100:
err_txt = (
f'Your answer seems too big: {len(user_answer)} symbols! '
)
reply(message, views.error(error_text=err_txt))
return
elif user_answer.isdigit():
err_txt = (
f'Your answer is just a number: {user_answer}'
)
reply(message, views.command_help(
command='answer', error_text=err_txt)
)
return
elif any([el in user_answer for el in '#$&^<>']):
err_txt = (
'Please do not use #$&^>< symbols in your answer.'
)
reply(message, views.error(error_text=err_txt))
return
# if all checks ok:
try:
answer = puzzle.register_new_answer(
user_id=user_id,
answer=user_answer,
username=user_name,
)
except Exception as exc:
reply(message, views.error(exc))
else:
send(message, views.got_answer(answer=answer,
username=user_name,
puzzle=puzzle))
@bot.message_handler(commands=['show'])
@models.db_session
def show_answers(message):
"""
Bot shows players' answers to puzzle
"""
user_id = message.from_user.id
user_name = message.from_user.username
command: list = message.text[5:].strip().split()
if len(command) != 2:
err_txt = 'Please check your command, bad syntax!'
reply(message, views.command_help(
command='show', error_text=err_txt)
)
return
puzzle_id, token = command
# puzzle_id checks
if not puzzle_id.isdigit():
err_txt = (
'Please check your command, bad syntax! '
f'Puzzle ID should be a number, not {puzzle_id}'
)
reply(message, views.command_help(
command='answer', error_text=err_txt)
)
return
if not models.Puzzle.exists(puzzle_id):
err_txt = (
'Please check your command, bad syntax! '
f'Puzzle ID should be a number, not {puzzle_id}'
)
reply(message, views.error(
error_text=f'No puzzle with ID {puzzle_id}'
)
)
return
if token != ADMIN_TOKEN:
logger.error(
f'User {user_id} (name: {user_name}) tried to get answers '
f'for puzzle No {puzzle_id}, but used bad ADMIN_TOKEN.'
)
err_txt = (
f'Authentication error. Your token {token} is not valid'
)
reply(message, views.error(error_text=err_txt))
return
try:
puzzle = models.Puzzle.get(id=puzzle_id)
except Exception as exc:
reply(message, views.error(error_text=exc))
else:
reply(message, views.show_answers(puzzle=puzzle))
@bot.message_handler(commands=['register'])
@models.db_session
def register_puzzle(message):
"""
Bot registers new puzzle and gives back id
"""
user_id = message.from_user.id
user_name = message.from_user.username
command: list = message.text[9:].strip().split()
if not len(command) >= 2:
err_txt = 'Please check your command, bad syntax!'
reply(message, views.command_help(
command='register', error_text=err_txt)
)
return
token = command.pop()
if token != ADMIN_TOKEN:
logger.error(
f'User {user_id} (name: {user_name}) tried to register '
f'a puzzle, but used bad ADMIN_TOKEN.'
)
err_txt = (
f'Authentication error. Your token {token} is not valid'
)
reply(message, views.error(error_text=err_txt))
return
puzzle_name: str = ' '.join(command)
if len(puzzle_name) > 100:
err_txt = (
f'The name for your puzzle is too long: {len(puzzle_name)}'
)
reply(message, views.command_help(
command='register', error_text=err_txt)
)
return
elif any([el in puzzle_name for el in '#$&^<>']):
err_txt = (
'Please do not use #$&^>< symbols '
'in your puzzle name.'
)
reply(message, views.error(error_text=err_txt))
return
try:
puzzle = models.Puzzle(name=puzzle_name)
models.commit()
except Exception as exc:
reply(message, views.error(error_text=exc))
else:
logger.info(f'Registered new puzzle, id: {puzzle.id}')
reply(message, views.got_puzzle(puzzle=puzzle))
@bot.message_handler(commands=['recent'])
@models.db_session
def show_recent_answers(message):
"""
Bot shows recent players' answers to all puzzles
"""
user_id = message.from_user.id
user_name = message.from_user.username
token = message.text[7:].strip()
if not token:
auth = False
elif token != ADMIN_TOKEN:
logger.error(
f'User {user_id} (name: {user_name}) tried to get recent'
f'answers, but used bad ADMIN_TOKEN.'
)
err_txt = (
f'Authentication error. Your token {token} is not valid'
)
reply(message, views.command_help(command='recent',
error_text=err_txt))
return
else:
auth = True
try:
answers = models.Answer.get_recent()
except Exception as exc:
reply(message, views.error(error_text=exc))
else:
reply(message, views.show_recent(answers=answers, auth=auth))
# Service functions
def run_long_polling():
logger.info('Starting polling...')
bot.infinity_polling(skip_pending=True)
if __name__ == '__main__':
run_long_polling()