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:
parent
335a5045d8
commit
96adb918c1
@ -489,4 +489,10 @@ Register a reply to wait for a message response.
|
||||
| messageId | <code>Number</code> | <code>String</code> | The message id to be replied. |
|
||||
| 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
|
||||
|
||||
* * *
|
||||
|
@ -67,6 +67,18 @@ class TelegramBot extends EventEmitter {
|
||||
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) {
|
||||
debug('Process Update %j', update);
|
||||
const message = update.message;
|
||||
|
@ -28,6 +28,12 @@ class TelegramBotPolling {
|
||||
this._polling();
|
||||
}
|
||||
|
||||
stopPolling() {
|
||||
this.abort = true;
|
||||
// wait until the last request is fulfilled
|
||||
return this.lastRequest;
|
||||
}
|
||||
|
||||
_polling() {
|
||||
this.lastRequest = this
|
||||
._getUpdates()
|
||||
|
@ -546,11 +546,14 @@ describe('Telegram', function telegramSuite() {
|
||||
}); // End Telegram
|
||||
|
||||
describe('#TelegramBotPolling', function TelegramBotPollingSuite() {
|
||||
|
||||
it('should call the callback on polling', function test(done) {
|
||||
const opts = { interval: 100, timeout: 1 };
|
||||
const polling = new TelegramPolling(TOKEN, opts, (msg) => {
|
||||
if (msg.update_id === 10) {
|
||||
done();
|
||||
polling.stopPolling().then(() => {
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
// 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: {} }]);
|
||||
};
|
||||
});
|
||||
|
||||
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: {} }]);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user