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('depd')('node-telegram-bot-api');
|
12 | var ANOTHER_WEB_HOOK_USED = 409;
|
13 |
|
14 | var TelegramBotPolling = function () {
|
15 | |
16 |
|
17 |
|
18 |
|
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 |
|
41 |
|
42 |
|
43 |
|
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 |
|
70 |
|
71 |
|
72 |
|
73 |
|
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 |
|
102 |
|
103 |
|
104 | }, {
|
105 | key: 'isPolling',
|
106 | value: function isPolling() {
|
107 | return !!this._lastRequest;
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
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);
|
121 | }
|
122 | return this.bot.emit('polling_error', error);
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
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 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
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 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
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 |
|
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 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | }, {
|
215 | key: '_unsetWebHook',
|
216 | value: function _unsetWebHook() {
|
217 | debug('unsetting webhook');
|
218 | return this.bot._request('setWebHook');
|
219 | }
|
220 |
|
221 | |
222 |
|
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; |
\ | No newline at end of file |