forked from dyanashek/Taro-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
401 lines (284 loc) · 11.6 KB
/
functions.py
File metadata and controls
401 lines (284 loc) · 11.6 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
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
import sqlite3
import logging
import inspect
import gspread
import datetime
import telebot
import keyboards
import config
bot = telebot.TeleBot(config.TELEGRAM_TOKEN)
service_acc = gspread.service_account(filename='service_account.json')
sheet = service_acc.open(config.SPREAD_NAME)
work_sheet = sheet.worksheet(config.LIST_NAME)
def is_new_user(user_id):
"""Checks if user already in database.
Returns an empty list if the user is new.
"""
database = sqlite3.connect("taro.db", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cursor = database.cursor()
user = cursor.execute(f"SELECT * FROM users WHERE user_id='{user_id}'").fetchall()
cursor.close()
database.close()
if user != []:
user = user[0]
return user
def add_user(user_id, user_username):
"""Adds a new user to database."""
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''
INSERT INTO users (user_id, user_username)
VALUES ("{user_id}", "{user_username}")
''')
database.commit()
cursor.close()
database.close()
logging.info(f'{inspect.currentframe().f_code.co_name}: Добавлен новый пользователь {user_username}.')
def is_form_filled(info):
"""Checks if user has already filled the form.
Return False if he doesn't"""
if None in info:
return False
return True
def set_to_none(user_id):
"""Sets all filled fields to None."""
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET name=NULL, family=NULL, instagram=NULL, birth=NULL
WHERE user_id="{user_id}"
''')
database.commit()
cursor.close()
database.close()
logging.info(f'{inspect.currentframe().f_code.co_name}: Сброшены заполненные поля пользователя {user_id}.')
def name_response(first_name):
"""Generates a response, depends on first name."""
if first_name is None:
keyboard = keyboards.enter_name_keyboard()
reply_text = config.FIRST_NAME_MESSAGE
else:
keyboard = keyboards.confirm_first_name_keyboard(first_name)
reply_text = f'Ваше имя - *{first_name}*?'
return reply_text, keyboard
def family_name_response(family_name):
"""Generates a response, depends on family name."""
if family_name is None:
keyboard = keyboards.enter_family_name_keyboard()
reply_text = config.FAMILY_NAME_MESSAGE
else:
keyboard = keyboards.confirm_family_name_keyboard(family_name)
reply_text = f'Ваша фамилия - *{family_name}*?'
return reply_text, keyboard
def enter_response(info_category):
"""Generates a response when user wants to enter something manual."""
if info_category == 'name':
keyboard = keyboards.enter_name_keyboard()
reply_text = config.FIRST_NAME_MESSAGE
elif info_category == 'family':
keyboard = keyboards.enter_family_name_keyboard()
reply_text = config.FAMILY_NAME_MESSAGE
return reply_text, keyboard
def fill_form_info(info_category, info, user_id):
"""Fill one of the form field."""
database = sqlite3.connect("taro.db", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET {info_category}="{info}"
WHERE user_id="{user_id}"
''')
database.commit()
cursor.close()
database.close()
logging.info(f'{inspect.currentframe().f_code.co_name}: У пользователя {user_id} заполнена графа {info_category} ({info}).')
def confirm_form_reply(name, family_name, instagram_link, birthday):
"""Constructs a text to confirm filled info."""
reply_text = f'''
\nВы ввели следующие данные:\
\n\
\n*Имя*: {name}\
\n*Фамилия:* {family_name}\
\n*Instagram:* {instagram_link}\
\n*Дата рождения:* {birthday}\
'''
return reply_text
def transfer_to_google_sheets(user_id):
"""Transfers data to google sheets."""
user_info = list(is_new_user(user_id))
user_info = user_info[1:7:]
print(user_info)
birthday = datetime.datetime.strftime(user_info[5], "%d.%m.%Y")
user_info[5] = birthday
fill_date = datetime.datetime.strftime((datetime.datetime.utcnow() + datetime.timedelta(hours=3)).date(), "%d.%m.%Y")
user_info.append(fill_date)
row = get_row()
try:
work_sheet.update(f'A{row}:G{row}', [user_info])
except:
pass
logging.info(f'{inspect.currentframe().f_code.co_name}: Данные пользователя {user_id} внесены в таблицу.')
def get_row():
"""Gets the number of first empty row in table."""
return len(work_sheet.col_values(1)) + 1
def notify_manager(user_id):
"""Notifies manager that user filled up the form."""
user_info = is_new_user(user_id)
username = user_info[2]
name = user_info[3]
family_name = user_info[4]
instagram_link = user_info[5]
birthday = datetime.datetime.strftime(user_info[6], "%d.%m.%Y")
reply_text = f'''
\nПользователь @{username} отправил форму на расшифровку:\
\n\
\n*Имя:* {name}\
\n*Фамилия:* {family_name}\
\n*Instagram:* {instagram_link}\
\n*День рождения:* {birthday}\
'''
try:
message = bot.send_message(chat_id=config.MANAGER_ID,
text=reply_text,
reply_markup=keyboards.mark_as_complete(user_id),
parse_mode='Markdown',
)
bot.pin_chat_message(chat_id=config.MANAGER_ID,
message_id=message.id,
disable_notification=True,
)
except:
pass
def color_spread(user_id):
"""Colors the row to green after answering the request."""
row = get_color_row(user_id)
work_sheet.format(f'A{row}:G{row}', {"backgroundColor": {"red": 0.4, "green": 0.67, "blue": 0.51}})
def get_color_row(user_id):
"""Gets the number of row that should be colored."""
return work_sheet.col_values(1).index(user_id) + 1
def update_service(user_id, service):
"""Sets all filled fields to None."""
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET service=?
WHERE user_id=?
''', (service, user_id,))
database.commit()
cursor.close()
database.close()
def update_username(user_id, username):
"""Sets all filled fields to None."""
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET user_username=?
WHERE user_id=?
''', (username, user_id,))
database.commit()
cursor.close()
database.close()
def update_request(user_id, request, request_type):
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET request=?, request_type=?
WHERE user_id=?
''', (request, request_type, user_id,))
database.commit()
cursor.close()
database.close()
def update_check(user_id, check, check_type):
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET check_id=?, check_type=?
WHERE user_id=?
''', (check, check_type, user_id,))
database.commit()
cursor.close()
database.close()
def update_photo(user_id, photo):
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET photo=?
WHERE user_id=?
''', (photo, user_id,))
database.commit()
cursor.close()
database.close()
def drop_payment(user_id):
"""Sets all filled fields to None."""
database = sqlite3.connect("taro.db")
cursor = database.cursor()
cursor.execute(f'''UPDATE users
SET service=NULL, check_id=NULL, check_type=NULL, photo=NULL, request=NULL, request_type=NULL
WHERE user_id="{user_id}"
''')
database.commit()
cursor.close()
database.close()
def handle_payment(user_id):
user_info = is_new_user(user_id)
username = user_info[2]
service = user_info[7]
check_id = user_info[8]
check_type = user_info[9]
photo = user_info[10]
request = user_info[11]
request_type = user_info[12]
drop_payment(user_id)
transfer_to_google_sheets_payments(user_id, username, config.SERVICES_NAMES[service])
try:
bot.send_message(chat_id=config.MANAGER_ID,
text=f'Пользователь @{username} оплатил услугу: *{config.SERVICES_NAMES[service]}*.',
parse_mode='Markdown',
)
check_message = bot.send_message(chat_id=config.MANAGER_ID,
text=f'Платежный документ пользователя @{username}:',
)
if check_type == 'document':
bot.send_document(chat_id=config.MANAGER_ID,
document=check_id,
reply_to_message_id=check_message.id,
)
else:
bot.send_photo(chat_id=config.MANAGER_ID,
photo=check_id,
reply_to_message_id=check_message.id,
)
bot.send_photo(chat_id=config.MANAGER_ID,
photo=photo,
caption=f'Селфи пользователя @{username}.',
)
request_message = bot.send_message(chat_id=config.MANAGER_ID,
text=f'Запрос пользователя @{username}:',
)
if request_type == 'text':
bot.send_message(chat_id=config.MANAGER_ID,
text=request,
reply_to_message_id=request_message.id,
)
else:
try:
bot.send_audio(chat_id=config.MANAGER_ID,
audio=request,
reply_to_message_id=request_message.id,
)
except Exception as ex:
print(ex)
except:
pass
def get_payment_row():
"""Gets the number of first empty row in table."""
work_sheet_payments = sheet.worksheet(config.PAYMENT_LIST)
return len(work_sheet_payments.col_values(1)) + 1
def transfer_to_google_sheets_payments(user_id, username, service):
"""Transfers data to google sheets."""
row = get_payment_row()
work_sheet_payments = sheet.worksheet(config.PAYMENT_LIST)
fill_date = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(hours=3), "%d.%m.%Y %H:%M")
try:
work_sheet_payments.update(f'A{row}:D{row}', [[user_id, username, service, fill_date]])
except:
pass