Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. let request = require('request');
  4.  
  5. class dialogBotVK {
  6.   constructor(login, password) {
  7.     this.login = login,
  8.       this.password = password,
  9.       this.safe     = true,
  10.       this.temp     = false,
  11.       this.tempPoll = {},
  12.       this.tempMsg  = {},
  13.       this.bodyMsg  = {},
  14.       this.typeMsg  = 0,
  15.       this.token    = 0,
  16.       this.botID    = 0;
  17.      
  18.     this.commandBot = [];
  19.     this.wordBot = {
  20.       'hello': {
  21.         1: 'соси хуй',
  22.         2: 'привет',
  23.         3: 'соси залупу'
  24.       }
  25.     };
  26.   }
  27.  
  28.   authBot() {
  29.     try {
  30.       request(this.getObject('auth'), (error, response, body) => {
  31.         if (!error && response.statusCode == 200) {
  32.           this.temp = this.parceJSON(body);
  33.           this.token = this.temp.access_token;
  34.           this.botID = this.temp.user_id;
  35.          
  36.           this.getLongPollServer();
  37.         } else {
  38.           throw new SyntaxError(`Ошибка отправки запроса авторизации, подробнее: ${body}`);
  39.         }
  40.       });
  41.     } catch(e) {
  42.       console.log(e);
  43.     }
  44.   }
  45.  
  46.   getLongPollServer() {
  47.     request(this.getObject('longpollServer'), (error, response, body) => {
  48.       try {
  49.         if (!error && response.statusCode == 200) {
  50.           this.tempPoll = this.parceJSON(body).response;
  51.           this.getLongPollMessage();
  52.         } else {
  53.           throw new SyntaxError(`Ошибка получения данных longpoll сервера, подробнее: ${body}`);
  54.         }
  55.       } catch(e) {
  56.         console.log(e);
  57.       }
  58.     });
  59.   }
  60.  
  61.   getLongPollMessage() {
  62.     request(this.getObject('longpollMessage'), (error, response, body) => {
  63.       try {
  64.         this.tempPoll['ts'] = this.parceJSON(body).ts;
  65.         this.getLongPollMessage().getMessage(this.parceJSON(body).updates);
  66.       } catch(e) {
  67.         this.getLongPollMessage();
  68.       }
  69.     });
  70.     return this;
  71.   }
  72.  
  73.   getMessage(message) {
  74.     try {
  75.       let block = message[0];
  76.       if(6 in block) {
  77.         this.tempMsgID = block[1];
  78.         request(this.getObject('getMessage'), (error, response, body) => {
  79.           if (!error && response.statusCode == 200) {
  80.             this.tempMsg = this.parceJSON(body);
  81.             this.filterMessage();
  82.           } else {
  83.             console.log(`Невозможно получить json данные сообщения ${this.tempMsgID}, подробнее: ${body}`);
  84.           }
  85.         });
  86.       }
  87.     } catch(e) {
  88.       //console.log(e);
  89.     }
  90.   }
  91.  
  92.   filterMessage() {
  93.     try {
  94.       this.bodyMsg = this.tempMsg['response'][1];
  95.      
  96.       if('chat_id' in this.bodyMsg)
  97.         this.typeMsg = 0;
  98.       else
  99.         this.typeMsg = 1;
  100.        
  101.       this.sendMessage('хуй');
  102.       console.log(this.typeMsg, this.tempMsg);
  103.     } catch(e) {
  104.       console.log(e);
  105.     }
  106.   }
  107.  
  108.   sendMessage(messageText) {
  109.     if(this.botID == this.bodyMsg['uid'])
  110.       return false;
  111.    
  112.     let object = {
  113.       url:                  'https://api.vk.com/method/messages.send',
  114.       method:               'GET',
  115.       qs: {
  116.         access_token:       this.token,
  117.         guid:               parseInt(Math.random() * (500010 - 1) + 1),
  118.         message:            messageText
  119.       }
  120.     };
  121.    
  122.     if(this.typeMsg == 0)
  123.       object['qs']['chat_id'] = this.bodyMsg['chat_id'];
  124.     else
  125.       object['qs']['user_id'] = this.bodyMsg['uid'];
  126.    
  127.    
  128.     request(object, (error, response, body) => {
  129.       if (!error && response.statusCode == 200) {
  130.         //console.log(body);
  131.       }
  132.     });
  133.   }
  134.  
  135.   addCommand(text, callback) {
  136.     this.commandBot.push(text, callback);
  137.   }
  138.  
  139.   parceJSON(json) {
  140.     try {
  141.       return JSON.parse(json);
  142.     } catch(e) {
  143.       console.log('Невозможно преобразовать в JSON формат');
  144.     }
  145.   }
  146.  
  147.   getObject(name) {
  148.     switch(name) {
  149.       case 'auth':
  150.         return {
  151.           url:              'https://oauth.vk.com/token',
  152.           method:           'GET',
  153.           qs: {
  154.             grant_type:     'password',
  155.             client_id:      2274003,
  156.             client_secret:  'hHbZxrka2uZ6jB1inYsH',
  157.             username:       this.login,
  158.             password:       this.password
  159.           }
  160.         };
  161.       break;
  162.       case 'longpollServer':
  163.         return {
  164.           url:              'https://api.vk.com/method/messages.getLongPollServer',
  165.           method:           'GET',
  166.           qs: {
  167.             access_token:   this.token
  168.           }
  169.         };
  170.       break;
  171.       case 'longpollMessage':
  172.         return {
  173.           url:              'http://'+this.tempPoll.server,
  174.           method:           'GET',
  175.           qs: {
  176.             act:            'a_check',
  177.             key:            this.tempPoll.key.substr(0, (this.tempPoll.key.length - 10)),
  178.             ts:             this.tempPoll.ts,
  179.             wait:           25,
  180.             mode:           2
  181.           }
  182.         };
  183.       break;
  184.       case 'getMessage':
  185.         return {
  186.           url:              'https://api.vk.com/method/messages.getById',
  187.           method:           'GET',
  188.           qs: {
  189.             access_token:   this.token,
  190.             message_ids:    this.tempMsgID
  191.           }
  192.         };
  193.       break;
  194.     }
  195.   }
  196. }
  197.  
  198. module.exports = dialogBotVK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement