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); } }); }); } }