Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const request = require('request');
  4. const config = require('./config');
  5. const vk = new(require('vk-io'));
  6. const iii = require('./iii');
  7. //--------------------------------------
  8. vk.setToken(config.access_token);
  9.  
  10. const app = express();
  11. app.use(bodyParser.urlencoded({ extended: true }));
  12. app.use(bodyParser.json());
  13.  
  14. app.post('/callback', function(req, res) {
  15.     processEvents(req, res)
  16. });
  17.  
  18. app.listen(config.port, function() {
  19.     console.log('We are live on ' + config.port);
  20. });
  21.  
  22. async function processEvents(req, res) {
  23.  
  24.     if(req.body.type == 'confirmation') {
  25.         res.send(config.group_secret)
  26.         console.log('confirmation');
  27.     } else  if(req.body.type == 'message_new') {
  28.  
  29.         const user_id = req.body.object.user_id;
  30.         let isUser = await isMember(user_id);
  31.  
  32.         if(!isUser) {
  33.             const subscription_error = "Привет! Сначала подпишись на меня! https://vk.com/botunya !";
  34.             await sendMessage(user_id, subscription_error);
  35.             res.status(200).send('ok');
  36.         }else{
  37.  
  38.             const message = req.body.object.body;
  39.             const attachments = req.body.object.attachments;
  40.  
  41.             const isValid = (message.search('/')>= 0)? true : false ;
  42.             const isAttach = ("undefined" !== typeof attachments);
  43.  
  44.             const isPhoto = isAttach?(attachments[0].type == "photo"): isAttach;
  45.             const isVoice = isAttach?(attachments[0].type == "doc"): isAttach;
  46.             const isSticker = isAttach?(attachments[0].type == "sticker"): isAttach;
  47.  
  48.  
  49.             if ( isVoice || isSticker) {
  50.                 let answer = 'Я не понимаю, пиши текст!';
  51.                 await sendMessage(user_id, answer)
  52.                 console.log("Болтаю");
  53.                 res.status(200).send('ok');
  54.             }
  55.  
  56.             if ( !isValid && !isPhoto && !isVoice && !isSticker) {
  57.                 let answer = await iii.answer(user_id, message);
  58.                 await sendMessage(user_id, answer)
  59.                 console.log("Болтаю");
  60.                 res.status(200).send('ok');
  61.             }
  62.  
  63.             if(!isValid && isPhoto) {
  64.                 let answer = "Неверный формат текста\n верхний текст/нижний текст - (разделяй символом / ) \nверхний текст/ - (закончи текст символом / ) \n/нижний текст - (начни текст символом / )";
  65.                 await sendMessage(user_id, answer)
  66.                 console.log("Подсказал");
  67.                 res.status(200).send('ok');
  68.             }
  69.  
  70.             if(isValid && !isPhoto) {
  71.                 let answer = "А где картинка? Пришли картинку и текст в ОДНОМ сообщении и получишь мем!";
  72.                 await sendMessage(user_id, answer)
  73.                 console.log("Подсказал");
  74.                 res.status(200).send('ok');
  75.             }
  76.  
  77.             if( isValid && isPhoto ) {
  78.                 let top_text = message.split('/')[0];
  79.                 let bottom_text = message.split('/')[1];
  80.  
  81.                 const attach = attachments[0].photo;
  82.                 const isSize = ("undefined" !== typeof attach.sizes);
  83.  
  84.                 let photo_url = attach.photo_604;
  85.  
  86.                 if(isSize) {
  87.                     photo_url = attachments[0].photo.sizes[3].src;
  88.                 }
  89.  
  90.                 console.log(photo_url);
  91.  
  92.                 const gen_url = config.gen_url + '?top_text=' + top_text + '&bottom_text=' + bottom_text + '&url=' + photo_url;
  93.                 const mem = await getMem(gen_url, user_id );
  94.                 await sendMem(mem.user_id, mem.photo);
  95.  
  96.                 console.log("Отправил мем");
  97.                 res.status(200).send('ok');
  98.             }
  99.         }
  100.         //res.status(200).send('ok');
  101.     }
  102. };
  103.  
  104. async function getMem(url, user_id) {
  105.  
  106.     const upload = await vk.upload.message({
  107.         source: {
  108.             value: request(encodeURI(url)),
  109.             options: {
  110.                 filename: 'image.jpg',
  111.                 contentType: 'image/jpg'
  112.             }
  113.         }
  114.     });
  115.     const photo = await vk.getAttachment('photo', upload);
  116.     return {photo: photo, user_id: user_id };
  117. }
  118.  
  119. async function sendMessage(user_id, mes) {
  120.    await vk.api.messages.send({
  121.         user_id : user_id,
  122.         message : mes
  123.     });
  124. };
  125.  
  126. async function sendMem(user_id, attach) {
  127.    await vk.api.messages.send({
  128.         user_id : user_id,
  129.         message : '',
  130.         attachment : attach
  131.     });
  132. };
  133.  
  134. async function isMember(user_id) {
  135.     const answer = await vk.api.groups.isMember({
  136.         group_id : '143053431',
  137.         user_id : user_id
  138.     });
  139.     return (answer == 1)?true:false
  140. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement