feat: If a required argument is not supplied, ask for it and then resolve the command listener.

Thanks to @amovah for his contributions
This commit is contained in:
Mahdi Dibaiee 2015-07-09 15:46:27 +04:30
parent 2aaed21195
commit 41ca326173
5 changed files with 162 additions and 107 deletions

View File

@ -15,6 +15,7 @@ var ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
var REQUIRED = 0; var REQUIRED = 0;
var OPTIONAL = 1; var OPTIONAL = 1;
var REST = 2;
/** /**
* Parses a message for arguments, based on format * 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) { format = format.replace(FORMAT_REST, function (full, arg, offset) {
indexes.push({ offset: offset, arg: arg }); indexes.push({ offset: offset, arg: arg });
params[arg] = REST;
return getFormat(null, 'rest'); return getFormat(null, 'rest');
}); });

View File

@ -44,6 +44,9 @@ var DEFAULTS = {
} }
}; };
var REQUIRED = 0;
var OPTIONAL = 1;
/** /**
* Bot class used to connect to a new bot * Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods, * 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) { update.forEach(function (res) {
var marked3$0 = [getAnswer].map(regeneratorRuntime.mark);
var text = res.message.text; var text = res.message.text;
if (!text) return; if (!text) return;
@ -222,81 +227,122 @@ var Bot = (function (_EventEmitter) {
return; return;
} }
if (ev.parse) { 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); ev.listener(res.message);
return context$5$0.abrupt('return'); 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 _ev$parse = ev.parse(res.message.text);
var params = _ev$parse.params; var params = _ev$parse.params;
var args = _ev$parse.args; var args = _ev$parse.args;
if (!Object.keys(params).length) { var requiredParams = Object.keys(params).filter(function (param) {
ev.listener(res.message); return params[param] === REQUIRED;
return { });
v: undefined
};
}
bot = _this2;
iter = gen(Object.keys(params));
iter.next(); 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);
})(); })();
if (typeof _ret === 'object') return _ret.v;
} else {
ev.listener(res.message);
}
}); });
} }
}]); }]);

View File

@ -6,6 +6,7 @@ const ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
const REQUIRED = 0; const REQUIRED = 0;
const OPTIONAL = 1; const OPTIONAL = 1;
const REST = 2;
/** /**
* Parses a message for arguments, based on format * Parses a message for arguments, based on format
@ -39,8 +40,7 @@ export default function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '').trim(); string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim();
if (!format) if (!format) return {args: {}, params: {}};
return {args: {}, params: {}};
let indexes = [], let indexes = [],
params = {}; params = {};
@ -60,6 +60,7 @@ export default function argumentParser(format, string) {
}); });
format = format.replace(FORMAT_REST, (full, arg, offset) => { format = format.replace(FORMAT_REST, (full, arg, offset) => {
indexes.push({offset, arg}); indexes.push({offset, arg});
params[arg] = REST;
return getFormat(null, 'rest'); return getFormat(null, 'rest');
}); });

View File

@ -13,6 +13,9 @@ const DEFAULTS = {
} }
}; };
const REQUIRED = 0;
const OPTIONAL = 1;
/** /**
* Bot class used to connect to a new bot * Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods, * Bots have an api property which gives access to all Telegram API methods,
@ -165,40 +168,43 @@ export default class Bot extends EventEmitter {
return; return;
} }
if (ev.parse) { if (!ev.parse) {
let {params, args} = ev.parse(res.message.text);
if (!Object.keys(params).length) {
ev.listener(res.message); ev.listener(res.message);
return; return;
} }
var bot = this;
function ask(param) { let {params, args} = ev.parse(res.message.text);
if (!args[param]) {
bot.send(new Message().text(`Enter value for ${param}`).to(res.message.chat.id)) const requiredParams = Object.keys(params).filter(param => {
.then(answer => { 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; args[param] = answer.text;
iter.next();
}); });
} }
} }
function* gen(par) {
let index = 0; const iterator = getAnswer();
while (index < par.length) { (function loop() {
while (args[par[index]] && index < par.length) index++; const next = iterator.next();
yield ask(par[index]); if (next.done) {
index++;
if (index == par.length) {
res.message.args = args;
ev.listener(res.message); ev.listener(res.message);
return; return;
} }
}
} next.value.then(loop);
var iter = gen(Object.keys(params)); }());
iter.next();
} else {
ev.listener(res.message);
}
}); });
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "telegram-api", "name": "telegram-api",
"version": "0.4.72", "version": "0.5.00",
"description": "Control Telegram bots easily using the new Telegram API", "description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {