forked from LarissaRudenko/Vkinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
139 lines (112 loc) · 4.94 KB
/
interface.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
import os
import vk_api
from dotenv import load_dotenv
from vk_api.longpoll import VkEventType, VkLongPoll
from vk_api.utils import get_random_id
from core import VkTools
from data_base import User
load_dotenv()
community_token = os.getenv('community_token')
access_token = os.getenv('access_token')
my_id = os.getenv('my_id')
class BotInterface():
def __init__(self, community_token, access_token):
self.vk_session = vk_api.VkApi(token=community_token)
self.longpoll = VkLongPoll(self.vk_session)
self.vk_tools = VkTools(access_token)
self.params = {}
self.worksheets = []
self.offset = 0
self.waiting_for_user_info = False
def request_info(self):
for event in self.longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
return event.text
def save_user_info(self, user_id):
self.params = self.vk_tools.get_profile_info(user_id)
if not self.params.get('sex'):
self.send_message(user_id, 'Введите ваш пол (мужской/женский):')
user_sex = self.request_info()
if user_sex.lower() == 'женский':
self.params['sex'] = 1
elif user_sex.lower() == 'мужской':
self.params['sex'] = 2
else:
user_sex = None
self.send_message(user_id, 'Неверное значение для пола')
if not self.params.get('city'):
self.send_message(user_id, 'Введите ваш город:')
self.params['city'] = self.request_info()
if not self.params.get('age'):
self.send_message(
user_id, 'Введите ваш возраст:'
)
self.params['age'] = int(self.request_info())
user = User(
self.params['sex'], self.params['city'], self.params['age']
)
self.send_message(user_id, 'Спасибо! Ваши данные сохранены.')
user_fields = {
'sex': user.profile_sex,
'city': user.profile_city,
'age': user.profile_age,
}
updated_params = self.params.copy()
updated_params.update(user_fields)
return updated_params
def send_message(self, user_id, message, attachment=None):
self.vk_session.method(
'messages.send',
{
'user_id': user_id,
'message': message,
'attachment': attachment,
'random_id': get_random_id()
}
)
def get_photos_from_worksheet(self, worksheet):
photos = self.vk_tools.get_photos(worksheet['id'])
photo_str = ''
for photo in photos:
photo_str += (f'photo{photo["owner_id"]}'
f'_{photo["id"]},')
return photo_str
def event_hanlder(self):
for event in self.longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
if event.text.lower() == 'привет':
self.params = self.vk_tools.get_profile_info(event.peer_id)
if (self.params['age'] is None or self.params['city']
is None or self.params['sex'] is None):
self.waiting_for_user_info = True
updated = self.save_user_info(event.user_id).copy()
self.params = updated
self.waiting_for_user_info = False
self.send_message(
event.user_id, f'Привет, {self.params["name"]}'
)
elif event.text.lower() == 'поиск':
self.send_message(event.user_id, 'Начинаем поиск')
if self.worksheets:
worksheet = self.worksheets.pop()
self.get_photos_from_worksheet(worksheet)
else:
self.worksheets = self.vk_tools.search_worksheet(
self.params, self.offset
)
worksheet = self.worksheets.pop()
self.get_photos_from_worksheet(worksheet)
self.offset += 50
self.send_message(
event.user_id,
(f'Имя {worksheet["name"]} '
f'ссылка: vk.com/id{worksheet["id"]}'),
attachment=self.get_photos_from_worksheet(worksheet)
)
elif event.text.lower() == 'пока':
self.send_message(event.user_id, 'До новых встреч!')
else:
self.send_message(event.user_id, 'Неизвестная команда')
if __name__ == '__main__':
bot_interface = BotInterface(community_token, access_token)
bot_interface.event_hanlder()