Skip to content

Commit 4bef607

Browse files
committed
add stopPolling, emitUpdate, emitCallbackQuery
1 parent 7926277 commit 4bef607

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# node-telegram-bot-api
1+
# :see_no_evil: node-telegram-bot-api :hear_no_evil:
22
monkey patch for https://github.com/yagop/node-telegram-bot-api
3+
4+
# Install
5+
6+
```
7+
npm i monkey-patches-node-telegram-bot-api --save --save-exact
8+
```
9+
10+
# Usage
11+
12+
```js
13+
var options = {/* ... */};
14+
TelegramBot = require('node-telegram-bot-api');
15+
require('monkey-patches-node-telegram-bot-api')(TelegramBot, options);
16+
17+
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
18+
var bot = new TelegramBot(token, {polling: true});
19+
```
20+
21+
# Options
22+
23+
## stopPolling
24+
25+
With this option you can stop polling.
26+
```js
27+
options = {stopPolling: true};
28+
// setup bot ...
29+
// usage
30+
bot.stopPolling(); // stop
31+
bot.initPolling(); // start
32+
bot.initPolling(); // restart
33+
```
34+
35+
[related pull request](https://github.com/yagop/node-telegram-bot-api/pull/51)
36+
37+
## emitUpdate
38+
this path cause `update`-event emitted every time update received
39+
```js
40+
options = {emitUpdate: true};
41+
// setup bot ...
42+
// usage
43+
bot.on('update', function(update){/* ... */})
44+
```
45+
46+
47+
## emitCallbackQuery
48+
this path add support for callback_query and cause `callback_query`-event emitted every time callback query received
49+
```js
50+
options = {emitCallbackQuery: true};
51+
// setup bot ...
52+
// usage
53+
bot.on('callback_query', function(callbackQuery){/* ... */})
54+
```
55+
[related pull request](https://github.com/yagop/node-telegram-bot-api/pull/118)
56+
57+
# Compatibility
58+
59+
I test this package with `[email protected]` and `[email protected]`
60+
61+
# Sem version, backward compatibility and ...
62+
No. this is only :monkey_face: patch. so every time install this package with `--save-exact` option.

index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var debug = require('debug')('monkey-patches:node-telegram-bot-api');
2+
3+
function patchStopPolling(TelegramBot) {
4+
TelegramBot.prototype.stopPolling = function () {
5+
if (this._polling) {
6+
this._polling.abort = true;
7+
this._polling.lastRequest.cancel('Stop polling');
8+
}
9+
this._polling = null;
10+
}
11+
}
12+
13+
function patchEmitUpdate(TelegramBot) {
14+
var _processUpdate = TelegramBot.prototype._processUpdate;
15+
TelegramBot.prototype._processUpdate = function (update) {
16+
debug('Process Update %j', update);
17+
this.emit('update', update);
18+
_processUpdate.call(this, update);
19+
}
20+
}
21+
22+
function patchEmitCallbackQuery(TelegramBot) {
23+
var _processUpdate = TelegramBot.prototype._processUpdate;
24+
TelegramBot.prototype._processUpdate = function (update) {
25+
_processUpdate.call(this, update);
26+
var callbackQuery = update.callback_query;
27+
if (callbackQuery) {
28+
debug('Process Update callback_query %j', callbackQuery);
29+
this.emit('callback_query', callbackQuery);
30+
}
31+
32+
}
33+
}
34+
35+
36+
module.exports = function (TelegramBot, options) {
37+
options = options || {};
38+
if (options.stopPolling) {
39+
patchStopPolling(TelegramBot);
40+
}
41+
if (options.emitUpdate) {
42+
patchEmitUpdate(TelegramBot);
43+
}
44+
if (options.emitCallbackQuery) {
45+
patchEmitCallbackQuery(TelegramBot);
46+
}
47+
};

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "monkey-patches-node-telegram-bot-api",
3+
"version": "0.0.1",
4+
"description": "monkey patch for https://github.com/yagop/node-telegram-bot-api",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/monkey-patches/node-telegram-bot-api.git"
12+
},
13+
"author": "",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/monkey-patches/node-telegram-bot-api/issues"
17+
},
18+
"homepage": "https://github.com/monkey-patches/node-telegram-bot-api#readme",
19+
"peerDependencies": {
20+
"debug": "^2.2.0"
21+
}
22+
}

0 commit comments

Comments
 (0)