diff --git a/build/functions/poll.js b/build/functions/poll.js index 89e41a7..8a3a87d 100644 --- a/build/functions/poll.js +++ b/build/functions/poll.js @@ -12,6 +12,7 @@ function poll(bot) { } bot.emit('update', response.result); + if (bot._stop) return null; return poll(bot); }); } diff --git a/build/functions/webhook.js b/build/functions/webhook.js index c00c59f..859aeda 100644 --- a/build/functions/webhook.js +++ b/build/functions/webhook.js @@ -29,7 +29,7 @@ function webhook(options, bot) { return bot.api.setWebhook(options.url).then(function () { - _http2['default'].createServer(options.server, function (req, res) { + bot._webhookServer = _http2['default'].createServer(options.server, function (req, res) { return (0, _fetch.getBody)(req).then(function (data) { bot.emit('update', _qs2['default'].parse(data).result); diff --git a/build/index.js b/build/index.js index 6516096..83b8e52 100644 --- a/build/index.js +++ b/build/index.js @@ -182,6 +182,22 @@ var Bot = (function (_EventEmitter) { value: function send(message) { return message.send(this)['catch'](console.error); } + }, { + key: 'stop', + + /** + * Stops the bot, deattaching all listeners and polling + */ + value: function stop() { + this._stop = true; + + if (this._webhookServer) { + this._webhookServer.close(); + } + + this.removeListener('update', this._update); + this._events = {}; + } }, { key: '_update', @@ -202,8 +218,6 @@ var Bot = (function (_EventEmitter) { this.update.offset += 1; } - console.log(update); - update.forEach(function (res) { var marked3$0 = [getAnswer].map(regeneratorRuntime.mark); diff --git a/demo.js b/demo.js index 5420883..c46cac0 100644 --- a/demo.js +++ b/demo.js @@ -46,3 +46,6 @@ const test = new Message().text('Test Command'); bot.command('test [count|number] ...rest', message => { bot.send(test.to(message.chat.id).text(message.args.subject)); }); + +// to stop a bot +// bot.stop(); diff --git a/lib/functions/poll.js b/lib/functions/poll.js index 8ff964d..669a8f5 100644 --- a/lib/functions/poll.js +++ b/lib/functions/poll.js @@ -5,6 +5,7 @@ export default function poll(bot) { } bot.emit('update', response.result); + if (bot._stop) return null; return poll(bot); }); } diff --git a/lib/functions/webhook.js b/lib/functions/webhook.js index 795978a..d45f742 100644 --- a/lib/functions/webhook.js +++ b/lib/functions/webhook.js @@ -12,7 +12,7 @@ export default function webhook(options = {}, bot) { return bot.api.setWebhook(options.url).then(() => { - https.createServer(options.server, (req, res) => { + bot._webhookServer = https.createServer(options.server, (req, res) => { return getBody(req).then(data => { bot.emit('update', qs.parse(data).result); diff --git a/lib/index.js b/lib/index.js index 7a7e89a..b32de1e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -133,6 +133,20 @@ export default class Bot extends EventEmitter { return message.send(this).catch(console.error); } + /** + * Stops the bot, deattaching all listeners and polling + */ + stop() { + this._stop = true; + + if (this._webhookServer) { + this._webhookServer.close(); + } + + this.removeListener('update', this._update); + this._events = {}; + } + /** * The internal update event listener, used to parse messages and fire * command/get events - YOU SHOULD NOT USE THIS @@ -148,8 +162,6 @@ export default class Bot extends EventEmitter { this.update.offset += 1; } - console.log(update); - update.forEach(res => { let text = res.message.text; if (!text) return; diff --git a/package.json b/package.json index fbbe1b7..b63763f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "0.5.22", + "version": "0.6.0", "description": "Control Telegram bots easily using the new Telegram API", "main": "index.js", "scripts": { @@ -34,14 +34,14 @@ }, "homepage": "https://github.com/mdibaiee/node-telegram-api", "dependencies": { - "babel": "^5.6.14", - "grunt": "^0.4.5", - "grunt-babel": "^5.0.1", - "grunt-contrib-copy": "^0.8.0", - "grunt-contrib-watch": "^0.6.1", - "grunt-eslint": "^16.0.0", - "mime": "^1.3.4", - "qs": "^4.0.0", - "unirest": "^0.4.2" + "babel": "5.6.14", + "grunt": "0.4.5", + "grunt-babel": "5.0.1", + "grunt-contrib-copy": "0.8.0", + "grunt-contrib-watch": "0.6.1", + "grunt-eslint": "16.0.0", + "mime": "1.3.4", + "qs": "4.0.0", + "unirest": "0.4.2" } }