mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-25 15:34:38 +03:00
refactor(): extract bot instace factory
This commit is contained in:
parent
34070fcf68
commit
d48123ea38
@ -7,7 +7,6 @@ import {
|
|||||||
Global,
|
Global,
|
||||||
Inject,
|
Inject,
|
||||||
OnApplicationShutdown,
|
OnApplicationShutdown,
|
||||||
Logger,
|
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
TelegrafModuleOptions,
|
TelegrafModuleOptions,
|
||||||
@ -24,8 +23,7 @@ import {
|
|||||||
UpdatesExplorerService,
|
UpdatesExplorerService,
|
||||||
} from './services';
|
} from './services';
|
||||||
import { getBotToken } from './utils';
|
import { getBotToken } from './utils';
|
||||||
import { Telegraf } from 'telegraf';
|
import { createBotFactory } from './utils/create-bot-factory.util';
|
||||||
import { defer } from 'rxjs';
|
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
@ -37,8 +35,6 @@ import { defer } from 'rxjs';
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class TelegrafCoreModule implements OnApplicationShutdown {
|
export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||||
private static logger = new Logger(TelegrafCoreModule.name);
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
|
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
|
||||||
private readonly moduleRef: ModuleRef,
|
private readonly moduleRef: ModuleRef,
|
||||||
@ -47,15 +43,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
||||||
const telegrafBotName = getBotToken(options.botName);
|
const telegrafBotName = getBotToken(options.botName);
|
||||||
|
|
||||||
const telegrafBotProvider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async (): Promise<any> =>
|
useFactory: async () => await createBotFactory(options),
|
||||||
await defer(async () => {
|
|
||||||
const bot = new Telegraf<any>(options.token);
|
|
||||||
this.applyBotMiddlewares(bot, options.middlewares);
|
|
||||||
await bot.launch(options.launchOptions);
|
|
||||||
return bot;
|
|
||||||
}).toPromise(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -80,20 +70,10 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
): DynamicModule {
|
): DynamicModule {
|
||||||
const telegrafBotName = getBotToken(options.botName);
|
const telegrafBotName = getBotToken(options.botName);
|
||||||
|
|
||||||
const telegrafBotProvider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async (
|
useFactory: async (options: TelegrafModuleOptions) =>
|
||||||
telegrafModuleOptions: TelegrafModuleOptions,
|
await createBotFactory(options),
|
||||||
): 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();
|
|
||||||
},
|
|
||||||
inject: [TELEGRAF_MODULE_OPTIONS],
|
inject: [TELEGRAF_MODULE_OPTIONS],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -113,12 +93,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static applyBotMiddlewares(bot, middlewares) {
|
async onApplicationShutdown(): Promise<void> {
|
||||||
if (middlewares) {
|
const bot = this.moduleRef.get<any>(this.botName);
|
||||||
middlewares.forEach((middleware) => {
|
bot && (await bot.stop());
|
||||||
bot.use(middleware);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static createAsyncProviders(
|
private static createAsyncProviders(
|
||||||
@ -158,9 +135,4 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
inject,
|
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