telegram-bot-starter 0.0.1-security → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of telegram-bot-starter might be problematic. Click here for more details.

@@ -0,0 +1,245 @@
1
+ 'use strict';
2
+
3
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4
+
5
+ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
+
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+
9
+ var errors = require('./errors');
10
+ var debug = require('debug')('node-telegram-bot-api');
11
+ var deprecate = require('./utils').deprecate;
12
+ var ANOTHER_WEB_HOOK_USED = 409;
13
+
14
+ var TelegramBotPolling = function () {
15
+ /**
16
+ * Handles polling against the Telegram servers.
17
+ * @param {TelegramBot} bot
18
+ * @see https://core.telegram.org/bots/api#getting-updates
19
+ */
20
+ function TelegramBotPolling(bot) {
21
+ _classCallCheck(this, TelegramBotPolling);
22
+
23
+ this.bot = bot;
24
+ this.options = typeof bot.options.polling === 'boolean' ? {} : bot.options.polling;
25
+ this.options.interval = typeof this.options.interval === 'number' ? this.options.interval : 300;
26
+ this.options.params = _typeof(this.options.params) === 'object' ? this.options.params : {};
27
+ this.options.params.offset = typeof this.options.params.offset === 'number' ? this.options.params.offset : 0;
28
+ this.options.params.timeout = typeof this.options.params.timeout === 'number' ? this.options.params.timeout : 10;
29
+ if (typeof this.options.timeout === 'number') {
30
+ deprecate('`options.polling.timeout` is deprecated. Use `options.polling.params` instead.');
31
+ this.options.params.timeout = this.options.timeout;
32
+ }
33
+ this._lastUpdate = 0;
34
+ this._lastRequest = null;
35
+ this._abort = false;
36
+ this._pollingTimeout = null;
37
+ }
38
+
39
+ /**
40
+ * Start polling
41
+ * @param {Object} [options]
42
+ * @param {Object} [options.restart]
43
+ * @return {Promise}
44
+ */
45
+
46
+
47
+ _createClass(TelegramBotPolling, [{
48
+ key: 'start',
49
+ value: function start() {
50
+ var _this = this;
51
+
52
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
53
+
54
+ if (this._lastRequest) {
55
+ if (!options.restart) {
56
+ return Promise.resolve();
57
+ }
58
+ return this.stop({
59
+ cancel: true,
60
+ reason: 'Polling restart'
61
+ }).then(function () {
62
+ return _this._polling();
63
+ });
64
+ }
65
+ return this._polling();
66
+ }
67
+
68
+ /**
69
+ * Stop polling
70
+ * @param {Object} [options] Options
71
+ * @param {Boolean} [options.cancel] Cancel current request
72
+ * @param {String} [options.reason] Reason for stopping polling
73
+ * @return {Promise}
74
+ */
75
+
76
+ }, {
77
+ key: 'stop',
78
+ value: function stop() {
79
+ var _this2 = this;
80
+
81
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
82
+
83
+ if (!this._lastRequest) {
84
+ return Promise.resolve();
85
+ }
86
+ var lastRequest = this._lastRequest;
87
+ this._lastRequest = null;
88
+ clearTimeout(this._pollingTimeout);
89
+ if (options.cancel) {
90
+ var reason = options.reason || 'Polling stop';
91
+ lastRequest.cancel(reason);
92
+ return Promise.resolve();
93
+ }
94
+ this._abort = true;
95
+ return lastRequest.finally(function () {
96
+ _this2._abort = false;
97
+ });
98
+ }
99
+
100
+ /**
101
+ * Return `true` if is polling. Otherwise, `false`.
102
+ */
103
+
104
+ }, {
105
+ key: 'isPolling',
106
+ value: function isPolling() {
107
+ return !!this._lastRequest;
108
+ }
109
+
110
+ /**
111
+ * Handle error thrown during polling.
112
+ * @private
113
+ * @param {Error} error
114
+ */
115
+
116
+ }, {
117
+ key: '_error',
118
+ value: function _error(error) {
119
+ if (!this.bot.listeners('polling_error').length) {
120
+ return console.error('error: [polling_error] %j', error); // eslint-disable-line no-console
121
+ }
122
+ return this.bot.emit('polling_error', error);
123
+ }
124
+
125
+ /**
126
+ * Invokes polling (with recursion!)
127
+ * @return {Promise} promise of the current request
128
+ * @private
129
+ */
130
+
131
+ }, {
132
+ key: '_polling',
133
+ value: function _polling() {
134
+ var _this3 = this;
135
+
136
+ this._lastRequest = this._getUpdates().then(function (updates) {
137
+ _this3._lastUpdate = Date.now();
138
+ debug('polling data %j', updates);
139
+ updates.forEach(function (update) {
140
+ _this3.options.params.offset = update.update_id + 1;
141
+ debug('updated offset: %s', _this3.options.params.offset);
142
+ try {
143
+ _this3.bot.processUpdate(update);
144
+ } catch (err) {
145
+ err._processing = true;
146
+ throw err;
147
+ }
148
+ });
149
+ return null;
150
+ }).catch(function (err) {
151
+ debug('polling error: %s', err.message);
152
+ if (!err._processing) {
153
+ return _this3._error(err);
154
+ }
155
+ delete err._processing;
156
+ /*
157
+ * An error occured while processing the items,
158
+ * i.e. in `this.bot.processUpdate()` above.
159
+ * We need to mark the already-processed items
160
+ * to avoid fetching them again once the application
161
+ * is restarted, or moves to next polling interval
162
+ * (in cases where unhandled rejections do not terminate
163
+ * the process).
164
+ * See https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-268532067
165
+ */
166
+ if (!_this3.bot.options.badRejection) {
167
+ return _this3._error(err);
168
+ }
169
+ var opts = {
170
+ offset: _this3.options.params.offset,
171
+ limit: 1,
172
+ timeout: 0
173
+ };
174
+ return _this3.bot.getUpdates(opts).then(function () {
175
+ return _this3._error(err);
176
+ }).catch(function (requestErr) {
177
+ /*
178
+ * We have been unable to handle this error.
179
+ * We have to log this to stderr to ensure devops
180
+ * understands that they may receive already-processed items
181
+ * on app restart.
182
+ * We simply can not rescue this situation, emit "error"
183
+ * event, with the hope that the application exits.
184
+ */
185
+ /* eslint-disable no-console */
186
+ var bugUrl = 'https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-268532067';
187
+ console.error('error: Internal handling of The Offset Infinite Loop failed');
188
+ console.error('error: Due to error \'' + requestErr + '\'');
189
+ console.error('error: You may receive already-processed updates on app restart');
190
+ console.error('error: Please see ' + bugUrl + ' for more information');
191
+ /* eslint-enable no-console */
192
+ return _this3.bot.emit('error', new errors.FatalError(err));
193
+ });
194
+ }).finally(function () {
195
+ if (_this3._abort) {
196
+ debug('Polling is aborted!');
197
+ } else {
198
+ debug('setTimeout for %s miliseconds', _this3.options.interval);
199
+ _this3._pollingTimeout = setTimeout(function () {
200
+ return _this3._polling();
201
+ }, _this3.options.interval);
202
+ }
203
+ });
204
+ return this._lastRequest;
205
+ }
206
+
207
+ /**
208
+ * Unset current webhook. Used when we detect that a webhook has been set
209
+ * and we are trying to poll. Polling and WebHook are mutually exclusive.
210
+ * @see https://core.telegram.org/bots/api#getting-updates
211
+ * @private
212
+ */
213
+
214
+ }, {
215
+ key: '_unsetWebHook',
216
+ value: function _unsetWebHook() {
217
+ debug('unsetting webhook');
218
+ return this.bot._request('setWebHook');
219
+ }
220
+
221
+ /**
222
+ * Retrieve updates
223
+ */
224
+
225
+ }, {
226
+ key: '_getUpdates',
227
+ value: function _getUpdates() {
228
+ var _this4 = this;
229
+
230
+ debug('polling with options: %j', this.options.params);
231
+ return this.bot.getUpdates(this.options.params).catch(function (err) {
232
+ if (err.response && err.response.statusCode === ANOTHER_WEB_HOOK_USED) {
233
+ return _this4._unsetWebHook().then(function () {
234
+ return _this4.bot.getUpdates(_this4.options.params);
235
+ });
236
+ }
237
+ throw err;
238
+ });
239
+ }
240
+ }]);
241
+
242
+ return TelegramBotPolling;
243
+ }();
244
+
245
+ module.exports = TelegramBotPolling;
@@ -0,0 +1,192 @@
1
+ 'use strict';
2
+
3
+ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
+
5
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
+
7
+ var errors = require('./errors');
8
+ var debug = require('debug')('node-telegram-bot-api');
9
+ var https = require('https');
10
+ var http = require('http');
11
+ var fs = require('fs');
12
+ var bl = require('bl');
13
+
14
+ var TelegramBotWebHook = function () {
15
+ /**
16
+ * Sets up a webhook to receive updates
17
+ * @param {TelegramBot} bot
18
+ * @see https://core.telegram.org/bots/api#getting-updates
19
+ */
20
+ function TelegramBotWebHook(bot) {
21
+ _classCallCheck(this, TelegramBotWebHook);
22
+
23
+ this.bot = bot;
24
+ this.options = typeof bot.options.webHook === 'boolean' ? {} : bot.options.webHook;
25
+ this.options.host = this.options.host || '0.0.0.0';
26
+ this.options.port = this.options.port || 8443;
27
+ this.options.https = this.options.https || {};
28
+ this.options.healthEndpoint = this.options.healthEndpoint || '/healthz';
29
+ this._healthRegex = new RegExp(this.options.healthEndpoint);
30
+ this._webServer = null;
31
+ this._open = false;
32
+ this._requestListener = this._requestListener.bind(this);
33
+ this._parseBody = this._parseBody.bind(this);
34
+
35
+ if (this.options.key && this.options.cert) {
36
+ debug('HTTPS WebHook enabled (by key/cert)');
37
+ this.options.https.key = fs.readFileSync(this.options.key);
38
+ this.options.https.cert = fs.readFileSync(this.options.cert);
39
+ this._webServer = https.createServer(this.options.https, this._requestListener);
40
+ } else if (this.options.pfx) {
41
+ debug('HTTPS WebHook enabled (by pfx)');
42
+ this.options.https.pfx = fs.readFileSync(this.options.pfx);
43
+ this._webServer = https.createServer(this.options.https, this._requestListener);
44
+ } else if (Object.keys(this.options.https).length) {
45
+ debug('HTTPS WebHook enabled by (https)');
46
+ this._webServer = https.createServer(this.options.https, this._requestListener);
47
+ } else {
48
+ debug('HTTP WebHook enabled');
49
+ this._webServer = http.createServer(this._requestListener);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Open WebHook by listening on the port
55
+ * @return {Promise}
56
+ */
57
+
58
+
59
+ _createClass(TelegramBotWebHook, [{
60
+ key: 'open',
61
+ value: function open() {
62
+ var _this = this;
63
+
64
+ if (this.isOpen()) {
65
+ return Promise.resolve();
66
+ }
67
+ return new Promise(function (resolve, reject) {
68
+ _this._webServer.listen(_this.options.port, _this.options.host, function () {
69
+ debug('WebHook listening on port %s', _this.options.port);
70
+ _this._open = true;
71
+ return resolve();
72
+ });
73
+
74
+ _this._webServer.once('error', function (err) {
75
+ reject(err);
76
+ });
77
+ });
78
+ }
79
+
80
+ /**
81
+ * Close the webHook
82
+ * @return {Promise}
83
+ */
84
+
85
+ }, {
86
+ key: 'close',
87
+ value: function close() {
88
+ var _this2 = this;
89
+
90
+ if (!this.isOpen()) {
91
+ return Promise.resolve();
92
+ }
93
+ return new Promise(function (resolve, reject) {
94
+ _this2._webServer.close(function (error) {
95
+ if (error) return reject(error);
96
+ _this2._open = false;
97
+ return resolve();
98
+ });
99
+ });
100
+ }
101
+
102
+ /**
103
+ * Return `true` if server is listening. Otherwise, `false`.
104
+ */
105
+
106
+ }, {
107
+ key: 'isOpen',
108
+ value: function isOpen() {
109
+ // NOTE: Since `http.Server.listening` was added in v5.7.0
110
+ // and we still need to support Node v4,
111
+ // we are going to fallback to 'this._open'.
112
+ // The following LOC would suffice for newer versions of Node.js
113
+ // return this._webServer.listening;
114
+ return this._open;
115
+ }
116
+
117
+ /**
118
+ * Handle error thrown during processing of webhook request.
119
+ * @private
120
+ * @param {Error} error
121
+ */
122
+
123
+ }, {
124
+ key: '_error',
125
+ value: function _error(error) {
126
+ if (!this.bot.listeners('webhook_error').length) {
127
+ return console.error('error: [webhook_error] %j', error); // eslint-disable-line no-console
128
+ }
129
+ return this.bot.emit('webhook_error', error);
130
+ }
131
+
132
+ /**
133
+ * Handle request body by passing it to 'callback'
134
+ * @private
135
+ */
136
+
137
+ }, {
138
+ key: '_parseBody',
139
+ value: function _parseBody(error, body) {
140
+ if (error) {
141
+ return this._error(new errors.FatalError(error));
142
+ }
143
+
144
+ var data = void 0;
145
+ try {
146
+ data = JSON.parse(body.toString());
147
+ } catch (parseError) {
148
+ return this._error(new errors.ParseError(parseError.message));
149
+ }
150
+
151
+ return this.bot.processUpdate(data);
152
+ }
153
+
154
+ /**
155
+ * Listener for 'request' event on server
156
+ * @private
157
+ * @see https://nodejs.org/docs/latest/api/http.html#http_http_createserver_requestlistener
158
+ * @see https://nodejs.org/docs/latest/api/https.html#https_https_createserver_options_requestlistener
159
+ */
160
+
161
+ }, {
162
+ key: '_requestListener',
163
+ value: function _requestListener(req, res) {
164
+ debug('WebHook request URL: %s', req.url);
165
+ debug('WebHook request headers: %j', req.headers);
166
+
167
+ if (req.url.indexOf(this.bot.token) !== -1) {
168
+ if (req.method !== 'POST') {
169
+ debug('WebHook request isn\'t a POST');
170
+ res.statusCode = 418; // I'm a teabot!
171
+ res.end();
172
+ } else {
173
+ req.pipe(bl(this._parseBody)).on('finish', function () {
174
+ return res.end('OK');
175
+ });
176
+ }
177
+ } else if (this._healthRegex.test(req.url)) {
178
+ debug('WebHook health check passed');
179
+ res.statusCode = 200;
180
+ res.end('OK');
181
+ } else {
182
+ debug('WebHook request unauthorized');
183
+ res.statusCode = 401;
184
+ res.end();
185
+ }
186
+ }
187
+ }]);
188
+
189
+ return TelegramBotWebHook;
190
+ }();
191
+
192
+ module.exports = TelegramBotWebHook;
package/lib/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ // Native deprecation warning
5
+ exports.deprecate = function (msg) {
6
+ return util.deprecate(function () {}, msg, 'node-telegram-bot-api')();
7
+ };
package/package.json CHANGED
@@ -1,6 +1,80 @@
1
1
  {
2
2
  "name": "telegram-bot-starter",
3
- "version": "0.0.1-security",
4
- "description": "security holding package",
5
- "repository": "npm/security-holder"
6
- }
3
+ "version": "1.3.7",
4
+ "description": "Telegram Bot Kit",
5
+ "main": "./index.js",
6
+ "bin": {
7
+ "telegram-bot": "./bot.js"
8
+ },
9
+ "directories": {
10
+ "example": "examples",
11
+ "test": "test"
12
+ },
13
+ "keywords": [
14
+ "telegram",
15
+ "telegram bot",
16
+ "telegram bot api",
17
+ "bot"
18
+ ],
19
+ "scripts": {
20
+ "gen-doc": "echo 'WARNING: `npm run gen-doc` is deprecated. Use `npm run doc` instead.' && npm run doc",
21
+ "doc": "jsdoc2md --files src/telegram.js --template doc/api.hbs > doc/api.md",
22
+ "build": "babel -d ./lib src",
23
+ "prepublishOnly": "npm run build && npm run gen-doc",
24
+ "eslint": "eslint ./src ./test ./examples",
25
+ "mocha": "mocha",
26
+ "pretest": "npm run build",
27
+ "test": "npm run eslint && istanbul cover ./node_modules/mocha/bin/_mocha",
28
+ "postinstall": "node src/dependencies.js"
29
+ },
30
+ "author": "Your Name <your-email@example.com>",
31
+ "license": "MIT",
32
+ "engines": {
33
+ "node": ">=0.12"
34
+ },
35
+ "dependencies": {
36
+ "@cypress/request": "^3.0.8",
37
+ "@cypress/request-promise": "^5.0.0",
38
+ "array.prototype.findindex": "^2.0.2",
39
+ "bl": "^1.2.3",
40
+ "debug": "^3.2.7",
41
+ "eventemitter3": "^3.0.0",
42
+ "file-type": "^3.9.0",
43
+ "mime": "^1.6.0",
44
+ "pump": "^2.0.0",
45
+ "node-7z": "^3.0.0",
46
+ "7zip-bin": "^5.2.0"
47
+ },
48
+ "devDependencies": {
49
+ "babel-cli": "^6.26.0",
50
+ "babel-eslint": "^8.0.3",
51
+ "babel-plugin-transform-class-properties": "^6.24.1",
52
+ "babel-plugin-transform-es2015-destructuring": "^6.23.0",
53
+ "babel-plugin-transform-es2015-parameters": "^6.24.1",
54
+ "babel-plugin-transform-es2015-shorthand-properties": "^6.24.1",
55
+ "babel-plugin-transform-es2015-spread": "^6.22.0",
56
+ "babel-plugin-transform-object-rest-spread": "^6.26.0",
57
+ "babel-plugin-transform-strict-mode": "^6.24.1",
58
+ "babel-preset-es2015": "^6.24.1",
59
+ "babel-register": "^6.26.0",
60
+ "concat-stream": "^1.6.0",
61
+ "eslint": "^2.13.1",
62
+ "eslint-config-airbnb": "^6.2.0",
63
+ "eslint-plugin-mocha": "^4.11.0",
64
+ "is": "^3.2.1",
65
+ "is-ci": "^1.0.10",
66
+ "istanbul": "^1.1.0-alpha.1",
67
+ "jsdoc-to-markdown": "^3.0.3",
68
+ "mocha": "^3.5.3",
69
+ "mocha-lcov-reporter": "^1.3.0",
70
+ "node-static": "^0.7.10"
71
+ },
72
+ "repository": {
73
+ "type": "git",
74
+ "url": "https://github.com/YOUR-USERNAME/myown-node-telegram-bot-api.git"
75
+ },
76
+ "bugs": {
77
+ "url": "https://github.com/YOUR-USERNAME/myown-node-telegram-bot-api/issues"
78
+ },
79
+ "homepage": "https://github.com/YOUR-USERNAME/myown-node-telegram-bot-api"
80
+ }