From ee535f790376fa495e668bd456976a1330b0fc94 Mon Sep 17 00:00:00 2001 From: Igor Date: Sun, 2 Sep 2018 20:54:59 +0300 Subject: [PATCH 1/4] Bot longpoll example --- examples/README.md | 1 + examples/bot_longpoll.py | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 examples/bot_longpoll.py 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() From ea7f810cebaded4ba73ee4ede2228a4dea59d741 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 3 Sep 2018 20:54:25 +0300 Subject: [PATCH 2/4] Trailing whitespace --- vk_api/bot_longpoll.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vk_api/bot_longpoll.py b/vk_api/bot_longpoll.py index a3785196..88176d0c 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' From f961d98830bf08cfa98abfbc57475c08af1082cf Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 4 Sep 2018 14:18:03 +0300 Subject: [PATCH 3/4] No traceback on control-c --- vk_api/bot_longpoll.py | 8 ++++++-- vk_api/longpoll.py | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/vk_api/bot_longpoll.py b/vk_api/bot_longpoll.py index 88176d0c..c839e46c 100644 --- a/vk_api/bot_longpoll.py +++ b/vk_api/bot_longpoll.py @@ -269,6 +269,10 @@ def listen(self): :yields: :class:`Event` """ + while True: - for event in self.check(): - yield event + try: + for event in self.check(): + yield event + except KeyboardInterrupt: + break diff --git a/vk_api/longpoll.py b/vk_api/longpoll.py index 69217eb8..4898743c 100644 --- a/vk_api/longpoll.py +++ b/vk_api/longpoll.py @@ -588,5 +588,8 @@ def listen(self): """ while True: - for event in self.check(): - yield event + try: + for event in self.check(): + yield event + except KeyboardInterrupt: + break From 5f514e636c7a0e9764cc521a3007bc3cbcbad0f4 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 5 Sep 2018 15:27:26 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vk_api/bot_longpoll.py | 7 ++----- vk_api/longpoll.py | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/vk_api/bot_longpoll.py b/vk_api/bot_longpoll.py index c839e46c..b7ac2efd 100644 --- a/vk_api/bot_longpoll.py +++ b/vk_api/bot_longpoll.py @@ -271,8 +271,5 @@ def listen(self): """ while True: - try: - for event in self.check(): - yield event - except KeyboardInterrupt: - break + for event in self.check(): + yield event diff --git a/vk_api/longpoll.py b/vk_api/longpoll.py index 4898743c..69217eb8 100644 --- a/vk_api/longpoll.py +++ b/vk_api/longpoll.py @@ -588,8 +588,5 @@ def listen(self): """ while True: - try: - for event in self.check(): - yield event - except KeyboardInterrupt: - break + for event in self.check(): + yield event