Created
March 13, 2019 12:06
-
-
Save FlyInk13/f8b6ec4b21d6250a28285a6f1804ac99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Запрашиваем библиотеку | |
| var VK = require('vk-call').VK; | |
| // Указываем access_token (ключ доступа) | |
| var token = '541a0b8dec33a2098882...4392ccbbd962cf'; | |
| // Версию API | |
| var version = '5.92'; | |
| // Создаем объект для работы с api используя данные выше | |
| var vk = new VK({ | |
| token, | |
| version, | |
| groupId: 179414514, | |
| }); | |
| // Получаем ответ метода | |
| vk.call('groups.getById', { | |
| // Здесь можно указать параметры запроса: | |
| // https://vk.com/dev/groups.getById | |
| // Например: | |
| fields: 'description', | |
| }).then(function onResponse(response) { | |
| console.log(response); | |
| }).catch(function onError(error) { | |
| console.log(error); | |
| }); | |
| // Запускаем лонгполл | |
| var longpoll = vk.persistentLongpoll(); | |
| // Ловим события лонгполла | |
| longpoll.sink.on('data', (events) => { | |
| events.forEach((event) => { | |
| // Логируем новые события | |
| console.log(events); | |
| // Создаем переменную с данными сообщения | |
| var msg = event.object; | |
| // Фильтруем сообщения по тексту | |
| if (msg.text == 'ping') { | |
| vk.call('messages.send', { | |
| message: 'pong', // Текст сообщения | |
| peer_id: msg.peer_id, // ID получателя (msg.from_id, если нужен ID пользователя) | |
| random_id: 0, // Звщита от повторной отправки (0 - выключить) | |
| }); | |
| } | |
| }) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment