Refactor classes and utils #3

Closed
mamal72 wants to merge 3 commits from new into new
5 changed files with 217 additions and 39 deletions
Showing only changes of commit 3f03ff9667 - Show all commits

16
lib/classes/Base.js Normal file
View File

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

View File

@ -1,45 +1,72 @@
import {setProperties, getProperties} from '../utils';
/**
* Keyboard layout class
* @class
*/
export default class Keyboard { export default class Keyboard {
/**
* @constructor
* @param {Object} options
*/
constructor(options = {keyboard: [], resize_keyboard: false, one_time_keyboard: false, selective: false}) { constructor(options = {keyboard: [], resize_keyboard: false, one_time_keyboard: false, selective: false}) {
this.properties = options; 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) { selective(value) {
this.setProperties({selective: value}); if (value) {
setProperties({selective: value});
return this;
}
return getProperties('selective');
} }
get keyboard() { /* setProperties(object) {
return this.getProperties('keyboard');
}
get resizeKeyboard() {
this.getProperties('resize_keyboard');
}
get oneTimeKeyboard() {
this.getProperties('one_time_keyboard');
}
get selective() {
this.getProperties('selective');
}
setProperties(object) {
this.properties = Object.assign(this.properties, object); this.properties = Object.assign(this.properties, object);
return this;
} }
getProperties(object) { getProperties(object) {
return object ? this.properties[object] : this.properties; return object ? this.properties[object] : this.properties;
} }*/
} }

View File

@ -1,29 +1,78 @@
import {EventEmitter} from 'events'; import Base from './Base'
import Keyboard from './Keyboard'; 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 = {}) { constructor(options = {}) {
super(); super();
this.setProperties(options);
this.params = options;
} }
/**
* Get or set chat_id of message recipient
* @param {number} chat
* @return {Message|number}
*/
to(chat) { to(chat) {
this.params.chat_id = chat; if (chat) {
this.setProperties({chat_id: chat});
return this; return this;
} }
return this.getProperties('chat_id');
}
/**
* Get or set text of message
* @param {string} text
* @return {Message|string}
*/
text(text) { text(text) {
this.params.text = text; if (text) {
this.setProperties({text: text});
return this; return this;
} }
return this.getProperties('text');
}
/**
* Get or set reply_to_message_id
* @param {number} chat
* @return {Message|number}
*/
reply(chat) { reply(chat) {
this.params.reply_to_message_id = chat; if (chat) {
this.setProperties({reply_to_message_id: chat});
return this; 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; let params;
if (this._keyboard && !options) { if (this._keyboard && !options) {
@ -73,5 +122,5 @@ export default class Message extends EventEmitter {
} }
}); });
}); });
} }*/
} }

View File

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

View File

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