feat(API): add previous API

copy types folder into module root path
This commit is contained in:
Ali Movahedi 2016-03-28 10:38:35 +04:30
parent 23cff62505
commit 03bfae0028
11 changed files with 922 additions and 3 deletions

View File

@ -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']);
};

View File

@ -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');

View File

@ -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');

View File

@ -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"
}

140
types/Base.js Normal file
View File

@ -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'];

91
types/BulkMessage.js Normal file
View File

@ -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'];

159
types/File.js Normal file
View File

@ -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'];

94
types/Forward.js Normal file
View File

@ -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'];

145
types/Keyboard.js Normal file
View File

@ -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'];

155
types/Message.js Normal file
View File

@ -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'];

116
types/Question.js Normal file
View File

@ -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'];