Skip to content

Commit

Permalink
Bot longpoll example (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor authored and python273 committed Sep 6, 2018
1 parent b598bd3 commit a6884e6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [Обработка двухфакторной аутентификации](./two_factor_auth.py)
* [Обработка капчи](./captcha_handle.py)
* [Работа с пользовательским Long Poll (VkLongpoll)](./longpoll.py)
* [Работа с Bots Long Poll (VkBotLongpoll)](./bot_longpoll.py)
* [Работа с оберткой над execute (VkFunction)](./execute_functions.py)
* [Получение альбомов музыки (VkAudio)](./get_album_audio.py)
* [Получение аудиозаписей (VkAudio)](./get_all_audio.py)
Expand Down
66 changes: 66 additions & 0 deletions examples/bot_longpoll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
3 changes: 2 additions & 1 deletion vk_api/bot_longpoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class VkBotEventType(Enum):
GROUP_CHANGE_SETTINGS = 'group_change_settings'

GROUP_CHANGE_PHOTO = 'group_change_photo'

VKPAY_TRANSACTION = 'vkpay_transaction'


Expand Down Expand Up @@ -269,6 +269,7 @@ def listen(self):
:yields: :class:`Event`
"""

while True:
for event in self.check():
yield event

0 comments on commit a6884e6

Please sign in to comment.