-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVkBot.py
68 lines (49 loc) · 2.08 KB
/
VkBot.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
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api import VkUpload
from VkBotStateHandler import VkBotStateHandler
from VkBotState import VkBotState
class VkBot:
def __init__(self, group_id, group_token):
self.state = VkBotState.INIT_STATE
self.vk_session = vk_api.VkApi(token=group_token)
self.vk = self.vk_session.get_api()
self.longpoll = VkBotLongPoll(self.vk_session, group_id=group_id)
self.state_handler = VkBotStateHandler(self)
self.attachments = {
'init_state': upload_photo(self.vk_session, 'resources/sgn.jpg'),
'shs1_state': upload_photo(self.vk_session, 'resources/sgn1.png'),
'shs2_state': upload_photo(self.vk_session, 'resources/sgn2.png'),
'shs3_state': upload_photo(self.vk_session, 'resources/sgn3.png'),
'shs4_state': upload_photo(self.vk_session, 'resources/sgn4.png')
}
def get_state(self):
return self.state
def set_state(self, state):
self.state = state
def get_vk(self):
return self.vk
def get_attachments(self):
return self.attachments
def start(self):
for event in self.longpoll.listen():
self.step(event)
def step(self, event):
if self.state == VkBotState.INIT_STATE:
self.state_handler.init_state_handler(event)
elif self.state == VkBotState.SHS1_STATE:
self.state_handler.shs1_state_handler(event)
elif self.state == VkBotState.SHS2_STATE:
self.state_handler.shs2_state_handler(event)
elif self.state == VkBotState.SHS3_STATE:
self.state_handler.shs3_state_handler(event)
elif self.state == VkBotState.SHS4_STATE:
self.state_handler.shs4_state_handler(event)
def upload_photo(vk_session, photo):
upload = VkUpload(vk_session)
response = upload.photo_messages(photo)[0]
owner_id = response['owner_id']
photo_id = response['id']
access_key = response['access_key']
attachment = f'photo{owner_id}_{photo_id}_{access_key}'
return attachment