Initial commit
This commit is contained in:
13
src/bot/middlewares/session.ts
Normal file
13
src/bot/middlewares/session.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Context, SessionData } from '#root/bot/context.js'
|
||||
import type { Middleware, SessionOptions } from 'grammy'
|
||||
import { session as createSession } from 'grammy'
|
||||
|
||||
type Options = Pick<SessionOptions<SessionData, Context>, 'getSessionKey' | 'storage'>
|
||||
|
||||
export function session(options: Options): Middleware<Context> {
|
||||
return createSession({
|
||||
getSessionKey: options.getSessionKey,
|
||||
storage: options.storage,
|
||||
initial: () => ({}),
|
||||
})
|
||||
}
|
||||
35
src/bot/middlewares/update-logger.ts
Normal file
35
src/bot/middlewares/update-logger.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { Context } from '#root/bot/context.js'
|
||||
import type { Middleware } from 'grammy'
|
||||
import { performance } from 'node:perf_hooks'
|
||||
import { getUpdateInfo } from '#root/bot/helpers/logging.js'
|
||||
|
||||
export function updateLogger(): Middleware<Context> {
|
||||
return async (ctx, next) => {
|
||||
ctx.api.config.use((previous, method, payload, signal) => {
|
||||
ctx.logger.debug({
|
||||
msg: 'Bot API call',
|
||||
method,
|
||||
payload,
|
||||
})
|
||||
|
||||
return previous(method, payload, signal)
|
||||
})
|
||||
|
||||
ctx.logger.debug({
|
||||
msg: 'Update received',
|
||||
update: getUpdateInfo(ctx),
|
||||
})
|
||||
|
||||
const startTime = performance.now()
|
||||
try {
|
||||
await next()
|
||||
}
|
||||
finally {
|
||||
const endTime = performance.now()
|
||||
ctx.logger.debug({
|
||||
msg: 'Update processed',
|
||||
elapsed: endTime - startTime,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user