forked from python273/vk_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_longpoll.py
66 lines (41 loc) · 1.58 KB
/
bot_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
66
# -*- coding: utf-8 -*-
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
def main():
""" Пример использования bots longpoll
https://vk.com/dev/bots_longpoll
"""
vk_session = vk_api.VkApi(token='your_group_token')
longpoll = VkBotLongPoll(vk_session, 'your_group_id')
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
print('Новое сообщение:')
print('Для меня от: ', end='')
print(event.obj.from_id)
print('Текст:', event.obj.text)
print()
elif event.type == VkBotEventType.MESSAGE_REPLY:
print('Новое сообщение:')
print('От меня для: ', end='')
print(event.obj.peer_id)
print('Текст:', event.obj.text)
print()
elif event.type == VkBotEventType.MESSAGE_TYPING_STATE:
print('Печатает ', end='')
print(event.obj.from_id, end=' ')
print('для ', end='')
print(event.obj.to_id)
print()
elif event.type == VkBotEventType.GROUP_JOIN:
print(event.obj.user_id, end=' ')
print('Вступил в группу!')
print()
elif event.type == VkBotEventType.GROUP_LEAVE:
print(event.obj.user_id, end=' ')
print('Покинул группу!')
print()
else:
print(event.type)
print()
if __name__ == '__main__':
main()