diff --git a/examples/README.md b/examples/README.md index d0c10cee..27fcdbc6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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) diff --git a/examples/bot_longpoll.py b/examples/bot_longpoll.py new file mode 100644 index 00000000..63ae410b --- /dev/null +++ b/examples/bot_longpoll.py @@ -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() diff --git a/vk_api/bot_longpoll.py b/vk_api/bot_longpoll.py index a3785196..b7ac2efd 100644 --- a/vk_api/bot_longpoll.py +++ b/vk_api/bot_longpoll.py @@ -83,7 +83,7 @@ class VkBotEventType(Enum): GROUP_CHANGE_SETTINGS = 'group_change_settings' GROUP_CHANGE_PHOTO = 'group_change_photo' - + VKPAY_TRANSACTION = 'vkpay_transaction' @@ -269,6 +269,7 @@ def listen(self): :yields: :class:`Event` """ + while True: for event in self.check(): yield event