diff --git a/package.json b/package.json index bc265bb..255895b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "1.0.1", + "version": "0.9.1", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { diff --git a/src/functions/poll.js b/src/functions/poll.js index eb71b03..089131a 100644 --- a/src/functions/poll.js +++ b/src/functions/poll.js @@ -1,13 +1,18 @@ export default function poll(bot) { - return bot.api.getUpdates(bot.update).then(response => { - if (!response.result.length) { - return poll(bot); - } - bot.emit('update', response.result); + return bot.api.getUpdates(bot.update) + .then(response => { + if (!response.result.length) { + return poll(bot); + } + bot.emit('update', response.result); - if (bot._stop) { - return null; - } - return poll(bot); - }); + if (bot._stop) { + return null; + } + return poll(bot); + }) + .catch(e => { + bot.emit('error', e); + return poll(bot); + }); } diff --git a/src/index.js b/src/index.js index 2710b06..445124c 100644 --- a/src/index.js +++ b/src/index.js @@ -75,17 +75,22 @@ export default class Bot extends EventEmitter { if (hook) { return webhook(hook, this); } - return this.api.getMe().then(response => { - this.info = response.result; + return this.api.getMe() + .then(response => { + this.info = response.result; - this.on('update', this._update); + this.on('update', this._update); - if (hook) { - return webhook(hook, this); - } + if (hook) { + return webhook(hook, this); + } - return poll(this); - }); + return poll(this); + }) + .catch(e => { + this.emit('error', e); + throw e; + }); } /** @@ -138,7 +143,11 @@ 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(e => { + this.emit('error', e); + throw e; + }); } /**