From 2a69d390fedfa0715171506b7663835b5f9927c7 Mon Sep 17 00:00:00 2001 From: akilan Date: Sat, 16 May 2020 11:40:08 +0400 Subject: [PATCH] allow mutiple users to use system at the same time --- whatsapp-web.js/website_test.js | 127 +++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 36 deletions(-) diff --git a/whatsapp-web.js/website_test.js b/whatsapp-web.js/website_test.js index 74c0f38..ba29006 100644 --- a/whatsapp-web.js/website_test.js +++ b/whatsapp-web.js/website_test.js @@ -2,53 +2,72 @@ const fs = require('fs'); var request = require('request'); const { Client, Location } = require('./index'); const MessageMedia = require('./src/structures/MessageMedia'); -let product_id = ""; + +// Data structure to store basic information : To ensure that user gets the website genreated wit the data which he wrote +/* +data = [ +{ +id = 971527547820 +csv = "", +basic_data = [] +} +] +*/ + +// Test varaibles to BETA stage +var data = [] +/*let product_id = ""; let product_name = ""; let description = ""; let cost = 0; let product_image = ""; +*/ var a = ""; var array; //Sample Test data given var j = "firstName: 'vfsdf',lastName: 'sdfsdf',companyName: 'sdfsdf',logoUrl: 'sdfsdf',bannerUrl: 'sdfsdfsdf',email: 'sdfsdf@fssdfsfd.com',description: 'sdfsdfsdf',template: 'Colo_Shop'"; const SESSION_FILE_PATH = './session.json'; let sessionCfg; -if (fs.existsSync(SESSION_FILE_PATH)) +if (fs.existsSync(SESSION_FILE_PATH)) { sessionCfg = require(SESSION_FILE_PATH); } const client = new Client({ puppeteer: { headless: false }, session: sessionCfg }); client.initialize(); -client.on('qr', (qr) => +client.on('qr', (qr) => { console.log('QR RECEIVED', qr); }); -client.on('authenticated', (session) => +client.on('authenticated', (session) => { console.log('AUTHENTICATED', session); sessionCfg=session; fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) { - if (err) + if (err) { console.error(err); } }); }); -client.on('auth_failure', msg => +client.on('auth_failure', msg => { console.error('AUTHENTICATION FAILURE', msg); }); -client.on('ready', () => +client.on('ready', () => { console.log('READY'); }); + +// varaible naming to be changed a = "product id,product name,description,cost,product image url"; -client.on('message', async msg => + + +client.on('message', async msg => { console.log('MESSAGE RECEIVED', msg); - if (msg.body == 'new row') + if (msg.body == 'new row') { msg.reply("Enter the following commands:"); client.sendMessage(msg.from, 'product id: *INFORMATION GOES HERE*'); @@ -64,26 +83,29 @@ client.on('message', async msg => cost = 0; product_image = ""; } - else if (msg.body.startsWith('product id:')) + else if (msg.body.startsWith('product id:')) { - product_id = msg.body.slice(11); - console.log("".concat("Product ID = ", product_id)); + id = check_id(msg.id.remote); + data[id].data.push({product_id:msg.body.slice(11)}); + //data[id].data[data.cur_iter_data].product_id= msg.body.slice(11); } - else if (msg.body.startsWith('product name:')) + else if (msg.body.startsWith('product name:')) { - product_name = msg.body.slice(13); - console.log("".concat("Product Name = ", product_name)); + id = check_id(msg.id.remote); + data[id].data[data[id].cur_iter_data].product_name = msg.body.slice(13); } else if (msg.body.startsWith("description:")) { - description = msg.body.slice(12); - console.log("".concat("Description = ", product_name)); + id = check_id(msg.id.remote); + data[id].data[data[id].cur_iter_data].description = msg.body.slice(12); } - else if (msg.body.startsWith('cost:')) + else if (msg.body.startsWith('cost:')) { - cost = Number(msg.body.slice(5)); - console.log("".concat("Cost = ", product_name)); + id = check_id(msg.id.remote); + data[id].data[data[id].cur_iter_data].cost = Number(msg.body.slice(12)); } + // Testing Phase + // Phase in fixing the bug /* else if (msg.body == 'product image:' && msg.hasMedia) { @@ -93,24 +115,29 @@ client.on('message', async msg => } */ else if(msg.body.startsWith('product image:')){ // const attachmentData = Number(msg.body.slice(13)); - product_image = msg.body.slice(13); - console.log("".concat("Product image = ", product_name)); + id = check_id(msg.id.remote); + data[id].data[data[id].cur_iter_data].product_image = msg.body.slice(14); } else if(msg.body == "!done") { var s = ""; var comma = "," ; + id = check_id(msg.id.remote); + // Using currently image Url until issue fixed /* s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma,"data:image/jpeg;base64 ",product_image["data"]); */ -s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma,product_image); + //s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma,product_image); + row = data[id].data[data[id].cur_iter_data]; //console.log(a) - a = a.concat(s) - console.log(a) - + data[id].csv = data[id].csv.concat("\n".concat(row.product_id,comma,row.product_name,comma,row.description,comma,row.cost,comma,row.product_image)); + console.log(data[id].csv); + + data[id].cur_iter_data++; + // fs.writeFileSync('./test.csv', a); } - + else if(msg.body == "!info") { client.sendMessage(msg.from,`*We are still working userflow interactions and use this is the BETA release*. Currently we only accept image url's.`); @@ -124,11 +151,18 @@ s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma } else if(msg.body.startsWith("!details")) { + var message = msg.body.slice(6); array = message.split(","); + + id = check_id(msg.id.remote); + data[id].intial_data = array; + } else if(msg.body == "!finished") { + id = check_id(msg.id.remote); + j = {firstName: "", lastName: "", companyName: "", @@ -139,14 +173,14 @@ s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma template: "Colo_Shop", csv: "" }; - j["firstName"] = array[0]; - j["lastName"] = array[1]; - j["companyName"] = array[2]; - j["logoUrl"] = array[3]; - j["bannerUrl"] = array[4]; - j["email"] = array[5]; - j["description"] = array[6]; - j["csv"] = a ; + j["firstName"] = data[id].intial_data[0]; + j["lastName"] = data[id].intial_data[1]; + j["companyName"] = data[id].intial_data[2]; + j["logoUrl"] = data[id].intial_data[3]; + j["bannerUrl"] = data[id].intial_data[4]; + j["email"] = data[id].intial_data[5]; + j["description"] = data[id].intial_data[6]; + j["csv"] = data[id].csv ; j = JSON.stringify(j); console.log(j) request({ @@ -161,14 +195,35 @@ s = "\n".concat(product_id,comma,product_name,comma,description,comma,cost,comma client.sendMessage(msg.from, process.argv[2] + "/pages/" + JSON.parse(response.body).directory); client.sendMessage(msg.from,"*Website ID:*" + JSON.parse(response.body).directory); client.sendMessage(msg.from,"*Important to remember the ID*"); + a = "product id,product name,description,cost,product image url"; }); // var website_link = "https://localhost:8004" + "/pages/" + data.directory } }); -client.on('disconnected', (reason) => +client.on('disconnected', (reason) => { console.log('Client was logged out', reason); }); + +// Helper functions + +// Checks id if does not exsist adds to the list +function check_id(id){ + var found = 0; + // Checks if the ID exsists + for(var i = 0;i < data.length; i++){ + if(data[i].id == id){ + found = 1; + return i; + } + } + + if(found == 0){ + data.push({id:id,data:[],cur_iter_data:0,csv:"product id,product name,description,cost,product image url"}); + return (data.length - 1); + } + +}