-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
185 lines (156 loc) · 6.87 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
import os
import json
import random
import vk_api
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import requests
from dotenv import load_dotenv
class Bot:
dotenv_path = '.env'
settings_path = 'settings.cfg'
base = 'https://api.vk.com/method/'
def __init__(self):
if os.path.exists(self.dotenv_path):
load_dotenv(self.dotenv_path)
self.key = None
self.server = None
self.ts = None
self.params = {'group_id': os.environ.get("GROUP_ID"),
'access_token': os.environ.get("GROUP_TOKEN"),
'v': '5.131'}
vk_session = vk_api.VkApi(token=os.environ.get("GROUP_TOKEN"))
self.vk = vk_session.get_api()
def get_server(self):
"""
Getting session data (required when first addressing to long poll)
:return: "session data successfully received" OR error message
"""
method = 'groups.getLongPollServer'
response = requests.get(self.base + method, params=self.params)
try:
self.key = response.json()['response']['key']
self.server = response.json()['response']['server']
self.ts = response.json()['response']['ts']
return "session data successfully received"
except KeyError:
return f'{response.json().get("error", {}).get("error_msg", "unknown error")}'
def get_settings(self):
"""
Saving settings of which events to operate with to a file --> "self.settings_path"
In case of an error, the error message is saved to the file.
:return: None
"""
method = 'groups.getLongPollSettings'
with open(self.settings_path, 'wt', encoding='UTF-8') as settings_file:
response = requests.get(self.base + method, params=self.params)
settings = response.json().get('response', {}).get('events', 'could not receive settings')
json.dump(settings, fp=settings_file, indent=4)
def set_settings(self):
"""
Commit new settings from file located at "self.settings_path"
:return: 'settings successfully changed' OR error message
"""
method = 'groups.setLongPollSettings'
with open(self.settings_path, 'rt', encoding='UTF-8') as settings_file:
settings = json.load(settings_file)
params = {**self.params, **settings}
response = requests.get(self.base + method, params=params)
if response.json().get('response') == 1:
return 'settings successfully changed'
else:
return response.json().get('error', {}).get('error_msg', 'unknown error')
def listen(self):
"""
Send a long poll request with 25 seconds timeout that check's if a message
has been sent to the group.
:return: A tuple of:
Message sender's user id
Text of the message
"""
url = f'{self.server}?act=a_check&key={self.key}&ts={self.ts}&wait=25'
response = requests.get(url, params=self.params)
self.ts = response.json().get('ts')
if response.json().get('updates'):
return (
response.json()['updates'][0]['object']['message']['from_id'],
response.json()['updates'][0]['object']['message']['text']
)
else:
return None
def say(self, recipient: int, message: str):
"""
Simple message sending method.
recipient: user id
message: text
"""
random_id = random.randint(-2147483648, 2147483647)
self.vk.messages.send(user_id=recipient, message=message, random_id=random_id)
def suggest(self, recipient: int, name: str, link: str, photos: list):
message = f'Я нашел для тебя отличный вариант для знакомства!\n\n' \
f'{name}\n' \
f'{link}\n\n'
attachment = ','.join(photos)
random_id = random.randint(-2147483648, 2147483647)
keyboard = VkKeyboard(one_time=False)
keyboard.add_button('next', color=VkKeyboardColor.PRIMARY)
keyboard.add_button('blacklist', color=VkKeyboardColor.NEGATIVE)
keyboard.add_button('favorites', color=VkKeyboardColor.POSITIVE)
keyboard.add_button('saved', color=VkKeyboardColor.PRIMARY)
self.vk.messages.send(user_id=recipient,
message=message,
attachment=attachment,
random_id=random_id,
keyboard=keyboard.get_keyboard())
def get_users_details(self, user: int):
method = 'users.get'
data = {'user_ids': f"{user}", 'fields': 'city, sex, bdate, interests'}
params = {**self.params, **data}
response = requests.get(self.base + method, params=params)
return response.json()['response'][0]
class Searcher(Bot):
scripts_path = 'vk_scripts/'
def __init__(self):
if os.path.exists(self.dotenv_path):
load_dotenv(self.dotenv_path)
access_token = os.environ.get("USER_TOKEN")
vk_session = vk_api.VkApi(token=access_token)
self.vk = vk_session.get_api()
def get_photos_and_details(self, account):
"""
Executes a vk script "get_photos_and_details".
account: {'id': int,
'bdate': str,
'city': {'id': int, 'title': str},
'interests': str,
'sex': int,
'first_name': str,
'last_name': str}
:return: A complex nested list with albums and photos in these albums.
"""
with open(self.scripts_path + 'get_photos_and_details') as f:
code = f.read().replace('<id>', str(account))
try:
response = self.vk.execute(code=code)
return response
except vk_api.exceptions.ApiError as e:
return e
def search_users(self, criteria):
"""
criteria: {'city': int,
'sex': int,
'age_to': int,
'age_from': int,
'interests': [str, ...]}
:return: vk.com API users.search output. A complex json with accounts matching the criteria.
IMPORTANT: Not all results fully match the criteria due to peculiarities of vk search algorythm.
"""
with open(self.scripts_path + 'users.search') as f:
code = f.read().replace('<city>', str(criteria['city']))\
.replace('<sex>', str(criteria['sex']))\
.replace('<age_from>', str(criteria['age_from']))\
.replace('<age_to>', str(criteria['age_to']))
try:
response = self.vk.execute(code=code)
return response[0]['items'] if response else None
except vk_api.exceptions.ApiError as e:
return e