2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 21:07:39 +00:00

Create a stop method #81

This commit is contained in:
Conor Fennell 2016-06-21 10:48:05 +01:00
parent 335a5045d8
commit 96adb918c1
4 changed files with 50 additions and 1 deletions

View File

@ -489,4 +489,10 @@ Register a reply to wait for a message response.
| messageId | <code>Number</code> &#124; <code>String</code> | The message id to be replied. | | messageId | <code>Number</code> &#124; <code>String</code> | The message id to be replied. |
| callback | <code>function</code> | Callback will be called with the reply message. | | callback | <code>function</code> | Callback will be called with the reply message. |
### telegramBot.stopPolling() => Promise
Stops polling after the last polling request resolves.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**Returns**: <code>Promise</code> - The resolved promise for the last poll request
* * * * * *

View File

@ -67,6 +67,18 @@ class TelegramBot extends EventEmitter {
this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate.bind(this)); this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate.bind(this));
} }
/**
* Stops polling after the last polling request resolves
*
* @return {Promise} promise Promise, of last polling request
*/
stopPolling() {
if (this._polling) {
return this._polling.stopPolling();
}
return Promise.resolve();
}
processUpdate(update) { processUpdate(update) {
debug('Process Update %j', update); debug('Process Update %j', update);
const message = update.message; const message = update.message;

View File

@ -28,6 +28,12 @@ class TelegramBotPolling {
this._polling(); this._polling();
} }
stopPolling() {
this.abort = true;
// wait until the last request is fulfilled
return this.lastRequest;
}
_polling() { _polling() {
this.lastRequest = this this.lastRequest = this
._getUpdates() ._getUpdates()

View File

@ -546,11 +546,14 @@ describe('Telegram', function telegramSuite() {
}); // End Telegram }); // End Telegram
describe('#TelegramBotPolling', function TelegramBotPollingSuite() { describe('#TelegramBotPolling', function TelegramBotPollingSuite() {
it('should call the callback on polling', function test(done) { it('should call the callback on polling', function test(done) {
const opts = { interval: 100, timeout: 1 }; const opts = { interval: 100, timeout: 1 };
const polling = new TelegramPolling(TOKEN, opts, (msg) => { const polling = new TelegramPolling(TOKEN, opts, (msg) => {
if (msg.update_id === 10) { if (msg.update_id === 10) {
done(); polling.stopPolling().then(() => {
done();
});
} }
}); });
// The second time _getUpdates is called it will return a message // The second time _getUpdates is called it will return a message
@ -559,4 +562,26 @@ describe('#TelegramBotPolling', function TelegramBotPollingSuite() {
return new Promise.resolve([{ update_id: 10, message: {} }]); return new Promise.resolve([{ update_id: 10, message: {} }]);
}; };
}); });
describe('#stopPolling', function stopPollingSuite() {
it('should stop polling after last poll request', function test(done) {
const opts = { interval: 200, timeout: 0.5 };
const polling = new TelegramPolling(TOKEN, opts, (msg) => {
// error if message received as only one poll will complete and there should be no more because of stopPolling
done(msg);
});
polling.stopPolling()
.then(() => {
setInterval(() => {
done();
}, 1000);
}).catch(done);
// The second time _getUpdates is called it will return a message
// Really dirty but it works
polling._getUpdates = () => {
return new Promise.resolve([{ update_id: 11, message: {} }]);
};
});
});
}); });