mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-23 22:52:59 +03:00
refactor(): extract bot instace factory
This commit is contained in:
parent
34070fcf68
commit
d48123ea38
@ -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<any> =>
|
||||
await defer(async () => {
|
||||
const bot = new Telegraf<any>(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<any> => {
|
||||
const { botName, ...telegrafOptions } = telegrafModuleOptions;
|
||||
|
||||
return await defer(async () => {
|
||||
const bot = new Telegraf<any>(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<void> {
|
||||
const bot = this.moduleRef.get<any>(this.botName);
|
||||
bot && (await bot.stop());
|
||||
}
|
||||
|
||||
private static createAsyncProviders(
|
||||
@ -158,9 +135,4 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||
inject,
|
||||
};
|
||||
}
|
||||
|
||||
async onApplicationShutdown(): Promise<void> {
|
||||
const bot = this.moduleRef.get<any>(this.botName);
|
||||
bot && (await bot.stop());
|
||||
}
|
||||
}
|
||||
|
13
lib/utils/create-bot-factory.util.ts
Normal file
13
lib/utils/create-bot-factory.util.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafModuleOptions } from '../interfaces';
|
||||
|
||||
export async function createBotFactory(
|
||||
options: TelegrafModuleOptions,
|
||||
): Promise<Telegraf<never>> {
|
||||
const bot = new Telegraf<never>(options.token, options.options);
|
||||
|
||||
bot.use(...(options.middlewares ?? []));
|
||||
await bot.launch(options.launchOptions);
|
||||
|
||||
return bot;
|
||||
}
|
Loading…
Reference in New Issue
Block a user