forked from python273/vk_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longpoll.py
65 lines (47 loc) · 1.94 KB
/
longpoll.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
# -*- coding: utf-8 -*-
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
def main():
""" Пример использования longpoll
https://vk.com/dev/using_longpoll
https://vk.com/dev/using_longpoll_2
"""
login, password = 'python@vk.com', 'mypassword'
vk_session = vk_api.VkApi(login, password)
try:
vk_session.auth()
except vk_api.AuthError as error_msg:
print(error_msg)
return
longpoll = VkLongPoll(vk_session)
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
print('Новое сообщение:')
if event.from_me:
print('От меня для: ', end='')
elif event.to_me:
print('Для меня от: ', end='')
if event.from_user:
print(event.user_id)
elif event.from_chat:
print(event.user_id, 'в беседе', event.chat_id)
elif event.from_group:
print('группы', event.group_id)
print('Текст: ', event.text)
print()
elif event.type == VkEventType.USER_TYPING:
print('Печатает ', end='')
if event.from_user:
print(event.user_id)
elif event.from_group:
print('администратор группы', event.group_id)
elif event.type == VkEventType.USER_TYPING_IN_CHAT:
print('Печатает ', event.user_id, 'в беседе', event.chat_id)
elif event.type == VkEventType.USER_ONLINE:
print('Пользователь', event.user_id, 'онлайн', event.platform)
elif event.type == VkEventType.USER_OFFLINE:
print('Пользователь', event.user_id, 'оффлайн', event.offline_type)
else:
print(event.type, event.raw[1:])
if __name__ == '__main__':
main()