diff --git a/lib/decorators/core/index.ts b/lib/decorators/core/index.ts index a982c9d..cd0c9b0 100644 --- a/lib/decorators/core/index.ts +++ b/lib/decorators/core/index.ts @@ -2,3 +2,4 @@ export * from './update.decorator'; export * from './scene.decorator'; export * from './wizard.decorator'; export * from './inject-bot.decorator'; +export * from './inject-all-bots.decorator'; diff --git a/lib/decorators/core/inject-all-bots.decorator.ts b/lib/decorators/core/inject-all-bots.decorator.ts new file mode 100644 index 0000000..2b6eb6f --- /dev/null +++ b/lib/decorators/core/inject-all-bots.decorator.ts @@ -0,0 +1,9 @@ +import { Inject } from '@nestjs/common'; +import { Telegraf } from 'telegraf'; + +import { getAllBotsToken } from '../../utils/get-all-bots-token.util'; + +export type AllBotsMap = Map>; + +export const InjectAllBots = (): ParameterDecorator => + Inject(getAllBotsToken()); diff --git a/lib/services/base-explorer.service.ts b/lib/services/base-explorer.service.ts index 2bfb50a..2c0ca17 100644 --- a/lib/services/base-explorer.service.ts +++ b/lib/services/base-explorer.service.ts @@ -10,8 +10,7 @@ export class BaseExplorerService { if (!include || isEmpty(include)) { return [...modulesContainer.values()]; } - const whitelisted = this.includeWhitelisted(modulesContainer, include); - return whitelisted; + return this.includeWhitelisted(modulesContainer, include); } includeWhitelisted( diff --git a/lib/telegraf-all-bots.provider.ts b/lib/telegraf-all-bots.provider.ts new file mode 100644 index 0000000..43df2e2 --- /dev/null +++ b/lib/telegraf-all-bots.provider.ts @@ -0,0 +1,12 @@ +import { Provider } from '@nestjs/common'; +import { Telegraf } from 'telegraf'; +import { TELEGRAF_ALL_BOTS } from './telegraf.constants'; + +export const allBotsMap = new Map>(); + +setTimeout(() => console.log(allBotsMap), 2000); + +export const telegrafAllBotsProvider: Provider = { + provide: TELEGRAF_ALL_BOTS, + useValue: allBotsMap, +}; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 9b4ab34..1a740e6 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -19,6 +19,10 @@ import { } from './telegraf.constants'; import { ListenersExplorerService, MetadataAccessorService } from './services'; import { telegrafStageProvider } from './stage.provider'; +import { + allBotsMap, + telegrafAllBotsProvider, +} from './telegraf-all-bots.provider'; import { createBotFactory, getBotToken } from './utils'; @Global() @@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async () => await createBotFactory(options), + useFactory: async () => { + const bot = await createBotFactory(options); + allBotsMap.set(telegrafBotName, bot); + return bot; + }, }; return { @@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], exports: [ telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], }; } @@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async (options: TelegrafModuleOptions) => - await createBotFactory(options), + useFactory: async (options: TelegrafModuleOptions) => { + const bot = await createBotFactory(options); + allBotsMap.set(telegrafBotName, bot); + return bot; + }, inject: [TELEGRAF_MODULE_OPTIONS], }; @@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], exports: [ telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], }; } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 26e4cd1..e5f824e 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA'; export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA; export const TELEGRAF_STAGE = 'TelegrafStage'; + +export const TELEGRAF_ALL_BOTS = 'TELEGRAF_ALL_BOTS'; diff --git a/lib/utils/get-all-bots-token.util.ts b/lib/utils/get-all-bots-token.util.ts new file mode 100644 index 0000000..cb17b0b --- /dev/null +++ b/lib/utils/get-all-bots-token.util.ts @@ -0,0 +1,3 @@ +import { TELEGRAF_ALL_BOTS } from '../telegraf.constants'; + +export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS;