feat(stop): ability to stop bot from listening on updates

resolve #12
This commit is contained in:
Mahdi Dibaiee
2015-11-08 06:52:06 +03:30
parent 82961d7fa5
commit cdb5adc71f
8 changed files with 47 additions and 16 deletions

View File

@ -5,6 +5,7 @@ export default function poll(bot) {
}
bot.emit('update', response.result);
if (bot._stop) return null;
return poll(bot);
});
}

View File

@ -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);

View File

@ -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;