diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index f33d5cc..1e8969e 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -7,7 +7,6 @@ import { Global, Inject, OnApplicationShutdown, - Logger, } from '@nestjs/common'; import { TelegrafModuleOptions, @@ -24,8 +23,7 @@ import { UpdatesExplorerService, } from './services'; import { getBotToken } from './utils'; -import { Telegraf } from 'telegraf'; -import { defer } from 'rxjs'; +import { createBotFactory } from './utils/create-bot-factory.util'; @Global() @Module({ @@ -37,8 +35,6 @@ import { defer } from 'rxjs'; ], }) export class TelegrafCoreModule implements OnApplicationShutdown { - private static logger = new Logger(TelegrafCoreModule.name); - constructor( @Inject(TELEGRAF_BOT_NAME) private readonly botName: string, private readonly moduleRef: ModuleRef, @@ -47,15 +43,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown { public static forRoot(options: TelegrafModuleOptions): DynamicModule { const telegrafBotName = getBotToken(options.botName); - const telegrafBotProvider = { + const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async (): Promise => - await defer(async () => { - const bot = new Telegraf(options.token); - this.applyBotMiddlewares(bot, options.middlewares); - await bot.launch(options.launchOptions); - return bot; - }).toPromise(), + useFactory: async () => await createBotFactory(options), }; return { @@ -80,20 +70,10 @@ export class TelegrafCoreModule implements OnApplicationShutdown { ): DynamicModule { const telegrafBotName = getBotToken(options.botName); - const telegrafBotProvider = { + const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async ( - telegrafModuleOptions: TelegrafModuleOptions, - ): Promise => { - const { botName, ...telegrafOptions } = telegrafModuleOptions; - - return await defer(async () => { - const bot = new Telegraf(telegrafOptions.token); - this.applyBotMiddlewares(bot, telegrafOptions.middlewares); - await bot.launch(telegrafOptions.launchOptions); - return bot; - }).toPromise(); - }, + useFactory: async (options: TelegrafModuleOptions) => + await createBotFactory(options), inject: [TELEGRAF_MODULE_OPTIONS], }; @@ -113,12 +93,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown { }; } - private static applyBotMiddlewares(bot, middlewares) { - if (middlewares) { - middlewares.forEach((middleware) => { - bot.use(middleware); - }); - } + async onApplicationShutdown(): Promise { + const bot = this.moduleRef.get(this.botName); + bot && (await bot.stop()); } private static createAsyncProviders( @@ -158,9 +135,4 @@ export class TelegrafCoreModule implements OnApplicationShutdown { inject, }; } - - async onApplicationShutdown(): Promise { - const bot = this.moduleRef.get(this.botName); - bot && (await bot.stop()); - } } diff --git a/lib/utils/create-bot-factory.util.ts b/lib/utils/create-bot-factory.util.ts new file mode 100644 index 0000000..ea09caa --- /dev/null +++ b/lib/utils/create-bot-factory.util.ts @@ -0,0 +1,13 @@ +import { Telegraf } from 'telegraf'; +import { TelegrafModuleOptions } from '../interfaces'; + +export async function createBotFactory( + options: TelegrafModuleOptions, +): Promise> { + const bot = new Telegraf(options.token, options.options); + + bot.use(...(options.middlewares ?? [])); + await bot.launch(options.launchOptions); + + return bot; +}