Refactor classes and utils #3

Closed
mamal72 wants to merge 3 commits from new into new
6 changed files with 232 additions and 67 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ node_modules
# Test file I use to test my module
test.js
# jetbrains
.idea

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,61 +1,72 @@
import {setProperties, getProperties} from '../utils';
/**
* Keyboard layout class
* @class
*/
export default class Keyboard {
constructor(message, options = {}) {
this.message = message;
this.replyMarkup = options;
/**
* @constructor
* @param {Object} options
*/
constructor(options = {keyboard: [], resize_keyboard: false, one_time_keyboard: false, selective: false}) {
this.properties = options;
}
keys(keys) {
this.setProperties({
keyboard: keys,
hide_keyboard: false
});
/**
* Get or set keyboard layout
* @param {Array} value
* @returns {Keyboard|Array}
*/
keyboard(value) {
if (value) {
setProperties({keyboard: value});
return this;
}
return getProperties('keyboard');
}
/**
* 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');
}
/**
* 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');
}
selective(value) {
if (value) {
setProperties({selective: value});
return this;
}
return getProperties('selective');
}
/* setProperties(object) {
this.properties = Object.assign(this.properties, object);
return this;
}
force(enable = true) {
this.setProperties({
force_keyboard: enable
});
return this;
}
resize(enable = true) {
this.setProperties({
resize: enable
});
return this;
}
oneTime(enable = true) {
this.setProperties({
one_time_keyboard: enable
});
return this;
}
selective(enable = true) {
this.setProperties({
selective: enable
});
return this;
}
hide() {
this.replyMarkup = {
hide_keyboard: true
};
return this;
}
get replyMarkup() {
return JSON.parse(this.message.params.reply_markup);
}
set replyMarkup(json) {
this.message.params.reply_markup = JSON.stringify(json);
}
setProperties(object) {
this.replyMarkup = Object.assign(this.replyMarkup, object);
}
getProperties(object) {
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';
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;
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;
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;
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) {
@ -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;
}