Skip to content

Commit

Permalink
Added stopPolling method to Slimbot
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonchee committed Jul 20, 2019
1 parent 9d7669b commit 334bc3c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ node_modules
coverage
.npmignore
yarn.lock
.idea
.idea
.coveralls.yml
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

A fuss-free, thin wrapper around Telegram Bot API for Node.js. No frills.

Updated for [Bot API 4.3](https://core.telegram.org/bots/api#may-31-2019).
Updated for [Telegram Bot API 4.3](https://core.telegram.org/bots/api#may-31-2019).
Works with [Node 12.6.0](https://github.com/nodejs/node/releases/tag/v12.6.0).

_**Note:** Slimbot patch versions (e.g. x.y.**Z**) do not track or reflect Telegram Bot API changes._

## Resources
* [Release Notes](https://github.com/edisonchee/slimbot/releases)
* [Changelog](https://github.com/edisonchee/slimbot/blob/master/CHANGELOG.md)
Expand Down
2 changes: 1 addition & 1 deletion examples/sendFile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Slimbot = require('./src/slimbot');
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_BOT_TOKEN']);
const fs = require('fs');

Expand Down
6 changes: 6 additions & 0 deletions examples/simpleUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ slimbot.on('edited_message', edited_message => {

// Call API
slimbot.startPolling();

console.log('polling...');

setTimeout(() => {
slimbot.stopPolling();
}, 10000);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slimbot",
"version": "4.3.0",
"version": "4.3.1",
"description": "Simple and minimal Telegram Bot API for Node.js. No frills.",
"author": "Edison Chee <edisonchee@live.com>",
"repository": {
Expand Down
19 changes: 19 additions & 0 deletions spec/core/telebotSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,24 @@ describe('Slimbot', () => {
done();
});
});

it('should store timeout object', done => {
spyOn(slimbot, '_request').and.returnValue(Bluebird.resolve({}));
spyOn(slimbot, '_processUpdates');
slimbot.startPolling().then(() => {
expect(slimbot._processUpdates).toHaveBeenCalled();
expect(slimbot._timeout).not.toBe(undefined);
done();
});
});
});

describe('stopPolling', () => {
it('should call slimbot.stopPolling', () => {
spyOn(slimbot, 'stopPolling');
slimbot.stopPolling();
expect(slimbot.stopPolling).toHaveBeenCalled();
expect(slimbot._timeout).toBe(undefined);
});
});
});
11 changes: 10 additions & 1 deletion src/slimbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Slimbot extends Telegram(EventEmitter) {
constructor(token, proxy) {
super(token, proxy);
this._offset = undefined;
this._timeout = undefined;
}

_processUpdates(updates) {
Expand Down Expand Up @@ -61,9 +62,17 @@ class Slimbot extends Telegram(EventEmitter) {
}
})
.finally(() => {
setTimeout(() => this.startPolling(callback), 100);
if (this._timeout) {
this._timeout.refresh();
} else {
this._timeout = setTimeout(() => this.startPolling(callback), 100);
}
});
}

stopPolling() {
clearTimeout(this._timeout);
}
}

module.exports = Slimbot;

0 comments on commit 334bc3c

Please sign in to comment.