|
| 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() |
0 commit comments