Skip to content

Commit

Permalink
Wrap session plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
deptyped committed Jul 23, 2024
1 parent 30b7522 commit fdaab0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { autoChatAction } from '@grammyjs/auto-chat-action'
import { hydrate } from '@grammyjs/hydrate'
import { hydrateReply, parseMode } from '@grammyjs/parse-mode'
import type { BotConfig, StorageAdapter } from 'grammy'
import { Bot as TelegramBot, session } from 'grammy'
import { Bot as TelegramBot } from 'grammy'
import { sequentialize } from '@grammyjs/runner'
import { welcomeFeature } from './features/welcome.js'
import { adminFeature } from './features/admin.js'
import { languageFeature } from './features/language.js'
import { unhandledFeature } from './features/unhandled.js'
import { errorHandler } from './handlers/error.js'
import { updateLogger } from './middlewares/update-logger.js'
import { session } from './middlewares/session.js'
import type { Context, SessionData } from '#root/bot/context.js'
import { createContextConstructor } from '#root/bot/context.js'
import { i18n, isMultipleLocales } from '#root/bot/i18n.js'
Expand Down Expand Up @@ -53,14 +54,7 @@ export function createBot(token: string, dependencies: Dependencies, options: Op
protectedBot.use(autoChatAction(bot.api))
protectedBot.use(hydrateReply)
protectedBot.use(hydrate())
protectedBot.filter(
ctx => getSessionKey(ctx) !== 'undefined',
session({
getSessionKey,
storage: options.botSessionStorage,
initial: () => ({}),
}),
)
protectedBot.use(session({ getSessionKey, storage: options.botSessionStorage }))
protectedBot.use(i18n)

// Handlers
Expand Down
12 changes: 12 additions & 0 deletions src/bot/middlewares/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type Middleware, type SessionOptions, session as createSession } from 'grammy'
import type { Context, SessionData } from '#root/bot/context.js'

type Options = Pick<SessionOptions<SessionData, Context>, 'getSessionKey' | 'storage'>

export function session(options: Options): Middleware<Context> {
return createSession({
getSessionKey: options.getSessionKey,
storage: options.storage,
initial: () => ({}),
})
}

0 comments on commit fdaab0e

Please sign in to comment.