node-telegram-api/lib/classes/Message.js

127 lines
2.6 KiB
JavaScript
Raw Normal View History

import Base from './Base'
2015-06-28 01:04:18 +00:00
import Keyboard from './Keyboard';
/**
* Message class
* @class
*/
export default class Message extends Base {
/**
* @constructor
* @param {Object} [options = {}]
*/
2015-06-28 01:04:18 +00:00
constructor(options = {}) {
super();
this.setProperties(options);
2015-06-28 01:04: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) {
if (chat) {
this.setProperties({chat_id: chat});
return this;
}
return this.getProperties('chat_id');
2015-06-28 01:04: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) {
if (text) {
this.setProperties({text: text});
return this;
}
return this.getProperties('text');
2015-06-28 01:04: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) {
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
}
/*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 &&
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 01:04:18 +00:00
}