new version

This commit is contained in:
Mahdi Dibaiee
2015-06-28 05:34:18 +04:30
parent a05b17db70
commit 4d72d21c13
6 changed files with 208 additions and 132 deletions

61
lib/classes/Keyboard.js Normal file
View File

@ -0,0 +1,61 @@
export default class Keyboard {
constructor(message, options = {}) {
this.message = message;
this.replyMarkup = options;
}
keys(keys) {
this.setProperties({
keyboard: keys,
hide_keyboard: false
});
return this;
}
force(enable = true) {
this.setProperties({
force_keyboard: enable
});
return this;
}
resize(enable = true) {
this.setProperties({
resize: enable
});
return this;
}
oneTime(enable = true) {
this.setProperties({
one_time_keyboard: enable
});
return this;
}
selective(enable = true) {
this.setProperties({
selective: enable
});
return this;
}
hide() {
this.replyMarkup = {
hide_keyboard: true
};
return this;
}
get replyMarkup() {
return JSON.parse(this.message.params.reply_markup);
}
set replyMarkup(json) {
this.message.params.reply_markup = JSON.stringify(json);
}
setProperties(object) {
this.replyMarkup = Object.assign(this.replyMarkup, object);
}
}

77
lib/classes/Message.js Normal file
View File

@ -0,0 +1,77 @@
import {EventEmitter} from 'events';
import Keyboard from './Keyboard';
export default class Message extends EventEmitter {
constructor(options = {}) {
super();
this.params = options;
}
to(chat) {
this.params.chat_id = chat;
return this;
}
text(text) {
this.params.text = text;
return this;
}
reply(chat) {
this.params.reply_to_message_id = chat;
return this;
}
keyboard(options) {
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;
} else {
return message.chat.id === chat;
}
});
if (update) {
resolve(update);
this.emit('message:answer', update);
bot.removeListener('update', listener);
}
});
});
}
}

43
lib/classes/Question.js Normal file
View File

@ -0,0 +1,43 @@
import Message from './Message';
export default class Question extends Message {
constructor(options = {}) {
super(options);
this.keyboard().force().oneTime().selective();
}
answers(answers) {
this.answers = answers;
this.keyboard().keys(answers);
return this;
}
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);
}
});
});
}
}