Comparing version 2.2.0 to 2.2.1
@@ -7,3 +7,3 @@ const Telegram = require('./telegram') | ||
constructor (token, update, options) { | ||
this.telegramInstance = new Telegram(token, options) | ||
this.telegramInstance = new Telegram(token, options.telegram) | ||
this.update = update | ||
@@ -10,0 +10,0 @@ this.contextState = {} |
@@ -18,6 +18,5 @@ const debug = require('debug')('telegraf:core') | ||
constructor (token, options) { | ||
const opts = Object.assign({webHookAnswer: true}, options) | ||
this.token = token | ||
this.telegram = new Telegram(token, opts.telegram) | ||
this.options = opts | ||
this.options = Object.assign({}, options) | ||
this.telegram = new Telegram(token, this.options.telegram) | ||
this.middlewares = [] | ||
@@ -50,9 +49,11 @@ this.context = {} | ||
* @param {number} limit | ||
* @param {concurrency} concurrency | ||
* @return {Telegraf} self | ||
* @api public | ||
*/ | ||
startPolling (timeout, limit) { | ||
startPolling (timeout, limit, concurrency) { | ||
this.polling.started = true | ||
this.polling.timeout = timeout || 0 | ||
this.polling.limit = limit || 100 | ||
this.polling.concurrency = concurrency || 1 | ||
this.pollingLoop() | ||
@@ -90,3 +91,2 @@ return this | ||
if (!res.finished) { | ||
res.writeHead(200) | ||
res.end() | ||
@@ -101,5 +101,5 @@ } | ||
} catch (error) { | ||
this.onError(error) | ||
res.writeHead(415) | ||
res.end() | ||
this.onError(error) | ||
} | ||
@@ -217,3 +217,5 @@ }) | ||
debug('⚡ update', update.update_id) | ||
const ctx = new TelegrafContext(this.token, update, {webHookResponse: webHookResponse}) | ||
const ctx = new TelegrafContext(this.token, update, { | ||
telegram: Object.assign({webHookResponse: webHookResponse}, this.options.telegram) | ||
}) | ||
for (let key in this.context) { | ||
@@ -223,3 +225,3 @@ ctx[key] = this.context[key] | ||
const middleware = Telegraf.compose(this.middlewares) | ||
return middleware(ctx).catch(this.onError) | ||
return middleware(ctx).catch(this.onError).then(() => Promise.resolve(update)) | ||
} | ||
@@ -236,5 +238,6 @@ | ||
} | ||
var lastUpdateId = 0 | ||
this.telegram.getUpdates(this.polling.timeout, this.polling.limit, this.polling.offset) | ||
.catch((err) => { | ||
console.error('Telegraf: network error', err) | ||
console.error('Telegraf: network error', err.message) | ||
return new Promise((resolve) => { | ||
@@ -244,12 +247,23 @@ setTimeout(() => resolve([]), 1000) | ||
}) | ||
.map((update) => { | ||
return this.handleUpdate(update).then(() => { | ||
.then((updates) => { | ||
if (updates.length > 0) { | ||
lastUpdateId = updates[updates.length - 1].update_id + 1 | ||
} | ||
return Promise.resolve(updates) | ||
}) | ||
.map((update) => this.handleUpdate(update) | ||
.then((update) => { | ||
this.polling.offset = update.update_id + 1 | ||
}) | ||
}, {concurrency: 1}) | ||
.then(() => this.pollingLoop()) | ||
}), {concurrency: this.polling.concurrency}) | ||
.catch((err) => { | ||
console.error('Telegraf: polling error', err) | ||
this.polling.started = false | ||
console.error('Telegraf: polling error', err.message) | ||
if (this.polling.concurrency === 1) { | ||
this.polling.started = false | ||
} else { | ||
this.polling.offset = lastUpdateId | ||
} | ||
}) | ||
.then(() => { | ||
this.pollingLoop() | ||
}) | ||
} | ||
@@ -256,0 +270,0 @@ } |
@@ -561,7 +561,8 @@ const debug = require('debug')('telegraf:api') | ||
const webHookResponse = this.options.webHookResponse | ||
if (webHookResponse && !webHookResponse.finished && !isMultipart && platform.webHookAnswerBlacklist.indexOf(method) === -1) { | ||
debug('▷ webhook', method) | ||
options.method = method | ||
webHookResponse.setHeader('Content-Type', 'application/json') | ||
if (!webHookResponse.headersSent) { | ||
webHookResponse.setHeader('Content-Type', 'application/json') | ||
} | ||
webHookResponse.end(JSON.stringify(options)) | ||
@@ -583,6 +584,3 @@ return Promise.resolve({ | ||
return buildPayload | ||
.then((payload) => { | ||
debug(payload) | ||
return fetch(`${this.options.apiRoot}/bot${this.token}/${method}`, payload) | ||
}) | ||
.then((payload) => fetch(`${this.options.apiRoot}/bot${this.token}/${method}`, payload)) | ||
.then((res) => res.json()) | ||
@@ -589,0 +587,0 @@ .then((data) => { |
{ | ||
"name": "telegraf", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "📢 Modern Telegram bot framework", | ||
@@ -5,0 +5,0 @@ "main": "lib/telegraf.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71671
1388