-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
927 lines (803 loc) · 47.2 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
import asyncio
import logging
import sys
from datetime import datetime, timedelta
from aiogram import Bot, Dispatcher, F
from aiogram.fsm.context import FSMContext
from aiogram.fsm.storage.base import StorageKey
from aiogram.fsm.storage.redis import RedisStorage
from aiogram.types import Message, CallbackQuery
from aiogram.filters.command import Command
from redis.asyncio import Redis
from config import *
import kb
from states import *
from db import DB
from payments import *
db = DB()
redis = Redis(
host=REDIS_HOST,
port=REDIS_PORT,
db=REDIS_DB,
)
storage = RedisStorage(redis)
bot = Bot(token=TOKEN)
dp = Dispatcher(storage=storage)
logging.basicConfig(filename="all.log", level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(filename)s function: %(funcName)s line: %(lineno)d - %(message)s')
errors = logging.getLogger("errors")
errors.setLevel(logging.ERROR)
fh = logging.FileHandler("errors.log")
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(filename)s function: %(funcName)s line: %(lineno)d - %(message)s')
fh.setFormatter(formatter)
errors.addHandler(fh)
def top(word: str, top_dict: dict) -> str:
st = ''
for i, j in top_dict.items():
st += f'{i}) {j["name"]} — <b>{j["count"]}</b> <i>{word}</i>.\n'
return st
# Главная ==============================================================================================================
@dp.message(Command('start'))
async def start(message: Message, state: FSMContext):
try:
await state.clear()
if not await db.user_exists(str(message.from_user.id)):
sp = message.text.split()
if len(sp) > 1:
user_id = sp[1]
await db.update_refs(str(user_id))
await db.update_points(str(user_id), 1)
if bool(await db.select_notifications(user_id)):
await bot.send_message(user_id, 'Кто-то присоединился к боту по вашей ссылке!')
if await db.select_refs(user_id) % 10 == 0:
await bot.send_message(user_id, 'Вы можете отключить уведомления о новых рефах в настройках.')
await message.answer(f'Добро пожаловать в анонимный чат!\n'
f'Перед тем как начать общение необходимо пройти регистрацию.\n'
f'После регистрации вы получите <b>вип на неделю бесплатно!</b>\n'
f'Продолжая пользование ботом вы соглашаетесь с правилами.\n',
reply_markup=kb.lobby_kb, parse_mode='HTML')
else:
await message.answer(f'Привет, {await db.select_name(str(message.from_user.id))}.', reply_markup=kb.main_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'to_main')
async def call_start(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Привет, {await db.select_name(str(call.from_user.id))}.',
reply_markup=kb.main_kb)
except Exception as e:
errors.error(e)
# Лобби ================================================================================================================
@dp.callback_query(F.data == 'lobby')
async def lobby(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Добро пожаловать в анонимный чат!\n'
f'Перед тем как начать общение необходимо пройти регистрацию.\n'
f'После регистрации вы получите <b>вип на неделю бесплатно!</b>\n'
f'Продолжая пользование ботом вы соглашаетесь с правилами.\n',
reply_markup=kb.lobby_kb, parse_mode='HTML')
except Exception as e:
errors.error(e)
@dp.message(Command('help'))
async def help(message: Message):
try:
await message.answer(f'/start - В начало')
except Exception as e:
errors.error(e)
@dp.message(Command('bug'))
async def bug(message: Message, state: FSMContext):
try:
await message.answer('Опишите ошибку с которой вы столкнулись.')
await state.set_state(Bug.bug)
except Exception as e:
errors.error(e)
@dp.message(Bug.bug)
async def set_bug(message: Message, state: FSMContext):
try:
sender = message.from_user.id if message.from_user.username is None else f'@{message.from_user.username}'
await bot.send_message(BUGS_GROUP_ID, f'Отправитель: {sender}.\n'
f'Сообщение: {message.text}.')
await message.answer('Разработчик оповещен о проблеме и скоро ее исправит.\n'
'Спасибо за помощь!')
await state.clear()
except Exception as e:
errors.error(e)
@dp.message(Command('idea'))
async def idea(message: Message, state: FSMContext):
try:
await message.answer('Что вы хотите предложить?')
await state.set_state(Idea.idea)
except Exception as e:
errors.error(e)
@dp.message(Idea.idea)
async def set_idea(message: Message, state: FSMContext):
try:
sender = message.from_user.id if message.from_user.username is None else f'@{message.from_user.username}'
await bot.send_message(IDEAS_GROUP_ID, f'Отправитель: {sender}.\n'
f'Сообщение: {message.text}.')
await message.answer('Идея передана разработчику.\n'
'Спасибо за помощь!')
await state.clear()
except Exception as e:
errors.error(e)
# Правила ==============================================================================================================
@dp.callback_query(F.data == 'rules')
async def rules(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'<b>В чате запрещены:</b>\n'
f'1) Любые упоминания психоактивных веществ (наркотиков).\n'
f'2) Обмен, распространение любых 18+ материалов.\n'
f'3) Любая реклама, спам, продажа чего либо.\n'
f'4) Оскорбительное поведение.\n'
f'5) Любые действия, нарушающие правила Telegram.\n',
reply_markup=kb.to_lobby_kb, parse_mode='HTML')
except Exception as e:
errors.error(e)
# Регистрация ==========================================================================================================
@dp.callback_query(F.data == 'registrate')
async def registrate(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Введите ваше имя.')
await state.set_state(RegState.name)
except Exception as e:
errors.error(e)
@dp.message(RegState.name)
async def reg_name(message: Message, state: FSMContext):
try:
await state.update_data(name=message.text)
await message.answer('Введите ваш возраст.')
await state.set_state(RegState.age)
except Exception as e:
errors.error(e)
@dp.message(RegState.age)
async def reg_age(message: Message, state: FSMContext):
try:
await state.update_data(age=message.text)
await message.answer('Выберите ваш пол.', reply_markup=kb.sex_kb)
await state.set_state(RegState.sex)
except Exception as e:
errors.error(e)
@dp.callback_query(RegState.sex, F.data.endswith('male'))
async def reg_sex(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
await state.update_data(sex=call.data)
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Регистрация завершена.\nВам выдан вип на 7 дней.', reply_markup=kb.main_kb)
data = await state.get_data()
await db.insert_in_users(str(call.from_user.id), data['name'], data['age'], data['sex'],
(datetime.now() + timedelta(days=7)).strftime('%d.%m.%Y %H:%M'))
await state.clear()
except Exception as e:
errors.error(e)
# Профиль ==============================================================================================================
@dp.callback_query(F.data == 'profile')
async def profile(call: CallbackQuery):
try:
await call.answer()
sex = 'Неизвестно'
if await db.select_sex(str(call.from_user.id)) == 'male':
sex = 'Мужской'
elif await db.select_sex(str(call.from_user.id)) == 'female':
sex = 'Женский'
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'🅰️ <b>Имя:</b> <i>{await db.select_name(str(call.from_user.id))}</i>\n'
f'🔞 <b>Возраст:</b> <i>{await db.select_age(str(call.from_user.id))}</i>\n'
f'👫 <b>Пол:</b> <i>{sex}</i>',
reply_markup=kb.profile_kb, parse_mode='HTML')
except Exception as e:
errors.error(e)
# Настройки ============================================================================================================
@dp.callback_query(F.data == 'settings')
async def settings(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Что вы хотите изменить?', reply_markup=kb.settings_kb)
except Exception as e:
errors.error(e)
# Имя ==================================================================================================================
@dp.callback_query(F.data == 'name')
async def edit_name(call: CallbackQuery, state: FSMContext):
try:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Введите свое имя.')
await state.set_state(NameState.name)
except Exception as e:
errors.error(e)
@dp.message(NameState.name)
async def set_name(message: Message, state: FSMContext):
try:
await db.update_name(str(message.from_user.id), message.text)
await message.answer(text='Имя сохранено.', reply_markup=kb.to_settings_kb)
await state.clear()
except Exception as e:
errors.error(e)
# Возраст ==============================================================================================================
@dp.callback_query(F.data == 'age')
async def edit_age(call: CallbackQuery, state: FSMContext):
try:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Введите свой возраст.')
await state.set_state(AgeState.age)
except Exception as e:
errors.error(e)
@dp.message(AgeState.age)
async def set_age(message: Message, state: FSMContext):
try:
await db.update_age(str(message.from_user.id), message.text)
await message.answer('Возраст сохранен.', reply_markup=kb.to_settings_kb)
await state.clear()
except Exception as e:
errors.error(e)
# Пол ==================================================================================================================
@dp.callback_query(F.data == 'sex')
async def edit_sex(call: CallbackQuery, state: FSMContext):
try:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Выберите свой пол.', reply_markup=kb.sex_kb)
await state.set_state(SexState.sex)
except Exception as e:
errors.error(e)
@dp.callback_query(SexState.sex, F.data.endswith('male'))
async def set_sex(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
await db.update_sex(str(call.from_user.id), call.data)
await bot.send_message(call.from_user.id, 'Пол сохранен.', reply_markup=kb.to_settings_kb)
await state.clear()
except Exception as e:
errors.error(e)
# Статистика ===========================================================================================================
@dp.callback_query(F.data == 'stats')
async def stats(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'💬 Чатов: {await db.select_chats(str(call.from_user.id))}\n'
f'⌨️ Сообщений: {await db.select_messages(str(call.from_user.id))}\n'
f'👍 Лайков: {await db.select_likes(str(call.from_user.id))}\n'
f'👎 Дизлайков: {await db.select_dislikes(str(call.from_user.id))}\n'
f'👨💻 Пользователей приглашено: {await db.select_refs(str(call.from_user.id))}',
reply_markup=kb.statistic_kb)
except Exception as e:
errors.error(e)
# Рефералка ============================================================================================================
@dp.callback_query(F.data == 'ref')
async def ref(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Распространяйте свою реферальную ссылку, чтобы получать 💎.\n'
f'1 переход по ссылке = 1 💎.\n'
f'5 💎 = 1 день VIP-статуса 👑.\n'
f'У вас {await db.select_points(str(call.from_user.id))} 💎.\n\n'
f'🆔 Ваша реферальная ссылка:\n'
f'{f"{RETURN_URL}?start=" + str(str(call.from_user.id))}.',
disable_web_page_preview=True,
reply_markup=kb.ref_kb(await db.select_notifications(str(call.from_user.id))))
except Exception as e:
errors.error(e)
# Обмен 💎 =============================================================================================================
@dp.callback_query(F.data == 'trade')
async def trade(call: CallbackQuery):
try:
if await db.select_points(str(call.from_user.id)) >= 5:
await db.update_points(str(call.from_user.id), -5)
if await db.select_vip_ends(str(call.from_user.id)) is None:
await db.update_vip_ends((datetime.now() + timedelta(days=1)).strftime('%d.%m.%Y %H:%M'),
str(call.from_user.id))
await call.answer('Успешно!')
else:
await db.update_vip_ends(
(datetime.strptime(await db.select_vip_ends(str(call.from_user.id)), '%d.%m.%Y %H:%M') +
timedelta(days=1)).strftime('%d.%m.%Y %H:%M'), str(call.from_user.id))
await call.answer('Успешно!')
else:
await call.answer('У вас недостаточно баллов.')
except Exception as e:
errors.error(e)
# Уведомления ==========================================================================================================
@dp.callback_query(F.data == 'on')
async def notifications_on(call: CallbackQuery):
try:
await call.answer()
await db.update_notifications(str(call.from_user.id), 1)
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Уведомления включены.', reply_markup=kb.to_ref_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'off')
async def notifications_off(call: CallbackQuery):
try:
await call.answer()
await db.update_notifications(str(call.from_user.id), 0)
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Уведомления выключены.', reply_markup=kb.to_ref_kb)
except Exception as e:
errors.error(e)
# Топы =================================================================================================================
@dp.callback_query(F.data == 'tops')
async def tops(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Ниже представлены рейтинги по разным критериям.', reply_markup=kb.top_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'top_messages')
async def top_messages(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=top('сообщений', await db.top_messages()), reply_markup=kb.to_tops_kb,
parse_mode='HTML')
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'top_likes')
async def top_likes(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=top('лайков', await db.top_likes()), reply_markup=kb.to_tops_kb,
parse_mode='HTML')
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'top_refs')
async def top_refs(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=top('рефов', await db.top_refs()), reply_markup=kb.to_tops_kb,
parse_mode='HTML')
except Exception as e:
errors.error(e)
# Вип ==================================================================================================================
@dp.callback_query(F.data == 'vip')
async def vip(call: CallbackQuery):
try:
await call.answer()
if await db.select_vip_ends(str(call.from_user.id)) is not None:
if datetime.strptime(await db.select_vip_ends(str(call.from_user.id)), '%d.%m.%Y %H:%M') > datetime.now():
delta = datetime.strptime(await db.select_vip_ends(str(call.from_user.id)),
'%d.%m.%Y %H:%M') - datetime.now()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Осталось {delta.days} дней, {delta.seconds // 3600} часов, {delta.seconds // 60 % 60} минут Випа.',
reply_markup=kb.vip_kb)
else:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Вип дает:\n'
f'1) Поиск по полу.\n'
f'2) Подробная информацию о собеседнике: отзывы, имя, пол, возраст.\n'
f'<b>Сейчас подключены ТЕСТОВЫЕ платежи, то есть деньги НЕ будут списаны, но вип вы получите.</b>',
reply_markup=kb.vip_kb, parse_mode='HTML')
else:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'Вип дает:\n'
f'1) Поиск по полу.\n'
f'2) Подробная информацию о собеседнике: отзывы, имя, пол, возраст.\n'
f'<b>Сейчас подключены ТЕСТОВЫЕ платежи, то есть деньги НЕ будут списаны, но вип вы получите.</b>',
reply_markup=kb.vip_kb, parse_mode='HTML')
except Exception as e:
errors.error(e)
# Купить вип ===========================================================================================================
@dp.callback_query(F.data == 'buy_vip')
async def buy_vip(call: CallbackQuery):
try:
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Выберите длительность:', reply_markup=kb.buy_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'vip_day')
async def buy_day(call: CallbackQuery):
try:
await call.answer()
url, payment_id = create_payment(20, 'Вип на 1 день')
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'<a href="{url}">Оплатить 20 рублей</a>', parse_mode='HTML',
reply_markup=kb.to_buy_kb)
c = 0
paid = False
while True:
if get_payment_status(payment_id) == 'waiting_for_capture':
paid = True
break
elif c == 600:
await bot.send_message(call.from_user.id, 'Платеж отменен.\n'
'Причина: прошло 10 минут с момента создания платежа.',
reply_markup=kb.to_main_kb)
break
else:
await asyncio.sleep(1)
c += 1
if paid:
response = json.loads(confirm_payment(payment_id))
if response['status'] == 'succeeded':
await db.update_vip_ends(str(call.from_user.id), (
datetime.strptime(str(await db.select_vip_ends(str(call.from_user.id))),
'%d.%m.%Y %H:%M') + timedelta(days=1)).strftime('%d.%m.%Y %H:%M'))
await bot.send_message(call.from_user.id, 'Вы успешно приобрели вип на 1 день.\n'
'Приятного пользования!', reply_markup=kb.to_main_kb)
else:
await bot.send_message(call.from_user.id, 'Произошла ошибка.\n'
'Деньги будут возвращены вам в течение 24 часов.',
reply_markup=kb.to_main_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'vip_week')
async def buy_week(call: CallbackQuery):
try:
await call.answer()
url, payment_id = create_payment(100, 'Вип на 1 неделю')
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'<a href="{url}">Оплатить 100 рублей</a>', parse_mode='HTML',
reply_markup=kb.to_buy_kb)
c = 0
paid = False
while True:
if get_payment_status(payment_id) == 'waiting_for_capture':
paid = True
break
elif c == 600:
await bot.send_message(call.from_user.id, 'Платеж отменен.\n'
'Причина: прошло 10 минут с момента создания платежа.',
reply_markup=kb.to_main_kb)
break
else:
await asyncio.sleep(1)
c += 1
if paid:
response = json.loads(confirm_payment(payment_id))
if response['status'] == 'succeeded':
await db.update_vip_ends(str(call.from_user.id),
(datetime.strptime(str(await db.select_vip_ends(str(call.from_user.id))),
'%d.%m.%Y %H:%M') + timedelta(days=7)).strftime(
'%d.%m.%Y %H:%M'))
await bot.send_message(call.from_user.id, 'Вы успешно приобрели вип на 1 неделю.\n'
'Приятного пользования!', reply_markup=kb.to_main_kb)
else:
await bot.send_message(call.from_user.id, 'Произошла ошибка.\n'
'Деньги будут возвращены вам в течение 24 часов.',
reply_markup=kb.to_main_kb)
except Exception as e:
errors.error(e)
@dp.callback_query(F.data == 'vip_month')
async def buy_month(call: CallbackQuery):
try:
await call.answer()
url, payment_id = create_payment(300, 'Вип на 1 месяц')
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text=f'<a href="{url}">Оплатить 300 рублей</a>', parse_mode='HTML',
reply_markup=kb.to_buy_kb)
c = 0
paid = False
while True:
if get_payment_status(payment_id) == 'waiting_for_capture':
paid = True
break
elif c == 600:
await bot.send_message(call.from_user.id, 'Платеж отменен.\n'
'Причина: прошло 10 минут с момента создания платежа.',
reply_markup=kb.to_main_kb)
break
else:
await asyncio.sleep(1)
c += 1
if paid:
response = json.loads(confirm_payment(payment_id))
if response['status'] == 'succeeded':
await db.update_vip_ends(str(call.from_user.id), (
datetime.strptime(str(await db.select_vip_ends(str(call.from_user.id))),
'%d.%m.%Y %H:%M') + timedelta(days=30)).strftime('%d.%m.%Y %H:%M'))
await bot.send_message(call.from_user.id, 'Вы успешно приобрели вип на 1 месяц.\n'
'Приятного пользования!', reply_markup=kb.to_main_kb)
else:
await bot.send_message(call.from_user.id, 'Произошла ошибка.\n'
'Деньги будут возвращены вам в течение 24 часов.',
reply_markup=kb.to_main_kb)
except Exception as e:
errors.error(e)
# Поиск ================================================================================================================
@dp.callback_query(F.data == 'search')
async def search(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
await db.insert_in_queue(str(call.from_user.id), await db.select_sex(str(call.from_user.id)))
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Ищем собеседника... 🔍', reply_markup=kb.cancel_search_kb)
while True:
await asyncio.sleep(0.5)
if await db.find_chat(str(call.from_user.id)) is not None:
await db.update_connect_with(str(call.from_user.id), await db.find_chat(str(call.from_user.id)))
break
while True:
await asyncio.sleep(0.5)
if await db.select_connect_with(str(call.from_user.id)) is not None:
await db.delete_from_queue(str(call.from_user.id))
break
await bot.send_message(call.from_user.id, 'Нашли для тебя собеседника 🥳\n'
'/stop - остановить диалог')
if datetime.strptime(await db.select_vip_ends(str(call.from_user.id)), '%d.%m.%Y %H:%M') > datetime.now():
sex = 'Неизвестно'
user_id = str(await db.select_connect_with(str(call.from_user.id)))
if await db.select_sex(user_id) == 'male':
sex = 'Мужской'
elif await db.select_sex(user_id) == 'female':
sex = 'Женский'
await bot.send_message(call.from_user.id,
f'🅰️ Имя: {await db.select_name(user_id)}\n'
f'🔞 Возраст: {await db.select_age(user_id)}\n'
f'👫 Пол: {sex}\n'
f'👍: {await db.select_likes(user_id)} 👎: {await db.select_dislikes(user_id)}\n', )
await state.set_state(Chatting.msg)
except Exception as e:
errors.error(e)
# Поиск ♂️ =============================================================================================================
@dp.callback_query(F.data == 'search_man')
async def search_man(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
if datetime.strptime(await db.select_vip_ends(str(call.from_user.id)), '%d.%m.%Y %H:%M') > datetime.now():
await db.insert_in_queue_vip(str(call.from_user.id), await db.select_sex(str(call.from_user.id)), 'male')
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Ищем собеседника... 🔍', reply_markup=kb.cancel_search_kb)
while True:
await asyncio.sleep(0.5)
if await db.find_chat_vip(str(call.from_user.id), await db.select_sex(str(call.from_user.id)),
'male') is not None:
await db.update_connect_with(
str(call.from_user.id), await db.find_chat_vip(str(call.from_user.id),
await db.select_sex(str(call.from_user.id)),
'male'))
break
while True:
await asyncio.sleep(0.5)
if await db.select_connect_with(str(call.from_user.id)) is not None:
await db.delete_from_queue(str(call.from_user.id))
break
await bot.send_message(call.from_user.id, 'Нашли для тебя собеседника 🥳\n'
'/stop - остановить диалог')
sex = 'Неизвестно'
user_id = str(await db.select_connect_with(str(call.from_user.id)))
if await db.select_sex(user_id) == 'male':
sex = 'Мужской'
elif await db.select_sex(user_id) == 'female':
sex = 'Женский'
await bot.send_message(call.from_user.id,
f'🅰️ Имя: {await db.select_name(user_id)}\n'
f'🔞 Возраст: {await db.select_age(user_id)}\n'
f'👫 Пол: {sex}\n'
f'👍: {await db.select_likes(user_id)} 👎: {await db.select_dislikes(user_id)}\n')
await state.set_state(Chatting.msg)
else:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Поиск по полу доступен только для вип-пользователей',
reply_markup=kb.sex_search_no_vip_kb)
except Exception as e:
errors.error(e)
# Поиск ♀️ =============================================================================================================
@dp.callback_query(F.data == 'search_woman')
async def search_woman(call: CallbackQuery, state: FSMContext):
try:
await call.answer()
if datetime.strptime(await db.select_vip_ends(str(call.from_user.id)), '%d.%m.%Y %H:%M') > datetime.now():
await db.insert_in_queue_vip(str(call.from_user.id), await db.select_sex(str(call.from_user.id)), 'female')
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Ищем собеседника... 🔍', reply_markup=kb.cancel_search_kb)
while True:
await asyncio.sleep(0.5)
if await db.find_chat_vip(str(call.from_user.id), await db.select_sex(str(call.from_user.id)),
'female') is not None:
await db.update_connect_with(
str(call.from_user.id), await db.find_chat_vip(str(call.from_user.id),
await db.select_sex(str(call.from_user.id)),
'female'))
break
while True:
await asyncio.sleep(0.5)
if await db.select_connect_with(str(call.from_user.id)) is not None:
await db.delete_from_queue(str(call.from_user.id))
break
await bot.send_message(call.from_user.id, 'Нашли для тебя собеседника 🥳\n'
'/stop - остановить диалог')
sex = 'Неизвестно'
user_id = str(await db.select_connect_with(str(call.from_user.id)))
if await db.select_sex(user_id) == 'male':
sex = 'Мужской'
elif await db.select_sex(user_id) == 'female':
sex = 'Женский'
await bot.send_message(call.from_user.id,
f'🅰️ Имя: {await db.select_name(user_id)}\n'
f'🔞 Возраст: {await db.select_age(user_id)}\n'
f'👫 Пол: {sex}\n'
f'👍: {await db.select_likes(user_id)} 👎: {await db.select_dislikes(user_id)}\n')
await state.set_state(Chatting.msg)
else:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Поиск по полу доступен только для вип-пользователей',
reply_markup=kb.sex_search_no_vip_kb)
except Exception as e:
errors.error(e)
# Отменить поиск =======================================================================================================
@dp.callback_query(F.data == 'cancel_search')
async def cancel_search(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Поиск отменен 😥.',
reply_markup=kb.main_kb)
await db.delete_from_queue(str(call.from_user.id))
except Exception as e:
errors.error(e)
# Лайк =================================================================================================================
@dp.callback_query(F.data == 'like')
async def like(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Спасибо за отзыв!', reply_markup=kb.review_kb)
await db.update_likes(await db.select_last_connect(str(call.from_user.id)))
except Exception as e:
errors.error(e)
# Дизлайк ==============================================================================================================
@dp.callback_query(F.data == 'dislike')
async def dislike(call: CallbackQuery):
try:
await call.answer()
await bot.edit_message_text(chat_id=call.from_user.id, message_id=call.message.message_id,
text='Спасибо за отзыв!', reply_markup=kb.review_kb)
await db.update_dislikes(await db.select_last_connect(str(call.from_user.id)))
except Exception as e:
errors.error(e)
# Ссылка ===============================================================================================================
@dp.message(Chatting.msg, Command('link'))
async def link(message: Message):
try:
if message.from_user.username is None:
await message.answer('Введите юзернейм в настройках телеграма!')
else:
await bot.send_message(await db.select_connect_with(str(message.from_user.id)),
f'Собеседник отправил свой юзернейм: @{message.from_user.username}.')
await message.answer('Юзернейм отправлен!')
except Exception as e:
errors.error(e)
# Остановить диалог ====================================================================================================
@dp.message(Chatting.msg, Command('stop'))
async def stop(message: Message, state: FSMContext):
try:
op_state = FSMContext(
storage=storage,
key=StorageKey(
chat_id=int(await db.select_connect_with(str(message.from_user.id))),
user_id=int(await db.select_connect_with(str(message.from_user.id))),
bot_id=bot.id)
)
await bot.send_message(message.from_user.id,
'Диалог остановлен 😞\nВы можете оценить собеседника ниже.',
reply_markup=kb.search_kb)
await bot.send_message(await db.select_connect_with(str(message.from_user.id)),
'Диалог остановлен 😞\nВы можете оценить собеседника ниже.',
reply_markup=kb.search_kb)
await db.update_chats(await db.select_connect_with(str(message.from_user.id)))
await db.update_chats(str(message.from_user.id))
await db.update_last_connect(await db.select_connect_with(str(message.from_user.id)))
await db.update_last_connect(str(message.from_user.id))
await db.update_connect_with(await db.select_connect_with(str(message.from_user.id)), None)
await db.update_connect_with(str(message.from_user.id), None)
await state.clear()
await op_state.clear()
except Exception as e:
errors.error(e)
# Общение ==============================================================================================================
@dp.message(Chatting.msg, F.text)
async def chatting_text(message: Message):
try:
await bot.send_message(await db.select_connect_with(str(message.from_user.id)), message.text)
await db.insert_in_messages(str(message.from_user.id), message.from_user.username, message.text,
datetime.now().strftime('%d.%m.%Y %H:%M:%S'))
await db.update_messages(str(message.from_user.id))
except Exception as e:
errors.error(e)
# Фото =================================================================================================================
@dp.message(Chatting.msg, F.photo)
async def chatting_photo(message: Message):
try:
await bot.send_photo(await db.select_connect_with(str(message.from_user.id)), message.photo[-1].file_id)
except Exception as e:
errors.error(e)
# Видео ================================================================================================================
@dp.message(Chatting.msg, F.video)
async def chatting_video(message: Message):
try:
await bot.send_video(await db.select_connect_with(str(message.from_user.id)), message.video.file_id)
except Exception as e:
errors.error(e)
# Гиф ==================================================================================================================
@dp.message(Chatting.msg, F.animation)
async def chatting_animation(message: Message):
try:
await bot.send_animation(await db.select_connect_with(str(message.from_user.id)), message.animation.file_id)
except Exception as e:
errors.error(e)
# Стикер ===============================================================================================================
@dp.message(Chatting.msg, F.sticker)
async def chatting_sticker(message: Message):
try:
await bot.send_sticker(await db.select_connect_with(str(message.from_user.id)), message.sticker.file_id)
except Exception as e:
errors.error(e)
# Документ =============================================================================================================
@dp.message(Chatting.msg, F.document)
async def chatting_document(message: Message):
try:
await bot.send_document(await db.select_connect_with(str(message.from_user.id)), message.document.file_id)
except Exception as e:
errors.error(e)
# Аудио ================================================================================================================
@dp.message(Chatting.msg, F.audio)
async def chatting_audio(message: Message):
try:
await bot.send_audio(await db.select_connect_with(str(message.from_user.id)), message.audio.file_id)
except Exception as e:
errors.error(e)
# Гс ===================================================================================================================
@dp.message(Chatting.msg, F.voice)
async def chatting_voice(message: Message):
try:
await bot.send_voice(await db.select_connect_with(str(message.from_user.id)), message.voice.file_id)
except Exception as e:
errors.error(e)
# Кружок ===============================================================================================================
@dp.message(Chatting.msg, F.video_note)
async def chatting_video_note(message: Message):
try:
await bot.send_video_note(await db.select_connect_with(str(message.from_user.id)), message.video_note.file_id)
except Exception as e:
errors.error(e)
# Остальное ===============================================================================================================
@dp.message(Chatting.msg, F.unknown)
async def chatting_unknown(message):
try:
await message.answer('Этот тип контента пока не поддерживается 😢.')
except Exception as e:
errors.error(e)
# id ===================================================================================================================
@dp.message(Command('id'))
async def ids(message: Message):
try:
await message.answer(str(message.from_user.id))
except Exception as e:
errors.error(e)
# group id =============================================================================================================
@dp.message(Command('gid'))
async def gids(message: Message):
try:
await message.answer(str(message.chat.id))
except Exception as e:
errors.error(e)
# all ==================================================================================================================
@dp.message()
async def all(message: Message):
try:
if str(message.chat.id) not in [BUGS_GROUP_ID, IDEAS_GROUP_ID]:
await message.answer('Команда не распознана. Отправьте /start для выхода в главное меню.')
except Exception as e:
errors.error(e)
async def main():
await db.connect()
await db.create_tables()
await dp.start_polling(bot)
if __name__ == '__main__':
print(f'Бот запущен ({datetime.now().strftime("%H:%M:%S %d.%m.%Y")}).')
asyncio.run(main())