Merge pull request #7 from HWTechClub/npmBot

Npm bot
This commit is contained in:
ashab272000
2020-07-21 14:56:58 +04:00
committed by GitHub
10 changed files with 236 additions and 41 deletions

View File

@@ -15,7 +15,6 @@ if (fs.existsSync(SESSION_FILE_PATH))
const client = new Client({ puppeteer: { headless: false }, session: sessionCfg });
client.initialize();
client.on('qr', (qr) =>
{
console.log('QR RECEIVED', qr);
@@ -32,10 +31,12 @@ client.on('authenticated', (session) =>
}
});
});
client.on('auth_failure', (msg) =>
{
console.error('AUTHENTICATION FAILURE', msg);
});
client.on('ready', () =>
{
console.log('READY');

View File

@@ -1,4 +1,20 @@
const { Website } = require("../models/website");
const { data } = require("../models/data");
module.exports.onWG = () => {
console.log('hello world');
}
module.exports.onCreate = (id, companyName) => {
//get the user
let user = data.getUser(id);
if(user == undefined)
{
data.addUser(id);
user = data.getUser(id);
}
//add the website with the company name
user.addWebsite(new Website(companyName));
}

View File

@@ -0,0 +1,47 @@
# Commands available for user interaction
*wg* -> Command wg help to get the avaialabe instructions
*wg help* -> The following are the available commands:
*wg create <company name> -> creates a website without details
# Note:
After the user uses *wg create <company name>*. The bot should instruct the user to enter the rest of the details.
To see the details to be entered, please check *website.js* under models folder.
The commands should follow the rules below :
*wg website firstName*
*wg website lastName*
...etc
# Following commands only works when a website is created
*wg product all* -> shows all the products that are created
*wg product new* -> creates a new product with generated Id
*wg product <id>* -> if the id exists, the product with “id“ is selected, else, create a new product with the id given
# NOTE :
User should be given feedback on their commands. For example:
If a user created a product, the bot should reply, "a product has been created with the id : <id>"
If a user created a product without a website, the bot should handle the error and reply, "A website should be selected or created before creating a product"
# Following command only works if a product is selected
*wg product info* -> shows the information about the product. Ie name, cost, description, image
*wg product name* <product-name> -> set the name of the product
*wg product cost* <product-cost> -> set the cost of the product
*wg product desc* <product-desc> -> set the description of the product
*wg product image* <product-image> -> sets the image of the product. The caption of the image should be the command.
*wg finished* -> creates the website and gives a link to the website maker
# delete
Users should be able to delete products and website
*wg delete product <id>*
*wg delete website <company name>*
# Note:
If you think these commands are not enough, please add them in a seperate section with a clear defination.

View File

@@ -0,0 +1,10 @@
class Command{
constructor({command,callback}){
this.command = command;
this.callback = callback;
}
}
module.exports.Command = Command;

View File

@@ -0,0 +1,50 @@
const { User } = require("./user");
const { Website } = require("./website");
module.exports.data = new StructuredData();
class StructuredData {
constructor() {
this.data={};
}
addUser(id){
if(this.data[id] != undefined)
{
this.data[id] = new User(id);
return true
}
return false;
}
/**
*
* @param {number} id
* @returns {User}
*/
getUser(id){
return this.data[id];
}
deleteUser(id){
if(this.data[id] != undefined)
{
return delete this.data[id];
}
return false;
}
}
/*
data structured:
data = {
<id of user> : <Instance of User>,
<id of user> : <Instance of User>
}
*/

View File

@@ -0,0 +1,43 @@
const {Website} = require('./website');
class User{
constructor(id){
this.id = id;
//uses key value pair to store website
//key is the companyName of the website
//value is the website itself
this.websites = {};
}
/**
*
* @param {Website} website
*/
addWebsite(website){
if(this.websites[website.companyName] != undefined)
{
return false;
}else{
this.websites[website.companyName] = website;
return true;
}
}
deleteWebsite(companyName){
if(this.websites[companyName] != undefined)
{
return delete this.websites[companyName];
}else{
return false
}
}
getWebsite(companyName){
return this.websites[companyName];
}
}
module.exports.User = User;

View File

@@ -3,11 +3,20 @@ const {Product} = require('./product');
class Website {
/**
* @param {Map<string,string>} details
* @param {Object} details
*/
constructor(details){
this.details = details;
this.products = [];
constructor(companyName){
this.firstName = '';
this.lastName = '';
this.companyName = companyName;
this.logoUrl = '';
this.bannerUrl = '';
this.desc = '';
this.email = '';
//uses key value pair to store product
//key is the productId of the product
//value is the product itself
this.products = {};
}
/**
@@ -15,9 +24,38 @@ class Website {
* @param {Product} product
*/
addProduct(product){
this.products.push(product);
if(this.products[product.id] == undefined)
{
this.products[product.id] = product;
return true
}
return false;
}
/**
*
* @param {string} productId
* @returns {Product}
*/
getProduct(productId){
return this.products[productId];
}
/**
*
* @param {string} productId
*/
deleteProduct(productId){
if(this.products[productId] != undefined)
{
return delete this.products[productId];
}
return false;
}
}
module.exports.Website = Website;

View File

@@ -0,0 +1,6 @@
1. add the commands
2. add the logic for every commands
3. add the messages with the logic on the command callback function

View File

@@ -1,23 +0,0 @@
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;

View File

@@ -1,20 +1,26 @@
const { Command } = require("./commands");
const { onWG } = require("../businessLogic/logic");
const { Command } = require("../models/commands");
const logic = require("../businessLogic/logic");
///////////
//add the commands here
///////////
const commands = [
new Command({
//User command
command: 'wg',
message : [
`stop it and get some help by using the following command *wg help*`,
`Hope everything works well`
] ,
//function to be executed
callback : () => {
onWG();
logic.onWG();
//mesage to be sent to the user
let message = [
`stop it and get some help by using the following command *wg help*`,
`Hope everything works well`
];
return message;
}
}),
@@ -22,7 +28,7 @@ const commands = [
module.exports= onMessage = (message, client) => {
const onMessage = (message, client) => {
let command = null;
@@ -30,7 +36,7 @@ module.exports= onMessage = (message, client) => {
if(message.body.startsWith("wg"))
{
for (let c of commands){
if(c.getCommand() == message.body)
if(c.command == message.body)
{
command = c;
break;
@@ -38,8 +44,8 @@ module.exports= onMessage = (message, client) => {
}
if(command != null){
command.getCallback()();
for (let msg of command.getMessage())
let messageToBeSent = command.callback();
for (let msg of messageToBeSent)
{
client.sendMessage(message.from, msg, new Object());
}
@@ -48,5 +54,6 @@ module.exports= onMessage = (message, client) => {
};
module.exports.onMessage = onMessage;
//export default onMessage;