From e5c25bd9c1990f464742338b8443e6cc509bcadc Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 00:59:00 +0430 Subject: [PATCH 1/6] refactor: remove grunt and babel remove grunt from dependencies, and use transformed code instead using babel/register. change API a litte --- .babelrc | 4 + lib/.eslintrc => .eslintrc | 0 .gitignore | 2 + Gruntfile.js | 19 +- build/api.js | 49 ---- build/fetch.js | 49 ---- build/functions/api.js | 17 +- build/functions/argument-parser.js | 27 +- build/functions/fetch.js | 16 +- build/functions/poll.js | 6 +- build/functions/webhook.js | 18 +- build/index.js | 340 ++++++++++++---------- build/poll.js | 19 -- build/types/Base.js | 88 +++--- build/types/BulkMessage.js | 59 ++-- build/types/File.js | 74 ++--- build/types/Forward.js | 60 ++-- build/types/Keyboard.js | 87 +++--- build/types/Message.js | 102 +++++-- build/types/Question.js | 67 +++-- build/utils.js | 1 - build/webhook.js | 42 --- index.js | 2 - package.json | 19 +- {lib => src}/functions/api.js | 0 {lib => src}/functions/argument-parser.js | 0 {lib => src}/functions/fetch.js | 0 {lib => src}/functions/poll.js | 0 {lib => src}/functions/webhook.js | 0 {lib => src}/index.js | 22 +- {lib => src}/types/Base.js | 0 {lib => src}/types/BulkMessage.js | 0 {lib => src}/types/File.js | 0 {lib => src}/types/Forward.js | 0 {lib => src}/types/Keyboard.js | 0 {lib => src}/types/Message.js | 0 {lib => src}/types/Question.js | 0 types/Base.js | 136 --------- types/BulkMessage.js | 88 ------ types/File.js | 153 ---------- types/Forward.js | 90 ------ types/Keyboard.js | 138 --------- types/Message.js | 117 -------- types/Question.js | 111 ------- 44 files changed, 576 insertions(+), 1446 deletions(-) create mode 100644 .babelrc rename lib/.eslintrc => .eslintrc (100%) delete mode 100644 build/api.js delete mode 100644 build/fetch.js delete mode 100644 build/poll.js delete mode 100644 build/utils.js delete mode 100644 build/webhook.js delete mode 100644 index.js rename {lib => src}/functions/api.js (100%) rename {lib => src}/functions/argument-parser.js (100%) rename {lib => src}/functions/fetch.js (100%) rename {lib => src}/functions/poll.js (100%) rename {lib => src}/functions/webhook.js (100%) rename {lib => src}/index.js (94%) rename {lib => src}/types/Base.js (100%) rename {lib => src}/types/BulkMessage.js (100%) rename {lib => src}/types/File.js (100%) rename {lib => src}/types/Forward.js (100%) rename {lib => src}/types/Keyboard.js (100%) rename {lib => src}/types/Message.js (100%) rename {lib => src}/types/Question.js (100%) delete mode 100644 types/Base.js delete mode 100644 types/BulkMessage.js delete mode 100644 types/File.js delete mode 100644 types/Forward.js delete mode 100644 types/Keyboard.js delete mode 100644 types/Message.js delete mode 100644 types/Question.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..781f9ea --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["es2015"], + "plugins": ["babel-plugin-add-module-exports"] +} diff --git a/lib/.eslintrc b/.eslintrc similarity index 100% rename from lib/.eslintrc rename to .eslintrc diff --git a/.gitignore b/.gitignore index f9d6ed0..e1a83cf 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ node_modules # Test file I use to test my module test.js + +.DS_Store diff --git a/Gruntfile.js b/Gruntfile.js index d34c5ce..a51cb08 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,28 +4,18 @@ module.exports = function(grunt) { scripts: { files: [{ expand: true, - cwd: 'lib', + cwd: 'src', src: '**/*.js', dest: 'build/' }] } }, eslint: { - scripts: ['lib/**/*.js'] - }, - copy: { - classes: { - files: [{ - expand: true, - cwd: 'build/types', - src: '*', - dest: 'types' - }] - } + scripts: ['src/**/*.js'] }, watch: { scripts: { - files: ['lib/**/*.js'], + files: ['src/**/*.js'], tasks: ['babel'] } } @@ -33,8 +23,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-babel'); grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-eslint'); - grunt.registerTask('default', ['babel', 'copy', 'eslint']); + grunt.registerTask('default', ['babel', 'eslint']); }; diff --git a/build/api.js b/build/api.js deleted file mode 100644 index 0852b14..0000000 --- a/build/api.js +++ /dev/null @@ -1,49 +0,0 @@ -// 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']; diff --git a/build/fetch.js b/build/fetch.js deleted file mode 100644 index 97275ee..0000000 --- a/build/fetch.js +++ /dev/null @@ -1,49 +0,0 @@ -'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); - }); -} diff --git a/build/functions/api.js b/build/functions/api.js index 0852b14..2fc817b 100644 --- a/build/functions/api.js +++ b/build/functions/api.js @@ -1,18 +1,18 @@ -// API methods 'use strict'; -Object.defineProperty(exports, '__esModule', { +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); +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"); } } // API methods + + /** * API class, has a function for each method of the Telegram API which take * an object argument, and send request to the API server @@ -33,10 +33,11 @@ function API(token) { this.token = token; }; -exports['default'] = API; +exports.default = API; + API.prototype.request = function request(method, data) { - return (0, _fetch2['default'])(this.token + '/' + 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']; diff --git a/build/functions/argument-parser.js b/build/functions/argument-parser.js index bfbde7e..81bc264 100644 --- a/build/functions/argument-parser.js +++ b/build/functions/argument-parser.js @@ -1,12 +1,12 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +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'); } }; })(); +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; +exports.default = argumentParser; var FORMAT_REQUIRED = /<(\W*)(\w+)\|?(\w+)?>/g; var FORMAT_OPTIONAL = /\[(\W*)(\w+)\|?(\w+)?\]/g; var FORMAT_REST = /\.{3}(\w+)/g; @@ -45,7 +45,6 @@ var REST = 2; * @param {string} string The message to parse * @return {object} Parsed arguments */ - function argumentParser(format, string) { string = string.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim(); @@ -56,15 +55,17 @@ function argumentParser(format, string) { params = {}; format = format.replace(/\s/g, '\\s*'); - format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg, type, offset) { - if (type === undefined) type = 'word'; + format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg) { + var type = arguments.length <= 3 || arguments[3] === undefined ? 'word' : arguments[3]; + var offset = arguments[4]; indexes.push({ arg: arg, offset: offset }); params[arg] = REQUIRED; return (escape(symbols) + getFormat(type, 'required')).trim(); }); - format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg, type, offset) { - if (type === undefined) type = 'word'; + format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg) { + var type = arguments.length <= 3 || arguments[3] === undefined ? 'word' : arguments[3]; + var offset = arguments[4]; indexes.push({ arg: arg, offset: offset }); params[arg] = OPTIONAL; @@ -107,8 +108,8 @@ function argumentParser(format, string) { _iteratorError = err; } finally { try { - if (!_iteratorNormalCompletion && _iterator['return']) { - _iterator['return'](); + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); } } finally { if (_didIteratorError) { @@ -121,7 +122,7 @@ function argumentParser(format, string) { } function escape(symbols) { - var append = arguments[1] === undefined ? '' : arguments[1]; + var append = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; return symbols.split('').map(function (symbol) { return (ESCAPABLE.indexOf(symbol) ? '\\' + symbol : symbol) + append; @@ -134,8 +135,8 @@ var TYPES = { }; function getFormat() { - var type = arguments[0] === undefined ? 'word' : arguments[0]; - var param = arguments[1] === undefined ? 'required' : arguments[1]; + var type = arguments.length <= 0 || arguments[0] === undefined ? 'word' : arguments[0]; + var param = arguments.length <= 1 || arguments[1] === undefined ? 'required' : arguments[1]; var t = TYPES[type]; diff --git a/build/functions/fetch.js b/build/functions/fetch.js index d29d59d..1af71bf 100644 --- a/build/functions/fetch.js +++ b/build/functions/fetch.js @@ -1,19 +1,19 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -exports['default'] = fetch; +exports.default = fetch; exports.getBody = getBody; -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } - var _unirest = require('unirest'); var _unirest2 = _interopRequireDefault(_unirest); +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + function fetch(path) { - var data = arguments[1] === undefined ? {} : arguments[1]; + var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; return new Promise(function (resolve, reject) { var files = {}; @@ -36,8 +36,8 @@ function fetch(path) { _iteratorError = err; } finally { try { - if (!_iteratorNormalCompletion && _iterator['return']) { - _iterator['return'](); + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); } } finally { if (_didIteratorError) { @@ -46,7 +46,7 @@ function fetch(path) { } } - _unirest2['default'].post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) { + _unirest2.default.post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) { if (response.statusType === 4 || response.statusType === 5 || !response.body || !response.body.ok) { reject(response); } else { diff --git a/build/functions/poll.js b/build/functions/poll.js index 8a3a87d..8eabd3f 100644 --- a/build/functions/poll.js +++ b/build/functions/poll.js @@ -1,10 +1,9 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -exports['default'] = poll; - +exports.default = poll; function poll(bot) { return bot.api.getUpdates(bot.update).then(function (response) { if (!response.result.length) { @@ -16,5 +15,4 @@ function poll(bot) { return poll(bot); }); } - module.exports = exports['default']; diff --git a/build/functions/webhook.js b/build/functions/webhook.js index 859aeda..03b64ab 100644 --- a/build/functions/webhook.js +++ b/build/functions/webhook.js @@ -1,11 +1,9 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -exports['default'] = webhook; - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } +exports.default = webhook; var _http = require('http'); @@ -17,26 +15,28 @@ var _qs2 = _interopRequireDefault(_qs); var _fetch = require('./fetch'); +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + var DEFAULTS = { server: {}, port: 443 }; -function webhook(options, bot) { - if (options === undefined) options = {}; +function webhook() { + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + var bot = arguments[1]; options = Object.assign(DEFAULTS, options); return bot.api.setWebhook(options.url).then(function () { - bot._webhookServer = _http2['default'].createServer(options.server, function (req, res) { + bot._webhookServer = _http2.default.createServer(options.server, function (req, res) { return (0, _fetch.getBody)(req).then(function (data) { - bot.emit('update', _qs2['default'].parse(data).result); + bot.emit('update', _qs2.default.parse(data).result); res.end('OK'); }); }).listen(options.port); }); } - module.exports = exports['default']; diff --git a/build/index.js b/build/index.js index 83b8e52..4524dec 100644 --- a/build/index.js +++ b/build/index.js @@ -1,40 +1,63 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Keyboard = exports.Question = exports.Forward = exports.BulkMessage = exports.Message = exports.File = undefined; -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; +require('babel-polyfill'); -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } +var _api = require('./functions/api'); -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } +var _api2 = _interopRequireDefault(_api); -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _webhook = require('./functions/webhook'); -var _functionsApi = require('./functions/api'); +var _webhook2 = _interopRequireDefault(_webhook); -var _functionsApi2 = _interopRequireDefault(_functionsApi); +var _poll = require('./functions/poll'); -var _functionsWebhook = require('./functions/webhook'); +var _poll2 = _interopRequireDefault(_poll); -var _functionsWebhook2 = _interopRequireDefault(_functionsWebhook); +var _argumentParser = require('./functions/argument-parser'); -var _functionsPoll = require('./functions/poll'); - -var _functionsPoll2 = _interopRequireDefault(_functionsPoll); - -var _functionsArgumentParser = require('./functions/argument-parser'); - -var _functionsArgumentParser2 = _interopRequireDefault(_functionsArgumentParser); +var _argumentParser2 = _interopRequireDefault(_argumentParser); var _events = require('events'); -var _typesMessage = require('./types/Message'); +var _Message = require('./types/Message'); -var _typesMessage2 = _interopRequireDefault(_typesMessage); +var _Message2 = _interopRequireDefault(_Message); + +var _File = require('./types/File'); + +var _File2 = _interopRequireDefault(_File); + +var _Keyboard = require('./types/Keyboard'); + +var _Keyboard2 = _interopRequireDefault(_Keyboard); + +var _BulkMessage = require('./types/BulkMessage'); + +var _BulkMessage2 = _interopRequireDefault(_BulkMessage); + +var _Question = require('./types/Question'); + +var _Question2 = _interopRequireDefault(_Question); + +var _Forward = require('./types/Forward'); + +var _Forward2 = _interopRequireDefault(_Forward); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var DEFAULTS = { update: { @@ -45,7 +68,8 @@ var DEFAULTS = { }; var REQUIRED = 0; -var OPTIONAL = 1; + +console.log(_poll2.default); /** * Bot class used to connect to a new bot @@ -53,79 +77,79 @@ var OPTIONAL = 1; * see API class */ -var Bot = (function (_EventEmitter) { +var Bot = function (_EventEmitter) { + _inherits(Bot, _EventEmitter); + /** * Create and connect to a new bot * @param {object} options Bot properties. */ function Bot() { - var options = arguments[0] === undefined ? { update: {} } : arguments[0]; + var options = arguments.length <= 0 || arguments[0] === undefined ? { update: {} } : arguments[0]; _classCallCheck(this, Bot); - _get(Object.getPrototypeOf(Bot.prototype), 'constructor', this).call(this); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Bot).call(this)); if (!options.token) { throw new Error('Token cannot be empty'); } - this.token = options.token; - this.update = Object.assign(options.update || {}, DEFAULTS.update); + _this.token = options.token; + _this.update = Object.assign(options.update || {}, DEFAULTS.update); - this.api = new _functionsApi2['default'](this.token); + _this.api = new _api2.default(_this.token); - this.msg = {}; + _this.msg = {}; // EventEmitter - this._events = {}; - this._userEvents = []; + _this._events = {}; + _this._userEvents = []; - this.setMaxListeners(100); + _this.setMaxListeners(100); + return _this; } - _inherits(Bot, _EventEmitter); + /** + * Gets information about the bot and then + * 1) starts polling updates from API + * 2) sets a webhook as defined by the first parameter and listens for updates + * Emits an `update` event after polling with the response from server + * Returns a promise which is resolved after the bot information is received + * and set to it's `info` property i.e. bot.info + * + * @param {object} hook An object containg options passed to webhook + * properties: + * - url: HTTPS url to listen on POST requests coming + * from the Telegram API + * - port: the port to listen to, defaults to 443 + * - server: An object passed to https.createServer + * + * @return {promise} A promise which is resolved with the response of getMe + */ + _createClass(Bot, [{ key: 'start', - - /** - * Gets information about the bot and then - * 1) starts polling updates from API - * 2) sets a webhook as defined by the first parameter and listens for updates - * Emits an `update` event after polling with the response from server - * Returns a promise which is resolved after the bot information is received - * and set to it's `info` property i.e. bot.info - * - * @param {object} hook An object containg options passed to webhook - * properties: - * - url: HTTPS url to listen on POST requests coming - * from the Telegram API - * - port: the port to listen to, defaults to 443 - * - server: An object passed to https.createServer - * - * @return {promise} A promise which is resolved with the response of getMe - */ value: function start(hook) { - var _this = this; + var _this2 = this; if (hook) { - return (0, _functionsWebhook2['default'])(hook, this); + return (0, _webhook2.default)(hook, this); } return this.api.getMe().then(function (response) { - _this.info = response.result; + _this2.info = response.result; - _this.on('update', _this._update); + _this2.on('update', _this2._update); if (hook) { - return (0, _functionsWebhook2['default'])(hook, _this); + return (0, _webhook2.default)(hook, _this2); } else { - return (0, _functionsPoll2['default'])(_this); + return (0, _poll2.default)(_this2); } }); } - }, { - key: 'get', /** * Listens on specific message matching the pattern which can be an string @@ -137,6 +161,9 @@ var Bot = (function (_EventEmitter) { * with the string i.e. /^yourString/ * @return {object} returns the bot object */ + + }, { + key: 'get', value: function get(pattern, listener) { if (typeof pattern === 'string') { pattern = new RegExp('^' + pattern); @@ -148,8 +175,6 @@ var Bot = (function (_EventEmitter) { return this; } - }, { - key: 'command', /** * Listens on a command @@ -158,6 +183,9 @@ var Bot = (function (_EventEmitter) { * gets the update * @return {object} returns the bot object */ + + }, { + key: 'command', value: function command(_command, listener) { var regex = /[^\s]+/; @@ -165,29 +193,31 @@ var Bot = (function (_EventEmitter) { this._userEvents.push({ pattern: new RegExp('^/' + cmd), - parse: _functionsArgumentParser2['default'].bind(null, _command), + parse: _argumentParser2.default.bind(null, _command), listener: listener }); return this; } - }, { - key: 'send', /** * Sends the message provided * @param {object} message The message to send. Gets it's send method called * @return {unknown} returns the result of calling message's send method */ - value: function send(message) { - return message.send(this)['catch'](console.error); - } + }, { - key: 'stop', + key: 'send', + value: function send(message) { + return message.send(this).catch(console.error); + } /** * Stops the bot, deattaching all listeners and polling */ + + }, { + key: 'stop', value: function stop() { this._stop = true; @@ -198,8 +228,6 @@ var Bot = (function (_EventEmitter) { this.removeListener('update', this._update); this._events = {}; } - }, { - key: '_update', /** * The internal update event listener, used to parse messages and fire @@ -207,8 +235,11 @@ var Bot = (function (_EventEmitter) { * * @param {object} update */ + + }, { + key: '_update', value: function _update(update) { - var _this2 = this; + var _this3 = this; if (!this.update.offset) { var updateId = update[update.length - 1].update_id; @@ -219,27 +250,29 @@ var Bot = (function (_EventEmitter) { } update.forEach(function (res) { - var marked3$0 = [getAnswer].map(regeneratorRuntime.mark); + var _marked = [getAnswer].map(regeneratorRuntime.mark); var text = res.message.text; - if (!text) return; + if (!text) { + return; + } - var selfUsername = '@' + _this2.info.username; + var selfUsername = '@' + _this3.info.username; if (text.startsWith('/') && text.indexOf(selfUsername) > -1) { // Commands are sent in /command@thisusername format in groups - var regex = new RegExp('(/.*)@' + _this2.info.username); + var regex = new RegExp('(/.*)@' + _this3.info.username); text = text.replace(regex, '$1'); res.message.text = text; } - var ev = _this2._userEvents.find(function (_ref) { + var ev = _this3._userEvents.find(function (_ref) { var pattern = _ref.pattern; return pattern.test(text); }); if (!ev) { - _this2.emit('command-notfound', res.message); + _this3.emit('command-notfound', res.message); return; } @@ -264,91 +297,95 @@ var Bot = (function (_EventEmitter) { return; } - var bot = _this2; + var bot = _this3; function getAnswer() { + var _this4 = this; + var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step; - return regeneratorRuntime.wrap(function getAnswer$(context$4$0) { - var _this3 = this; + return regeneratorRuntime.wrap(function getAnswer$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + _iteratorNormalCompletion = true; + _didIteratorError = false; + _iteratorError = undefined; + _context2.prev = 3; + _loop = regeneratorRuntime.mark(function _loop() { + var param, msg; + return regeneratorRuntime.wrap(function _loop$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + param = _step.value; + msg = new _Message2.default().to(res.message.chat.id).text('Enter value for ' + param); + _context.next = 4; + return bot.send(msg).then(function (answer) { + args[param] = answer.text; + }); - 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.stop(); + } + } + }, _loop, _this4); + }); + _iterator = requiredParams[Symbol.iterator](); - 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) { + _context2.next = 11; + break; + } - case 6: - if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { - context$4$0.next = 11; + return _context2.delegateYield(_loop(), 't0', 8); + + case 8: + _iteratorNormalCompletion = true; + _context2.next = 6; 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; + case 11: + _context2.next = 17; break; - } - throw _iteratorError; + case 13: + _context2.prev = 13; + _context2.t1 = _context2['catch'](3); + _didIteratorError = true; + _iteratorError = _context2.t1; - case 23: - return context$4$0.finish(20); + case 17: + _context2.prev = 17; + _context2.prev = 18; - case 24: - return context$4$0.finish(17); + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } - case 25: - case 'end': - return context$4$0.stop(); + case 20: + _context2.prev = 20; + + if (!_didIteratorError) { + _context2.next = 23; + break; + } + + throw _iteratorError; + + case 23: + return _context2.finish(20); + + case 24: + return _context2.finish(17); + + case 25: + case 'end': + return _context2.stop(); + } } - }, marked3$0[0], this, [[3, 13, 17, 25], [18,, 20, 24]]); + }, _marked[0], this, [[3, 13, 17, 25], [18,, 20, 24]]); } var iterator = getAnswer(); @@ -366,7 +403,12 @@ var Bot = (function (_EventEmitter) { }]); return Bot; -})(_events.EventEmitter); +}(_events.EventEmitter); -exports['default'] = Bot; -module.exports = exports['default']; +exports.default = Bot; +exports.File = _File2.default; +exports.Message = _Message2.default; +exports.BulkMessage = _BulkMessage2.default; +exports.Forward = _Forward2.default; +exports.Question = _Question2.default; +exports.Keyboard = _Keyboard2.default; diff --git a/build/poll.js b/build/poll.js deleted file mode 100644 index 89e41a7..0000000 --- a/build/poll.js +++ /dev/null @@ -1,19 +0,0 @@ -'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']; diff --git a/build/types/Base.js b/build/types/Base.js index fb2dc96..8f5e5f1 100644 --- a/build/types/Base.js +++ b/build/types/Base.js @@ -1,74 +1,76 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _events = require('events'); +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + var ANSWER_THRESHOLD = 10; /** * Base class of all classes */ -var Base = (function (_EventEmitter) { +var Base = function (_EventEmitter) { + _inherits(Base, _EventEmitter); + function Base(method) { _classCallCheck(this, Base); - _get(Object.getPrototypeOf(Base.prototype), 'constructor', this).call(this); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Base).call(this)); - this.method = method; - this.properties = {}; + _this.method = method; + _this.properties = {}; + return _this; } - _inherits(Base, _EventEmitter); + /** + * Sends the message, you should only use this method yourself if + * you are extending this class. Normally you should call bot.send(message) + * + * Events: message:sent => Emitted after sending the message to API, gets the + * API's response + * + * message:answer => Emitted when your message gets an answer from + * the contact (reply in case of groups) + * gets the Update object containing message + * + * @param {object} bot + * @return {promise} returns a promise, resolved with message:answer + */ + _createClass(Base, [{ key: 'send', - - /** - * Sends the message, you should only use this method yourself if - * you are extending this class. Normally you should call bot.send(message) - * - * Events: message:sent => Emitted after sending the message to API, gets the - * API's response - * - * message:answer => Emitted when your message gets an answer from - * the contact (reply in case of groups) - * gets the Update object containing message - * - * @param {object} bot - * @return {promise} returns a promise, resolved with message:answer - */ value: function send(bot) { - var _this = this; + var _this2 = this; if (this._keyboard) { var reply_markup = JSON.stringify(this._keyboard.getProperties()); this.properties.reply_markup = reply_markup; } - var messageId = undefined; + var messageId = void 0; return new Promise(function (resolve) { - bot.api[_this.method](_this.properties).then(function (response) { + bot.api[_this2.method](_this2.properties).then(function (response) { messageId = response.result.message_id; - _this.emit('message:sent', response); + _this2.emit('message:sent', response); }); - if (_this._keyboard.one_time_keyboard) { - _this._keyboard.replyMarkup = ''; + if (_this2._keyboard.one_time_keyboard) { + _this2._keyboard.replyMarkup = ''; } - var chat = _this.properties.chat_id; + var chat = _this2.properties.chat_id; var answers = 0; bot.on('update', function listener(result) { answers += result.length; @@ -98,18 +100,17 @@ var Base = (function (_EventEmitter) { }); }); } - }, { - key: 'getProperties', /** * Returns properties of the object * @return {object} properties of object */ + + }, { + key: 'getProperties', value: function getProperties() { return this.properties; } - }, { - key: 'setProperties', /** * Set properties of the object @@ -120,8 +121,11 @@ var Base = (function (_EventEmitter) { * defaults to true * @return {object} returns the properties (same as getProperties) */ + + }, { + key: 'setProperties', value: function setProperties(object) { - var extend = arguments[1] === undefined ? true : arguments[1]; + var extend = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; this.properties = extend ? Object.assign(this.properties, object) : object; @@ -130,7 +134,7 @@ var Base = (function (_EventEmitter) { }]); return Base; -})(_events.EventEmitter); +}(_events.EventEmitter); -exports['default'] = Base; +exports.default = Base; module.exports = exports['default']; diff --git a/build/types/BulkMessage.js b/build/types/BulkMessage.js index 2359ec5..f87c786 100644 --- a/build/types/BulkMessage.js +++ b/build/types/BulkMessage.js @@ -1,53 +1,55 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Message2 = require('./Message'); var _Message3 = _interopRequireDefault(_Message2); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + /** * Message class, used to send a message to multiple chats */ -var BulkMessage = (function (_Message) { +var BulkMessage = function (_Message) { + _inherits(BulkMessage, _Message); + /** * Create a new message * @param {object} properties Message properties, as defined by Telegram API */ function BulkMessage() { - var properties = arguments[0] === undefined ? {} : arguments[0]; + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, BulkMessage); - _get(Object.getPrototypeOf(BulkMessage.prototype), 'constructor', this).call(this, properties); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(BulkMessage).call(this, properties)); - this.chats = []; + _this.chats = []; + return _this; } - _inherits(BulkMessage, _Message); + /** + * Set multiple chat_id's for the message + * @param {number} chat + * @return {object} returns the message object + */ + _createClass(BulkMessage, [{ key: 'to', - - /** - * Set multiple chat_id's for the message - * @param {number} chat - * @return {object} returns the message object - */ value: function to() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; @@ -60,20 +62,21 @@ var BulkMessage = (function (_Message) { this.chats = chats; return this; } - }, { - key: 'send', /** * Send the message to all chats * @param {Bot} bot * @return {Promise} Resolved when the message is sent to all chats */ + + }, { + key: 'send', value: function send(bot) { - var _this = this; + var _this2 = this; var promises = this.chats.map(function (chat) { - var clone = Object.assign({}, _this.properties); - var message = new _Message3['default'](clone).to(chat); + var clone = Object.assign({}, _this2.properties); + var message = new _Message3.default(clone).to(chat); return message.send(bot); }); @@ -82,7 +85,7 @@ var BulkMessage = (function (_Message) { }]); return BulkMessage; -})(_Message3['default']); +}(_Message3.default); -exports['default'] = BulkMessage; +exports.default = BulkMessage; module.exports = exports['default']; diff --git a/build/types/File.js b/build/types/File.js index 30f54f4..a318aa6 100644 --- a/build/types/File.js +++ b/build/types/File.js @@ -1,20 +1,12 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +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'); } }; })(); +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"); } }; }(); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Base2 = require('./Base'); @@ -24,45 +16,53 @@ var _mime = require('mime'); var _mime2 = _interopRequireDefault(_mime); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + var TYPES = ['photo', 'video', 'document', 'audio']; /** * File class, used to send pictures/movies/audios/documents to chat */ -var File = (function (_Base) { +var File = function (_Base) { + _inherits(File, _Base); + /** * Create a new file instance * @param {object} properties File properties, as defined by Telegram API */ function File() { - var properties = arguments[0] === undefined ? {} : arguments[0]; + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, File); - _get(Object.getPrototypeOf(File.prototype), 'constructor', this).call(this, 'sendDocument'); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(File).call(this, 'sendDocument')); - this.properties = properties; - this._keyboard = new _Base3['default'](); + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; } - _inherits(File, _Base); + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + _createClass(File, [{ key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ value: function to(chat) { this.properties.chat_id = chat; return this; } - }, { - key: 'file', /** * Set file of the message @@ -71,6 +71,9 @@ var File = (function (_Base) { * file_id string, this option indicates file type * @return {object} returns the message object */ + + }, { + key: 'file', value: function file(_file, fileType) { if (fileType) { this.properties[fileType] = { file: _file }; @@ -78,7 +81,7 @@ var File = (function (_Base) { return this; } - var _mime$lookup$split = _mime2['default'].lookup(_file).split('/'); + var _mime$lookup$split = _mime2.default.lookup(_file).split('/'); var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); @@ -103,32 +106,32 @@ var File = (function (_Base) { return this; } - }, { - key: 'caption', /** * Set caption for photos * @param {string} text caption's text * @return {object} returns the message object */ + + }, { + key: 'caption', value: function caption(text) { this.properties.caption = text; return this; } - }, { - key: 'reply', /** * Set reply_to_message_id of the message * @param {number} id message_id of the message to reply to * @return {object} returns the message object */ + + }, { + key: 'reply', value: function reply(id) { this.properties.reply_to_message_id = id; return this; } - }, { - key: 'keyboard', /** * Sets keyboard of the message @@ -137,6 +140,9 @@ var File = (function (_Base) { * @param {object} kb A Keyboard instance * @return {object} returns the message object */ + + }, { + key: 'keyboard', value: function keyboard(kb) { this._keyboard = kb; return this; @@ -147,7 +153,7 @@ var File = (function (_Base) { }]); return File; -})(_Base3['default']); +}(_Base3.default); -exports['default'] = File; +exports.default = File; module.exports = exports['default']; diff --git a/build/types/Forward.js b/build/types/Forward.js index 44871c3..14a0e26 100644 --- a/build/types/Forward.js +++ b/build/types/Forward.js @@ -1,28 +1,30 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Base2 = require('./Base'); var _Base3 = _interopRequireDefault(_Base2); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + /** * Forward class, used to forward messages from a chat to another */ -var Forward = (function (_Base) { +var Forward = function (_Base) { + _inherits(Forward, _Base); + /** * Create a new forward message * @param {object} properties Forward Message properties, as defined by @@ -30,50 +32,52 @@ var Forward = (function (_Base) { */ function Forward() { - var properties = arguments[0] === undefined ? {} : arguments[0]; + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Forward); - _get(Object.getPrototypeOf(Forward.prototype), 'constructor', this).call(this, 'forwardMessage'); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Forward).call(this, 'forwardMessage')); - this.properties = properties; - this._keyboard = new _Base3['default'](); + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; } - _inherits(Forward, _Base); + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + _createClass(Forward, [{ key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ value: function to(chat) { this.properties.chat_id = chat; return this; } - }, { - key: 'from', /** * Set from_chat_id, source of message's chat's id * @param {number} chat Source chat id * @return {object} returns the message object */ + + }, { + key: 'from', value: function from(chat) { this.properties.from_chat_id = chat; return this; } - }, { - key: 'message', /** * Sets message_id, the message to forward from source to target chat * @param {number} message ID of the message to forward * @return {object} returns the message object */ + + }, { + key: 'message', value: function message(_message) { this.properties.message_id = _message; return this; @@ -84,7 +88,7 @@ var Forward = (function (_Base) { }]); return Forward; -})(_Base3['default']); +}(_Base3.default); -exports['default'] = Forward; +exports.default = Forward; module.exports = exports['default']; diff --git a/build/types/Keyboard.js b/build/types/Keyboard.js index c1fc8d0..460b17f 100644 --- a/build/types/Keyboard.js +++ b/build/types/Keyboard.js @@ -1,29 +1,31 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x6, _x7, _x8) { var _again = true; _function: while (_again) { var object = _x6, property = _x7, receiver = _x8; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x6 = parent; _x7 = property; _x8 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Base2 = require('./Base'); var _Base3 = _interopRequireDefault(_Base2); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + /** * Keyboard class, used to configure keyboards for messages. * You should pass your instance of this class to message.keyboard() method */ -var Keyboard = (function (_Base) { +var Keyboard = function (_Base) { + _inherits(Keyboard, _Base); + /** * Create a new keyboard * @param {object} properties Keyboard properties, as defined by Telegram API @@ -32,96 +34,101 @@ var Keyboard = (function (_Base) { */ function Keyboard() { - var properties = arguments[0] === undefined ? {} : arguments[0]; + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Keyboard); - _get(Object.getPrototypeOf(Keyboard.prototype), 'constructor', this).call(this); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Keyboard).call(this)); - this.properties = properties; + _this.properties = properties; + return _this; } - _inherits(Keyboard, _Base); + /** + * Set the keyboard property of reply_markup + * @param {array} keys An array of arrays, with the format of + * Column Column + * Row [['TopLeft', 'TopRight'], + * Row ['BottomLeft', 'BottomRight']] + * @return {object} returns the keyboard object + */ + _createClass(Keyboard, [{ key: 'keys', - - /** - * Set the keyboard property of reply_markup - * @param {array} keys An array of arrays, with the format of - * Column Column - * Row [['TopLeft', 'TopRight'], - * Row ['BottomLeft', 'BottomRight']] - * @return {object} returns the keyboard object - */ value: function keys(_keys) { this.properties.keyboard = _keys; this.properties.hide_keyboard = false; return this; } - }, { - key: 'force', /** * Set force_keyboard property of reply_markup * @param {boolean} enable value of force_keyboard, defaults to true * @return {object} returns the keyboard object */ + + }, { + key: 'force', value: function force() { - var enable = arguments[0] === undefined ? true : arguments[0]; + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; this.properties.force_keyboard = enable; return this; } - }, { - key: 'resize', /** * Set resize_keyboard property of reply_markup * @param {boolean} enable value of resize_keyboard, defaults to true * @return {object} returns the keyboard object */ + + }, { + key: 'resize', value: function resize() { - var enable = arguments[0] === undefined ? true : arguments[0]; + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; this.properties.resize_keyboard = enable; return this; } - }, { - key: 'oneTime', /** * Set force_keyboard property of reply_markup * @param {boolean} enable value of force_keyboard, defaults to true * @return {object} returns the keyboard object */ + + }, { + key: 'oneTime', value: function oneTime() { - var enable = arguments[0] === undefined ? true : arguments[0]; + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; this.properties.one_time_keyboard = enable; return this; } - }, { - key: 'selective', /** * Set selective property of reply_markup * @param {boolean} enable value of force_keyboard, defaults to true * @return {object} returns the keyboard object */ + + }, { + key: 'selective', value: function selective() { - var enable = arguments[0] === undefined ? true : arguments[0]; + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; this.properties.selective = enable; return this; } - }, { - key: 'hide', /** * Set hide_keyboard property of reply_markup to true * @return {object} returns the keyboard object */ + + }, { + key: 'hide', value: function hide() { this.properties = { hide_keyboard: true @@ -132,7 +139,7 @@ var Keyboard = (function (_Base) { }]); return Keyboard; -})(_Base3['default']); +}(_Base3.default); -exports['default'] = Keyboard; +exports.default = Keyboard; module.exports = exports['default']; diff --git a/build/types/Message.js b/build/types/Message.js index 1f18b09..9084494 100644 --- a/build/types/Message.js +++ b/build/types/Message.js @@ -1,98 +1,133 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Base2 = require('./Base'); var _Base3 = _interopRequireDefault(_Base2); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + /** * Message class, used to send message to a chat */ -var Message = (function (_Base) { +var Message = function (_Base) { + _inherits(Message, _Base); + /** * Create a new message * @param {object} properties Message properties, as defined by Telegram API */ function Message() { - var properties = arguments[0] === undefined ? {} : arguments[0]; + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Message); - _get(Object.getPrototypeOf(Message.prototype), 'constructor', this).call(this, 'sendMessage'); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Message).call(this, 'sendMessage')); - this.properties = properties; - this._keyboard = new _Base3['default'](); + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; } - _inherits(Message, _Base); + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + _createClass(Message, [{ key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ value: function to(chat) { this.properties.chat_id = chat; return this; } - }, { - key: 'text', /** * Set text of the message * @param {string} text Message's content * @return {object} returns the message object */ + + }, { + key: 'text', value: function text(_text) { this.properties.text = _text; return this; } + + /** + * Set text of the message in HTML format + * @param {string} text Message's content in HTML format + * @return {object} returns the message object + */ + }, { - key: 'reply', + key: 'html', + value: function html(text) { + this.properties.parse_mode = 'HTML'; + if (text) { + this.properties.text = text; + } + return this; + } + + /** + * Set text of the message in Markdown format + * @param {string} text Message's content in Markdown format + * @return {object} returns the message object + */ + + }, { + key: 'markdown', + value: function markdown(text) { + this.properties.parse_mode = 'Markdown'; + if (text) { + this.properties.text = text; + } + return this; + } /** * Set reply_to_message_id of the message * @param {number} id message_id of the message to reply to * @return {object} returns the message object */ + + }, { + key: 'reply', value: function reply(id) { this.properties.reply_to_message_id = id; return this; } - }, { - key: 'preview', /** * Set disable_web_page_preview of the message * @param {boolean} enable * @return {object} returns the message object */ + + }, { + key: 'preview', value: function preview() { - var enable = arguments[0] === undefined ? true : arguments[0]; + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; this.properties.disable_web_page_preview = !enable; return this; } - }, { - key: 'keyboard', /** * Sets keyboard of the message @@ -101,6 +136,9 @@ var Message = (function (_Base) { * @param {object} kb A Keyboard instance * @return {object} returns the message object */ + + }, { + key: 'keyboard', value: function keyboard(kb) { this._keyboard = kb; return this; @@ -111,7 +149,7 @@ var Message = (function (_Base) { }]); return Message; -})(_Base3['default']); +}(_Base3.default); -exports['default'] = Message; +exports.default = Message; module.exports = exports['default']; diff --git a/build/types/Question.js b/build/types/Question.js index 824ad40..7aa5819 100644 --- a/build/types/Question.js +++ b/build/types/Question.js @@ -1,18 +1,12 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { +Object.defineProperty(exports, "__esModule", { value: true }); -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } +var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _Message2 = require('./Message'); @@ -22,13 +16,23 @@ var _Keyboard = require('./Keyboard'); var _Keyboard2 = _interopRequireDefault(_Keyboard); +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + /** * Question class, extends Message * Sends a message, shows a keyboard with the answers provided, and validates * the answer */ -var Question = (function (_Message) { +var Question = function (_Message) { + _inherits(Question, _Message); + /** * Create a new question * @param {object} options Options, same as Message, plus `answers` which @@ -36,36 +40,34 @@ var Question = (function (_Message) { */ function Question() { - var options = arguments[0] === undefined ? {} : arguments[0]; + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Question); - _get(Object.getPrototypeOf(Question.prototype), 'constructor', this).call(this, options); + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Question).call(this, options)); - var kb = new _Keyboard2['default']().force().oneTime().selective(); - this.keyboard(kb); + var kb = new _Keyboard2.default().force().oneTime().selective(); + _this.keyboard(kb); - this.answers(options.answers); + _this.answers(options.answers); + return _this; } - _inherits(Question, _Message); + /** + * Sets answers of the question. This is passed to Keyboard#keys, and then + * used to validate the answer given + * @param {array} answers Array of arrays of strings, same as Keyboard#keys + * @return {object} returns the question object + */ + _createClass(Question, [{ key: 'answers', - - /** - * Sets answers of the question. This is passed to Keyboard#keys, and then - * used to validate the answer given - * @param {array} answers Array of arrays of strings, same as Keyboard#keys - * @return {object} returns the question object - */ value: function answers(_answers) { this._answers = _answers; this._keyboard.keys(_answers); return this; } - }, { - key: 'send', /** * Sends the question (same as Message#send), and validates the answer given @@ -76,13 +78,16 @@ var Question = (function (_Message) { * @return {promise} A promise which is resolved in case of valid answer, and * rejected in case of invalid answer */ + + }, { + key: 'send', value: function send(bot) { - var _this = this; + var _this2 = this; var answers = this._answers; return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) { - var answer = undefined; + var answer = void 0; answers.forEach(function find(a) { if (Array.isArray(a)) { @@ -94,10 +99,10 @@ var Question = (function (_Message) { }); if (answer) { - _this.emit('question:answer', answer, message); + _this2.emit('question:answer', answer, message); return message; } else { - _this.emit('question:invalid', message); + _this2.emit('question:invalid', message); throw message; } }); @@ -105,7 +110,7 @@ var Question = (function (_Message) { }]); return Question; -})(_Message3['default']); +}(_Message3.default); -exports['default'] = Question; +exports.default = Question; module.exports = exports['default']; diff --git a/build/utils.js b/build/utils.js deleted file mode 100644 index 3918c74..0000000 --- a/build/utils.js +++ /dev/null @@ -1 +0,0 @@ -"use strict"; diff --git a/build/webhook.js b/build/webhook.js deleted file mode 100644 index c00c59f..0000000 --- a/build/webhook.js +++ /dev/null @@ -1,42 +0,0 @@ -'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']; diff --git a/index.js b/index.js deleted file mode 100644 index 8ae3093..0000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -require('babel/polyfill'); -module.exports = require('./build/'); diff --git a/package.json b/package.json index d4725a9..7008afb 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "telegram-api", - "version": "0.6.1", + "version": "0.7.0", "description": "Control Telegram bots easily using the new Telegram API", - "main": "index.js", + "main": "build/index.js", "scripts": { "test": "grunt test" }, @@ -34,14 +34,17 @@ }, "homepage": "https://github.com/mdibaiee/node-telegram-api", "dependencies": { - "babel": "5.6.14", - "grunt": "0.4.5", - "grunt-babel": "5.0.1", - "grunt-contrib-copy": "0.8.0", - "grunt-contrib-watch": "0.6.1", - "grunt-eslint": "16.0.0", + "babel-polyfill": "^6.7.4", "mime": "1.3.4", "qs": "4.0.0", "unirest": "0.4.2" + }, + "devDependencies": { + "babel-plugin-add-module-exports": "^0.1.2", + "babel-preset-es2015": "^6.6.0", + "grunt": "^0.4.5", + "grunt-babel": "^6.0.0", + "grunt-contrib-watch": "^1.0.0", + "grunt-copy": "^0.1.0" } } diff --git a/lib/functions/api.js b/src/functions/api.js similarity index 100% rename from lib/functions/api.js rename to src/functions/api.js diff --git a/lib/functions/argument-parser.js b/src/functions/argument-parser.js similarity index 100% rename from lib/functions/argument-parser.js rename to src/functions/argument-parser.js diff --git a/lib/functions/fetch.js b/src/functions/fetch.js similarity index 100% rename from lib/functions/fetch.js rename to src/functions/fetch.js diff --git a/lib/functions/poll.js b/src/functions/poll.js similarity index 100% rename from lib/functions/poll.js rename to src/functions/poll.js diff --git a/lib/functions/webhook.js b/src/functions/webhook.js similarity index 100% rename from lib/functions/webhook.js rename to src/functions/webhook.js diff --git a/lib/index.js b/src/index.js similarity index 94% rename from lib/index.js rename to src/index.js index b32de1e..1909af4 100644 --- a/lib/index.js +++ b/src/index.js @@ -1,9 +1,15 @@ +import 'babel-polyfill'; import API from './functions/api'; 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'; +import File from './types/File'; +import Keyboard from './types/Keyboard'; +import BulkMessage from './types/BulkMessage'; +import Question from './types/Question'; +import Forward from './types/Forward'; const DEFAULTS = { update: { @@ -14,7 +20,8 @@ const DEFAULTS = { }; const REQUIRED = 0; -const OPTIONAL = 1; + +console.log(poll); /** * Bot class used to connect to a new bot @@ -164,7 +171,9 @@ export default class Bot extends EventEmitter { update.forEach(res => { let text = res.message.text; - if (!text) return; + if (!text) { + return; + } const selfUsername = `@${this.info.username}`; @@ -223,3 +232,12 @@ export default class Bot extends EventEmitter { }); } } + +export { + File, + Message, + BulkMessage, + Forward, + Question, + Keyboard +}; diff --git a/lib/types/Base.js b/src/types/Base.js similarity index 100% rename from lib/types/Base.js rename to src/types/Base.js diff --git a/lib/types/BulkMessage.js b/src/types/BulkMessage.js similarity index 100% rename from lib/types/BulkMessage.js rename to src/types/BulkMessage.js diff --git a/lib/types/File.js b/src/types/File.js similarity index 100% rename from lib/types/File.js rename to src/types/File.js diff --git a/lib/types/Forward.js b/src/types/Forward.js similarity index 100% rename from lib/types/Forward.js rename to src/types/Forward.js diff --git a/lib/types/Keyboard.js b/src/types/Keyboard.js similarity index 100% rename from lib/types/Keyboard.js rename to src/types/Keyboard.js diff --git a/lib/types/Message.js b/src/types/Message.js similarity index 100% rename from lib/types/Message.js rename to src/types/Message.js diff --git a/lib/types/Question.js b/src/types/Question.js similarity index 100% rename from lib/types/Question.js rename to src/types/Question.js diff --git a/types/Base.js b/types/Base.js deleted file mode 100644 index fb2dc96..0000000 --- a/types/Base.js +++ /dev/null @@ -1,136 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _events = require('events'); - -var ANSWER_THRESHOLD = 10; - -/** - * Base class of all classes - */ - -var Base = (function (_EventEmitter) { - function Base(method) { - _classCallCheck(this, Base); - - _get(Object.getPrototypeOf(Base.prototype), 'constructor', this).call(this); - - this.method = method; - this.properties = {}; - } - - _inherits(Base, _EventEmitter); - - _createClass(Base, [{ - key: 'send', - - /** - * Sends the message, you should only use this method yourself if - * you are extending this class. Normally you should call bot.send(message) - * - * Events: message:sent => Emitted after sending the message to API, gets the - * API's response - * - * message:answer => Emitted when your message gets an answer from - * the contact (reply in case of groups) - * gets the Update object containing message - * - * @param {object} bot - * @return {promise} returns a promise, resolved with message:answer - */ - value: function send(bot) { - var _this = this; - - if (this._keyboard) { - var reply_markup = JSON.stringify(this._keyboard.getProperties()); - this.properties.reply_markup = reply_markup; - } - - var messageId = undefined; - return new Promise(function (resolve) { - bot.api[_this.method](_this.properties).then(function (response) { - messageId = response.result.message_id; - _this.emit('message:sent', response); - }); - - if (_this._keyboard.one_time_keyboard) { - _this._keyboard.replyMarkup = ''; - } - - var chat = _this.properties.chat_id; - var answers = 0; - bot.on('update', function listener(result) { - answers += result.length; - - var update = result.find(function (_ref) { - var message = _ref.message; - - // if in a group, there will be a reply to this message - if (chat < 0) { - return message.chat.id === chat && message.reply_to_message && message.reply_to_message.message_id === messageId; - } else { - return message.chat.id === chat; - } - }); - - if (update) { - resolve(update.message); - - this.emit('message:answer', update.message); - - bot.removeListener('update', listener); - } - - if (answers >= ANSWER_THRESHOLD) { - bot.removeListener('update', listener); - } - }); - }); - } - }, { - key: 'getProperties', - - /** - * Returns properties of the object - * @return {object} properties of object - */ - value: function getProperties() { - return this.properties; - } - }, { - key: 'setProperties', - - /** - * Set properties of the object - * @param {object} object properties to set - * @param {boolean} extend A boolean indicating if the properties should be - * extended by the object provided (Object.assign) - * or properties should be replaced by the object - * defaults to true - * @return {object} returns the properties (same as getProperties) - */ - value: function setProperties(object) { - var extend = arguments[1] === undefined ? true : arguments[1]; - - this.properties = extend ? Object.assign(this.properties, object) : object; - - return this.getProperties(); - } - }]); - - return Base; -})(_events.EventEmitter); - -exports['default'] = Base; -module.exports = exports['default']; diff --git a/types/BulkMessage.js b/types/BulkMessage.js deleted file mode 100644 index 2359ec5..0000000 --- a/types/BulkMessage.js +++ /dev/null @@ -1,88 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -/** - * Message class, used to send a message to multiple chats - */ - -var BulkMessage = (function (_Message) { - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function BulkMessage() { - var properties = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, BulkMessage); - - _get(Object.getPrototypeOf(BulkMessage.prototype), 'constructor', this).call(this, properties); - - this.chats = []; - } - - _inherits(BulkMessage, _Message); - - _createClass(BulkMessage, [{ - key: 'to', - - /** - * Set multiple chat_id's for the message - * @param {number} chat - * @return {object} returns the message object - */ - value: function to() { - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; - } - - var chats = args.reduce(function (a, b) { - return a.concat(b); - }, []); - - this.chats = chats; - return this; - } - }, { - key: 'send', - - /** - * Send the message to all chats - * @param {Bot} bot - * @return {Promise} Resolved when the message is sent to all chats - */ - value: function send(bot) { - var _this = this; - - var promises = this.chats.map(function (chat) { - var clone = Object.assign({}, _this.properties); - var message = new _Message3['default'](clone).to(chat); - return message.send(bot); - }); - - return Promise.all(promises); - } - }]); - - return BulkMessage; -})(_Message3['default']); - -exports['default'] = BulkMessage; -module.exports = exports['default']; diff --git a/types/File.js b/types/File.js deleted file mode 100644 index 30f54f4..0000000 --- a/types/File.js +++ /dev/null @@ -1,153 +0,0 @@ -'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'); } }; })(); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -var _mime = require('mime'); - -var _mime2 = _interopRequireDefault(_mime); - -var TYPES = ['photo', 'video', 'document', 'audio']; - -/** - * File class, used to send pictures/movies/audios/documents to chat - */ - -var File = (function (_Base) { - /** - * Create a new file instance - * @param {object} properties File properties, as defined by Telegram API - */ - - function File() { - var properties = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, File); - - _get(Object.getPrototypeOf(File.prototype), 'constructor', this).call(this, 'sendDocument'); - - this.properties = properties; - this._keyboard = new _Base3['default'](); - } - - _inherits(File, _Base); - - _createClass(File, [{ - key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - }, { - key: 'file', - - /** - * Set file of the message - * @param {string} file File path - * @param {string} fileType (optional) if the first argument is a - * file_id string, this option indicates file type - * @return {object} returns the message object - */ - value: function file(_file, fileType) { - if (fileType) { - this.properties[fileType] = { file: _file }; - - return this; - } - - var _mime$lookup$split = _mime2['default'].lookup(_file).split('/'); - - var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); - - var type = _mime$lookup$split2[0]; - var extension = _mime$lookup$split2[1]; - - if (type === 'image') { - type = 'photo'; - } - - if (extension === 'gif') { - type = 'document'; - } - - if (TYPES.indexOf(type) === -1) { - type = 'document'; - } - - this.properties[type] = { file: _file }; - - this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); - - return this; - } - }, { - key: 'caption', - - /** - * Set caption for photos - * @param {string} text caption's text - * @return {object} returns the message object - */ - value: function caption(text) { - this.properties.caption = text; - return this; - } - }, { - key: 'reply', - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - }, { - key: 'keyboard', - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return File; -})(_Base3['default']); - -exports['default'] = File; -module.exports = exports['default']; diff --git a/types/Forward.js b/types/Forward.js deleted file mode 100644 index 44871c3..0000000 --- a/types/Forward.js +++ /dev/null @@ -1,90 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -/** - * Forward class, used to forward messages from a chat to another - */ - -var Forward = (function (_Base) { - /** - * Create a new forward message - * @param {object} properties Forward Message properties, as defined by - * Telegram API - */ - - function Forward() { - var properties = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Forward); - - _get(Object.getPrototypeOf(Forward.prototype), 'constructor', this).call(this, 'forwardMessage'); - - this.properties = properties; - this._keyboard = new _Base3['default'](); - } - - _inherits(Forward, _Base); - - _createClass(Forward, [{ - key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - }, { - key: 'from', - - /** - * Set from_chat_id, source of message's chat's id - * @param {number} chat Source chat id - * @return {object} returns the message object - */ - value: function from(chat) { - this.properties.from_chat_id = chat; - return this; - } - }, { - key: 'message', - - /** - * Sets message_id, the message to forward from source to target chat - * @param {number} message ID of the message to forward - * @return {object} returns the message object - */ - value: function message(_message) { - this.properties.message_id = _message; - return this; - } - - // This class inherits Base's send method - - }]); - - return Forward; -})(_Base3['default']); - -exports['default'] = Forward; -module.exports = exports['default']; diff --git a/types/Keyboard.js b/types/Keyboard.js deleted file mode 100644 index c1fc8d0..0000000 --- a/types/Keyboard.js +++ /dev/null @@ -1,138 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x6, _x7, _x8) { var _again = true; _function: while (_again) { var object = _x6, property = _x7, receiver = _x8; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x6 = parent; _x7 = property; _x8 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -/** - * Keyboard class, used to configure keyboards for messages. - * You should pass your instance of this class to message.keyboard() method - */ - -var Keyboard = (function (_Base) { - /** - * Create a new keyboard - * @param {object} properties Keyboard properties, as defined by Telegram API - * See ReplyKeyboardMarkup, ReplyKeyboardHide, - * ForceReply - */ - - function Keyboard() { - var properties = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Keyboard); - - _get(Object.getPrototypeOf(Keyboard.prototype), 'constructor', this).call(this); - - this.properties = properties; - } - - _inherits(Keyboard, _Base); - - _createClass(Keyboard, [{ - key: 'keys', - - /** - * Set the keyboard property of reply_markup - * @param {array} keys An array of arrays, with the format of - * Column Column - * Row [['TopLeft', 'TopRight'], - * Row ['BottomLeft', 'BottomRight']] - * @return {object} returns the keyboard object - */ - value: function keys(_keys) { - this.properties.keyboard = _keys; - this.properties.hide_keyboard = false; - return this; - } - }, { - key: 'force', - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - value: function force() { - var enable = arguments[0] === undefined ? true : arguments[0]; - - this.properties.force_keyboard = enable; - return this; - } - }, { - key: 'resize', - - /** - * Set resize_keyboard property of reply_markup - * @param {boolean} enable value of resize_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - value: function resize() { - var enable = arguments[0] === undefined ? true : arguments[0]; - - this.properties.resize_keyboard = enable; - return this; - } - }, { - key: 'oneTime', - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - value: function oneTime() { - var enable = arguments[0] === undefined ? true : arguments[0]; - - this.properties.one_time_keyboard = enable; - return this; - } - }, { - key: 'selective', - - /** - * Set selective property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - value: function selective() { - var enable = arguments[0] === undefined ? true : arguments[0]; - - this.properties.selective = enable; - return this; - } - }, { - key: 'hide', - - /** - * Set hide_keyboard property of reply_markup to true - * @return {object} returns the keyboard object - */ - value: function hide() { - this.properties = { - hide_keyboard: true - }; - - return this; - } - }]); - - return Keyboard; -})(_Base3['default']); - -exports['default'] = Keyboard; -module.exports = exports['default']; diff --git a/types/Message.js b/types/Message.js deleted file mode 100644 index 1f18b09..0000000 --- a/types/Message.js +++ /dev/null @@ -1,117 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -/** - * Message class, used to send message to a chat - */ - -var Message = (function (_Base) { - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function Message() { - var properties = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Message); - - _get(Object.getPrototypeOf(Message.prototype), 'constructor', this).call(this, 'sendMessage'); - - this.properties = properties; - this._keyboard = new _Base3['default'](); - } - - _inherits(Message, _Base); - - _createClass(Message, [{ - key: 'to', - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - }, { - key: 'text', - - /** - * Set text of the message - * @param {string} text Message's content - * @return {object} returns the message object - */ - value: function text(_text) { - this.properties.text = _text; - return this; - } - }, { - key: 'reply', - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - }, { - key: 'preview', - - /** - * Set disable_web_page_preview of the message - * @param {boolean} enable - * @return {object} returns the message object - */ - value: function preview() { - var enable = arguments[0] === undefined ? true : arguments[0]; - - this.properties.disable_web_page_preview = !enable; - return this; - } - }, { - key: 'keyboard', - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return Message; -})(_Base3['default']); - -exports['default'] = Message; -module.exports = exports['default']; diff --git a/types/Question.js b/types/Question.js deleted file mode 100644 index 824ad40..0000000 --- a/types/Question.js +++ /dev/null @@ -1,111 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, '__esModule', { - value: true -}); - -var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); - -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -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'); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; } - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -var _Keyboard = require('./Keyboard'); - -var _Keyboard2 = _interopRequireDefault(_Keyboard); - -/** - * Question class, extends Message - * Sends a message, shows a keyboard with the answers provided, and validates - * the answer - */ - -var Question = (function (_Message) { - /** - * Create a new question - * @param {object} options Options, same as Message, plus `answers` which - * is a keyboard layout, see Keyboard#keys - */ - - function Question() { - var options = arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Question); - - _get(Object.getPrototypeOf(Question.prototype), 'constructor', this).call(this, options); - - var kb = new _Keyboard2['default']().force().oneTime().selective(); - this.keyboard(kb); - - this.answers(options.answers); - } - - _inherits(Question, _Message); - - _createClass(Question, [{ - key: 'answers', - - /** - * Sets answers of the question. This is passed to Keyboard#keys, and then - * used to validate the answer given - * @param {array} answers Array of arrays of strings, same as Keyboard#keys - * @return {object} returns the question object - */ - value: function answers(_answers) { - this._answers = _answers; - this._keyboard.keys(_answers); - return this; - } - }, { - key: 'send', - - /** - * Sends the question (same as Message#send), and validates the answer given - * if the answer is one of the defined answers, resolves, else rejects - * You should not manually use this method unless you're extending this class - * You should instead use bot.send(question); - * @param {object} bot - * @return {promise} A promise which is resolved in case of valid answer, and - * rejected in case of invalid answer - */ - value: function send(bot) { - var _this = this; - - var answers = this._answers; - - return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) { - var answer = undefined; - - answers.forEach(function find(a) { - if (Array.isArray(a)) { - a.forEach(find); - } - if (a === message.text) { - answer = a; - } - }); - - if (answer) { - _this.emit('question:answer', answer, message); - return message; - } else { - _this.emit('question:invalid', message); - throw message; - } - }); - } - }]); - - return Question; -})(_Message3['default']); - -exports['default'] = Question; -module.exports = exports['default']; -- 2.34.1 From 23cff62505465f0c169815627ad12ef25eff3de3 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 01:08:42 +0430 Subject: [PATCH 2/6] style: lint code lint code on files: index.js, poll.js and argument-parser.js --- build/functions/argument-parser.js | 8 ++++++-- build/functions/poll.js | 4 +++- build/index.js | 2 -- src/functions/argument-parser.js | 8 ++++++-- src/functions/poll.js | 4 +++- src/index.js | 2 -- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/build/functions/argument-parser.js b/build/functions/argument-parser.js index 81bc264..a1417ef 100644 --- a/build/functions/argument-parser.js +++ b/build/functions/argument-parser.js @@ -49,7 +49,9 @@ function argumentParser(format, string) { string = string.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim(); - if (!format) return { args: {}, params: {} }; + if (!format) { + return { args: {}, params: {} }; + } var indexes = [], params = {}; @@ -77,7 +79,9 @@ function argumentParser(format, string) { return getFormat(null, 'rest'); }); - if (!string) return { args: {}, params: params }; + if (!string) { + return { args: {}, params: params }; + } indexes = indexes.sort(function (a, b) { return a.offset < b.offset ? -1 : 1; diff --git a/build/functions/poll.js b/build/functions/poll.js index 8eabd3f..6698138 100644 --- a/build/functions/poll.js +++ b/build/functions/poll.js @@ -11,7 +11,9 @@ function poll(bot) { } bot.emit('update', response.result); - if (bot._stop) return null; + if (bot._stop) { + return null; + } return poll(bot); }); } diff --git a/build/index.js b/build/index.js index 4524dec..cf68987 100644 --- a/build/index.js +++ b/build/index.js @@ -69,8 +69,6 @@ var DEFAULTS = { var REQUIRED = 0; -console.log(_poll2.default); - /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, diff --git a/src/functions/argument-parser.js b/src/functions/argument-parser.js index 9ed7460..48376a0 100644 --- a/src/functions/argument-parser.js +++ b/src/functions/argument-parser.js @@ -40,7 +40,9 @@ 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 = {}; @@ -64,7 +66,9 @@ export default function argumentParser(format, string) { return getFormat(null, 'rest'); }); - if (!string) return {args: {}, params}; + if (!string) { + return {args: {}, params}; + } indexes = indexes.sort((a, b) => { return a.offset < b.offset ? -1 : 1; diff --git a/src/functions/poll.js b/src/functions/poll.js index 669a8f5..eb71b03 100644 --- a/src/functions/poll.js +++ b/src/functions/poll.js @@ -5,7 +5,9 @@ export default function poll(bot) { } bot.emit('update', response.result); - if (bot._stop) return null; + if (bot._stop) { + return null; + } return poll(bot); }); } diff --git a/src/index.js b/src/index.js index 1909af4..5f909ac 100644 --- a/src/index.js +++ b/src/index.js @@ -21,8 +21,6 @@ const DEFAULTS = { const REQUIRED = 0; -console.log(poll); - /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, -- 2.34.1 From 03bfae00283be78505c911d7a9171b4072d26db9 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 10:38:35 +0430 Subject: [PATCH 3/6] feat(API): add previous API copy types folder into module root path --- Gruntfile.js | 17 ++++- README.md | 4 ++ demo.js | 2 +- package.json | 2 + types/Base.js | 140 +++++++++++++++++++++++++++++++++++++ types/BulkMessage.js | 91 +++++++++++++++++++++++++ types/File.js | 159 +++++++++++++++++++++++++++++++++++++++++++ types/Forward.js | 94 +++++++++++++++++++++++++ types/Keyboard.js | 145 +++++++++++++++++++++++++++++++++++++++ types/Message.js | 155 +++++++++++++++++++++++++++++++++++++++++ types/Question.js | 116 +++++++++++++++++++++++++++++++ 11 files changed, 922 insertions(+), 3 deletions(-) create mode 100644 types/Base.js create mode 100644 types/BulkMessage.js create mode 100644 types/File.js create mode 100644 types/Forward.js create mode 100644 types/Keyboard.js create mode 100644 types/Message.js create mode 100644 types/Question.js diff --git a/Gruntfile.js b/Gruntfile.js index a51cb08..be67f3d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -13,17 +13,30 @@ module.exports = function(grunt) { eslint: { scripts: ['src/**/*.js'] }, + copy: { + classes: { + files: [{ + expand: true, + cwd: 'build/types', + src: '*', + dest: 'types' + }] + } + }, watch: { scripts: { files: ['src/**/*.js'], tasks: ['babel'] } - } + }, + clean: ['build', 'types'] }); grunt.loadNpmTasks('grunt-babel'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-eslint'); + grunt.loadNpmTasks('grunt-contrib-copy'); + grunt.loadNpmTasks('grunt-contrib-clean'); - grunt.registerTask('default', ['babel', 'eslint']); + grunt.registerTask('default', ['clean', 'babel', 'copy', 'eslint']); }; diff --git a/README.md b/README.md index 39b9766..897d9e3 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,10 @@ If you are looking for a real-life example of a bot written using this module, s Example ======= ```javascript +// ES6: +import Bot, { Message, File } from 'telegram-api'; + +// ES5: var Bot = require('telegram-api'); var Message = require('telegram-api/types/Message'); var File = require('telegram-api/types/File'); diff --git a/demo.js b/demo.js index c46cac0..e0ea837 100644 --- a/demo.js +++ b/demo.js @@ -1,4 +1,4 @@ -var Bot = require('telegram-api'); +var Bot = require('telegram-api').default; // only require the message types you need, more coming soon! var Message = require('telegram-api/types/Message'); diff --git a/package.json b/package.json index 7008afb..fadc76a 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,8 @@ "babel-preset-es2015": "^6.6.0", "grunt": "^0.4.5", "grunt-babel": "^6.0.0", + "grunt-contrib-clean": "^1.0.0", + "grunt-contrib-copy": "^1.0.0", "grunt-contrib-watch": "^1.0.0", "grunt-copy": "^0.1.0" } diff --git a/types/Base.js b/types/Base.js new file mode 100644 index 0000000..8f5e5f1 --- /dev/null +++ b/types/Base.js @@ -0,0 +1,140 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _events = require('events'); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var ANSWER_THRESHOLD = 10; + +/** + * Base class of all classes + */ + +var Base = function (_EventEmitter) { + _inherits(Base, _EventEmitter); + + function Base(method) { + _classCallCheck(this, Base); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Base).call(this)); + + _this.method = method; + _this.properties = {}; + return _this; + } + + /** + * Sends the message, you should only use this method yourself if + * you are extending this class. Normally you should call bot.send(message) + * + * Events: message:sent => Emitted after sending the message to API, gets the + * API's response + * + * message:answer => Emitted when your message gets an answer from + * the contact (reply in case of groups) + * gets the Update object containing message + * + * @param {object} bot + * @return {promise} returns a promise, resolved with message:answer + */ + + + _createClass(Base, [{ + key: 'send', + value: function send(bot) { + var _this2 = this; + + if (this._keyboard) { + var reply_markup = JSON.stringify(this._keyboard.getProperties()); + this.properties.reply_markup = reply_markup; + } + + var messageId = void 0; + return new Promise(function (resolve) { + bot.api[_this2.method](_this2.properties).then(function (response) { + messageId = response.result.message_id; + _this2.emit('message:sent', response); + }); + + if (_this2._keyboard.one_time_keyboard) { + _this2._keyboard.replyMarkup = ''; + } + + var chat = _this2.properties.chat_id; + var answers = 0; + bot.on('update', function listener(result) { + answers += result.length; + + var update = result.find(function (_ref) { + var message = _ref.message; + + // if in a group, there will be a reply to this message + if (chat < 0) { + return message.chat.id === chat && message.reply_to_message && message.reply_to_message.message_id === messageId; + } else { + return message.chat.id === chat; + } + }); + + if (update) { + resolve(update.message); + + this.emit('message:answer', update.message); + + bot.removeListener('update', listener); + } + + if (answers >= ANSWER_THRESHOLD) { + bot.removeListener('update', listener); + } + }); + }); + } + + /** + * Returns properties of the object + * @return {object} properties of object + */ + + }, { + key: 'getProperties', + value: function getProperties() { + return this.properties; + } + + /** + * Set properties of the object + * @param {object} object properties to set + * @param {boolean} extend A boolean indicating if the properties should be + * extended by the object provided (Object.assign) + * or properties should be replaced by the object + * defaults to true + * @return {object} returns the properties (same as getProperties) + */ + + }, { + key: 'setProperties', + value: function setProperties(object) { + var extend = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; + + this.properties = extend ? Object.assign(this.properties, object) : object; + + return this.getProperties(); + } + }]); + + return Base; +}(_events.EventEmitter); + +exports.default = Base; +module.exports = exports['default']; diff --git a/types/BulkMessage.js b/types/BulkMessage.js new file mode 100644 index 0000000..f87c786 --- /dev/null +++ b/types/BulkMessage.js @@ -0,0 +1,91 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _Message2 = require('./Message'); + +var _Message3 = _interopRequireDefault(_Message2); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +/** + * Message class, used to send a message to multiple chats + */ + +var BulkMessage = function (_Message) { + _inherits(BulkMessage, _Message); + + /** + * Create a new message + * @param {object} properties Message properties, as defined by Telegram API + */ + + function BulkMessage() { + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, BulkMessage); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(BulkMessage).call(this, properties)); + + _this.chats = []; + return _this; + } + + /** + * Set multiple chat_id's for the message + * @param {number} chat + * @return {object} returns the message object + */ + + + _createClass(BulkMessage, [{ + key: 'to', + value: function to() { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + var chats = args.reduce(function (a, b) { + return a.concat(b); + }, []); + + this.chats = chats; + return this; + } + + /** + * Send the message to all chats + * @param {Bot} bot + * @return {Promise} Resolved when the message is sent to all chats + */ + + }, { + key: 'send', + value: function send(bot) { + var _this2 = this; + + var promises = this.chats.map(function (chat) { + var clone = Object.assign({}, _this2.properties); + var message = new _Message3.default(clone).to(chat); + return message.send(bot); + }); + + return Promise.all(promises); + } + }]); + + return BulkMessage; +}(_Message3.default); + +exports.default = BulkMessage; +module.exports = exports['default']; diff --git a/types/File.js b/types/File.js new file mode 100644 index 0000000..a318aa6 --- /dev/null +++ b/types/File.js @@ -0,0 +1,159 @@ +'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"); } }; }(); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _Base2 = require('./Base'); + +var _Base3 = _interopRequireDefault(_Base2); + +var _mime = require('mime'); + +var _mime2 = _interopRequireDefault(_mime); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var TYPES = ['photo', 'video', 'document', 'audio']; + +/** + * File class, used to send pictures/movies/audios/documents to chat + */ + +var File = function (_Base) { + _inherits(File, _Base); + + /** + * Create a new file instance + * @param {object} properties File properties, as defined by Telegram API + */ + + function File() { + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, File); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(File).call(this, 'sendDocument')); + + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; + } + + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + + + _createClass(File, [{ + key: 'to', + value: function to(chat) { + this.properties.chat_id = chat; + return this; + } + + /** + * Set file of the message + * @param {string} file File path + * @param {string} fileType (optional) if the first argument is a + * file_id string, this option indicates file type + * @return {object} returns the message object + */ + + }, { + key: 'file', + value: function file(_file, fileType) { + if (fileType) { + this.properties[fileType] = { file: _file }; + + return this; + } + + var _mime$lookup$split = _mime2.default.lookup(_file).split('/'); + + var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); + + var type = _mime$lookup$split2[0]; + var extension = _mime$lookup$split2[1]; + + if (type === 'image') { + type = 'photo'; + } + + if (extension === 'gif') { + type = 'document'; + } + + if (TYPES.indexOf(type) === -1) { + type = 'document'; + } + + this.properties[type] = { file: _file }; + + this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); + + return this; + } + + /** + * Set caption for photos + * @param {string} text caption's text + * @return {object} returns the message object + */ + + }, { + key: 'caption', + value: function caption(text) { + this.properties.caption = text; + return this; + } + + /** + * Set reply_to_message_id of the message + * @param {number} id message_id of the message to reply to + * @return {object} returns the message object + */ + + }, { + key: 'reply', + value: function reply(id) { + this.properties.reply_to_message_id = id; + return this; + } + + /** + * Sets keyboard of the message + * The value of reply_markup is set to the sanitized keyboard properties + * i.e. reply_markup = JSON.stringify(kb.getProperties()) + * @param {object} kb A Keyboard instance + * @return {object} returns the message object + */ + + }, { + key: 'keyboard', + value: function keyboard(kb) { + this._keyboard = kb; + return this; + } + + // This class inherits Base's send method + + }]); + + return File; +}(_Base3.default); + +exports.default = File; +module.exports = exports['default']; diff --git a/types/Forward.js b/types/Forward.js new file mode 100644 index 0000000..14a0e26 --- /dev/null +++ b/types/Forward.js @@ -0,0 +1,94 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _Base2 = require('./Base'); + +var _Base3 = _interopRequireDefault(_Base2); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +/** + * Forward class, used to forward messages from a chat to another + */ + +var Forward = function (_Base) { + _inherits(Forward, _Base); + + /** + * Create a new forward message + * @param {object} properties Forward Message properties, as defined by + * Telegram API + */ + + function Forward() { + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, Forward); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Forward).call(this, 'forwardMessage')); + + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; + } + + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + + + _createClass(Forward, [{ + key: 'to', + value: function to(chat) { + this.properties.chat_id = chat; + return this; + } + + /** + * Set from_chat_id, source of message's chat's id + * @param {number} chat Source chat id + * @return {object} returns the message object + */ + + }, { + key: 'from', + value: function from(chat) { + this.properties.from_chat_id = chat; + return this; + } + + /** + * Sets message_id, the message to forward from source to target chat + * @param {number} message ID of the message to forward + * @return {object} returns the message object + */ + + }, { + key: 'message', + value: function message(_message) { + this.properties.message_id = _message; + return this; + } + + // This class inherits Base's send method + + }]); + + return Forward; +}(_Base3.default); + +exports.default = Forward; +module.exports = exports['default']; diff --git a/types/Keyboard.js b/types/Keyboard.js new file mode 100644 index 0000000..460b17f --- /dev/null +++ b/types/Keyboard.js @@ -0,0 +1,145 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _Base2 = require('./Base'); + +var _Base3 = _interopRequireDefault(_Base2); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +/** + * Keyboard class, used to configure keyboards for messages. + * You should pass your instance of this class to message.keyboard() method + */ + +var Keyboard = function (_Base) { + _inherits(Keyboard, _Base); + + /** + * Create a new keyboard + * @param {object} properties Keyboard properties, as defined by Telegram API + * See ReplyKeyboardMarkup, ReplyKeyboardHide, + * ForceReply + */ + + function Keyboard() { + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, Keyboard); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Keyboard).call(this)); + + _this.properties = properties; + return _this; + } + + /** + * Set the keyboard property of reply_markup + * @param {array} keys An array of arrays, with the format of + * Column Column + * Row [['TopLeft', 'TopRight'], + * Row ['BottomLeft', 'BottomRight']] + * @return {object} returns the keyboard object + */ + + + _createClass(Keyboard, [{ + key: 'keys', + value: function keys(_keys) { + this.properties.keyboard = _keys; + this.properties.hide_keyboard = false; + return this; + } + + /** + * Set force_keyboard property of reply_markup + * @param {boolean} enable value of force_keyboard, defaults to true + * @return {object} returns the keyboard object + */ + + }, { + key: 'force', + value: function force() { + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + this.properties.force_keyboard = enable; + return this; + } + + /** + * Set resize_keyboard property of reply_markup + * @param {boolean} enable value of resize_keyboard, defaults to true + * @return {object} returns the keyboard object + */ + + }, { + key: 'resize', + value: function resize() { + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + this.properties.resize_keyboard = enable; + return this; + } + + /** + * Set force_keyboard property of reply_markup + * @param {boolean} enable value of force_keyboard, defaults to true + * @return {object} returns the keyboard object + */ + + }, { + key: 'oneTime', + value: function oneTime() { + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + this.properties.one_time_keyboard = enable; + return this; + } + + /** + * Set selective property of reply_markup + * @param {boolean} enable value of force_keyboard, defaults to true + * @return {object} returns the keyboard object + */ + + }, { + key: 'selective', + value: function selective() { + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + this.properties.selective = enable; + return this; + } + + /** + * Set hide_keyboard property of reply_markup to true + * @return {object} returns the keyboard object + */ + + }, { + key: 'hide', + value: function hide() { + this.properties = { + hide_keyboard: true + }; + + return this; + } + }]); + + return Keyboard; +}(_Base3.default); + +exports.default = Keyboard; +module.exports = exports['default']; diff --git a/types/Message.js b/types/Message.js new file mode 100644 index 0000000..9084494 --- /dev/null +++ b/types/Message.js @@ -0,0 +1,155 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _Base2 = require('./Base'); + +var _Base3 = _interopRequireDefault(_Base2); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +/** + * Message class, used to send message to a chat + */ + +var Message = function (_Base) { + _inherits(Message, _Base); + + /** + * Create a new message + * @param {object} properties Message properties, as defined by Telegram API + */ + + function Message() { + var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, Message); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Message).call(this, 'sendMessage')); + + _this.properties = properties; + _this._keyboard = new _Base3.default(); + return _this; + } + + /** + * Set chat_id of the message + * @param {number} chat + * @return {object} returns the message object + */ + + + _createClass(Message, [{ + key: 'to', + value: function to(chat) { + this.properties.chat_id = chat; + return this; + } + + /** + * Set text of the message + * @param {string} text Message's content + * @return {object} returns the message object + */ + + }, { + key: 'text', + value: function text(_text) { + this.properties.text = _text; + return this; + } + + /** + * Set text of the message in HTML format + * @param {string} text Message's content in HTML format + * @return {object} returns the message object + */ + + }, { + key: 'html', + value: function html(text) { + this.properties.parse_mode = 'HTML'; + if (text) { + this.properties.text = text; + } + return this; + } + + /** + * Set text of the message in Markdown format + * @param {string} text Message's content in Markdown format + * @return {object} returns the message object + */ + + }, { + key: 'markdown', + value: function markdown(text) { + this.properties.parse_mode = 'Markdown'; + if (text) { + this.properties.text = text; + } + return this; + } + + /** + * Set reply_to_message_id of the message + * @param {number} id message_id of the message to reply to + * @return {object} returns the message object + */ + + }, { + key: 'reply', + value: function reply(id) { + this.properties.reply_to_message_id = id; + return this; + } + + /** + * Set disable_web_page_preview of the message + * @param {boolean} enable + * @return {object} returns the message object + */ + + }, { + key: 'preview', + value: function preview() { + var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; + + this.properties.disable_web_page_preview = !enable; + return this; + } + + /** + * Sets keyboard of the message + * The value of reply_markup is set to the sanitized keyboard properties + * i.e. reply_markup = JSON.stringify(kb.getProperties()) + * @param {object} kb A Keyboard instance + * @return {object} returns the message object + */ + + }, { + key: 'keyboard', + value: function keyboard(kb) { + this._keyboard = kb; + return this; + } + + // This class inherits Base's send method + + }]); + + return Message; +}(_Base3.default); + +exports.default = Message; +module.exports = exports['default']; diff --git a/types/Question.js b/types/Question.js new file mode 100644 index 0000000..7aa5819 --- /dev/null +++ b/types/Question.js @@ -0,0 +1,116 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; + +var _Message2 = require('./Message'); + +var _Message3 = _interopRequireDefault(_Message2); + +var _Keyboard = require('./Keyboard'); + +var _Keyboard2 = _interopRequireDefault(_Keyboard); + +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"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +/** + * Question class, extends Message + * Sends a message, shows a keyboard with the answers provided, and validates + * the answer + */ + +var Question = function (_Message) { + _inherits(Question, _Message); + + /** + * Create a new question + * @param {object} options Options, same as Message, plus `answers` which + * is a keyboard layout, see Keyboard#keys + */ + + function Question() { + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + _classCallCheck(this, Question); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Question).call(this, options)); + + var kb = new _Keyboard2.default().force().oneTime().selective(); + _this.keyboard(kb); + + _this.answers(options.answers); + return _this; + } + + /** + * Sets answers of the question. This is passed to Keyboard#keys, and then + * used to validate the answer given + * @param {array} answers Array of arrays of strings, same as Keyboard#keys + * @return {object} returns the question object + */ + + + _createClass(Question, [{ + key: 'answers', + value: function answers(_answers) { + this._answers = _answers; + this._keyboard.keys(_answers); + return this; + } + + /** + * Sends the question (same as Message#send), and validates the answer given + * if the answer is one of the defined answers, resolves, else rejects + * You should not manually use this method unless you're extending this class + * You should instead use bot.send(question); + * @param {object} bot + * @return {promise} A promise which is resolved in case of valid answer, and + * rejected in case of invalid answer + */ + + }, { + key: 'send', + value: function send(bot) { + var _this2 = this; + + var answers = this._answers; + + return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) { + var answer = void 0; + + answers.forEach(function find(a) { + if (Array.isArray(a)) { + a.forEach(find); + } + if (a === message.text) { + answer = a; + } + }); + + if (answer) { + _this2.emit('question:answer', answer, message); + return message; + } else { + _this2.emit('question:invalid', message); + throw message; + } + }); + } + }]); + + return Question; +}(_Message3.default); + +exports.default = Question; +module.exports = exports['default']; -- 2.34.1 From 6e732a36fb1dff2aa29062ac58aa45946c581776 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 12:09:16 +0430 Subject: [PATCH 4/6] chore: README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 897d9e3..5b3337b 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Example import Bot, { Message, File } from 'telegram-api'; // ES5: -var Bot = require('telegram-api'); +var Bot = require('telegram-api').default; var Message = require('telegram-api/types/Message'); var File = require('telegram-api/types/File'); -- 2.34.1 From 55b89482b416deb561966c11342f99bba246d459 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 12:41:40 +0430 Subject: [PATCH 5/6] chore: update .gitignore and package.json add build and types folder into .gitignore remove index.js from files in package.json --- .gitignore | 3 +++ package.json | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e1a83cf..047fc8f 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ node_modules test.js .DS_Store + +build +types diff --git a/package.json b/package.json index fadc76a..4f72051 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,7 @@ }, "files": [ "types", - "build", - "index.js" + "build" ], "directories": { "lib": "lib" -- 2.34.1 From 779176489211e6bbb2bcdc9cb5da9dbddcd88e74 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Mon, 28 Mar 2016 12:50:37 +0430 Subject: [PATCH 6/6] chore: update package.json, remove ignored files update directories in package.json, remove build and types folder --- build/functions/api.js | 50 ---- build/functions/argument-parser.js | 156 ----------- build/functions/fetch.js | 73 ----- build/functions/poll.js | 20 -- build/functions/webhook.js | 42 --- build/index.js | 412 ----------------------------- build/types/Base.js | 140 ---------- build/types/BulkMessage.js | 91 ------- build/types/File.js | 159 ----------- build/types/Forward.js | 94 ------- build/types/Keyboard.js | 145 ---------- build/types/Message.js | 155 ----------- build/types/Question.js | 116 -------- package.json | 2 +- types/Base.js | 140 ---------- types/BulkMessage.js | 91 ------- types/File.js | 159 ----------- types/Forward.js | 94 ------- types/Keyboard.js | 145 ---------- types/Message.js | 155 ----------- types/Question.js | 116 -------- 21 files changed, 1 insertion(+), 2554 deletions(-) delete mode 100644 build/functions/api.js delete mode 100644 build/functions/argument-parser.js delete mode 100644 build/functions/fetch.js delete mode 100644 build/functions/poll.js delete mode 100644 build/functions/webhook.js delete mode 100644 build/index.js delete mode 100644 build/types/Base.js delete mode 100644 build/types/BulkMessage.js delete mode 100644 build/types/File.js delete mode 100644 build/types/Forward.js delete mode 100644 build/types/Keyboard.js delete mode 100644 build/types/Message.js delete mode 100644 build/types/Question.js delete mode 100644 types/Base.js delete mode 100644 types/BulkMessage.js delete mode 100644 types/File.js delete mode 100644 types/Forward.js delete mode 100644 types/Keyboard.js delete mode 100644 types/Message.js delete mode 100644 types/Question.js diff --git a/build/functions/api.js b/build/functions/api.js deleted file mode 100644 index 2fc817b..0000000 --- a/build/functions/api.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _fetch = require('./fetch'); - -var _fetch2 = _interopRequireDefault(_fetch); - -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"); } } // API methods - - -/** - * 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']; diff --git a/build/functions/argument-parser.js b/build/functions/argument-parser.js deleted file mode 100644 index a1417ef..0000000 --- a/build/functions/argument-parser.js +++ /dev/null @@ -1,156 +0,0 @@ -'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(''); - -var REQUIRED = 0; -var OPTIONAL = 1; -var REST = 2; - -/** - * Parses a message for arguments, based on format - * - * The format option may include '' and '[optionalParam]' and - * '...[restParam]' - * 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: ' [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]+/, '').trim(); - format = format.replace(/[^\s]+/, '').trim(); - - if (!format) { - return { args: {}, params: {} }; - } - - var indexes = [], - params = {}; - - format = format.replace(/\s/g, '\\s*'); - format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg) { - var type = arguments.length <= 3 || arguments[3] === undefined ? 'word' : arguments[3]; - var offset = arguments[4]; - - indexes.push({ arg: arg, offset: offset }); - params[arg] = REQUIRED; - return (escape(symbols) + getFormat(type, 'required')).trim(); - }); - format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg) { - var type = arguments.length <= 3 || arguments[3] === undefined ? 'word' : arguments[3]; - var offset = arguments[4]; - - indexes.push({ arg: arg, offset: offset }); - params[arg] = OPTIONAL; - return (escape(symbols, '?') + getFormat(type, 'optional')).trim(); - }); - format = format.replace(FORMAT_REST, function (full, arg, offset) { - indexes.push({ offset: offset, arg: arg }); - params[arg] = REST; - return getFormat(null, 'rest'); - }); - - if (!string) { - return { args: {}, params: params }; - } - - indexes = indexes.sort(function (a, b) { - return a.offset < b.offset ? -1 : 1; - }); - - 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 { args: object, params: params }; -} - -function escape(symbols) { - var append = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; - - return symbols.split('').map(function (symbol) { - return (ESCAPABLE.indexOf(symbol) ? '\\' + symbol : symbol) + append; - }).join(''); -} - -var TYPES = { - 'number': '\\d', - 'word': '\\S' -}; - -function getFormat() { - var type = arguments.length <= 0 || arguments[0] === undefined ? 'word' : arguments[0]; - var param = arguments.length <= 1 || 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']; diff --git a/build/functions/fetch.js b/build/functions/fetch.js deleted file mode 100644 index 1af71bf..0000000 --- a/build/functions/fetch.js +++ /dev/null @@ -1,73 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.default = fetch; -exports.getBody = getBody; - -var _unirest = require('unirest'); - -var _unirest2 = _interopRequireDefault(_unirest); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -function fetch(path) { - var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - - return new Promise(function (resolve, reject) { - var files = {}; - - var _iteratorNormalCompletion = true; - var _didIteratorError = false; - var _iteratorError = undefined; - - try { - for (var _iterator = Object.keys(data)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var key = _step.value; - - if (data[key].file) { - files[key] = data[key].file; - delete data[key]; - } - } - } catch (err) { - _didIteratorError = true; - _iteratorError = err; - } finally { - try { - if (!_iteratorNormalCompletion && _iterator.return) { - _iterator.return(); - } - } finally { - if (_didIteratorError) { - throw _iteratorError; - } - } - } - - _unirest2.default.post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) { - if (response.statusType === 4 || response.statusType === 5 || !response.body || !response.body.ok) { - reject(response); - } else { - resolve(response.body); - } - }); - }); -} - -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); - }); -} diff --git a/build/functions/poll.js b/build/functions/poll.js deleted file mode 100644 index 6698138..0000000 --- a/build/functions/poll.js +++ /dev/null @@ -1,20 +0,0 @@ -'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); - - if (bot._stop) { - return null; - } - return poll(bot); - }); -} -module.exports = exports['default']; diff --git a/build/functions/webhook.js b/build/functions/webhook.js deleted file mode 100644 index 03b64ab..0000000 --- a/build/functions/webhook.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.default = webhook; - -var _http = require('http'); - -var _http2 = _interopRequireDefault(_http); - -var _qs = require('qs'); - -var _qs2 = _interopRequireDefault(_qs); - -var _fetch = require('./fetch'); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var DEFAULTS = { - server: {}, - port: 443 -}; - -function webhook() { - var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - var bot = arguments[1]; - - options = Object.assign(DEFAULTS, options); - - return bot.api.setWebhook(options.url).then(function () { - - bot._webhookServer = _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']; diff --git a/build/index.js b/build/index.js deleted file mode 100644 index cf68987..0000000 --- a/build/index.js +++ /dev/null @@ -1,412 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.Keyboard = exports.Question = exports.Forward = exports.BulkMessage = exports.Message = exports.File = undefined; - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -require('babel-polyfill'); - -var _api = require('./functions/api'); - -var _api2 = _interopRequireDefault(_api); - -var _webhook = require('./functions/webhook'); - -var _webhook2 = _interopRequireDefault(_webhook); - -var _poll = require('./functions/poll'); - -var _poll2 = _interopRequireDefault(_poll); - -var _argumentParser = require('./functions/argument-parser'); - -var _argumentParser2 = _interopRequireDefault(_argumentParser); - -var _events = require('events'); - -var _Message = require('./types/Message'); - -var _Message2 = _interopRequireDefault(_Message); - -var _File = require('./types/File'); - -var _File2 = _interopRequireDefault(_File); - -var _Keyboard = require('./types/Keyboard'); - -var _Keyboard2 = _interopRequireDefault(_Keyboard); - -var _BulkMessage = require('./types/BulkMessage'); - -var _BulkMessage2 = _interopRequireDefault(_BulkMessage); - -var _Question = require('./types/Question'); - -var _Question2 = _interopRequireDefault(_Question); - -var _Forward = require('./types/Forward'); - -var _Forward2 = _interopRequireDefault(_Forward); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var DEFAULTS = { - update: { - offset: 0, - timeout: 20, - limit: 100 - } -}; - -var REQUIRED = 0; - -/** - * Bot class used to connect to a new bot - * Bots have an api property which gives access to all Telegram API methods, - * see API class - */ - -var Bot = function (_EventEmitter) { - _inherits(Bot, _EventEmitter); - - /** - * Create and connect to a new bot - * @param {object} options Bot properties. - */ - - function Bot() { - var options = arguments.length <= 0 || arguments[0] === undefined ? { update: {} } : arguments[0]; - - _classCallCheck(this, Bot); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Bot).call(this)); - - if (!options.token) { - throw new Error('Token cannot be empty'); - } - - _this.token = options.token; - _this.update = Object.assign(options.update || {}, DEFAULTS.update); - - _this.api = new _api2.default(_this.token); - - _this.msg = {}; - - // EventEmitter - _this._events = {}; - _this._userEvents = []; - - _this.setMaxListeners(100); - return _this; - } - - /** - * Gets information about the bot and then - * 1) starts polling updates from API - * 2) sets a webhook as defined by the first parameter and listens for updates - * Emits an `update` event after polling with the response from server - * Returns a promise which is resolved after the bot information is received - * and set to it's `info` property i.e. bot.info - * - * @param {object} hook An object containg options passed to webhook - * properties: - * - url: HTTPS url to listen on POST requests coming - * from the Telegram API - * - port: the port to listen to, defaults to 443 - * - server: An object passed to https.createServer - * - * @return {promise} A promise which is resolved with the response of getMe - */ - - - _createClass(Bot, [{ - key: 'start', - value: function start(hook) { - var _this2 = this; - - if (hook) { - return (0, _webhook2.default)(hook, this); - } - return this.api.getMe().then(function (response) { - _this2.info = response.result; - - _this2.on('update', _this2._update); - - if (hook) { - return (0, _webhook2.default)(hook, _this2); - } else { - return (0, _poll2.default)(_this2); - } - }); - } - - /** - * Listens on specific message matching the pattern which can be an string - * or a regexp. - * @param {string/regex} pattern - * @param {function} listener function to call when a message matching the - * pattern is found, gets the Update - * In case of string, the message should start - * with the string i.e. /^yourString/ - * @return {object} returns the bot object - */ - - }, { - key: 'get', - value: function get(pattern, listener) { - if (typeof pattern === 'string') { - pattern = new RegExp('^' + pattern); - } - - this._userEvents.push({ - pattern: pattern, listener: listener - }); - - return this; - } - - /** - * Listens on a command - * @param {string} command the command string, should not include slash (/) - * @param {function} listener function to call when the command is received, - * gets the update - * @return {object} returns the bot object - */ - - }, { - key: 'command', - value: function command(_command, listener) { - var regex = /[^\s]+/; - - var cmd = _command.match(regex)[0].trim(); - - this._userEvents.push({ - pattern: new RegExp('^/' + cmd), - parse: _argumentParser2.default.bind(null, _command), - listener: listener - }); - - return this; - } - - /** - * Sends the message provided - * @param {object} message The message to send. Gets it's send method called - * @return {unknown} returns the result of calling message's send method - */ - - }, { - key: 'send', - value: function send(message) { - return message.send(this).catch(console.error); - } - - /** - * Stops the bot, deattaching all listeners and polling - */ - - }, { - key: 'stop', - value: function stop() { - this._stop = true; - - if (this._webhookServer) { - this._webhookServer.close(); - } - - this.removeListener('update', this._update); - this._events = {}; - } - - /** - * The internal update event listener, used to parse messages and fire - * command/get events - YOU SHOULD NOT USE THIS - * - * @param {object} update - */ - - }, { - key: '_update', - value: function _update(update) { - var _this3 = this; - - if (!this.update.offset) { - var updateId = update[update.length - 1].update_id; - this.update.offset = updateId; - } - if (this.update) { - this.update.offset += 1; - } - - update.forEach(function (res) { - var _marked = [getAnswer].map(regeneratorRuntime.mark); - - var text = res.message.text; - if (!text) { - return; - } - - var selfUsername = '@' + _this3.info.username; - - if (text.startsWith('/') && text.indexOf(selfUsername) > -1) { - // Commands are sent in /command@thisusername format in groups - var regex = new RegExp('(/.*)@' + _this3.info.username); - text = text.replace(regex, '$1'); - res.message.text = text; - } - - var ev = _this3._userEvents.find(function (_ref) { - var pattern = _ref.pattern; - return pattern.test(text); - }); - - if (!ev) { - _this3.emit('command-notfound', res.message); - return; - } - - 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; - - res.message.args = args; - - var requiredParams = Object.keys(params).filter(function (param) { - return params[param] === REQUIRED && !args[param]; - }); - - if (!requiredParams.length) { - ev.listener(res.message); - return; - } - - var bot = _this3; - function getAnswer() { - var _this4 = this; - - var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step; - - return regeneratorRuntime.wrap(function getAnswer$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - _iteratorNormalCompletion = true; - _didIteratorError = false; - _iteratorError = undefined; - _context2.prev = 3; - _loop = regeneratorRuntime.mark(function _loop() { - var param, msg; - return regeneratorRuntime.wrap(function _loop$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - param = _step.value; - msg = new _Message2.default().to(res.message.chat.id).text('Enter value for ' + param); - _context.next = 4; - return bot.send(msg).then(function (answer) { - args[param] = answer.text; - }); - - case 4: - case 'end': - return _context.stop(); - } - } - }, _loop, _this4); - }); - _iterator = requiredParams[Symbol.iterator](); - - case 6: - if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { - _context2.next = 11; - break; - } - - return _context2.delegateYield(_loop(), 't0', 8); - - case 8: - _iteratorNormalCompletion = true; - _context2.next = 6; - break; - - case 11: - _context2.next = 17; - break; - - case 13: - _context2.prev = 13; - _context2.t1 = _context2['catch'](3); - _didIteratorError = true; - _iteratorError = _context2.t1; - - case 17: - _context2.prev = 17; - _context2.prev = 18; - - if (!_iteratorNormalCompletion && _iterator.return) { - _iterator.return(); - } - - case 20: - _context2.prev = 20; - - if (!_didIteratorError) { - _context2.next = 23; - break; - } - - throw _iteratorError; - - case 23: - return _context2.finish(20); - - case 24: - return _context2.finish(17); - - case 25: - case 'end': - return _context2.stop(); - } - } - }, _marked[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); - })(); - }); - } - }]); - - return Bot; -}(_events.EventEmitter); - -exports.default = Bot; -exports.File = _File2.default; -exports.Message = _Message2.default; -exports.BulkMessage = _BulkMessage2.default; -exports.Forward = _Forward2.default; -exports.Question = _Question2.default; -exports.Keyboard = _Keyboard2.default; diff --git a/build/types/Base.js b/build/types/Base.js deleted file mode 100644 index 8f5e5f1..0000000 --- a/build/types/Base.js +++ /dev/null @@ -1,140 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _events = require('events'); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var ANSWER_THRESHOLD = 10; - -/** - * Base class of all classes - */ - -var Base = function (_EventEmitter) { - _inherits(Base, _EventEmitter); - - function Base(method) { - _classCallCheck(this, Base); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Base).call(this)); - - _this.method = method; - _this.properties = {}; - return _this; - } - - /** - * Sends the message, you should only use this method yourself if - * you are extending this class. Normally you should call bot.send(message) - * - * Events: message:sent => Emitted after sending the message to API, gets the - * API's response - * - * message:answer => Emitted when your message gets an answer from - * the contact (reply in case of groups) - * gets the Update object containing message - * - * @param {object} bot - * @return {promise} returns a promise, resolved with message:answer - */ - - - _createClass(Base, [{ - key: 'send', - value: function send(bot) { - var _this2 = this; - - if (this._keyboard) { - var reply_markup = JSON.stringify(this._keyboard.getProperties()); - this.properties.reply_markup = reply_markup; - } - - var messageId = void 0; - return new Promise(function (resolve) { - bot.api[_this2.method](_this2.properties).then(function (response) { - messageId = response.result.message_id; - _this2.emit('message:sent', response); - }); - - if (_this2._keyboard.one_time_keyboard) { - _this2._keyboard.replyMarkup = ''; - } - - var chat = _this2.properties.chat_id; - var answers = 0; - bot.on('update', function listener(result) { - answers += result.length; - - var update = result.find(function (_ref) { - var message = _ref.message; - - // if in a group, there will be a reply to this message - if (chat < 0) { - return message.chat.id === chat && message.reply_to_message && message.reply_to_message.message_id === messageId; - } else { - return message.chat.id === chat; - } - }); - - if (update) { - resolve(update.message); - - this.emit('message:answer', update.message); - - bot.removeListener('update', listener); - } - - if (answers >= ANSWER_THRESHOLD) { - bot.removeListener('update', listener); - } - }); - }); - } - - /** - * Returns properties of the object - * @return {object} properties of object - */ - - }, { - key: 'getProperties', - value: function getProperties() { - return this.properties; - } - - /** - * Set properties of the object - * @param {object} object properties to set - * @param {boolean} extend A boolean indicating if the properties should be - * extended by the object provided (Object.assign) - * or properties should be replaced by the object - * defaults to true - * @return {object} returns the properties (same as getProperties) - */ - - }, { - key: 'setProperties', - value: function setProperties(object) { - var extend = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; - - this.properties = extend ? Object.assign(this.properties, object) : object; - - return this.getProperties(); - } - }]); - - return Base; -}(_events.EventEmitter); - -exports.default = Base; -module.exports = exports['default']; diff --git a/build/types/BulkMessage.js b/build/types/BulkMessage.js deleted file mode 100644 index f87c786..0000000 --- a/build/types/BulkMessage.js +++ /dev/null @@ -1,91 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Message class, used to send a message to multiple chats - */ - -var BulkMessage = function (_Message) { - _inherits(BulkMessage, _Message); - - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function BulkMessage() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, BulkMessage); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(BulkMessage).call(this, properties)); - - _this.chats = []; - return _this; - } - - /** - * Set multiple chat_id's for the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(BulkMessage, [{ - key: 'to', - value: function to() { - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; - } - - var chats = args.reduce(function (a, b) { - return a.concat(b); - }, []); - - this.chats = chats; - return this; - } - - /** - * Send the message to all chats - * @param {Bot} bot - * @return {Promise} Resolved when the message is sent to all chats - */ - - }, { - key: 'send', - value: function send(bot) { - var _this2 = this; - - var promises = this.chats.map(function (chat) { - var clone = Object.assign({}, _this2.properties); - var message = new _Message3.default(clone).to(chat); - return message.send(bot); - }); - - return Promise.all(promises); - } - }]); - - return BulkMessage; -}(_Message3.default); - -exports.default = BulkMessage; -module.exports = exports['default']; diff --git a/build/types/File.js b/build/types/File.js deleted file mode 100644 index a318aa6..0000000 --- a/build/types/File.js +++ /dev/null @@ -1,159 +0,0 @@ -'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"); } }; }(); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -var _mime = require('mime'); - -var _mime2 = _interopRequireDefault(_mime); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var TYPES = ['photo', 'video', 'document', 'audio']; - -/** - * File class, used to send pictures/movies/audios/documents to chat - */ - -var File = function (_Base) { - _inherits(File, _Base); - - /** - * Create a new file instance - * @param {object} properties File properties, as defined by Telegram API - */ - - function File() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, File); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(File).call(this, 'sendDocument')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(File, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set file of the message - * @param {string} file File path - * @param {string} fileType (optional) if the first argument is a - * file_id string, this option indicates file type - * @return {object} returns the message object - */ - - }, { - key: 'file', - value: function file(_file, fileType) { - if (fileType) { - this.properties[fileType] = { file: _file }; - - return this; - } - - var _mime$lookup$split = _mime2.default.lookup(_file).split('/'); - - var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); - - var type = _mime$lookup$split2[0]; - var extension = _mime$lookup$split2[1]; - - if (type === 'image') { - type = 'photo'; - } - - if (extension === 'gif') { - type = 'document'; - } - - if (TYPES.indexOf(type) === -1) { - type = 'document'; - } - - this.properties[type] = { file: _file }; - - this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); - - return this; - } - - /** - * Set caption for photos - * @param {string} text caption's text - * @return {object} returns the message object - */ - - }, { - key: 'caption', - value: function caption(text) { - this.properties.caption = text; - return this; - } - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - - }, { - key: 'reply', - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - - }, { - key: 'keyboard', - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return File; -}(_Base3.default); - -exports.default = File; -module.exports = exports['default']; diff --git a/build/types/Forward.js b/build/types/Forward.js deleted file mode 100644 index 14a0e26..0000000 --- a/build/types/Forward.js +++ /dev/null @@ -1,94 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Forward class, used to forward messages from a chat to another - */ - -var Forward = function (_Base) { - _inherits(Forward, _Base); - - /** - * Create a new forward message - * @param {object} properties Forward Message properties, as defined by - * Telegram API - */ - - function Forward() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Forward); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Forward).call(this, 'forwardMessage')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(Forward, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set from_chat_id, source of message's chat's id - * @param {number} chat Source chat id - * @return {object} returns the message object - */ - - }, { - key: 'from', - value: function from(chat) { - this.properties.from_chat_id = chat; - return this; - } - - /** - * Sets message_id, the message to forward from source to target chat - * @param {number} message ID of the message to forward - * @return {object} returns the message object - */ - - }, { - key: 'message', - value: function message(_message) { - this.properties.message_id = _message; - return this; - } - - // This class inherits Base's send method - - }]); - - return Forward; -}(_Base3.default); - -exports.default = Forward; -module.exports = exports['default']; diff --git a/build/types/Keyboard.js b/build/types/Keyboard.js deleted file mode 100644 index 460b17f..0000000 --- a/build/types/Keyboard.js +++ /dev/null @@ -1,145 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Keyboard class, used to configure keyboards for messages. - * You should pass your instance of this class to message.keyboard() method - */ - -var Keyboard = function (_Base) { - _inherits(Keyboard, _Base); - - /** - * Create a new keyboard - * @param {object} properties Keyboard properties, as defined by Telegram API - * See ReplyKeyboardMarkup, ReplyKeyboardHide, - * ForceReply - */ - - function Keyboard() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Keyboard); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Keyboard).call(this)); - - _this.properties = properties; - return _this; - } - - /** - * Set the keyboard property of reply_markup - * @param {array} keys An array of arrays, with the format of - * Column Column - * Row [['TopLeft', 'TopRight'], - * Row ['BottomLeft', 'BottomRight']] - * @return {object} returns the keyboard object - */ - - - _createClass(Keyboard, [{ - key: 'keys', - value: function keys(_keys) { - this.properties.keyboard = _keys; - this.properties.hide_keyboard = false; - return this; - } - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'force', - value: function force() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.force_keyboard = enable; - return this; - } - - /** - * Set resize_keyboard property of reply_markup - * @param {boolean} enable value of resize_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'resize', - value: function resize() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.resize_keyboard = enable; - return this; - } - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'oneTime', - value: function oneTime() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.one_time_keyboard = enable; - return this; - } - - /** - * Set selective property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'selective', - value: function selective() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.selective = enable; - return this; - } - - /** - * Set hide_keyboard property of reply_markup to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'hide', - value: function hide() { - this.properties = { - hide_keyboard: true - }; - - return this; - } - }]); - - return Keyboard; -}(_Base3.default); - -exports.default = Keyboard; -module.exports = exports['default']; diff --git a/build/types/Message.js b/build/types/Message.js deleted file mode 100644 index 9084494..0000000 --- a/build/types/Message.js +++ /dev/null @@ -1,155 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Message class, used to send message to a chat - */ - -var Message = function (_Base) { - _inherits(Message, _Base); - - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function Message() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Message); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Message).call(this, 'sendMessage')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(Message, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set text of the message - * @param {string} text Message's content - * @return {object} returns the message object - */ - - }, { - key: 'text', - value: function text(_text) { - this.properties.text = _text; - return this; - } - - /** - * Set text of the message in HTML format - * @param {string} text Message's content in HTML format - * @return {object} returns the message object - */ - - }, { - key: 'html', - value: function html(text) { - this.properties.parse_mode = 'HTML'; - if (text) { - this.properties.text = text; - } - return this; - } - - /** - * Set text of the message in Markdown format - * @param {string} text Message's content in Markdown format - * @return {object} returns the message object - */ - - }, { - key: 'markdown', - value: function markdown(text) { - this.properties.parse_mode = 'Markdown'; - if (text) { - this.properties.text = text; - } - return this; - } - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - - }, { - key: 'reply', - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - - /** - * Set disable_web_page_preview of the message - * @param {boolean} enable - * @return {object} returns the message object - */ - - }, { - key: 'preview', - value: function preview() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.disable_web_page_preview = !enable; - return this; - } - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - - }, { - key: 'keyboard', - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return Message; -}(_Base3.default); - -exports.default = Message; -module.exports = exports['default']; diff --git a/build/types/Question.js b/build/types/Question.js deleted file mode 100644 index 7aa5819..0000000 --- a/build/types/Question.js +++ /dev/null @@ -1,116 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -var _Keyboard = require('./Keyboard'); - -var _Keyboard2 = _interopRequireDefault(_Keyboard); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Question class, extends Message - * Sends a message, shows a keyboard with the answers provided, and validates - * the answer - */ - -var Question = function (_Message) { - _inherits(Question, _Message); - - /** - * Create a new question - * @param {object} options Options, same as Message, plus `answers` which - * is a keyboard layout, see Keyboard#keys - */ - - function Question() { - var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Question); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Question).call(this, options)); - - var kb = new _Keyboard2.default().force().oneTime().selective(); - _this.keyboard(kb); - - _this.answers(options.answers); - return _this; - } - - /** - * Sets answers of the question. This is passed to Keyboard#keys, and then - * used to validate the answer given - * @param {array} answers Array of arrays of strings, same as Keyboard#keys - * @return {object} returns the question object - */ - - - _createClass(Question, [{ - key: 'answers', - value: function answers(_answers) { - this._answers = _answers; - this._keyboard.keys(_answers); - return this; - } - - /** - * Sends the question (same as Message#send), and validates the answer given - * if the answer is one of the defined answers, resolves, else rejects - * You should not manually use this method unless you're extending this class - * You should instead use bot.send(question); - * @param {object} bot - * @return {promise} A promise which is resolved in case of valid answer, and - * rejected in case of invalid answer - */ - - }, { - key: 'send', - value: function send(bot) { - var _this2 = this; - - var answers = this._answers; - - return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) { - var answer = void 0; - - answers.forEach(function find(a) { - if (Array.isArray(a)) { - a.forEach(find); - } - if (a === message.text) { - answer = a; - } - }); - - if (answer) { - _this2.emit('question:answer', answer, message); - return message; - } else { - _this2.emit('question:invalid', message); - throw message; - } - }); - } - }]); - - return Question; -}(_Message3.default); - -exports.default = Question; -module.exports = exports['default']; diff --git a/package.json b/package.json index 4f72051..ca2d8ee 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "build" ], "directories": { - "lib": "lib" + "lib": "src" }, "engines": { "node": ">=0.12.0" diff --git a/types/Base.js b/types/Base.js deleted file mode 100644 index 8f5e5f1..0000000 --- a/types/Base.js +++ /dev/null @@ -1,140 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _events = require('events'); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var ANSWER_THRESHOLD = 10; - -/** - * Base class of all classes - */ - -var Base = function (_EventEmitter) { - _inherits(Base, _EventEmitter); - - function Base(method) { - _classCallCheck(this, Base); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Base).call(this)); - - _this.method = method; - _this.properties = {}; - return _this; - } - - /** - * Sends the message, you should only use this method yourself if - * you are extending this class. Normally you should call bot.send(message) - * - * Events: message:sent => Emitted after sending the message to API, gets the - * API's response - * - * message:answer => Emitted when your message gets an answer from - * the contact (reply in case of groups) - * gets the Update object containing message - * - * @param {object} bot - * @return {promise} returns a promise, resolved with message:answer - */ - - - _createClass(Base, [{ - key: 'send', - value: function send(bot) { - var _this2 = this; - - if (this._keyboard) { - var reply_markup = JSON.stringify(this._keyboard.getProperties()); - this.properties.reply_markup = reply_markup; - } - - var messageId = void 0; - return new Promise(function (resolve) { - bot.api[_this2.method](_this2.properties).then(function (response) { - messageId = response.result.message_id; - _this2.emit('message:sent', response); - }); - - if (_this2._keyboard.one_time_keyboard) { - _this2._keyboard.replyMarkup = ''; - } - - var chat = _this2.properties.chat_id; - var answers = 0; - bot.on('update', function listener(result) { - answers += result.length; - - var update = result.find(function (_ref) { - var message = _ref.message; - - // if in a group, there will be a reply to this message - if (chat < 0) { - return message.chat.id === chat && message.reply_to_message && message.reply_to_message.message_id === messageId; - } else { - return message.chat.id === chat; - } - }); - - if (update) { - resolve(update.message); - - this.emit('message:answer', update.message); - - bot.removeListener('update', listener); - } - - if (answers >= ANSWER_THRESHOLD) { - bot.removeListener('update', listener); - } - }); - }); - } - - /** - * Returns properties of the object - * @return {object} properties of object - */ - - }, { - key: 'getProperties', - value: function getProperties() { - return this.properties; - } - - /** - * Set properties of the object - * @param {object} object properties to set - * @param {boolean} extend A boolean indicating if the properties should be - * extended by the object provided (Object.assign) - * or properties should be replaced by the object - * defaults to true - * @return {object} returns the properties (same as getProperties) - */ - - }, { - key: 'setProperties', - value: function setProperties(object) { - var extend = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; - - this.properties = extend ? Object.assign(this.properties, object) : object; - - return this.getProperties(); - } - }]); - - return Base; -}(_events.EventEmitter); - -exports.default = Base; -module.exports = exports['default']; diff --git a/types/BulkMessage.js b/types/BulkMessage.js deleted file mode 100644 index f87c786..0000000 --- a/types/BulkMessage.js +++ /dev/null @@ -1,91 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Message class, used to send a message to multiple chats - */ - -var BulkMessage = function (_Message) { - _inherits(BulkMessage, _Message); - - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function BulkMessage() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, BulkMessage); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(BulkMessage).call(this, properties)); - - _this.chats = []; - return _this; - } - - /** - * Set multiple chat_id's for the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(BulkMessage, [{ - key: 'to', - value: function to() { - for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { - args[_key] = arguments[_key]; - } - - var chats = args.reduce(function (a, b) { - return a.concat(b); - }, []); - - this.chats = chats; - return this; - } - - /** - * Send the message to all chats - * @param {Bot} bot - * @return {Promise} Resolved when the message is sent to all chats - */ - - }, { - key: 'send', - value: function send(bot) { - var _this2 = this; - - var promises = this.chats.map(function (chat) { - var clone = Object.assign({}, _this2.properties); - var message = new _Message3.default(clone).to(chat); - return message.send(bot); - }); - - return Promise.all(promises); - } - }]); - - return BulkMessage; -}(_Message3.default); - -exports.default = BulkMessage; -module.exports = exports['default']; diff --git a/types/File.js b/types/File.js deleted file mode 100644 index a318aa6..0000000 --- a/types/File.js +++ /dev/null @@ -1,159 +0,0 @@ -'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"); } }; }(); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -var _mime = require('mime'); - -var _mime2 = _interopRequireDefault(_mime); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var TYPES = ['photo', 'video', 'document', 'audio']; - -/** - * File class, used to send pictures/movies/audios/documents to chat - */ - -var File = function (_Base) { - _inherits(File, _Base); - - /** - * Create a new file instance - * @param {object} properties File properties, as defined by Telegram API - */ - - function File() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, File); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(File).call(this, 'sendDocument')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(File, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set file of the message - * @param {string} file File path - * @param {string} fileType (optional) if the first argument is a - * file_id string, this option indicates file type - * @return {object} returns the message object - */ - - }, { - key: 'file', - value: function file(_file, fileType) { - if (fileType) { - this.properties[fileType] = { file: _file }; - - return this; - } - - var _mime$lookup$split = _mime2.default.lookup(_file).split('/'); - - var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); - - var type = _mime$lookup$split2[0]; - var extension = _mime$lookup$split2[1]; - - if (type === 'image') { - type = 'photo'; - } - - if (extension === 'gif') { - type = 'document'; - } - - if (TYPES.indexOf(type) === -1) { - type = 'document'; - } - - this.properties[type] = { file: _file }; - - this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); - - return this; - } - - /** - * Set caption for photos - * @param {string} text caption's text - * @return {object} returns the message object - */ - - }, { - key: 'caption', - value: function caption(text) { - this.properties.caption = text; - return this; - } - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - - }, { - key: 'reply', - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - - }, { - key: 'keyboard', - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return File; -}(_Base3.default); - -exports.default = File; -module.exports = exports['default']; diff --git a/types/Forward.js b/types/Forward.js deleted file mode 100644 index 14a0e26..0000000 --- a/types/Forward.js +++ /dev/null @@ -1,94 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Forward class, used to forward messages from a chat to another - */ - -var Forward = function (_Base) { - _inherits(Forward, _Base); - - /** - * Create a new forward message - * @param {object} properties Forward Message properties, as defined by - * Telegram API - */ - - function Forward() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Forward); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Forward).call(this, 'forwardMessage')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(Forward, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set from_chat_id, source of message's chat's id - * @param {number} chat Source chat id - * @return {object} returns the message object - */ - - }, { - key: 'from', - value: function from(chat) { - this.properties.from_chat_id = chat; - return this; - } - - /** - * Sets message_id, the message to forward from source to target chat - * @param {number} message ID of the message to forward - * @return {object} returns the message object - */ - - }, { - key: 'message', - value: function message(_message) { - this.properties.message_id = _message; - return this; - } - - // This class inherits Base's send method - - }]); - - return Forward; -}(_Base3.default); - -exports.default = Forward; -module.exports = exports['default']; diff --git a/types/Keyboard.js b/types/Keyboard.js deleted file mode 100644 index 460b17f..0000000 --- a/types/Keyboard.js +++ /dev/null @@ -1,145 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Keyboard class, used to configure keyboards for messages. - * You should pass your instance of this class to message.keyboard() method - */ - -var Keyboard = function (_Base) { - _inherits(Keyboard, _Base); - - /** - * Create a new keyboard - * @param {object} properties Keyboard properties, as defined by Telegram API - * See ReplyKeyboardMarkup, ReplyKeyboardHide, - * ForceReply - */ - - function Keyboard() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Keyboard); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Keyboard).call(this)); - - _this.properties = properties; - return _this; - } - - /** - * Set the keyboard property of reply_markup - * @param {array} keys An array of arrays, with the format of - * Column Column - * Row [['TopLeft', 'TopRight'], - * Row ['BottomLeft', 'BottomRight']] - * @return {object} returns the keyboard object - */ - - - _createClass(Keyboard, [{ - key: 'keys', - value: function keys(_keys) { - this.properties.keyboard = _keys; - this.properties.hide_keyboard = false; - return this; - } - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'force', - value: function force() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.force_keyboard = enable; - return this; - } - - /** - * Set resize_keyboard property of reply_markup - * @param {boolean} enable value of resize_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'resize', - value: function resize() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.resize_keyboard = enable; - return this; - } - - /** - * Set force_keyboard property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'oneTime', - value: function oneTime() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.one_time_keyboard = enable; - return this; - } - - /** - * Set selective property of reply_markup - * @param {boolean} enable value of force_keyboard, defaults to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'selective', - value: function selective() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.selective = enable; - return this; - } - - /** - * Set hide_keyboard property of reply_markup to true - * @return {object} returns the keyboard object - */ - - }, { - key: 'hide', - value: function hide() { - this.properties = { - hide_keyboard: true - }; - - return this; - } - }]); - - return Keyboard; -}(_Base3.default); - -exports.default = Keyboard; -module.exports = exports['default']; diff --git a/types/Message.js b/types/Message.js deleted file mode 100644 index 9084494..0000000 --- a/types/Message.js +++ /dev/null @@ -1,155 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _Base2 = require('./Base'); - -var _Base3 = _interopRequireDefault(_Base2); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Message class, used to send message to a chat - */ - -var Message = function (_Base) { - _inherits(Message, _Base); - - /** - * Create a new message - * @param {object} properties Message properties, as defined by Telegram API - */ - - function Message() { - var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Message); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Message).call(this, 'sendMessage')); - - _this.properties = properties; - _this._keyboard = new _Base3.default(); - return _this; - } - - /** - * Set chat_id of the message - * @param {number} chat - * @return {object} returns the message object - */ - - - _createClass(Message, [{ - key: 'to', - value: function to(chat) { - this.properties.chat_id = chat; - return this; - } - - /** - * Set text of the message - * @param {string} text Message's content - * @return {object} returns the message object - */ - - }, { - key: 'text', - value: function text(_text) { - this.properties.text = _text; - return this; - } - - /** - * Set text of the message in HTML format - * @param {string} text Message's content in HTML format - * @return {object} returns the message object - */ - - }, { - key: 'html', - value: function html(text) { - this.properties.parse_mode = 'HTML'; - if (text) { - this.properties.text = text; - } - return this; - } - - /** - * Set text of the message in Markdown format - * @param {string} text Message's content in Markdown format - * @return {object} returns the message object - */ - - }, { - key: 'markdown', - value: function markdown(text) { - this.properties.parse_mode = 'Markdown'; - if (text) { - this.properties.text = text; - } - return this; - } - - /** - * Set reply_to_message_id of the message - * @param {number} id message_id of the message to reply to - * @return {object} returns the message object - */ - - }, { - key: 'reply', - value: function reply(id) { - this.properties.reply_to_message_id = id; - return this; - } - - /** - * Set disable_web_page_preview of the message - * @param {boolean} enable - * @return {object} returns the message object - */ - - }, { - key: 'preview', - value: function preview() { - var enable = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; - - this.properties.disable_web_page_preview = !enable; - return this; - } - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - - }, { - key: 'keyboard', - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } - - // This class inherits Base's send method - - }]); - - return Message; -}(_Base3.default); - -exports.default = Message; -module.exports = exports['default']; diff --git a/types/Question.js b/types/Question.js deleted file mode 100644 index 7aa5819..0000000 --- a/types/Question.js +++ /dev/null @@ -1,116 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; - -var _Message2 = require('./Message'); - -var _Message3 = _interopRequireDefault(_Message2); - -var _Keyboard = require('./Keyboard'); - -var _Keyboard2 = _interopRequireDefault(_Keyboard); - -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"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -/** - * Question class, extends Message - * Sends a message, shows a keyboard with the answers provided, and validates - * the answer - */ - -var Question = function (_Message) { - _inherits(Question, _Message); - - /** - * Create a new question - * @param {object} options Options, same as Message, plus `answers` which - * is a keyboard layout, see Keyboard#keys - */ - - function Question() { - var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; - - _classCallCheck(this, Question); - - var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Question).call(this, options)); - - var kb = new _Keyboard2.default().force().oneTime().selective(); - _this.keyboard(kb); - - _this.answers(options.answers); - return _this; - } - - /** - * Sets answers of the question. This is passed to Keyboard#keys, and then - * used to validate the answer given - * @param {array} answers Array of arrays of strings, same as Keyboard#keys - * @return {object} returns the question object - */ - - - _createClass(Question, [{ - key: 'answers', - value: function answers(_answers) { - this._answers = _answers; - this._keyboard.keys(_answers); - return this; - } - - /** - * Sends the question (same as Message#send), and validates the answer given - * if the answer is one of the defined answers, resolves, else rejects - * You should not manually use this method unless you're extending this class - * You should instead use bot.send(question); - * @param {object} bot - * @return {promise} A promise which is resolved in case of valid answer, and - * rejected in case of invalid answer - */ - - }, { - key: 'send', - value: function send(bot) { - var _this2 = this; - - var answers = this._answers; - - return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) { - var answer = void 0; - - answers.forEach(function find(a) { - if (Array.isArray(a)) { - a.forEach(find); - } - if (a === message.text) { - answer = a; - } - }); - - if (answer) { - _this2.emit('question:answer', answer, message); - return message; - } else { - _this2.emit('question:invalid', message); - throw message; - } - }); - } - }]); - - return Question; -}(_Message3.default); - -exports.default = Question; -module.exports = exports['default']; -- 2.34.1