foundation is set
This commit is contained in:
53
whatsapp-web.js/index.js
Normal file
53
whatsapp-web.js/index.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
var request = require('request');
|
||||||
|
const { Client } = require('whatsapp-web.js');
|
||||||
|
const onMessage = require('./src/userInteraction/messages');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const SESSION_FILE_PATH = './session.json';
|
||||||
|
let sessionCfg;
|
||||||
|
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) =>
|
||||||
|
{
|
||||||
|
console.log('QR RECEIVED', qr);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('authenticated', (session) =>
|
||||||
|
{
|
||||||
|
console.log('AUTHENTICATED', session);
|
||||||
|
sessionCfg=session;
|
||||||
|
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
client.on('auth_failure', (msg) =>
|
||||||
|
{
|
||||||
|
console.error('AUTHENTICATION FAILURE', msg);
|
||||||
|
});
|
||||||
|
client.on('ready', () =>
|
||||||
|
{
|
||||||
|
console.log('READY');
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('message', async (msg) => {
|
||||||
|
|
||||||
|
onMessage(msg, client);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('disconnected', (reason) =>
|
||||||
|
{
|
||||||
|
console.log('Client was logged out', reason);
|
||||||
|
});
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "test.js",
|
"main": "test.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "node website_test.js http://localhost:8004",
|
||||||
|
"start": "node index.js"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
|||||||
4
whatsapp-web.js/src/businessLogic/logic.js
Normal file
4
whatsapp-web.js/src/businessLogic/logic.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
module.exports.onWG = () => {
|
||||||
|
console.log('hello world');
|
||||||
|
}
|
||||||
50
whatsapp-web.js/src/models/product.js
Normal file
50
whatsapp-web.js/src/models/product.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
class Product {
|
||||||
|
|
||||||
|
constructor({ id = '', name = '', desc = '', cost = 0, image = null }) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.desc = desc;
|
||||||
|
this.cost = cost;
|
||||||
|
this.image = image;
|
||||||
|
}
|
||||||
|
|
||||||
|
getId(){
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
setName(name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
getName(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
setDesc(desc){
|
||||||
|
this.desc = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
getDesc(){
|
||||||
|
return this.desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
setCost(cost){
|
||||||
|
this.cost = cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCost(){
|
||||||
|
return this.cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
setImage(image){
|
||||||
|
this.imageUrl = image;
|
||||||
|
}
|
||||||
|
|
||||||
|
getImage(){
|
||||||
|
return this.imageUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.Product = Product;
|
||||||
23
whatsapp-web.js/src/models/website.js
Normal file
23
whatsapp-web.js/src/models/website.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
const {Product} = require('./product');
|
||||||
|
|
||||||
|
class Website {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Map<string,string>} details
|
||||||
|
*/
|
||||||
|
constructor(details){
|
||||||
|
this.details = details;
|
||||||
|
this.products = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Product} product
|
||||||
|
*/
|
||||||
|
addProduct(product){
|
||||||
|
this.products.push(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.Website = Website;
|
||||||
23
whatsapp-web.js/src/userInteraction/commands.js
Normal file
23
whatsapp-web.js/src/userInteraction/commands.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
class Command{
|
||||||
|
|
||||||
|
|
||||||
|
constructor({command, message, callback}){
|
||||||
|
this.command = command;
|
||||||
|
this.message = message;
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCommand(){
|
||||||
|
return this.command;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCallback(){
|
||||||
|
return this.callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
getMessage(){
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.Command = Command;
|
||||||
52
whatsapp-web.js/src/userInteraction/messages.js
Normal file
52
whatsapp-web.js/src/userInteraction/messages.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
|
||||||
|
const { Command } = require("./commands");
|
||||||
|
const { onWG } = require("../businessLogic/logic");
|
||||||
|
|
||||||
|
///////////
|
||||||
|
//add the commands here
|
||||||
|
///////////
|
||||||
|
const commands = [
|
||||||
|
|
||||||
|
new Command({
|
||||||
|
command: 'wg',
|
||||||
|
message : [
|
||||||
|
`stop it and get some help by using the following command *wg help*`,
|
||||||
|
`Hope everything works well`
|
||||||
|
] ,
|
||||||
|
callback : () => {
|
||||||
|
onWG();
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports= onMessage = (message, client) => {
|
||||||
|
|
||||||
|
let command = null;
|
||||||
|
|
||||||
|
//if the message start with wg then proceed
|
||||||
|
if(message.body.startsWith("wg"))
|
||||||
|
{
|
||||||
|
for (let c of commands){
|
||||||
|
if(c.getCommand() == message.body)
|
||||||
|
{
|
||||||
|
command = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(command != null){
|
||||||
|
command.getCallback()();
|
||||||
|
for (let msg of command.getMessage())
|
||||||
|
{
|
||||||
|
client.sendMessage(message.from, msg, new Object());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//export default onMessage;
|
||||||
@@ -34,6 +34,7 @@ if (fs.existsSync(SESSION_FILE_PATH))
|
|||||||
|
|
||||||
const client = new Client({ puppeteer: { headless: false }, session: sessionCfg });
|
const client = new Client({ puppeteer: { headless: false }, session: sessionCfg });
|
||||||
client.initialize();
|
client.initialize();
|
||||||
|
|
||||||
client.on('qr', (qr) =>
|
client.on('qr', (qr) =>
|
||||||
{
|
{
|
||||||
console.log('QR RECEIVED', qr);
|
console.log('QR RECEIVED', qr);
|
||||||
|
|||||||
Reference in New Issue
Block a user