-
Notifications
You must be signed in to change notification settings - Fork 1
/
vkbot.py
47 lines (42 loc) · 1.67 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
import vk_api
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.longpoll import VkLongPoll
import config
from vk_api.utils import get_random_id
class VKBot:
"""
Creates a class to work with chatbot VK
"""
def __init__(self):
self.token = config.token_vkinder
self.vk_session = vk_api.VkApi(token=self.token)
self.longpoll = VkLongPoll(self.vk_session)
self.keyboard = self.current_keyboard()
def send_msg(self, user_id, message, attachment=None):
"""
Sends a new message to the user in the chat.
:param user_id: int
:param message: string
:param attachment: string
"""
self.vk_session.method('messages.send',
{'user_id': user_id,
'message': message,
'random_id': get_random_id(),
'keyboard': self.keyboard,
'attachment': attachment})
@staticmethod
def current_keyboard():
"""
Creates a keyboard to interact with the chatbot.
:return Keyboard JSON-object
"""
keyboard = VkKeyboard(one_time=False)
keyboard.add_button('Show', color=VkKeyboardColor.PRIMARY)
keyboard.add_line()
keyboard.add_button('Add to favorites', color=VkKeyboardColor.POSITIVE)
keyboard.add_button('No, thank you', color=VkKeyboardColor.NEGATIVE)
keyboard.add_line()
keyboard.add_button('Favorites list', color=VkKeyboardColor.SECONDARY)
keyboard.add_button('Black list', color=VkKeyboardColor.SECONDARY)
return keyboard.get_keyboard()