From 7b8ca407e330535e797f2982710b340e9adf2b9e Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Fri, 3 Jul 2015 21:38:24 +0430 Subject: [PATCH] Instead of symlinking types, copy them to be compatible with windows and npm --- Gruntfile.js | 6 +- classes/Base.js | 1 - classes/Keyboard.js | 1 - classes/Message.js | 1 - classes/Question.js | 1 - package.json | 7 +- types/Base.js | 128 +++++++++++++++++++++++++++++++- types/BulkMessage.js | 89 ++++++++++++++++++++++- types/File.js | 169 ++++++++++++++++++++++++++++++++++++++++++- types/Forward.js | 91 ++++++++++++++++++++++- types/Keyboard.js | 139 ++++++++++++++++++++++++++++++++++- types/Message.js | 118 +++++++++++++++++++++++++++++- types/Question.js | 112 +++++++++++++++++++++++++++- 13 files changed, 845 insertions(+), 18 deletions(-) delete mode 120000 classes/Base.js delete mode 120000 classes/Keyboard.js delete mode 120000 classes/Message.js delete mode 120000 classes/Question.js mode change 120000 => 100644 types/Base.js mode change 120000 => 100644 types/BulkMessage.js mode change 120000 => 100644 types/File.js mode change 120000 => 100644 types/Forward.js mode change 120000 => 100644 types/Keyboard.js mode change 120000 => 100644 types/Message.js mode change 120000 => 100644 types/Question.js diff --git a/Gruntfile.js b/Gruntfile.js index 5bc9e42..d34c5ce 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -13,7 +13,7 @@ module.exports = function(grunt) { eslint: { scripts: ['lib/**/*.js'] }, - symlink: { + copy: { classes: { files: [{ expand: true, @@ -33,8 +33,8 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-babel'); grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-contrib-symlink'); + grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-eslint'); - grunt.registerTask('default', ['babel', 'symlink', 'eslint']); + grunt.registerTask('default', ['babel', 'copy', 'eslint']); }; diff --git a/classes/Base.js b/classes/Base.js deleted file mode 120000 index 0331749..0000000 --- a/classes/Base.js +++ /dev/null @@ -1 +0,0 @@ -../build/classes/Base.js \ No newline at end of file diff --git a/classes/Keyboard.js b/classes/Keyboard.js deleted file mode 120000 index 33e9abe..0000000 --- a/classes/Keyboard.js +++ /dev/null @@ -1 +0,0 @@ -../build/classes/Keyboard.js \ No newline at end of file diff --git a/classes/Message.js b/classes/Message.js deleted file mode 120000 index dd58465..0000000 --- a/classes/Message.js +++ /dev/null @@ -1 +0,0 @@ -../build/classes/Message.js \ No newline at end of file diff --git a/classes/Question.js b/classes/Question.js deleted file mode 120000 index f9185e0..0000000 --- a/classes/Question.js +++ /dev/null @@ -1 +0,0 @@ -../build/classes/Question.js \ No newline at end of file diff --git a/package.json b/package.json index 5a0ee0a..3c484d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "0.4.0", + "version": "0.4.1", "description": "Control Telegram bots easily using the new Telegram API", "main": "index.js", "scripts": { @@ -22,8 +22,7 @@ "url": "https://github.com/mdibaiee/node-telegram-api/issues" }, "directories": { - "lib": "lib", - "doc": "docs" + "lib": "lib" }, "engines": { "node": ">=0.12.0" @@ -33,7 +32,7 @@ "babel": "^5.6.14", "grunt": "^0.4.5", "grunt-babel": "^5.0.1", - "grunt-contrib-symlink": "^0.3.0", + "grunt-contrib-copy": "^0.8.0", "grunt-contrib-watch": "^0.6.1", "grunt-eslint": "^16.0.0", "mime": "^1.3.4", diff --git a/types/Base.js b/types/Base.js deleted file mode 120000 index bb923bf..0000000 --- a/types/Base.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/Base.js \ No newline at end of file diff --git a/types/Base.js b/types/Base.js new file mode 100644 index 0000000..fc2cdfb --- /dev/null +++ b/types/Base.js @@ -0,0 +1,127 @@ +'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'); + +/** + * 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; + bot.on('update', function listener(result) { + 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); + } + }); + }); + } + }, { + 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 120000 index a8730df..0000000 --- a/types/BulkMessage.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/BulkMessage.js \ No newline at end of file diff --git a/types/BulkMessage.js b/types/BulkMessage.js new file mode 100644 index 0000000..2359ec5 --- /dev/null +++ b/types/BulkMessage.js @@ -0,0 +1,88 @@ +'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 120000 index a835995..0000000 --- a/types/File.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/File.js \ No newline at end of file diff --git a/types/File.js b/types/File.js new file mode 100644 index 0000000..04e78e6 --- /dev/null +++ b/types/File.js @@ -0,0 +1,168 @@ +'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 _fs = require('fs'); + +var _fs2 = _interopRequireDefault(_fs); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _restler = require('restler'); + +var _restler2 = _interopRequireDefault(_restler); + +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; + + return this; + } + + var stat = _fs2['default'].statSync(_file); + var name = _path2['default'].basename(_file); + + 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] = _restler2['default'].file(_file, name, stat.size, 'utf-8'); + + 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 120000 index 3202592..0000000 --- a/types/Forward.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/Forward.js \ No newline at end of file diff --git a/types/Forward.js b/types/Forward.js new file mode 100644 index 0000000..44871c3 --- /dev/null +++ b/types/Forward.js @@ -0,0 +1,90 @@ +'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 120000 index 18d709c..0000000 --- a/types/Keyboard.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/Keyboard.js \ No newline at end of file diff --git a/types/Keyboard.js b/types/Keyboard.js new file mode 100644 index 0000000..c1fc8d0 --- /dev/null +++ b/types/Keyboard.js @@ -0,0 +1,138 @@ +'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 120000 index d7fc2c6..0000000 --- a/types/Message.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/Message.js \ No newline at end of file diff --git a/types/Message.js b/types/Message.js new file mode 100644 index 0000000..1f18b09 --- /dev/null +++ b/types/Message.js @@ -0,0 +1,117 @@ +'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 120000 index 1d74df5..0000000 --- a/types/Question.js +++ /dev/null @@ -1 +0,0 @@ -../build/types/Question.js \ No newline at end of file diff --git a/types/Question.js b/types/Question.js new file mode 100644 index 0000000..824ad40 --- /dev/null +++ b/types/Question.js @@ -0,0 +1,111 @@ +'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'];