feat Types: Forward Type

This commit is contained in:
Mahdi Dibaiee
2015-07-01 02:34:44 +04:30
parent c0308f5287
commit 5f07eaa456
11 changed files with 302 additions and 117 deletions

View File

@ -4,11 +4,68 @@ import {EventEmitter} from 'events';
* Base class of all classes
*/
export default class Base extends EventEmitter {
constructor() {
constructor(method) {
super();
this.method = method;
this.properties = {};
}
/**
* 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
*/
send(bot) {
if (this._keyboard) {
const reply_markup = JSON.stringify(this._keyboard.getProperties());
this.properties.reply_markup = reply_markup;
}
let messageId;
return new Promise(resolve => {
bot.api[this.method](this.properties).then(response => {
messageId = response.result.message_id;
this.emit('message:sent', response);
});
if (this._keyboard.one_time_keyboard) {
this._keyboard.replyMarkup = '';
}
const chat = this.properties.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.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);
}
});
});
}
/**
* Returns properties of the object
* @return {object} properties of object

62
lib/types/Forward.js Normal file
View File

@ -0,0 +1,62 @@
import Base from './Base';
/**
* Forward class, used to forward messages from a chat to another
*/
export default class Forward extends Base {
/**
* Create a new forward message
* @param {object} properties Forward Message properties, as defined by
* Telegram API
*/
constructor(properties = {}) {
super('forwardMessage');
this.properties = properties;
this._keyboard = new Base();
}
/**
* Set chat_id of the message
* @param {number} chat
* @return {object} returns the message object
*/
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
*/
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
*/
message(message) {
this.properties.message_id = message;
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
*/
keyboard(kb) {
this._keyboard = kb;
return this;
}
// This class inherits Base's send method
}

View File

@ -9,7 +9,7 @@ export default class Message extends Base {
* @param {object} properties Message properties, as defined by Telegram API
*/
constructor(properties = {}) {
super();
super('sendMessage');
this.properties = properties;
this._keyboard = new Base();
@ -57,58 +57,5 @@ export default class Message extends Base {
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
*/
send(bot) {
console.log('sending message');
let messageId;
const reply_markup = JSON.stringify(this._keyboard.getProperties());
this.properties.reply_markup = reply_markup;
return new Promise(resolve => {
bot.api.sendMessage(this.properties).then(response => {
messageId = response.result.message_id;
this.emit('message:sent', response);
});
if (this._keyboard.one_time_keyboard) {
this._keyboard.replyMarkup = '';
}
const chat = this.properties.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.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);
}
});
});
}
// This class inherits Base's send method
}