Add Argument Parser 💪
This commit is contained in:
49
build/functions/api.js
Normal file
49
build/functions/api.js
Normal file
@ -0,0 +1,49 @@
|
||||
// API methods
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var _fetch = require('./fetch');
|
||||
|
||||
var _fetch2 = _interopRequireDefault(_fetch);
|
||||
|
||||
/**
|
||||
* API class, has a function for each method of the Telegram API which take
|
||||
* an object argument, and send request to the API server
|
||||
*
|
||||
* Methods: getMe, sendMessage, forwardMessage, sendPhoto, sendAudio,
|
||||
* sendDocument, sendSticker, sendVideo, sendLocation, sendChatAction,
|
||||
* getUserProfilePhotos, getUpdates
|
||||
*/
|
||||
|
||||
var API =
|
||||
/**
|
||||
* Create a new api object with the given token
|
||||
* @param {string} token
|
||||
*/
|
||||
function API(token) {
|
||||
_classCallCheck(this, API);
|
||||
|
||||
this.token = token;
|
||||
};
|
||||
|
||||
exports['default'] = API;
|
||||
|
||||
API.prototype.request = function request(method, data) {
|
||||
return (0, _fetch2['default'])(this.token + '/' + method, data);
|
||||
};
|
||||
|
||||
var methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto', 'sendAudio', 'sendDocument', 'sendSticker', 'sendVideo', 'sendLocation', 'sendChatAction', 'getUserProfilePhotos', 'getUpdates', 'setWebhook'];
|
||||
|
||||
methods.forEach(function (method) {
|
||||
API.prototype[method] = function (data) {
|
||||
return this.request(method, data);
|
||||
};
|
||||
});
|
||||
module.exports = exports['default'];
|
139
build/functions/argument-parser.js
Normal file
139
build/functions/argument-parser.js
Normal file
@ -0,0 +1,139 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
|
||||
|
||||
exports['default'] = argumentParser;
|
||||
var FORMAT_REQUIRED = /<(\W*)(\w+)\|?(\w+)?>/g;
|
||||
var FORMAT_OPTIONAL = /\[(\W*)(\w+)\|?(\w+)?\]/g;
|
||||
var FORMAT_REST = /\.{3}(\w+)/g;
|
||||
|
||||
var ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
|
||||
|
||||
/**
|
||||
* Parses a message for arguments, based on format
|
||||
*
|
||||
* The format option may include '<requiredParam>' and '[optionalParam]' and
|
||||
* '...[restParam]'
|
||||
* <requiredParam> indicates a required, single-word argument
|
||||
* [optionalParam] indicates an optinal, single-word argument
|
||||
* ...[restParam] indicates a multi-word argument which records until end
|
||||
*
|
||||
* You can define a type for your arguments using pipe | sign, like this:
|
||||
* [count|number]
|
||||
* Supported Types are: number and word, defaults to word
|
||||
*
|
||||
* Example:
|
||||
* format: '<name> [count|number] ...text'
|
||||
* string 1: 'Someone Hey, wassup'
|
||||
* {name: 'Someone',
|
||||
* count: undefined,
|
||||
* text: 'Hey, wassup'}
|
||||
*
|
||||
* string 2: 'Someone 5 Hey, wassup'
|
||||
* {name: 'Someone',
|
||||
* count: 5,
|
||||
* text: 'Hey, wassup'}
|
||||
* @param {string} format Format, as described above
|
||||
* @param {string} string The message to parse
|
||||
* @return {object} Parsed arguments
|
||||
*/
|
||||
|
||||
function argumentParser(format, string) {
|
||||
string = string.replace(/[^\s]+/, '');
|
||||
format = format.replace(/[^\s]+/, '');
|
||||
var indexes = [];
|
||||
|
||||
format = format.replace(/\s/g, '\\s*');
|
||||
format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg, type, offset) {
|
||||
if (type === undefined) type = 'word';
|
||||
|
||||
indexes.push({ arg: arg, offset: offset });
|
||||
return (escape(symbols) + getFormat(type, 'required')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg, type, offset) {
|
||||
if (type === undefined) type = 'word';
|
||||
|
||||
indexes.push({ arg: arg, offset: offset });
|
||||
return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_REST, function (full, arg, offset) {
|
||||
indexes.push({ offset: offset, arg: arg });
|
||||
return getFormat(null, 'rest');
|
||||
});
|
||||
|
||||
indexes = indexes.sort(function (a, b) {
|
||||
return a.offset < b.offset ? -1 : 1;
|
||||
});
|
||||
|
||||
console.log(format);
|
||||
var regex = new RegExp(format);
|
||||
|
||||
var matched = regex.exec(string).slice(1);
|
||||
|
||||
var object = {};
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator = matched.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var _step$value = _slicedToArray(_step.value, 2);
|
||||
|
||||
var index = _step$value[0];
|
||||
var match = _step$value[1];
|
||||
|
||||
var argument = indexes[index];
|
||||
|
||||
object[argument.arg] = match;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator['return']) {
|
||||
_iterator['return']();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
function escape(symbols) {
|
||||
var append = arguments[1] === undefined ? '' : arguments[1];
|
||||
|
||||
return symbols.split('').map(function (symbol) {
|
||||
return (ESCAPABLE.indexOf(symbol) ? '\\' + symbol : symbol) + append;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
var TYPES = {
|
||||
'number': '\\d',
|
||||
'word': '\\w'
|
||||
};
|
||||
|
||||
function getFormat() {
|
||||
var type = arguments[0] === undefined ? 'word' : arguments[0];
|
||||
var param = arguments[1] === undefined ? 'required' : arguments[1];
|
||||
|
||||
var t = TYPES[type];
|
||||
|
||||
switch (param) {
|
||||
case 'required':
|
||||
return '(' + t + '+)';
|
||||
case 'optional':
|
||||
return '(' + t + '+)?';
|
||||
case 'rest':
|
||||
return '(.*)';
|
||||
}
|
||||
}
|
||||
module.exports = exports['default'];
|
49
build/functions/fetch.js
Normal file
49
build/functions/fetch.js
Normal file
@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports['default'] = fetch;
|
||||
exports.getBody = getBody;
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
||||
|
||||
var _restler = require('restler');
|
||||
|
||||
var _restler2 = _interopRequireDefault(_restler);
|
||||
|
||||
function fetch(path) {
|
||||
var data = arguments[1] === undefined ? {} : arguments[1];
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
var method = Object.keys(data).length ? 'POST' : 'GET';
|
||||
var multipart = method === 'POST' ? true : false;
|
||||
|
||||
_restler2['default'].request('https://api.telegram.org/bot' + path, {
|
||||
data: data, method: method, multipart: multipart
|
||||
}).on('complete', function (response) {
|
||||
try {
|
||||
var json = JSON.parse(response);
|
||||
resolve(json);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getBody(stream) {
|
||||
var data = '';
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
stream.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
stream.on('end', function () {
|
||||
resolve(data);
|
||||
});
|
||||
|
||||
stream.on('error', reject);
|
||||
});
|
||||
}
|
19
build/functions/poll.js
Normal file
19
build/functions/poll.js
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports['default'] = poll;
|
||||
|
||||
function poll(bot) {
|
||||
return bot.api.getUpdates(bot.update).then(function (response) {
|
||||
if (!response.result.length) {
|
||||
return poll(bot);
|
||||
}
|
||||
bot.emit('update', response.result);
|
||||
|
||||
return poll(bot);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = exports['default'];
|
42
build/functions/webhook.js
Normal file
42
build/functions/webhook.js
Normal file
@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports['default'] = webhook;
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
||||
|
||||
var _http = require('http');
|
||||
|
||||
var _http2 = _interopRequireDefault(_http);
|
||||
|
||||
var _qs = require('qs');
|
||||
|
||||
var _qs2 = _interopRequireDefault(_qs);
|
||||
|
||||
var _fetch = require('./fetch');
|
||||
|
||||
var DEFAULTS = {
|
||||
server: {},
|
||||
port: 443
|
||||
};
|
||||
|
||||
function webhook(options, bot) {
|
||||
if (options === undefined) options = {};
|
||||
|
||||
options = Object.assign(DEFAULTS, options);
|
||||
|
||||
return bot.api.setWebhook(options.url).then(function () {
|
||||
|
||||
_http2['default'].createServer(options.server, function (req, res) {
|
||||
return (0, _fetch.getBody)(req).then(function (data) {
|
||||
bot.emit('update', _qs2['default'].parse(data).result);
|
||||
|
||||
res.end('OK');
|
||||
});
|
||||
}).listen(options.port);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = exports['default'];
|
Reference in New Issue
Block a user