Skip to content

Commit 7c2bdf5

Browse files
committed
Refactor
1 parent 480432f commit 7c2bdf5

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed

src/bot/context.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import type { AutoChatActionFlavor } from '@grammyjs/auto-chat-action'
44
import type { HydrateFlavor } from '@grammyjs/hydrate'
55
import type { I18nFlavor } from '@grammyjs/i18n'
66
import type { ParseModeFlavor } from '@grammyjs/parse-mode'
7-
import type { Update, UserFromGetMe } from '@grammyjs/types'
8-
import type { Api, SessionFlavor } from 'grammy'
9-
import { Context as DefaultContext } from 'grammy'
7+
import type { Context as DefaultContext, SessionFlavor } from 'grammy'
108

119
export interface SessionData {
1210
// field?: string;
@@ -26,29 +24,3 @@ export type Context = ParseModeFlavor<
2624
AutoChatActionFlavor
2725
>
2826
>
29-
30-
interface Dependencies {
31-
logger: Logger
32-
config: Config
33-
}
34-
35-
export function createContextConstructor(
36-
{
37-
logger,
38-
config,
39-
}: Dependencies,
40-
) {
41-
return class extends DefaultContext implements ExtendedContextFlavor {
42-
logger: Logger
43-
config: Config
44-
45-
constructor(update: Update, api: Api, me: UserFromGetMe) {
46-
super(update, api, me)
47-
48-
this.logger = logger.child({
49-
update_id: this.update.update_id,
50-
})
51-
this.config = config
52-
}
53-
} as unknown as new (update: Update, api: Api, me: UserFromGetMe) => Context
54-
}

src/bot/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { Context, SessionData } from '#root/bot/context.js'
1+
import type { Context } from '#root/bot/context.js'
22
import type { Config } from '#root/config.js'
33
import type { Logger } from '#root/logger.js'
4-
import type { BotConfig, StorageAdapter } from 'grammy'
5-
import { createContextConstructor } from '#root/bot/context.js'
4+
import type { BotConfig } from 'grammy'
65
import { adminFeature } from '#root/bot/features/admin.js'
76
import { languageFeature } from '#root/bot/features/language.js'
87
import { unhandledFeature } from '#root/bot/features/unhandled.js'
@@ -15,35 +14,34 @@ import { autoChatAction } from '@grammyjs/auto-chat-action'
1514
import { hydrate } from '@grammyjs/hydrate'
1615
import { hydrateReply, parseMode } from '@grammyjs/parse-mode'
1716
import { sequentialize } from '@grammyjs/runner'
18-
import { Bot as TelegramBot } from 'grammy'
17+
import { MemorySessionStorage, Bot as TelegramBot } from 'grammy'
1918

2019
interface Dependencies {
2120
config: Config
2221
logger: Logger
2322
}
2423

25-
interface Options {
26-
botSessionStorage?: StorageAdapter<SessionData>
27-
botConfig?: Omit<BotConfig<Context>, 'ContextConstructor'>
28-
}
29-
3024
function getSessionKey(ctx: Omit<Context, 'session'>) {
3125
return ctx.chat?.id.toString()
3226
}
3327

34-
export function createBot(token: string, dependencies: Dependencies, options: Options = {}) {
28+
export function createBot(token: string, dependencies: Dependencies, botConfig?: BotConfig<Context>) {
3529
const {
3630
config,
3731
logger,
3832
} = dependencies
3933

40-
const bot = new TelegramBot(token, {
41-
...options.botConfig,
42-
ContextConstructor: createContextConstructor({
43-
logger,
44-
config,
45-
}),
34+
const bot = new TelegramBot<Context>(token, botConfig)
35+
36+
bot.use(async (ctx, next) => {
37+
ctx.config = config
38+
ctx.logger = logger.child({
39+
update_id: ctx.update.update_id,
40+
})
41+
42+
await next()
4643
})
44+
4745
const protectedBot = bot.errorBoundary(errorHandler)
4846

4947
// Middlewares
@@ -56,7 +54,10 @@ export function createBot(token: string, dependencies: Dependencies, options: Op
5654
protectedBot.use(autoChatAction(bot.api))
5755
protectedBot.use(hydrateReply)
5856
protectedBot.use(hydrate())
59-
protectedBot.use(session({ getSessionKey, storage: options.botSessionStorage }))
57+
protectedBot.use(session({
58+
getSessionKey,
59+
storage: new MemorySessionStorage(),
60+
}))
6061
protectedBot.use(i18n)
6162

6263
// Handlers

0 commit comments

Comments
 (0)