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

@ -47,7 +47,7 @@ function fetch(path, data) {
req.write(post); req.write(post);
} }
req.end(); req.end();
}); })['catch'](console.error);
} }
module.exports = exports['default']; module.exports = exports['default'];

View File

@ -30,6 +30,10 @@ var DEFAULTS = {
} }
}; };
process.on('uncaughtException', function (err) {
console.error(err.stack);
});
/** /**
* Bot class used to connect to a new bot * Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods, * Bots have an api property which gives access to all Telegram API methods,
@ -83,12 +87,12 @@ var Bot = (function (_EventEmitter) {
var poll = (function () { var poll = (function () {
var _this = this; var _this = this;
this.api.getUpdates(this.update).then(function (response) { return this.api.getUpdates(this.update).then(function (response) {
setTimeout(poll, _this.update.timeout * 1000); var again = wait(_this.update.timeout * 1000).then(poll);
var result = response.result; var result = response.result;
if (!result.length) { if (!result.length) {
return; return again;
} }
if (!_this.update.offset) { if (!_this.update.offset) {
@ -99,6 +103,8 @@ var Bot = (function (_EventEmitter) {
_this.update.offset += 1; _this.update.offset += 1;
} }
_this.emit('update', result);
result.forEach(function (res) { result.forEach(function (res) {
var text = res.message.text; var text = res.message.text;
if (text.startsWith('/')) { if (text.startsWith('/')) {
@ -111,16 +117,20 @@ var Bot = (function (_EventEmitter) {
var pattern = _ref.pattern; var pattern = _ref.pattern;
return pattern.test(text); return pattern.test(text);
}); });
if (!ev) {
return;
}
ev.listener(res.message); ev.listener(res.message);
}); });
_this.emit('update', result); return again;
}); });
}).bind(this); }).bind(this);
return this.api.getMe().then(function (response) { return this.api.getMe().then(function (response) {
_this2.info = response.result; _this2.info = response.result;
poll(); return poll();
}); });
} }
}, { }, {
@ -174,7 +184,7 @@ var Bot = (function (_EventEmitter) {
* @return {unknown} returns the result of calling message's send method * @return {unknown} returns the result of calling message's send method
*/ */
value: function send(message) { value: function send(message) {
return message.send(this); return message.send(this)['catch'](console.error);
} }
}]); }]);
@ -182,4 +192,10 @@ var Bot = (function (_EventEmitter) {
})(_events.EventEmitter); })(_events.EventEmitter);
exports['default'] = Bot; exports['default'] = Bot;
var wait = function wait(miliseconds) {
return new Promise(function (resolve) {
setTimeout(resolve, miliseconds);
});
};
module.exports = exports['default']; module.exports = exports['default'];

View File

@ -134,20 +134,21 @@ var Message = (function (_Base) {
// if in a group, there will be a reply to this message // if in a group, there will be a reply to this message
if (chat < 0) { 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 { } else {
return message.chat.id === chat; return message.chat.id === chat;
} }
}); });
if (update) { if (update) {
resolve(update); resolve(update.message);
this.emit('message:answer', update);
this.emit('message:answer', update.message);
bot.removeListener('update', listener); bot.removeListener('update', listener);
} }
}); });
}); })['catch'](console.error);
} }
}]); }]);

View File

@ -42,8 +42,10 @@ var Question = (function (_Message) {
_get(Object.getPrototypeOf(Question.prototype), 'constructor', this).call(this, options); _get(Object.getPrototypeOf(Question.prototype), 'constructor', this).call(this, options);
var kb = new _Keyboard2['default']().force().oneTime().selective().keys(this.properties.answers); var kb = new _Keyboard2['default']().force().oneTime().selective();
this.keyboard(kb); this.keyboard(kb);
this.answers(options.answers);
} }
_inherits(Question, _Message); _inherits(Question, _Message);
@ -58,7 +60,7 @@ var Question = (function (_Message) {
* @return {object} returns the question object * @return {object} returns the question object
*/ */
value: function answers(_answers) { value: function answers(_answers) {
this.answers = _answers; this._answers = _answers;
this._keyboard.keys(_answers); this._keyboard.keys(_answers);
return this; return this;
} }
@ -77,10 +79,9 @@ var Question = (function (_Message) {
value: function send(bot) { value: function send(bot) {
var _this = this; var _this = this;
var answers = this.answers; var answers = this._answers;
return new Promise(function (resolve, reject) { return _get(Object.getPrototypeOf(Question.prototype), 'send', this).call(this, bot).then(function (message) {
_get(Object.getPrototypeOf(Question.prototype), 'send', _this).call(_this, bot).then(function (message) {
var answer = undefined; var answer = undefined;
answers.forEach(function find(a) { answers.forEach(function find(a) {
@ -93,14 +94,13 @@ var Question = (function (_Message) {
}); });
if (answer) { if (answer) {
resolve(answer, update); _this.emit('question:answer', answer, message);
_this.emit('question:answer', answer, update); return message;
} else { } else {
reject(update); _this.emit('question:invalid', message);
_this.emit('question:invalid', update); throw update;
} }
}); });
});
} }
}]); }]);

View File

@ -5,11 +5,11 @@ var Message = require('telegram-api/types/Message');
var Question = require('telegram-api/types/Question'); var Question = require('telegram-api/types/Question');
var bot = new Bot({ var bot = new Bot({
token: 'YOUR_KEY' token: '121143906:AAE6pcpBoARNZZjr3fUpvKuLInJ5Eee5Ajk'
}); });
bot.start().then(() => { bot.start().catch(err => {
console.log(bot.info); console.error(err, '\n', err.stack);
}); });
// polling // polling
@ -28,7 +28,7 @@ bot.get(/Hi\sBot/, message => {
question.to(id).reply(message.message_id); question.to(id).reply(message.message_id);
bot.send(question).then(answer => { bot.send(question).then(answer => {
const msg = new Message().to(id).text('Your answer: ' + answer); const msg = new Message().to(id).text('Your answer: ' + answer.text);
bot.send(msg); bot.send(msg);
}, () => { }, () => {
const msg = new Message().to(id).text('Invalid answer'); const msg = new Message().to(id).text('Invalid answer');

View File

@ -33,5 +33,7 @@ export default function fetch(path, data) {
req.write(post); req.write(post);
} }
req.end(); 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 * Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods, * Bots have an api property which gives access to all Telegram API methods,
@ -48,12 +52,12 @@ export default class Bot extends EventEmitter {
*/ */
start() { start() {
let poll = function() { let poll = function() {
this.api.getUpdates(this.update).then(response => { return this.api.getUpdates(this.update).then(response => {
setTimeout(poll, this.update.timeout * 1000); const again = wait(this.update.timeout * 1000).then(poll);
const result = response.result; const result = response.result;
if (!result.length) { if (!result.length) {
return; return again;
} }
if (!this.update.offset) { if (!this.update.offset) {
@ -64,6 +68,8 @@ export default class Bot extends EventEmitter {
this.update.offset += 1; this.update.offset += 1;
} }
this.emit('update', result);
result.forEach(res => { result.forEach(res => {
let text = res.message.text; let text = res.message.text;
if (text.startsWith('/')) { if (text.startsWith('/')) {
@ -73,16 +79,20 @@ export default class Bot extends EventEmitter {
} }
let ev = this._userEvents.find(({pattern}) => pattern.test(text)); let ev = this._userEvents.find(({pattern}) => pattern.test(text));
if (!ev) {
return;
}
ev.listener(res.message); ev.listener(res.message);
}); });
this.emit('update', result); return again;
}); });
}.bind(this); }.bind(this);
return this.api.getMe().then(response => { return this.api.getMe().then(response => {
this.info = response.result; 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 * @return {unknown} returns the result of calling message's send method
*/ */
send(message) { 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}) => { const update = result.find(({message}) => {
// if in a group, there will be a reply to this message // if in a group, there will be a reply to this message
if (chat < 0) { if (chat < 0) {
return message.chat.id === chat && return message.chat.id === chat
message.reply_to_message.message_id === messageId; && message.reply_to_message
&& message.reply_to_message.message_id === messageId;
} else { } else {
return message.chat.id === chat; return message.chat.id === chat;
} }
}); });
if (update) { if (update) {
resolve(update); resolve(update.message);
this.emit('message:answer', update);
this.emit('message:answer', update.message);
bot.removeListener('update', listener); bot.removeListener('update', listener);
} }

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name": "telegram-api", "name": "telegram-api",
"version": "0.2.1", "version": "0.2.2",
"description": "Control Telegram bots easily using the new Telegram API", "description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {