Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- let request = require('request');
- class dialogBotVK {
- constructor(login, password) {
- this.login = login,
- this.password = password,
- this.safe = true,
- this.temp = false,
- this.tempPoll = {},
- this.tempMsg = {},
- this.bodyMsg = {},
- this.typeMsg = 0,
- this.token = 0,
- this.botID = 0;
- this.commandBot = [];
- this.wordBot = {
- 'hello': {
- 1: 'соси хуй',
- 2: 'привет',
- 3: 'соси залупу'
- }
- };
- }
- authBot() {
- try {
- request(this.getObject('auth'), (error, response, body) => {
- if (!error && response.statusCode == 200) {
- this.temp = this.parceJSON(body);
- this.token = this.temp.access_token;
- this.botID = this.temp.user_id;
- this.getLongPollServer();
- } else {
- throw new SyntaxError(`Ошибка отправки запроса авторизации, подробнее: ${body}`);
- }
- });
- } catch(e) {
- console.log(e);
- }
- }
- getLongPollServer() {
- request(this.getObject('longpollServer'), (error, response, body) => {
- try {
- if (!error && response.statusCode == 200) {
- this.tempPoll = this.parceJSON(body).response;
- this.getLongPollMessage();
- } else {
- throw new SyntaxError(`Ошибка получения данных longpoll сервера, подробнее: ${body}`);
- }
- } catch(e) {
- console.log(e);
- }
- });
- }
- getLongPollMessage() {
- request(this.getObject('longpollMessage'), (error, response, body) => {
- try {
- this.tempPoll['ts'] = this.parceJSON(body).ts;
- this.getLongPollMessage().getMessage(this.parceJSON(body).updates);
- } catch(e) {
- this.getLongPollMessage();
- }
- });
- return this;
- }
- getMessage(message) {
- try {
- let block = message[0];
- if(6 in block) {
- this.tempMsgID = block[1];
- request(this.getObject('getMessage'), (error, response, body) => {
- if (!error && response.statusCode == 200) {
- this.tempMsg = this.parceJSON(body);
- this.filterMessage();
- } else {
- console.log(`Невозможно получить json данные сообщения ${this.tempMsgID}, подробнее: ${body}`);
- }
- });
- }
- } catch(e) {
- //console.log(e);
- }
- }
- filterMessage() {
- try {
- this.bodyMsg = this.tempMsg['response'][1];
- if('chat_id' in this.bodyMsg)
- this.typeMsg = 0;
- else
- this.typeMsg = 1;
- this.sendMessage('хуй');
- console.log(this.typeMsg, this.tempMsg);
- } catch(e) {
- console.log(e);
- }
- }
- sendMessage(messageText) {
- if(this.botID == this.bodyMsg['uid'])
- return false;
- let object = {
- url: 'https://api.vk.com/method/messages.send',
- method: 'GET',
- qs: {
- access_token: this.token,
- guid: parseInt(Math.random() * (500010 - 1) + 1),
- message: messageText
- }
- };
- if(this.typeMsg == 0)
- object['qs']['chat_id'] = this.bodyMsg['chat_id'];
- else
- object['qs']['user_id'] = this.bodyMsg['uid'];
- request(object, (error, response, body) => {
- if (!error && response.statusCode == 200) {
- //console.log(body);
- }
- });
- }
- addCommand(text, callback) {
- this.commandBot.push(text, callback);
- }
- parceJSON(json) {
- try {
- return JSON.parse(json);
- } catch(e) {
- console.log('Невозможно преобразовать в JSON формат');
- }
- }
- getObject(name) {
- switch(name) {
- case 'auth':
- return {
- url: 'https://oauth.vk.com/token',
- method: 'GET',
- qs: {
- grant_type: 'password',
- client_id: 2274003,
- client_secret: 'hHbZxrka2uZ6jB1inYsH',
- username: this.login,
- password: this.password
- }
- };
- break;
- case 'longpollServer':
- return {
- url: 'https://api.vk.com/method/messages.getLongPollServer',
- method: 'GET',
- qs: {
- access_token: this.token
- }
- };
- break;
- case 'longpollMessage':
- return {
- url: 'http://'+this.tempPoll.server,
- method: 'GET',
- qs: {
- act: 'a_check',
- key: this.tempPoll.key.substr(0, (this.tempPoll.key.length - 10)),
- ts: this.tempPoll.ts,
- wait: 25,
- mode: 2
- }
- };
- break;
- case 'getMessage':
- return {
- url: 'https://api.vk.com/method/messages.getById',
- method: 'GET',
- qs: {
- access_token: this.token,
- message_ids: this.tempMsgID
- }
- };
- break;
- }
- }
- }
- module.exports = dialogBotVK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement