Skip to content

Instantly share code, notes, and snippets.

@FlyInk13
Created March 13, 2019 12:06
Show Gist options
  • Save FlyInk13/f8b6ec4b21d6250a28285a6f1804ac99 to your computer and use it in GitHub Desktop.
Save FlyInk13/f8b6ec4b21d6250a28285a6f1804ac99 to your computer and use it in GitHub Desktop.
// Запрашиваем библиотеку
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