From 60367476d87587bfceff7ea5736e9765a9cb2d32 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 22 Jul 2020 10:10:57 +0000 Subject: [PATCH 1/3] Bump lodash from 4.17.15 to 4.17.19 in /website-generator Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.15...4.17.19) Signed-off-by: dependabot[bot] --- website-generator/package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website-generator/package-lock.json b/website-generator/package-lock.json index e1f4ad6..1b3a44b 100644 --- a/website-generator/package-lock.json +++ b/website-generator/package-lock.json @@ -1132,9 +1132,9 @@ "integrity": "sha1-ZMTwJfF/1Tv7RXY/rrFvAVp0dVA=" }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + "version": "4.17.19", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" }, "lodash.debounce": { "version": "4.0.8", From 507e62e049ab2182ea5f365451573e8ae1739436 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Fri, 24 Jul 2020 14:58:32 +0400 Subject: [PATCH 2/3] added input functionalities on command --- whatsapp-web.js/src/models/commands.js | 101 ++++++++++++++- whatsapp-web.js/test/test_command.js | 167 +++++++++++++++++++++++++ 2 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 whatsapp-web.js/test/test_command.js diff --git a/whatsapp-web.js/src/models/commands.js b/whatsapp-web.js/src/models/commands.js index b64187c..be55ada 100644 --- a/whatsapp-web.js/src/models/commands.js +++ b/whatsapp-web.js/src/models/commands.js @@ -1,10 +1,109 @@ class Command{ - + /** + * + * @param {{string, function()}} param0 + */ constructor({command,callback}){ this.command = command; this.callback = callback; + this.requireInput = false; + this.inputPos = []; + this._requireInput(); + this._getInputPosition(); } + + /** + * + * @param {string} message + */ + equals(message){ + let msg = message.trim(); + + //check if the command is exactly equals to the msg + //if mathces then return true + //else if we check if the command requires an input + //if not, then we return false + if(this.command == msg){ + return true + }else if(this.requireInput){ + //the command requires input + + let splitCommand = this.command.split(' '); + let splitMsg = msg.split(' '); + + //check the number of words both has + //if not same then return false + //else iterate through all the words + + + if(splitCommand.length == splitMsg.length) + { + for(let i = 0; i < splitMsg.length; i++) + { + //if words matches then continue + //if the word is a input then continue + //else return false + if(splitMsg[i] == splitCommand[i]){ + continue; + }else if(splitCommand[i].match(/[<>]/g).length > 1){ + continue; + }else{ + return false; + } + } + // all the words are matched to each other or to an input field + return true; + } + //the message is not the same as the command + return false; + } + return false; + } + + /** + * Only used in class + * + * This determines if the command requires input + * if the command has in the string + * then the command requires input + */ + _requireInput(){ + let msg = this.command; + + let matched = msg.match(/[<>]/g); + + this.requireInput = matched != null && matched.length > 1; + } + + _getInputPosition(){ + let splitCommand = this.command.split(' '); + let inputPos = []; + for (let i = 0; i < splitCommand.length; i++) + { + if(splitCommand[i].startsWith('<')){ + inputPos.push(i); + } + } + + this.inputPos = inputPos; + } + + /** + * returns an array of string representing the inputs of the command + * @param {string} message + */ + getInput(message){ + + let splitMsg = message.split(' '); + let input = []; + for(let pos of this.inputPos){ + input.push(splitMsg[pos]); + } + + return input; + } + } module.exports.Command = Command; diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js new file mode 100644 index 0000000..01e4772 --- /dev/null +++ b/whatsapp-web.js/test/test_command.js @@ -0,0 +1,167 @@ +const { Command } = require('../src/models/commands'); + +const expect = require('chai').expect; + +describe('Command', () => { + + it('Test : requireInput() without input', () => { + let a = new Command({ + command: "wg create", + callback: () => { + + } + }); + + expect(a.requireInput).to.false; + }); + + it('Test : requireInput() with input', () => { + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.requireInput).to.true; + }); + + it('Test : requireInput() with 2 inputs', () => { + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.requireInput).to.true; + }); + + it('Test : equals() without input', () => { + + let message = 'wg website'; + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + let b = new Command({ + command: "wg website", + callback: () => { + + } + }); + + let c = new Command({ + command: "wg product p12", + callback: () => { + + } + }); + + let d = new Command({ + command: "wg", + callback: () => { + + } + }); + + expect(a.equals(message)).to.false; + expect(b.equals(message)).to.true; + expect(c.equals(message)).to.false; + expect(d.equals(message)).to.false; + }); + + + it('Test : equals() with input', () => { + + let message = 'wg create p12'; + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.equals(message)).to.true; + + }); + + it('Test : equals() with spelling error', () => { + + let message = 'wg websit'; + + let b = new Command({ + command: "wg website", + callback: () => { + + } + }); + + expect(b.equals(message)).to.false; + + }); + it('Test : equals() with extra inputs', () => { + + let message = 'wg create p12 pr'; + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + + expect(a.equals(message)).to.false; + }); + + it('Test : _getInputPosition()', () => { + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.inputPos[0]).to.equal(2); + + }) + + it('Test : getInput() with 1 input', () => { + + let message = 'wg create p12' + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.getInput(message)[0]).to.equal('p12'); + }); + + it('Test : getInput() with 2 input', () => { + + let message = 'wg create p12 pikachu' + + let a = new Command({ + command: "wg create ", + callback: () => { + + } + }); + + expect(a.getInput(message)[0]).to.equal('p12'); + expect(a.getInput(message)[1]).to.equal('pikachu'); + }); + + + +}); \ No newline at end of file From 11063d120defba1b320ddf0b5dfa3e890207a89a Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Fri, 24 Jul 2020 15:59:23 +0400 Subject: [PATCH 3/3] fixed gettng input from the message --- whatsapp-web.js/src/models/commands.js | 25 ++++++------------- .../src/userInteraction/messages.js | 11 +++++--- whatsapp-web.js/test/test_command.js | 21 +++------------- 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/whatsapp-web.js/src/models/commands.js b/whatsapp-web.js/src/models/commands.js index be55ada..ab084c2 100644 --- a/whatsapp-web.js/src/models/commands.js +++ b/whatsapp-web.js/src/models/commands.js @@ -10,7 +10,6 @@ class Command{ this.requireInput = false; this.inputPos = []; this._requireInput(); - this._getInputPosition(); } /** @@ -76,19 +75,6 @@ class Command{ this.requireInput = matched != null && matched.length > 1; } - _getInputPosition(){ - let splitCommand = this.command.split(' '); - let inputPos = []; - for (let i = 0; i < splitCommand.length; i++) - { - if(splitCommand[i].startsWith('<')){ - inputPos.push(i); - } - } - - this.inputPos = inputPos; - } - /** * returns an array of string representing the inputs of the command * @param {string} message @@ -96,9 +82,14 @@ class Command{ getInput(message){ let splitMsg = message.split(' '); - let input = []; - for(let pos of this.inputPos){ - input.push(splitMsg[pos]); + let splitCommand = this.command.split(' '); + let input = {}; + + for(let i = 0; i < splitMsg.length; i++){ + if(splitCommand[i].startsWith('<')){ + let key = splitCommand[i].slice(1, splitCommand[i].length-1); + input[key] = splitMsg[i]; + } } return input; diff --git a/whatsapp-web.js/src/userInteraction/messages.js b/whatsapp-web.js/src/userInteraction/messages.js index 5884fee..e62c716 100644 --- a/whatsapp-web.js/src/userInteraction/messages.js +++ b/whatsapp-web.js/src/userInteraction/messages.js @@ -11,7 +11,7 @@ const commands = [ command: 'wg', //function to be executed - callback : () => { + callback : (input) => { logic.onWG(); //mesage to be sent to the user @@ -36,7 +36,7 @@ module.exports= onMessage = (message, client) => { if(message.body.startsWith("wg")) { for (let c of commands){ - if(c.command == message.body) + if(c.equals(message.body)) { command = c; break; @@ -44,7 +44,12 @@ module.exports= onMessage = (message, client) => { } if(command != null){ - let messageToBeSent = command.callback(); + let input = {}; + if(command.requireInput){ + input = command.getInput(); + } + + let messageToBeSent = command.callback(input); for (let msg of messageToBeSent) { client.sendMessage(message.from, msg, new Object()); diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index 01e4772..1fca64a 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -119,19 +119,6 @@ describe('Command', () => { expect(a.equals(message)).to.false; }); - - it('Test : _getInputPosition()', () => { - - let a = new Command({ - command: "wg create ", - callback: () => { - - } - }); - - expect(a.inputPos[0]).to.equal(2); - - }) it('Test : getInput() with 1 input', () => { @@ -144,7 +131,7 @@ describe('Command', () => { } }); - expect(a.getInput(message)[0]).to.equal('p12'); + expect(a.getInput(message)['id']).to.equal('p12'); }); it('Test : getInput() with 2 input', () => { @@ -158,10 +145,10 @@ describe('Command', () => { } }); - expect(a.getInput(message)[0]).to.equal('p12'); - expect(a.getInput(message)[1]).to.equal('pikachu'); + expect(a.getInput(message)['id']).to.equal('p12'); + expect(a.getInput(message)['name']).to.equal('pikachu'); }); - + }); \ No newline at end of file