From da12078d7346562abeb91d2d815168339fa82446 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Fri, 3 Jul 2015 17:36:07 +0430 Subject: [PATCH] Add BulkMessage Type --- README.md | 2 +- build/types/BulkMessage.js | 88 ++++++++++++++++++++++++++++++++++++++ lib/types/BulkMessage.js | 45 +++++++++++++++++++ package.json | 2 +- types/BulkMessage.js | 1 + 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 build/types/BulkMessage.js create mode 100644 lib/types/BulkMessage.js create mode 120000 types/BulkMessage.js diff --git a/README.md b/README.md index 73d1a16..47f9d5e 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ This will result in: - [x] Webhook support - [x] Forward Type -- [ ] BulkMessage Type +- [x] BulkMessage Type - [x] File Type - [ ] Sticker Type - [ ] Location Type diff --git a/build/types/BulkMessage.js b/build/types/BulkMessage.js new file mode 100644 index 0000000..2359ec5 --- /dev/null +++ b/build/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/lib/types/BulkMessage.js b/lib/types/BulkMessage.js new file mode 100644 index 0000000..1354e9d --- /dev/null +++ b/lib/types/BulkMessage.js @@ -0,0 +1,45 @@ +import Message from './Message'; + +/** + * Message class, used to send a message to multiple chats + */ +export default class BulkMessage extends Message { + /** + * Create a new message + * @param {object} properties Message properties, as defined by Telegram API + */ + constructor(properties = {}) { + super(properties); + + this.chats = []; + } + + /** + * Set multiple chat_id's for the message + * @param {number} chat + * @return {object} returns the message object + */ + to(...args) { + const chats = args.reduce((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 + */ + send(bot) { + const promises = this.chats.map(chat => { + const clone = Object.assign({}, this.properties); + const message = new Message(clone).to(chat); + return message.send(bot); + }); + + return Promise.all(promises); + } +} diff --git a/package.json b/package.json index 6b305c8..1de90c3 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "grunt-contrib-watch": "^0.6.1", "grunt-eslint": "^16.0.0", "mime": "^1.3.4", - "qs": "^3.1.0", + "qs": "^4.0.0", "restler": "^3.3.0" } } diff --git a/types/BulkMessage.js b/types/BulkMessage.js new file mode 120000 index 0000000..a8730df --- /dev/null +++ b/types/BulkMessage.js @@ -0,0 +1 @@ +../build/types/BulkMessage.js \ No newline at end of file