Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const bodyParser = require('body-parser');
- const request = require('request');
- const config = require('./config');
- const vk = new(require('vk-io'));
- const iii = require('./iii');
- //--------------------------------------
- vk.setToken(config.access_token);
- const app = express();
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(bodyParser.json());
- app.post('/callback', function(req, res) {
- processEvents(req, res)
- });
- app.listen(config.port, function() {
- console.log('We are live on ' + config.port);
- });
- async function processEvents(req, res) {
- if(req.body.type == 'confirmation') {
- res.send(config.group_secret)
- console.log('confirmation');
- } else if(req.body.type == 'message_new') {
- const user_id = req.body.object.user_id;
- let isUser = await isMember(user_id);
- if(!isUser) {
- const subscription_error = "Привет! Сначала подпишись на меня! https://vk.com/botunya !";
- await sendMessage(user_id, subscription_error);
- res.status(200).send('ok');
- }else{
- const message = req.body.object.body;
- const attachments = req.body.object.attachments;
- const isValid = (message.search('/')>= 0)? true : false ;
- const isAttach = ("undefined" !== typeof attachments);
- const isPhoto = isAttach?(attachments[0].type == "photo"): isAttach;
- const isVoice = isAttach?(attachments[0].type == "doc"): isAttach;
- const isSticker = isAttach?(attachments[0].type == "sticker"): isAttach;
- if ( isVoice || isSticker) {
- let answer = 'Я не понимаю, пиши текст!';
- await sendMessage(user_id, answer)
- console.log("Болтаю");
- res.status(200).send('ok');
- }
- if ( !isValid && !isPhoto && !isVoice && !isSticker) {
- let answer = await iii.answer(user_id, message);
- await sendMessage(user_id, answer)
- console.log("Болтаю");
- res.status(200).send('ok');
- }
- if(!isValid && isPhoto) {
- let answer = "Неверный формат текста\n верхний текст/нижний текст - (разделяй символом / ) \nверхний текст/ - (закончи текст символом / ) \n/нижний текст - (начни текст символом / )";
- await sendMessage(user_id, answer)
- console.log("Подсказал");
- res.status(200).send('ok');
- }
- if(isValid && !isPhoto) {
- let answer = "А где картинка? Пришли картинку и текст в ОДНОМ сообщении и получишь мем!";
- await sendMessage(user_id, answer)
- console.log("Подсказал");
- res.status(200).send('ok');
- }
- if( isValid && isPhoto ) {
- let top_text = message.split('/')[0];
- let bottom_text = message.split('/')[1];
- const attach = attachments[0].photo;
- const isSize = ("undefined" !== typeof attach.sizes);
- let photo_url = attach.photo_604;
- if(isSize) {
- photo_url = attachments[0].photo.sizes[3].src;
- }
- console.log(photo_url);
- const gen_url = config.gen_url + '?top_text=' + top_text + '&bottom_text=' + bottom_text + '&url=' + photo_url;
- const mem = await getMem(gen_url, user_id );
- await sendMem(mem.user_id, mem.photo);
- console.log("Отправил мем");
- res.status(200).send('ok');
- }
- }
- //res.status(200).send('ok');
- }
- };
- async function getMem(url, user_id) {
- const upload = await vk.upload.message({
- source: {
- value: request(encodeURI(url)),
- options: {
- filename: 'image.jpg',
- contentType: 'image/jpg'
- }
- }
- });
- const photo = await vk.getAttachment('photo', upload);
- return {photo: photo, user_id: user_id };
- }
- async function sendMessage(user_id, mes) {
- await vk.api.messages.send({
- user_id : user_id,
- message : mes
- });
- };
- async function sendMem(user_id, attach) {
- await vk.api.messages.send({
- user_id : user_id,
- message : '',
- attachment : attach
- });
- };
- async function isMember(user_id) {
- const answer = await vk.api.groups.isMember({
- group_id : '143053431',
- user_id : user_id
- });
- return (answer == 1)?true:false
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement