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

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

View File

@ -29,7 +29,7 @@ function webhook(options, bot) {
return bot.api.setWebhook(options.url).then(function () { 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) { return (0, _fetch.getBody)(req).then(function (data) {
bot.emit('update', _qs2['default'].parse(data).result); bot.emit('update', _qs2['default'].parse(data).result);

View File

@ -182,6 +182,22 @@ var Bot = (function (_EventEmitter) {
value: function send(message) { value: function send(message) {
return message.send(this)['catch'](console.error); 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', key: '_update',
@ -202,8 +218,6 @@ var Bot = (function (_EventEmitter) {
this.update.offset += 1; this.update.offset += 1;
} }
console.log(update);
update.forEach(function (res) { update.forEach(function (res) {
var marked3$0 = [getAnswer].map(regeneratorRuntime.mark); var marked3$0 = [getAnswer].map(regeneratorRuntime.mark);

View File

@ -46,3 +46,6 @@ const test = new Message().text('Test Command');
bot.command('test <subject> [count|number] ...rest', message => { bot.command('test <subject> [count|number] ...rest', message => {
bot.send(test.to(message.chat.id).text(message.args.subject)); bot.send(test.to(message.chat.id).text(message.args.subject));
}); });
// to stop a bot
// bot.stop();

View File

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

View File

@ -12,7 +12,7 @@ export default function webhook(options = {}, bot) {
return bot.api.setWebhook(options.url).then(() => { 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 => { return getBody(req).then(data => {
bot.emit('update', qs.parse(data).result); 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); 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 * The internal update event listener, used to parse messages and fire
* command/get events - YOU SHOULD NOT USE THIS * command/get events - YOU SHOULD NOT USE THIS
@ -148,8 +162,6 @@ export default class Bot extends EventEmitter {
this.update.offset += 1; this.update.offset += 1;
} }
console.log(update);
update.forEach(res => { update.forEach(res => {
let text = res.message.text; let text = res.message.text;
if (!text) return; if (!text) return;

View File

@ -1,6 +1,6 @@
{ {
"name": "telegram-api", "name": "telegram-api",
"version": "0.5.22", "version": "0.6.0",
"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": {
@ -34,14 +34,14 @@
}, },
"homepage": "https://github.com/mdibaiee/node-telegram-api", "homepage": "https://github.com/mdibaiee/node-telegram-api",
"dependencies": { "dependencies": {
"babel": "^5.6.14", "babel": "5.6.14",
"grunt": "^0.4.5", "grunt": "0.4.5",
"grunt-babel": "^5.0.1", "grunt-babel": "5.0.1",
"grunt-contrib-copy": "^0.8.0", "grunt-contrib-copy": "0.8.0",
"grunt-contrib-watch": "^0.6.1", "grunt-contrib-watch": "0.6.1",
"grunt-eslint": "^16.0.0", "grunt-eslint": "16.0.0",
"mime": "^1.3.4", "mime": "1.3.4",
"qs": "^4.0.0", "qs": "4.0.0",
"unirest": "^0.4.2" "unirest": "0.4.2"
} }
} }