Fix promises getting rejected silently - Thanks to @joepie91

Fix bot not working properly in groups
Fix bot not answering properly
Bump to version 0.2.2
This commit is contained in:
Mahdi Dibaiee
2015-06-29 05:30:29 +04:30
parent 75798a1a63
commit 714ffa9458
10 changed files with 105 additions and 69 deletions

View File

@ -33,5 +33,7 @@ export default function fetch(path, data) {
req.write(post);
}
req.end();
}).catch(err => {
console.error('Error sending request', err);
});
}

View File

@ -10,6 +10,10 @@ const DEFAULTS = {
}
};
process.on('uncaughtException', function(err) {
console.error(err.stack);
});
/**
* Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods,
@ -48,12 +52,12 @@ export default class Bot extends EventEmitter {
*/
start() {
let poll = function() {
this.api.getUpdates(this.update).then(response => {
setTimeout(poll, this.update.timeout * 1000);
return this.api.getUpdates(this.update).then(response => {
const again = wait(this.update.timeout * 1000).then(poll);
const result = response.result;
if (!result.length) {
return;
return again;
}
if (!this.update.offset) {
@ -64,6 +68,8 @@ export default class Bot extends EventEmitter {
this.update.offset += 1;
}
this.emit('update', result);
result.forEach(res => {
let text = res.message.text;
if (text.startsWith('/')) {
@ -73,16 +79,20 @@ export default class Bot extends EventEmitter {
}
let ev = this._userEvents.find(({pattern}) => pattern.test(text));
if (!ev) {
return;
}
ev.listener(res.message);
});
this.emit('update', result);
return again;
});
}.bind(this);
return this.api.getMe().then(response => {
this.info = response.result;
poll();
return poll();
});
}
@ -130,6 +140,12 @@ export default class Bot extends EventEmitter {
* @return {unknown} returns the result of calling message's send method
*/
send(message) {
return message.send(this);
return message.send(this).catch(console.error);
}
}
const wait = (miliseconds) => {
return new Promise(resolve => {
setTimeout(resolve, miliseconds);
});
};

View File

@ -93,16 +93,18 @@ export default class Message extends Base {
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;
return message.chat.id === chat
&& message.reply_to_message
&& message.reply_to_message.message_id === messageId;
} else {
return message.chat.id === chat;
}
});
if (update) {
resolve(update);
this.emit('message:answer', update);
resolve(update.message);
this.emit('message:answer', update.message);
bot.removeListener('update', listener);
}

View File

@ -15,9 +15,10 @@ export default class Question extends Message {
constructor(options = {}) {
super(options);
let kb = new Keyboard().force().oneTime().selective()
.keys(this.properties.answers);
let kb = new Keyboard().force().oneTime().selective();
this.keyboard(kb);
this.answers(options.answers);
}
/**
@ -27,7 +28,7 @@ export default class Question extends Message {
* @return {object} returns the question object
*/
answers(answers) {
this.answers = answers;
this._answers = answers;
this._keyboard.keys(answers);
return this;
}
@ -42,29 +43,27 @@ export default class Question extends Message {
* rejected in case of invalid answer
*/
send(bot) {
const answers = this.answers;
const answers = this._answers;
return new Promise((resolve, reject) => {
super.send(bot).then(message => {
let answer;
return super.send(bot).then(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);
answers.forEach(function find(a) {
if (Array.isArray(a)) {
a.forEach(find);
}
if (a === message.text) {
answer = a;
}
});
if (answer) {
this.emit('question:answer', answer, message);
return message;
} else {
this.emit('question:invalid', message);
throw update;
}
});
}
}