Skip to content

Commit a6884e6

Browse files
Igorpython273
authored andcommitted
Bot longpoll example (#173)
1 parent b598bd3 commit a6884e6

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [Обработка двухфакторной аутентификации](./two_factor_auth.py)
99
* [Обработка капчи](./captcha_handle.py)
1010
* [Работа с пользовательским Long Poll (VkLongpoll)](./longpoll.py)
11+
* [Работа с Bots Long Poll (VkBotLongpoll)](./bot_longpoll.py)
1112
* [Работа с оберткой над execute (VkFunction)](./execute_functions.py)
1213
* [Получение альбомов музыки (VkAudio)](./get_album_audio.py)
1314
* [Получение аудиозаписей (VkAudio)](./get_all_audio.py)

examples/bot_longpoll.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
import vk_api
3+
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
4+
5+
6+
def main():
7+
""" Пример использования bots longpoll
8+
9+
https://vk.com/dev/bots_longpoll
10+
"""
11+
12+
vk_session = vk_api.VkApi(token='your_group_token')
13+
14+
longpoll = VkBotLongPoll(vk_session, 'your_group_id')
15+
16+
for event in longpoll.listen():
17+
18+
if event.type == VkBotEventType.MESSAGE_NEW:
19+
print('Новое сообщение:')
20+
21+
print('Для меня от: ', end='')
22+
23+
print(event.obj.from_id)
24+
25+
print('Текст:', event.obj.text)
26+
print()
27+
28+
elif event.type == VkBotEventType.MESSAGE_REPLY:
29+
print('Новое сообщение:')
30+
31+
print('От меня для: ', end='')
32+
33+
print(event.obj.peer_id)
34+
35+
print('Текст:', event.obj.text)
36+
print()
37+
38+
elif event.type == VkBotEventType.MESSAGE_TYPING_STATE:
39+
print('Печатает ', end='')
40+
41+
print(event.obj.from_id, end=' ')
42+
43+
print('для ', end='')
44+
45+
print(event.obj.to_id)
46+
print()
47+
48+
elif event.type == VkBotEventType.GROUP_JOIN:
49+
print(event.obj.user_id, end=' ')
50+
51+
print('Вступил в группу!')
52+
print()
53+
54+
elif event.type == VkBotEventType.GROUP_LEAVE:
55+
print(event.obj.user_id, end=' ')
56+
57+
print('Покинул группу!')
58+
print()
59+
60+
else:
61+
print(event.type)
62+
print()
63+
64+
65+
if __name__ == '__main__':
66+
main()

vk_api/bot_longpoll.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class VkBotEventType(Enum):
8383
GROUP_CHANGE_SETTINGS = 'group_change_settings'
8484

8585
GROUP_CHANGE_PHOTO = 'group_change_photo'
86-
86+
8787
VKPAY_TRANSACTION = 'vkpay_transaction'
8888

8989

@@ -269,6 +269,7 @@ def listen(self):
269269
270270
:yields: :class:`Event`
271271
"""
272+
272273
while True:
273274
for event in self.check():
274275
yield event

0 commit comments

Comments
 (0)