Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
55 lines (38 loc) · 1.95 KB
/
Copy pathserver.py
File metadata and controls
55 lines (38 loc) · 1.95 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
import random
import vk_api.vk_api
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api.bot_longpoll import VkBotEventType
from commander import Commander
class Server:
def __init__(self, api_token, group_id, server_name: str="Empty"):
# Даем серверу имя
self.server_name = server_name
self.group_id = group_id
# Для Long Poll
self.vk = vk_api.VkApi(token=api_token)
# Для использоания Long Poll API
self.long_poll = VkBotLongPoll(self.vk, group_id, wait=30)
# Для вызова методов vk_api
self.vk_api = self.vk.get_api()
# Словарь дял каждого отдельного пользователя
self.users = {}
def send_msg(self, send_id, message):
"""
Отправка сообщения через метод messages.send
:param send_id: vk id пользователя, который получит сообщение
:param message: содержимое отправляемого письма
:return: None
"""
return self.vk_api.messages.send(peer_id=send_id,
message=message,
random_id=random.randint(0, 2048),
keyboard=open("keyboards/default.json", "r", encoding="UTF-8").read())
def start(self):
for event in self.long_poll.listen(): # Слушаем сервер
if event.type == VkBotEventType.MESSAGE_NEW:
if event.object.from_id not in self.users:
self.users[event.object.from_id] = Commander()
# Пришло новое сообщение
if event.type == VkBotEventType.MESSAGE_NEW:
self.send_msg(event.object.peer_id,
self.users[event.object.from_id].input(event.object.text))