2015-06-28 18:32:18 +00:00
|
|
|
import Base from './Base'
|
2015-06-28 01:04:18 +00:00
|
|
|
import Keyboard from './Keyboard';
|
|
|
|
|
2015-06-28 18:32:18 +00:00
|
|
|
/**
|
|
|
|
* Message class
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
export default class Message extends Base {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [options = {}]
|
|
|
|
*/
|
2015-06-28 01:04:18 +00:00
|
|
|
constructor(options = {}) {
|
|
|
|
super();
|
2015-06-28 18:32:18 +00:00
|
|
|
this.setProperties(options);
|
2015-06-28 01:04:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 18:32:18 +00:00
|
|
|
/**
|
|
|
|
* Get or set chat_id of message recipient
|
|
|
|
* @param {number} chat
|
|
|
|
* @return {Message|number}
|
|
|
|
*/
|
2015-06-28 01:04:18 +00:00
|
|
|
to(chat) {
|
2015-06-28 18:32:18 +00:00
|
|
|
if (chat) {
|
|
|
|
this.setProperties({chat_id: chat});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return this.getProperties('chat_id');
|
2015-06-28 01:04:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 18:32:18 +00:00
|
|
|
/**
|
|
|
|
* Get or set text of message
|
|
|
|
* @param {string} text
|
|
|
|
* @return {Message|string}
|
|
|
|
*/
|
2015-06-28 01:04:18 +00:00
|
|
|
text(text) {
|
2015-06-28 18:32:18 +00:00
|
|
|
if (text) {
|
|
|
|
this.setProperties({text: text});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return this.getProperties('text');
|
2015-06-28 01:04:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 18:32:18 +00:00
|
|
|
/**
|
|
|
|
* Get or set reply_to_message_id
|
|
|
|
* @param {number} chat
|
|
|
|
* @return {Message|number}
|
|
|
|
*/
|
2015-06-28 01:04:18 +00:00
|
|
|
reply(chat) {
|
2015-06-28 18:32:18 +00:00
|
|
|
if (chat) {
|
|
|
|
this.setProperties({reply_to_message_id: chat});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return this.getProperties('reply_to_message_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
|
2015-06-28 01:04:18 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 18:32:18 +00:00
|
|
|
/*keyboard(options) {
|
2015-06-28 01:04:18 +00:00
|
|
|
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 &&
|
2015-06-28 18:32:18 +00:00
|
|
|
message.reply_to_message.message_id === messageId;
|
2015-06-28 01:04:18 +00:00
|
|
|
} else {
|
|
|
|
return message.chat.id === chat;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
resolve(update);
|
|
|
|
this.emit('message:answer', update);
|
|
|
|
|
|
|
|
bot.removeListener('update', listener);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-06-28 18:32:18 +00:00
|
|
|
}*/
|
2015-06-28 01:04:18 +00:00
|
|
|
}
|