From 88af10a8b42d9441e40de7871d875727c975ae95 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Tue, 12 Sep 2017 13:39:58 +0430 Subject: [PATCH 1/4] feat: customizable message for required params --- src/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 3895f2c..d469ed3 100644 --- a/src/index.js +++ b/src/index.js @@ -117,7 +117,12 @@ export default class Bot extends EventEmitter { * gets the update * @return {object} returns the bot object */ - command(command, listener) { + command(command, customMessage, listener) { + if (!listener) { + listener = customMessage; + customMessage = {}; + } + const regex = /[^\s]+/; const cmd = command.match(regex)[0].trim(); @@ -125,6 +130,7 @@ export default class Bot extends EventEmitter { this._userEvents.push({ pattern: new RegExp(`^/${cmd}`), parse: argumentParser.bind(null, command), + customMessage, listener }); @@ -211,9 +217,16 @@ export default class Bot extends EventEmitter { const bot = this; function* getAnswer() { + const customMessage = ev.customMessage; + for (const param of requiredParams) { - const ga = new Message().to(msg.chat.id) - .text(`Enter value for ${param}`); + let ga; + if (customMessage[param]) { + ga = new Message().to(msg.chat.id).text(customMessage[param]); + } else { + ga = new Message().to(msg.chat.id) + .text(`Enter value for ${param}`); + } yield bot.send(ga).then(answer => { args[param] = answer.text; }); From bc5f14ebdec0a9023545a32b0600891d2d02da4a Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Tue, 12 Sep 2017 18:39:09 +0430 Subject: [PATCH 2/4] fix: move optional argument to end --- src/index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index d469ed3..719766f 100644 --- a/src/index.js +++ b/src/index.js @@ -117,12 +117,7 @@ export default class Bot extends EventEmitter { * gets the update * @return {object} returns the bot object */ - command(command, customMessage, listener) { - if (!listener) { - listener = customMessage; - customMessage = {}; - } - + command(command, listener, customMessage = {}) { const regex = /[^\s]+/; const cmd = command.match(regex)[0].trim(); @@ -130,8 +125,8 @@ export default class Bot extends EventEmitter { this._userEvents.push({ pattern: new RegExp(`^/${cmd}`), parse: argumentParser.bind(null, command), - customMessage, - listener + listener, + customMessage }); return this; From 5754b249129ad4151a5c9281f194b0c990ebe4be Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Tue, 12 Sep 2017 18:41:39 +0430 Subject: [PATCH 3/4] style: remove extra white space --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 719766f..40a4ef3 100644 --- a/src/index.js +++ b/src/index.js @@ -220,7 +220,7 @@ export default class Bot extends EventEmitter { ga = new Message().to(msg.chat.id).text(customMessage[param]); } else { ga = new Message().to(msg.chat.id) - .text(`Enter value for ${param}`); + .text(`Enter value for ${param}`); } yield bot.send(ga).then(answer => { args[param] = answer.text; From 9c7adddb0a7fbf930a239a1e71d2fb596c35eaf3 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Tue, 12 Sep 2017 20:26:28 +0430 Subject: [PATCH 4/4] perf: remove if for checking customMessage --- src/index.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 40a4ef3..c810269 100644 --- a/src/index.js +++ b/src/index.js @@ -215,13 +215,9 @@ export default class Bot extends EventEmitter { const customMessage = ev.customMessage; for (const param of requiredParams) { - let ga; - if (customMessage[param]) { - ga = new Message().to(msg.chat.id).text(customMessage[param]); - } else { - ga = new Message().to(msg.chat.id) - .text(`Enter value for ${param}`); - } + const ga = new Message() + .to(msg.chat.id) + .text(customMessage[param] || `Enter value for ${param}`); yield bot.send(ga).then(answer => { args[param] = answer.text; });