Ask for arguments

This commit is contained in:
Ali Movahedi 2015-07-09 14:26:17 +04:30
parent 477f99b40c
commit 2aaed21195
4 changed files with 117 additions and 16 deletions

View File

@ -49,11 +49,7 @@ function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim();
if (!format) {
return {};
}
if (!string) return { args: {}, params: params };
if (!format) return { args: {}, params: {} };
var indexes = [],
params = {};
@ -78,6 +74,8 @@ function argumentParser(format, string) {
return getFormat(null, 'rest');
});
if (!string) return { args: {}, params: params };
indexes = indexes.sort(function (a, b) {
return a.offset < b.offset ? -1 : 1;
});

View File

@ -32,6 +32,10 @@ var _functionsArgumentParser2 = _interopRequireDefault(_functionsArgumentParser)
var _events = require('events');
var _typesMessage = require('./types/Message');
var _typesMessage2 = _interopRequireDefault(_typesMessage);
var DEFAULTS = {
update: {
offset: 0,
@ -219,10 +223,80 @@ var Bot = (function (_EventEmitter) {
}
if (ev.parse) {
res.message.args = ev.parse(res.message.text);
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 {
ev.listener(res.message);
}
});
}
}]);

View File

@ -39,11 +39,8 @@ export default function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim();
if (!format) {
return {};
}
if (!string) return {args: {}, params};
if (!format)
return {args: {}, params: {}};
let indexes = [],
params = {};
@ -66,6 +63,8 @@ export default function argumentParser(format, string) {
return getFormat(null, 'rest');
});
if (!string) return {args: {}, params};
indexes = indexes.sort((a, b) => {
return a.offset < b.offset ? -1 : 1;
});

View File

@ -3,6 +3,7 @@ import webhook from './functions/webhook';
import poll from './functions/poll';
import argumentParser from './functions/argument-parser';
import {EventEmitter} from 'events';
import Message from './types/Message';
const DEFAULTS = {
update: {
@ -165,10 +166,39 @@ export default class Bot extends EventEmitter {
}
if (ev.parse) {
res.message.args = ev.parse(res.message.text);
}
let {params, args} = ev.parse(res.message.text);
if (!Object.keys(params).length) {
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);
}
});
}
}