Last active
March 9, 2019 13:19
-
-
Save Termina1/18bf8d2c19f224b30c8f366d2ce64358 to your computer and use it in GitHub Desktop.
Примеры ботов
This file contains 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, | |
}); | |
// Запускаем лонгполл | |
var longpoll = vk.persistentLongpoll(); | |
// Ловим события лонгполла | |
longpoll.sink.on('data', (events) => { | |
events.forEach((event) => { | |
var msg = event.object; | |
console.log(msg); | |
if (msg.text == 'ping') { | |
vk.call('messages.send', { | |
message: 'pong', | |
peer_id: msg.peer_id, | |
random_id: 0 | |
}); | |
} | |
if (msg.text == 'pong') { | |
vk.call('messages.send', { | |
message: 'ping', | |
peer_id: msg.peer_id, | |
random_id: 0 | |
}); | |
} | |
}) | |
}); |
This file contains 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
# -*- coding: utf-8 -*- | |
import vk_api | |
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType | |
from vk_api.utils import get_random_id | |
def bot_process(vk, event): | |
if event.object.text == 'ping': | |
vk.messages.send( | |
peer_id=event.object.from_id, | |
random_id=get_random_id(), | |
message='pong' | |
) | |
elif event.object.text == 'pong': | |
vk.messages.send( | |
peer_id=event.object.from_id, | |
random_id=get_random_id(), | |
message='ping' | |
) | |
def main(): | |
vk_session = vk_api.VkApi(token='token') | |
vk = vk_session.get_api() | |
longpoll = VkBotLongPoll(vk_session, 'group_id') | |
for event in longpoll.listen(): | |
if event.type == VkBotEventType.MESSAGE_NEW: | |
bot_process(vk, event) | |
else: | |
print(event.type) | |
print('Неизвестное событие') | |
print() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment