Bug Fixes
This commit is contained in:
1 parent
82bc086e92
commit
b1af6e078c
131 files changed
+8597
-1930
No files matched your search
+42
-18
@@ -5,14 +5,17 @@ const Composer = require('./composer')
|
||||
const Markup = require('./markup')
|
||||
const session = require('./session')
|
||||
const Router = require('./router')
|
||||
const Context = require('./core/context')
|
||||
const Stage = require('./stage')
|
||||
const BaseScene = require('./scenes/base')
|
||||
const Context = require('./context')
|
||||
const generateCallback = require('./core/network/webhook')
|
||||
const crypto = require('crypto')
|
||||
const { URL } = require('url')
|
||||
|
||||
const DefaultOptions = {
|
||||
const DEFAULT_OPTIONS = {
|
||||
retryAfter: 1,
|
||||
handlerTimeout: 0
|
||||
handlerTimeout: 0,
|
||||
contextType: Context
|
||||
}
|
||||
|
||||
const noop = () => { }
|
||||
@@ -20,7 +23,10 @@ const noop = () => { }
|
||||
class Telegraf extends Composer {
|
||||
constructor (token, options) {
|
||||
super()
|
||||
this.options = Object.assign({}, DefaultOptions, options)
|
||||
this.options = {
|
||||
...DEFAULT_OPTIONS,
|
||||
...options
|
||||
}
|
||||
this.token = token
|
||||
this.handleError = (err) => {
|
||||
console.error()
|
||||
@@ -92,6 +98,7 @@ class Telegraf extends Composer {
|
||||
}
|
||||
|
||||
launch (config = {}) {
|
||||
debug('Connecting to Telegram')
|
||||
return this.telegram.getMe()
|
||||
.then((botInfo) => {
|
||||
debug(`Launching @${botInfo.username}`)
|
||||
@@ -101,7 +108,7 @@ class Telegraf extends Composer {
|
||||
const { timeout, limit, allowedUpdates, stopCallback } = config.polling || {}
|
||||
return this.telegram.deleteWebhook()
|
||||
.then(() => this.startPolling(timeout, limit, allowedUpdates, stopCallback))
|
||||
.then(() => debug(`Bot started with long-polling`))
|
||||
.then(() => debug('Bot started with long-polling'))
|
||||
}
|
||||
if (typeof config.webhook.domain !== 'string' && typeof config.webhook.hookPath !== 'string') {
|
||||
throw new Error('Webhook domain or webhook path is required')
|
||||
@@ -114,23 +121,31 @@ class Telegraf extends Composer {
|
||||
const { port, host, tlsOptions, cb } = config.webhook
|
||||
this.startWebhook(hookPath, tlsOptions, port, host, cb)
|
||||
if (!domain) {
|
||||
debug(`Bot started with webhook`)
|
||||
debug('Bot started with webhook')
|
||||
return
|
||||
}
|
||||
return this.telegram
|
||||
.setWebhook(`https://${domain}${hookPath}`)
|
||||
.then(() => debug(`Bot started with webhook @ https://${domain}`))
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Launch failed')
|
||||
console.error(err.stack || err.toString())
|
||||
})
|
||||
}
|
||||
|
||||
stop (cb = noop) {
|
||||
this.polling.started = false
|
||||
if (this.webhookServer) {
|
||||
this.webhookServer.close(cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
return this
|
||||
debug('Stopping bot...')
|
||||
return new Promise((resolve) => {
|
||||
const done = () => resolve() & cb()
|
||||
if (this.webhookServer) {
|
||||
return this.webhookServer.close(done)
|
||||
} else if (!this.polling.started) {
|
||||
return done()
|
||||
}
|
||||
this.polling.stopCallback = done
|
||||
this.polling.started = false
|
||||
})
|
||||
}
|
||||
|
||||
handleUpdates (updates) {
|
||||
@@ -150,16 +165,18 @@ class Telegraf extends Composer {
|
||||
handleUpdate (update, webhookResponse) {
|
||||
debug('Processing update', update.update_id)
|
||||
const tg = new Telegram(this.token, this.telegram.options, webhookResponse)
|
||||
const ctx = new Context(update, tg, this.options)
|
||||
const TelegrafContext = this.options.contextType
|
||||
const ctx = new TelegrafContext(update, tg, this.options)
|
||||
Object.assign(ctx, this.context)
|
||||
return this.middleware()(ctx).catch(this.handleError)
|
||||
return this.middleware()(ctx).catch((err) => this.handleError(err, ctx))
|
||||
}
|
||||
|
||||
fetchUpdates () {
|
||||
const { timeout, limit, offset, started, allowedUpdates } = this.polling
|
||||
if (!started) {
|
||||
if (!this.polling.started) {
|
||||
this.polling.stopCallback && this.polling.stopCallback()
|
||||
return
|
||||
}
|
||||
const { timeout, limit, offset, allowedUpdates } = this.polling
|
||||
this.telegram.getUpdates(timeout, limit, offset, allowedUpdates)
|
||||
.catch((err) => {
|
||||
if (err.code === 401 || err.code === 409) {
|
||||
@@ -169,7 +186,10 @@ class Telegraf extends Composer {
|
||||
console.error(`Failed to fetch updates. Waiting: ${wait}s`, err.message)
|
||||
return new Promise((resolve) => setTimeout(resolve, wait * 1000, []))
|
||||
})
|
||||
.then((updates) => this.handleUpdates(updates).then(() => updates))
|
||||
.then((updates) => this.polling.started
|
||||
? this.handleUpdates(updates).then(() => updates)
|
||||
: []
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error('Failed to process updates.', err)
|
||||
this.polling.started = false
|
||||
@@ -187,6 +207,7 @@ class Telegraf extends Composer {
|
||||
}
|
||||
|
||||
module.exports = Object.assign(Telegraf, {
|
||||
Context,
|
||||
Composer,
|
||||
Extra,
|
||||
Markup,
|
||||
@@ -196,10 +217,13 @@ module.exports = Object.assign(Telegraf, {
|
||||
})
|
||||
|
||||
module.exports.default = Object.assign(Telegraf, {
|
||||
Context,
|
||||
Composer,
|
||||
Extra,
|
||||
Markup,
|
||||
Router,
|
||||
Telegram,
|
||||
Stage,
|
||||
BaseScene,
|
||||
session
|
||||
})
|
||||
Reference in new issue
Block a user