V2 - Big update
This commit is contained in:
35
lib/types/Base.js
Normal file
35
lib/types/Base.js
Normal file
@ -0,0 +1,35 @@
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
/**
|
||||
* Base class of all classes
|
||||
*/
|
||||
export default class Base extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.properties = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns properties of the object
|
||||
* @return {object} properties of object
|
||||
*/
|
||||
getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set properties of the object
|
||||
* @param {object} object properties to set
|
||||
* @param {boolean} extend A boolean indicating if the properties should be
|
||||
* extended by the object provided (Object.assign)
|
||||
* or properties should be replaced by the object
|
||||
* defaults to true
|
||||
* @return {object} returns the properties (same as getProperties)
|
||||
*/
|
||||
setProperties(object, extend = true) {
|
||||
this.properties = extend ? Object.assign(this.properties, object)
|
||||
: object;
|
||||
|
||||
return this.getProperties();
|
||||
}
|
||||
}
|
85
lib/types/Keyboard.js
Normal file
85
lib/types/Keyboard.js
Normal file
@ -0,0 +1,85 @@
|
||||
import Base from './Base';
|
||||
|
||||
/**
|
||||
* Keyboard class, used to configure keyboards for messages.
|
||||
* You should pass your instance of this class to message.keyboard() method
|
||||
*/
|
||||
export default class Keyboard extends Base {
|
||||
/**
|
||||
* Create a new keyboard
|
||||
* @param {object} properties Keyboard properties, as defined by Telegram API
|
||||
* See ReplyKeyboardMarkup, ReplyKeyboardHide,
|
||||
* ForceReply
|
||||
*/
|
||||
constructor(properties = {}) {
|
||||
super();
|
||||
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the keyboard property of reply_markup
|
||||
* @param {array} keys An array of arrays, with the format of
|
||||
* Column Column
|
||||
* Row [['TopLeft', 'TopRight'],
|
||||
* Row ['BottomLeft', 'BottomRight']]
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
keys(keys) {
|
||||
this.properties.keyboard = keys;
|
||||
this.properties.hide_keyboard = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set force_keyboard property of reply_markup
|
||||
* @param {boolean} enable value of force_keyboard, defaults to true
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
force(enable = true) {
|
||||
this.properties.force_keyboard = enable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set resize_keyboard property of reply_markup
|
||||
* @param {boolean} enable value of resize_keyboard, defaults to true
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
resize(enable = true) {
|
||||
this.properties.resize_keyboard = enable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set force_keyboard property of reply_markup
|
||||
* @param {boolean} enable value of force_keyboard, defaults to true
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
oneTime(enable = true) {
|
||||
this.properties.one_time_keyboard = enable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set selective property of reply_markup
|
||||
* @param {boolean} enable value of force_keyboard, defaults to true
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
selective(enable = true) {
|
||||
this.properties.selective = enable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hide_keyboard property of reply_markup to true
|
||||
* @return {object} returns the keyboard object
|
||||
*/
|
||||
hide() {
|
||||
this.properties = {
|
||||
hide_keyboard: true
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
110
lib/types/Message.js
Normal file
110
lib/types/Message.js
Normal file
@ -0,0 +1,110 @@
|
||||
import Base from './Base';
|
||||
|
||||
/**
|
||||
* Message class, used to send message to a chat
|
||||
*/
|
||||
export default class Message extends Base {
|
||||
/**
|
||||
* Create a new message
|
||||
* @param {object} properties Message properties, as defined by Telegram API
|
||||
*/
|
||||
constructor(properties = {}) {
|
||||
super();
|
||||
|
||||
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 text of the message
|
||||
* @param {string} text Message's content
|
||||
* @return {object} returns the message object
|
||||
*/
|
||||
text(text) {
|
||||
this.properties.text = text;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set reply_to_message_id of the message
|
||||
* @param {number} id message_id of the message to reply to
|
||||
* @return {object} returns the message object
|
||||
*/
|
||||
reply(id) {
|
||||
this.properties.reply_to_message_id = id;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
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_id === messageId;
|
||||
} else {
|
||||
return message.chat.id === chat;
|
||||
}
|
||||
});
|
||||
|
||||
if (update) {
|
||||
resolve(update);
|
||||
this.emit('message:answer', update);
|
||||
|
||||
bot.removeListener('update', listener);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
71
lib/types/Question.js
Normal file
71
lib/types/Question.js
Normal file
@ -0,0 +1,71 @@
|
||||
import Message from './Message';
|
||||
import Keyboard from './Keyboard';
|
||||
|
||||
/**
|
||||
* Question class, extends Message
|
||||
* Sends a message, shows a keyboard with the answers provided, and validates
|
||||
* the answer
|
||||
*/
|
||||
export default class Question extends Message {
|
||||
/**
|
||||
* Create a new question
|
||||
* @param {object} options Options, same as Message, plus `answers` which
|
||||
* is a keyboard layout, see Keyboard#keys
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
super(options);
|
||||
|
||||
let kb = new Keyboard().force().oneTime().selective()
|
||||
.keys(this.properties.answers);
|
||||
this.keyboard(kb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets answers of the question. This is passed to Keyboard#keys, and then
|
||||
* used to validate the answer given
|
||||
* @param {array} answers Array of arrays of strings, same as Keyboard#keys
|
||||
* @return {object} returns the question object
|
||||
*/
|
||||
answers(answers) {
|
||||
this.answers = answers;
|
||||
this._keyboard.keys(answers);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the question (same as Message#send), and validates the answer given
|
||||
* if the answer is one of the defined answers, resolves, else rejects
|
||||
* You should not manually use this method unless you're extending this class
|
||||
* You should instead use bot.send(question);
|
||||
* @param {object} bot
|
||||
* @return {promise} A promise which is resolved in case of valid answer, and
|
||||
* rejected in case of invalid answer
|
||||
*/
|
||||
send(bot) {
|
||||
const answers = this.answers;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
super.send(bot).then(update => {
|
||||
const message = update.message;
|
||||
let answer;
|
||||
|
||||
answers.forEach(function find(a) {
|
||||
if (Array.isArray(a)) {
|
||||
a.forEach(find);
|
||||
}
|
||||
if (a === message.text) {
|
||||
answer = a;
|
||||
}
|
||||
});
|
||||
|
||||
if (answer) {
|
||||
resolve(answer, update);
|
||||
this.emit('question:answer', answer, update);
|
||||
} else {
|
||||
reject(update);
|
||||
this.emit('question:invalid', update);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user