diff --git a/build/functions/argument-parser.js b/build/functions/argument-parser.js index 31aeb6a..bfbde7e 100644 --- a/build/functions/argument-parser.js +++ b/build/functions/argument-parser.js @@ -15,6 +15,7 @@ var ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); var REQUIRED = 0; var OPTIONAL = 1; +var REST = 2; /** * Parses a message for arguments, based on format @@ -71,6 +72,7 @@ function argumentParser(format, string) { }); format = format.replace(FORMAT_REST, function (full, arg, offset) { indexes.push({ offset: offset, arg: arg }); + params[arg] = REST; return getFormat(null, 'rest'); }); diff --git a/build/index.js b/build/index.js index fa78207..8365dcb 100644 --- a/build/index.js +++ b/build/index.js @@ -44,6 +44,9 @@ var DEFAULTS = { } }; +var REQUIRED = 0; +var OPTIONAL = 1; + /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, @@ -200,6 +203,8 @@ var Bot = (function (_EventEmitter) { } update.forEach(function (res) { + var marked3$0 = [getAnswer].map(regeneratorRuntime.mark); + var text = res.message.text; if (!text) return; @@ -222,81 +227,122 @@ var Bot = (function (_EventEmitter) { return; } - if (ev.parse) { - var bot; - var iter; - - var _ret = (function () { - var ask = function ask(param) { - if (!args[param]) { - bot.send(new _typesMessage2['default']().text('Enter value for ' + param).to(res.message.chat.id)).then(function (answer) { - args[param] = answer.text; - iter.next(); - }); - } - }; - - var gen = regeneratorRuntime.mark(function gen(par) { - var index; - return regeneratorRuntime.wrap(function gen$(context$5$0) { - while (1) switch (context$5$0.prev = context$5$0.next) { - case 0: - index = 0; - - case 1: - if (!(index < par.length)) { - context$5$0.next = 12; - break; - } - - while (args[par[index]] && index < par.length) index++; - context$5$0.next = 5; - return ask(par[index]); - - case 5: - index++; - - if (!(index == par.length)) { - context$5$0.next = 10; - break; - } - - res.message.args = args; - ev.listener(res.message); - return context$5$0.abrupt('return'); - - case 10: - context$5$0.next = 1; - break; - - case 12: - case 'end': - return context$5$0.stop(); - } - }, gen, this); - }); - - var _ev$parse = ev.parse(res.message.text); - - var params = _ev$parse.params; - var args = _ev$parse.args; - - if (!Object.keys(params).length) { - ev.listener(res.message); - return { - v: undefined - }; - } - bot = _this2; - iter = gen(Object.keys(params)); - - iter.next(); - })(); - - if (typeof _ret === 'object') return _ret.v; - } else { + if (!ev.parse) { ev.listener(res.message); + return; } + + var _ev$parse = ev.parse(res.message.text); + + var params = _ev$parse.params; + var args = _ev$parse.args; + + var requiredParams = Object.keys(params).filter(function (param) { + return params[param] === REQUIRED; + }); + + if (!requiredParams.length) { + ev.listener(res.message); + return; + } + + var bot = _this2; + function getAnswer() { + var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step; + + return regeneratorRuntime.wrap(function getAnswer$(context$4$0) { + var _this3 = this; + + while (1) switch (context$4$0.prev = context$4$0.next) { + case 0: + _iteratorNormalCompletion = true; + _didIteratorError = false; + _iteratorError = undefined; + context$4$0.prev = 3; + _loop = regeneratorRuntime.mark(function callee$4$0() { + var param, msg; + return regeneratorRuntime.wrap(function callee$4$0$(context$5$0) { + while (1) switch (context$5$0.prev = context$5$0.next) { + case 0: + param = _step.value; + msg = new _typesMessage2['default']().to(res.message.chat.id).text('Enter value for ' + param); + context$5$0.next = 4; + return bot.send(msg).then(function (answer) { + args[param] = answer.text; + }); + + case 4: + case 'end': + return context$5$0.stop(); + } + }, callee$4$0, _this3); + }); + _iterator = requiredParams[Symbol.iterator](); + + case 6: + if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { + context$4$0.next = 11; + break; + } + + return context$4$0.delegateYield(_loop(), 't0', 8); + + case 8: + _iteratorNormalCompletion = true; + context$4$0.next = 6; + break; + + case 11: + context$4$0.next = 17; + break; + + case 13: + context$4$0.prev = 13; + context$4$0.t1 = context$4$0['catch'](3); + _didIteratorError = true; + _iteratorError = context$4$0.t1; + + case 17: + context$4$0.prev = 17; + context$4$0.prev = 18; + + if (!_iteratorNormalCompletion && _iterator['return']) { + _iterator['return'](); + } + + case 20: + context$4$0.prev = 20; + + if (!_didIteratorError) { + context$4$0.next = 23; + break; + } + + throw _iteratorError; + + case 23: + return context$4$0.finish(20); + + case 24: + return context$4$0.finish(17); + + case 25: + case 'end': + return context$4$0.stop(); + } + }, marked3$0[0], this, [[3, 13, 17, 25], [18,, 20, 24]]); + } + + var iterator = getAnswer(); + (function loop() { + var next = iterator.next(); + if (next.done) { + ev.listener(res.message); + return; + } + + next.value.then(loop); + })(); }); } }]); diff --git a/lib/functions/argument-parser.js b/lib/functions/argument-parser.js index ecf1c86..9ed7460 100644 --- a/lib/functions/argument-parser.js +++ b/lib/functions/argument-parser.js @@ -6,6 +6,7 @@ const ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); const REQUIRED = 0; const OPTIONAL = 1; +const REST = 2; /** * Parses a message for arguments, based on format @@ -39,8 +40,7 @@ export default function argumentParser(format, string) { string = string.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim(); - if (!format) - return {args: {}, params: {}}; + if (!format) return {args: {}, params: {}}; let indexes = [], params = {}; @@ -60,6 +60,7 @@ export default function argumentParser(format, string) { }); format = format.replace(FORMAT_REST, (full, arg, offset) => { indexes.push({offset, arg}); + params[arg] = REST; return getFormat(null, 'rest'); }); diff --git a/lib/index.js b/lib/index.js index 7b4894a..3033a4c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,6 +13,9 @@ const DEFAULTS = { } }; +const REQUIRED = 0; +const OPTIONAL = 1; + /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, @@ -165,40 +168,43 @@ export default class Bot extends EventEmitter { return; } - if (ev.parse) { - let {params, args} = ev.parse(res.message.text); - if (!Object.keys(params).length) { + if (!ev.parse) { + ev.listener(res.message); + return; + } + + let {params, args} = ev.parse(res.message.text); + + const requiredParams = Object.keys(params).filter(param => { + return params[param] === REQUIRED; + }); + + if (!requiredParams.length) { + ev.listener(res.message); + return; + } + + const bot = this; + function* getAnswer() { + for (let param of requiredParams) { + const msg = new Message().to(res.message.chat.id) + .text(`Enter value for ${param}`); + yield bot.send(msg).then(answer => { + args[param] = answer.text; + }); + } + } + + const iterator = getAnswer(); + (function loop() { + const next = iterator.next(); + if (next.done) { ev.listener(res.message); return; } - var bot = this; - function ask(param) { - if (!args[param]) { - bot.send(new Message().text(`Enter value for ${param}`).to(res.message.chat.id)) - .then(answer => { - args[param] = answer.text; - iter.next(); - }); - } - } - function* gen(par) { - let index = 0; - while (index < par.length) { - while (args[par[index]] && index < par.length) index++; - yield ask(par[index]); - index++; - if (index == par.length) { - res.message.args = args; - ev.listener(res.message); - return; - } - } - } - var iter = gen(Object.keys(params)); - iter.next(); - } else { - ev.listener(res.message); - } + + next.value.then(loop); + }()); }); } } diff --git a/package.json b/package.json index b9aa23a..e7b6880 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "0.4.72", + "version": "0.5.00", "description": "Control Telegram bots easily using the new Telegram API", "main": "index.js", "scripts": {