From 3f03ff9667e4ed412fcac03b76960663fb9f8a19 Mon Sep 17 00:00:00 2001 From: mamal72 Date: Sun, 28 Jun 2015 23:02:18 +0430 Subject: [PATCH] refactored Keyboard and Message class add Base class add set and get properties utils --- lib/classes/Base.js | 16 ++++++++ lib/classes/Keyboard.js | 79 +++++++++++++++++++++++++------------- lib/classes/Message.js | 75 +++++++++++++++++++++++++++++------- lib/classes/Message_old.js | 77 +++++++++++++++++++++++++++++++++++++ lib/utils.js | 9 +++++ 5 files changed, 217 insertions(+), 39 deletions(-) create mode 100644 lib/classes/Base.js create mode 100644 lib/classes/Message_old.js diff --git a/lib/classes/Base.js b/lib/classes/Base.js new file mode 100644 index 0000000..ee2ba9c --- /dev/null +++ b/lib/classes/Base.js @@ -0,0 +1,16 @@ +import {EventEmitter} from 'events'; +import * as utils from '../utils'; + +export default class Base extends EventEmitter { + constructor() { + super(); + } + + setProperties(object) { + return utils.setProperties(this, object); + } + + getProperties(object) { + return utils.getProperties(this, object); + } +} \ No newline at end of file diff --git a/lib/classes/Keyboard.js b/lib/classes/Keyboard.js index 0aca2d7..b4619f0 100644 --- a/lib/classes/Keyboard.js +++ b/lib/classes/Keyboard.js @@ -1,45 +1,72 @@ +import {setProperties, getProperties} from '../utils'; + +/** + * Keyboard layout class + * @class + */ export default class Keyboard { + + /** + * @constructor + * @param {Object} options + */ constructor(options = {keyboard: [], resize_keyboard: false, one_time_keyboard: false, selective: false}) { this.properties = options; } - set keyboard(value) { - this.setProperties({keyboard: value}); + /** + * Get or set keyboard layout + * @param {Array} value + * @returns {Keyboard|Array} + */ + keyboard(value) { + if (value) { + setProperties({keyboard: value}); + return this; + } + return getProperties('keyboard'); } - set resizeKeyboard(value) { - this.setProperties({resize_keyboard: value}); + /** + * Get or set resize keyboard + * @param {boolean} value + * @returns {Keyboard|Object} + */ + resize(value) { + if (value) { + setProperties({resize_keyboard: value}); + return this; + } + return getProperties('resize_keyboard'); } - set oneTimeKeyboard(value) { - this.setProperties({one_time_keyboard: value}); + /** + * Get or set one time keyboard + * @param {boolean} value + * @return {Keyboard|Object} + */ + oneTime(value) { + if (value) { + setProperties({one_time_keyboard: value}); + return this; + } + return getProperties('one_time_keyboard'); } - set selective(value) { - this.setProperties({selective: value}); + selective(value) { + if (value) { + setProperties({selective: value}); + return this; + } + return getProperties('selective'); } - get keyboard() { - return this.getProperties('keyboard'); - } - - get resizeKeyboard() { - this.getProperties('resize_keyboard'); - } - - get oneTimeKeyboard() { - this.getProperties('one_time_keyboard'); - } - - get selective() { - this.getProperties('selective'); - } - - setProperties(object) { +/* setProperties(object) { this.properties = Object.assign(this.properties, object); + return this; } getProperties(object) { return object ? this.properties[object] : this.properties; - } + }*/ } diff --git a/lib/classes/Message.js b/lib/classes/Message.js index 6b75e16..d8eb047 100644 --- a/lib/classes/Message.js +++ b/lib/classes/Message.js @@ -1,29 +1,78 @@ -import {EventEmitter} from 'events'; +import Base from './Base' import Keyboard from './Keyboard'; -export default class Message extends EventEmitter { +/** + * Message class + * @class + */ +export default class Message extends Base { + + /** + * @constructor + * @param {Object} [options = {}] + */ constructor(options = {}) { super(); - - this.params = options; + this.setProperties(options); } + /** + * Get or set chat_id of message recipient + * @param {number} chat + * @return {Message|number} + */ to(chat) { - this.params.chat_id = chat; - return this; + if (chat) { + this.setProperties({chat_id: chat}); + return this; + } + return this.getProperties('chat_id'); } + /** + * Get or set text of message + * @param {string} text + * @return {Message|string} + */ text(text) { - this.params.text = text; - return this; + if (text) { + this.setProperties({text: text}); + return this; + } + return this.getProperties('text'); } + /** + * Get or set reply_to_message_id + * @param {number} chat + * @return {Message|number} + */ reply(chat) { - this.params.reply_to_message_id = chat; - return this; + if (chat) { + this.setProperties({reply_to_message_id: chat}); + return this; + } + return this.getProperties('reply_to_message_id'); } - keyboard(options) { + /** + * Get or set reply_markup of message + * @param {Array} markup + * @return {Message|Array} + */ + keyboard(markup) { + if (markup) { + this.setProperties({reply_markup: JSON.stringify(markup)}); + return this; + } + return JSON.parse(this.getProperties('reply_markup')); + } + + send() { + + } + + /*keyboard(options) { let params; if (this._keyboard && !options) { @@ -59,7 +108,7 @@ export default class Message extends EventEmitter { // 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_id === messageId; + message.reply_to_message.message_id === messageId; } else { return message.chat.id === chat; } @@ -73,5 +122,5 @@ export default class Message extends EventEmitter { } }); }); - } + }*/ } diff --git a/lib/classes/Message_old.js b/lib/classes/Message_old.js new file mode 100644 index 0000000..6b75e16 --- /dev/null +++ b/lib/classes/Message_old.js @@ -0,0 +1,77 @@ +import {EventEmitter} from 'events'; +import Keyboard from './Keyboard'; + +export default class Message extends EventEmitter { + constructor(options = {}) { + super(); + + this.params = options; + } + + to(chat) { + this.params.chat_id = chat; + return this; + } + + text(text) { + this.params.text = text; + return this; + } + + reply(chat) { + this.params.reply_to_message_id = chat; + return this; + } + + keyboard(options) { + let params; + + if (this._keyboard && !options) { + return this._keyboard; + } + + if (this._keyboard) { + params = Object.assign(this._keyboard.replyMarkup, options); + } else { + params = options; + } + + this._keyboard = new Keyboard(this, params); + return this._keyboard; + } + + send(bot) { + let messageId; + + return new Promise(resolve => { + bot.api.sendMessage(this.params).then(response => { + messageId = response.result.message_id; + this.emit('message:sent', response); + }); + + if (this.keyboard().replyMarkup.one_time_keyboard) { + this.keyboard().replyMarkup = ''; + } + + const chat = this.params.chat_id; + bot.on('update', function listener(result) { + const update = result.find(({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_id === messageId; + } else { + return message.chat.id === chat; + } + }); + + if (update) { + resolve(update); + this.emit('message:answer', update); + + bot.removeListener('update', listener); + } + }); + }); + } +} diff --git a/lib/utils.js b/lib/utils.js index e69de29..4efd2db 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -0,0 +1,9 @@ +export function setProperties(sourceObject, object) { + sourceObject.properties = Object.assign(sourceObject.properties, object); + return sourceObject; +} + + +export function getProperties(sourceObject, object) { + return object ? sourceObject.properties[object] : sourceObject.properties; +} \ No newline at end of file