-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
483 lines (363 loc) · 24.3 KB
/
bot.py
File metadata and controls
483 lines (363 loc) · 24.3 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import config
import core
import telebot
import random
import datetime
import markup
import sys
from telebot import apihelper
if config.PROXY_URL:
apihelper.proxy = {'https': config.PROXY_URL}
bot = telebot.TeleBot(config.TOKEN, skip_pending=True)
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, '👋🏻 Привет! Это бот для технической поддержки пользователей.\nЕсли у тебя есть какой-либо вопрос или проблема - нажми на кнопку <b>Написать запрос</b> и наши сотрудники в скором времени тебе ответят!', parse_mode='html', reply_markup=markup.markup_main())
@bot.message_handler(commands=['agent'])
def agent(message):
user_id = message.from_user.id
if core.check_agent_status(user_id) == True:
bot.send_message(message.chat.id, '🔑 Вы авторизованы как Агент поддержки', parse_mode='html', reply_markup=markup.markup_agent())
else:
take_password_message = bot.send_message(message.chat.id, '⚠️ Тебя нет в базе. Отправь одноразовый пароль доступа.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_password_message, get_password_message)
@bot.message_handler(commands=['admin'])
def admin(message):
user_id = message.from_user.id
if str(user_id) == config.ADMIN_ID:
bot.send_message(message.chat.id, '🔑 Вы авторизованы как Админ', reply_markup=markup.markup_admin())
else:
bot.send_message(message.chat.id, '🚫 Эта команда доступна только администратору.')
@bot.message_handler(content_types=['text'])
def send_text(message):
user_id = message.from_user.id
if message.text == '✏️ Написать запрос':
take_new_request = bot.send_message(message.chat.id, 'Введите свой запрос и наши сотрудники скоро с вами свяжутся.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_new_request, get_new_request)
elif message.text == '✉️ Мои запросы':
markup_and_value = markup.markup_reqs(user_id, 'my_reqs', '1')
markup_req = markup_and_value[0]
value = markup_and_value[1]
if value == 0:
bot.send_message(message.chat.id, 'У вас пока ещё нет запросов.', reply_markup=markup.markup_main())
else:
bot.send_message(message.chat.id, 'Ваши запросы:', reply_markup=markup_req)
else:
bot.send_message(message.chat.id, 'Вы возвращены в главное меню.', parse_mode='html', reply_markup=markup.markup_main())
def get_password_message(message):
password = message.text
user_id = message.from_user.id
if password == None:
send_message = bot.send_message(message.chat.id, '⚠️ Вы отправляете не текст. Попробуйте еще раз.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(send_message, get_password_message)
elif password.lower() == 'отмена':
bot.send_message(message.chat.id, 'Отменено.', reply_markup=markup.markup_main())
return
elif core.valid_password(password) == True:
core.delete_password(password)
core.add_agent(user_id)
bot.send_message(message.chat.id, '🔑 Вы авторизованы как Агент поддержки', parse_mode='html', reply_markup=markup.markup_main())
bot.send_message(message.chat.id, 'Выберите раздел технической панели:', parse_mode='html', reply_markup=markup.markup_agent())
else:
send_message = bot.send_message(message.chat.id, '⚠️ Неверный пароль. Попробуй ещё раз.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(send_message, get_password_message)
def get_agent_id_message(message):
agent_id = message.text
if agent_id == None:
take_agent_id_message = bot.send_message(message.chat.id, '⚠️ Вы отправляете не текст. Попробуйте еще раз.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_agent_id_message, get_agent_id_message)
elif agent_id.lower() == 'отмена':
bot.send_message(message.chat.id, 'Отменено.', reply_markup=markup.markup_main())
return
else:
core.add_agent(agent_id)
bot.send_message(message.chat.id, '✅ Агент успешно добавлен.', reply_markup=markup.markup_main())
bot.send_message(message.chat.id, 'Выберите раздел админ панели:', reply_markup=markup.markup_admin())
def get_new_request(message):
request = message.text
user_id = message.from_user.id
check_file = core.get_file(message)
#Если пользователь отправляет файл
if check_file != None:
file_id = check_file['file_id']
file_name = check_file['file_name']
type = check_file['type']
request = check_file['text']
if str(request) == 'None':
take_new_request = bot.send_message(message.chat.id, '⚠️ Вы не ввели ваш запрос. Попробуйте ещё раз, отправив текст вместе с файлом.', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_new_request, get_new_request)
else:
req_id = core.new_req(user_id, request)
core.add_file(req_id, file_id, file_name, type)
bot.send_message(message.chat.id, f'✅ Ваш запрос под ID {req_id} создан. Посмотреть текущие запросы можно нажав кнопку <b>Мои текущие запросы</b>', parse_mode='html', reply_markup=markup.markup_main())
#Если пользователь отправляет только текст
else:
if request == None:
take_new_request = bot.send_message(message.chat.id, '⚠️ Отправляемый вами тип данных не поддерживается в боте. Попробуйте еще раз отправить ваш запрос, использовав один из доступных типов данных (текст, файлы, фото, видео, аудио, голосовые сообщения)', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_new_request, get_new_request)
elif request.lower() == 'отмена':
bot.send_message(message.chat.id, 'Отменено.', reply_markup=markup.markup_main())
return
else:
req_id = core.new_req(user_id, request)
bot.send_message(message.chat.id, f'✅ Ваш запрос под ID {req_id} создан. Посмотреть текущие запросы можно нажав кнопку <b>Мои текущие запросы</b>', parse_mode='html', reply_markup=markup.markup_main())
def get_additional_message(message, req_id, status):
additional_message = message.text
check_file = core.get_file(message)
#Если пользователь отправляет файл
if check_file != None:
file_id = check_file['file_id']
file_name = check_file['file_name']
type = check_file['type']
additional_message = check_file['text']
core.add_file(req_id, file_id, file_name, type)
if additional_message == None:
take_additional_message = bot.send_message(chat_id=message.chat.id, text='⚠️ Отправляемый вами тип данных не поддерживается в боте. Попробуйте еще раз отправить ваше сообщение, использовав один из доступных типов данных (текст, файлы, фото, видео, аудио, голосовые сообщения).', reply_markup=markup.markup_cancel())
bot.clear_step_handler_by_chat_id(message.chat.id)
bot.register_next_step_handler(take_additional_message, get_additional_message, req_id, status)
elif additional_message.lower() == 'отмена':
bot.send_message(message.chat.id, 'Отменено.', reply_markup=markup.markup_main())
return
else:
if additional_message != 'None':
core.add_message(req_id, additional_message, status)
if check_file != None:
if additional_message != 'None':
text = '✅ Ваш файл и сообщение успешно отправлены!'
else:
text = '✅ Ваш файл успешно отправлен!'
else:
text = '✅ Ваше сообщение успешно отправлено!'
bot.send_message(message.chat.id, text, reply_markup=markup.markup_main())
if status == 'agent':
user_id = core.get_user_id_of_req(req_id)
try:
if additional_message == 'None':
additional_message = ''
bot.send_message(user_id, f'⚠️ Получен новый ответ на ваш запрос ID {req_id}!\n\n🧑💻 Ответ агента поддержки:\n{additional_message}', reply_markup=markup.markup_main())
if type == 'photo':
bot.send_photo(user_id, photo=file_id, reply_markup=markup.markup_main())
elif type == 'document':
bot.send_document(user_id, data=file_id, reply_markup=markup.markup_main())
elif type == 'video':
bot.send_video(user_id, data=file_id, reply_markup=markup.markup_main())
elif type == 'audio':
bot.send_audio(user_id, audio=file_id, reply_markup=markup.markup_main())
elif type == 'voice':
bot.send_voice(user_id, voice=file_id, reply_markup=markup.markup_main())
else:
bot.send_message(user_id, additional_message, reply_markup=markup.markup_main())
except:
pass
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
user_id = call.message.chat.id
if call.message:
if ('my_reqs:' in call.data) or ('waiting_reqs:' in call.data) or ('answered_reqs:' in call.data) or ('confirm_reqs:' in call.data):
"""
Обработчик кнопок для:
✉️ Мои запросы
❗️ Ожидают ответа от поддержки,
⏳ Ожидают ответа от пользователя
✅ Завершенные запросы
"""
parts = call.data.split(':')
callback = parts[0]
number = parts[1]
markup_and_value = markup.markup_reqs(user_id, callback, number)
markup_req = markup_and_value[0]
value = markup_and_value[1]
if value == 0:
bot.send_message(chat_id=call.message.chat.id, text='⚠️ Запросы не обнаружены.', reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
return
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на запрос, чтобы посмотреть историю переписки, либо добавить сообщение:', reply_markup=markup_req)
except:
bot.send_message(chat_id=call.message.chat.id, text='Ваши запросы:', reply_markup=markup_req)
bot.answer_callback_query(call.id)
#Открыть запрос
elif 'open_req:' in call.data:
parts = call.data.split(':')
req_id = parts[1]
callback = parts[2]
req_status = core.get_req_status(req_id)
request_data = core.get_request_data(req_id, callback)
len_req_data = len(request_data)
i = 1
for data in request_data:
if i == len_req_data:
markup_req = markup.markup_request_action(req_id, req_status, callback)
else:
markup_req = None
bot.send_message(chat_id=call.message.chat.id, text=data, parse_mode='html', reply_markup=markup_req)
i += 1
bot.answer_callback_query(call.id)
#Добавить сообщение в запрос
elif 'add_message:' in call.data:
parts = call.data.split(':')
req_id = parts[1]
status_user = parts[2]
take_additional_message = bot.send_message(chat_id=call.message.chat.id, text='Отправьте ваше сообщение, использовав один из доступных типов данных (текст, файлы, фото, видео, аудио, голосовые сообщения)', reply_markup=markup.markup_cancel())
bot.register_next_step_handler(take_additional_message, get_additional_message, req_id, status_user)
bot.answer_callback_query(call.id)
#Завершить запрос
elif 'confirm_req:' in call.data:
parts = call.data.split(':')
confirm_status = parts[1]
req_id = parts[2]
if core.get_req_status(req_id) == 'confirm':
bot.send_message(chat_id=call.message.chat.id, text="⚠️ Этот запрос уже завершен.", reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
return
#Запросить подтверждение завершения
if confirm_status == 'wait':
bot.send_message(chat_id=call.message.chat.id, text="Для завершения запроса - нажмите кнопку <b>Подтвердить</b>", parse_mode='html', reply_markup=markup.markup_confirm_req(req_id))
#Подтвердить завершение
elif confirm_status == 'true':
core.confirm_req(req_id)
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="✅ Запрос успешно завершён.", reply_markup=markup.markup_main())
except:
bot.send_message(chat_id=call.message.chat.id, text="✅ Запрос успешно завершён.", reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
#Файлы запроса
elif 'req_files:' in call.data:
parts = call.data.split(':')
req_id = parts[1]
callback = parts[2]
number = parts[3]
markup_and_value = markup.markup_files(number, req_id, callback)
markup_files = markup_and_value[0]
value = markup_and_value[1]
if value == 0:
bot.send_message(chat_id=call.message.chat.id, text='⚠️ Файлы не обнаружены.', reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
return
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на файл, чтобы получить его.', reply_markup=markup_files)
except:
bot.send_message(chat_id=call.message.chat.id, text='Нажмите на файл, чтобы получить его.', reply_markup=markup_files)
bot.answer_callback_query(call.id)
#Отправить файл
elif 'send_file:' in call.data:
parts = call.data.split(':')
id = parts[1]
type = parts[2]
file_id = core.get_file_id(id)
if type == 'photo':
bot.send_photo(call.message.chat.id, photo=file_id, reply_markup=markup.markup_main())
elif type == 'document':
bot.send_document(call.message.chat.id, data=file_id, reply_markup=markup.markup_main())
elif type == 'video':
bot.send_video(call.message.chat.id, data=file_id, reply_markup=markup.markup_main())
elif type == 'audio':
bot.send_audio(call.message.chat.id, audio=file_id, reply_markup=markup.markup_main())
elif type == 'voice':
bot.send_voice(call.message.chat.id, voice=file_id, reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
#Вернуться назад в панель агента
elif call.data == 'back_agent':
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='🔑 Вы авторизованы как Агент поддержки', parse_mode='html', reply_markup=markup.markup_agent())
except:
bot.send_message(call.message.chat.id, '🔑 Вы авторизованы как Агент поддержки', parse_mode='html', reply_markup=markup.markup_agent())
bot.answer_callback_query(call.id)
#Вернуться назад в панель админа
elif call.data == 'back_admin':
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='🔑 Вы авторизованы как Админ', parse_mode='html', reply_markup=markup.markup_admin())
except:
bot.send_message(call.message.chat.id, '🔑 Вы авторизованы как Админ', parse_mode='html', reply_markup=markup.markup_admin())
bot.answer_callback_query(call.id)
#Добавить агента
elif call.data == 'add_agent':
take_agent_id_message = bot.send_message(chat_id=call.message.chat.id, text='Чтобы добавить агента поддержки - введите его ID Telegram.', reply_markup=markup.markup_cancel())
bot.register_next_step_handler(take_agent_id_message, get_agent_id_message)
#Все агенты
elif 'all_agents:' in call.data:
number = call.data.split(':')[1]
markup_and_value = markup.markup_agents(number)
markup_agents = markup_and_value[0]
len_agents = markup_and_value[1]
if len_agents == 0:
bot.send_message(chat_id=call.message.chat.id, text='⚠️ Агенты не обнаружены.', reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
return
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на агента поддержки, чтобы удалить его', parse_mode='html', reply_markup=markup_agents)
except:
bot.send_message(call.message.chat.id, 'Нажмите на агента поддержки, чтобы удалить его', parse_mode='html', reply_markup=markup_agents)
bot.answer_callback_query(call.id)
#Удалить агента
elif 'delete_agent:' in call.data:
agent_id = call.data.split(':')[1]
core.delete_agent(agent_id)
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на агента поддержки, чтобы удалить его', parse_mode='html', reply_markup=markup.markup_agents('1')[0])
except:
bot.send_message(call.message.chat.id, 'Нажмите на агента поддержки, чтобы удалить его', parse_mode='html', reply_markup=markup.markup_agents('1')[0])
bot.answer_callback_query(call.id)
#Все пароли
elif 'all_passwords:' in call.data:
number = call.data.split(':')[1]
markup_and_value = markup.markup_passwords(number)
markup_passwords = markup_and_value[0]
len_passwords = markup_and_value[1]
if len_passwords == 0:
bot.send_message(chat_id=call.message.chat.id, text='⚠️ Пароли не обнаружены.', reply_markup=markup.markup_main())
bot.answer_callback_query(call.id)
return
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на пароль, чтобы удалить его', parse_mode='html', reply_markup=markup_passwords)
except:
bot.send_message(call.message.chat.id, 'Нажмите на пароль, чтобы удалить его', parse_mode='html', reply_markup=markup_passwords)
bot.answer_callback_query(call.id)
#Удалить пароль
elif 'delete_password:' in call.data:
password = call.data.split(':')[1]
core.delete_password(password)
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Нажмите на пароль, чтобы удалить его', parse_mode='html', reply_markup=markup.markup_passwords('1')[0])
except:
bot.send_message(call.message.chat.id, 'Нажмите на пароль, чтобы удалить его', parse_mode='html', reply_markup=markup.markup_passwords('1')[0])
bot.answer_callback_query(call.id)
#Сгенерировать пароли
elif call.data == 'generate_passwords':
#10 - количество паролей, 16 - длина пароля
passwords = core.generate_passwords(10, 16)
core.add_passwords(passwords)
text_passwords = ''
i = 1
for password in passwords:
text_passwords += f'{i}. {password}\n'
i += 1
bot.send_message(call.message.chat.id, f"✅ Сгенерировано {i-1} паролей:\n\n{text_passwords}", parse_mode='html', reply_markup=markup.markup_main())
bot.send_message(call.message.chat.id, 'Нажмите на пароль, чтобы удалить его', parse_mode='html', reply_markup=markup.markup_passwords('1')[0])
bot.answer_callback_query(call.id)
#Остановить бота
elif 'stop_bot:' in call.data:
status = call.data.split(':')[1]
#Запросить подтверждение на отключение
if status == 'wait':
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=f"Вы точно хотите отключить бота?", parse_mode='html', reply_markup=markup.markup_confirm_stop())
except:
bot.send_message(call.message.chat.id, f"Вы точно хотите отключить бота?", parse_mode='html', reply_markup=markup.markup_confirm_stop())
#Подтверждение получено
elif status == 'confirm':
try:
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='✅ Бот оключен.')
except:
bot.send_message(chat_id=call.message.chat.id, text='✅ Бот оключен.')
bot.answer_callback_query(call.id)
bot.stop_polling()
sys.exit()
if __name__ == "__main__":
bot.polling(none_stop=True)