From 5fe60dc5b2d1c8dd58d8acaa2ed0d35931cacffc Mon Sep 17 00:00:00 2001 From: Morb0 Date: Tue, 5 Jan 2021 12:42:41 +0300 Subject: [PATCH 1/4] fix(): change botName to name --- lib/interfaces/telegraf-options.interface.ts | 2 +- lib/telegraf-core.module.ts | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index d9b9c33..8000cc6 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -24,7 +24,7 @@ export interface TelegrafOptionsFactory { export interface TelegrafModuleAsyncOptions extends Pick { - botName?: string; + name?: string; useExisting?: Type; useClass?: Type; useFactory?: ( diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 8707c67..eb2e94f 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -14,19 +14,13 @@ import { TelegrafOptionsFactory, } from './interfaces'; import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; -import { - MetadataAccessorService, - ListenersExplorerService, -} from './services'; +import { MetadataAccessorService, ListenersExplorerService } from './services'; import { getBotToken, createBotFactory } from './utils'; @Global() @Module({ imports: [DiscoveryModule], - providers: [ - ListenersExplorerService, - MetadataAccessorService, - ], + providers: [ListenersExplorerService, MetadataAccessorService], }) export class TelegrafCoreModule implements OnApplicationShutdown { constructor( @@ -57,7 +51,7 @@ export class TelegrafCoreModule implements OnApplicationShutdown { public static forRootAsync( options: TelegrafModuleAsyncOptions, ): DynamicModule { - const telegrafBotName = getBotToken(options.botName); + const telegrafBotName = getBotToken(options.name); const telegrafBotProvider: Provider = { provide: telegrafBotName, From 751b15b4b288c313f198c8e2de9d94e088077d21 Mon Sep 17 00:00:00 2001 From: Morb0 Date: Tue, 5 Jan 2021 13:36:12 +0300 Subject: [PATCH 2/4] fix(): use bot token in di --- lib/decorators/core/inject-bot.decorator.ts | 4 +- lib/interfaces/telegraf-options.interface.ts | 4 +- lib/services/listeners-explorer.service.ts | 13 ++++-- lib/telegraf-core.module.ts | 49 ++++++++++++++------ lib/telegraf.constants.ts | 1 + 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/decorators/core/inject-bot.decorator.ts b/lib/decorators/core/inject-bot.decorator.ts index 73299e8..5a3fe57 100644 --- a/lib/decorators/core/inject-bot.decorator.ts +++ b/lib/decorators/core/inject-bot.decorator.ts @@ -1,5 +1,5 @@ import { Inject } from '@nestjs/common'; import { getBotToken } from '../../utils'; -export const InjectBot = (name?: string): ParameterDecorator => - Inject(getBotToken(name)); +export const InjectBot = (botName?: string): ParameterDecorator => + Inject(getBotToken(botName)); diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index 8000cc6..3a25a4f 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -8,7 +8,7 @@ import { export interface TelegrafModuleOptions { token: string; - name?: string; + botName?: string; options?: TelegrafOptions; launchOptions?: { polling?: LaunchPollingOptions; @@ -24,7 +24,7 @@ export interface TelegrafOptionsFactory { export interface TelegrafModuleAsyncOptions extends Pick { - name?: string; + botName?: string; useExisting?: Type; useClass?: Type; useFactory?: ( diff --git a/lib/services/listeners-explorer.service.ts b/lib/services/listeners-explorer.service.ts index eb0b789..ac22b96 100644 --- a/lib/services/listeners-explorer.service.ts +++ b/lib/services/listeners-explorer.service.ts @@ -6,10 +6,12 @@ import { Module } from '@nestjs/core/injector/module'; import { BaseScene, Composer, Stage, Telegraf } from 'telegraf'; import { MetadataAccessorService } from './metadata-accessor.service'; -import { TELEGRAF_MODULE_OPTIONS } from '../telegraf.constants'; +import { + TELEGRAF_BOT_NAME, + TELEGRAF_MODULE_OPTIONS, +} from '../telegraf.constants'; import { TelegrafModuleOptions } from '../interfaces'; import { BaseExplorerService } from './base-explorer.service'; -import { getBotToken } from '../utils'; @Injectable() export class ListenersExplorerService @@ -19,6 +21,8 @@ export class ListenersExplorerService private bot: Telegraf; constructor( + @Inject(TELEGRAF_BOT_NAME) + private readonly botName: string, @Inject(TELEGRAF_MODULE_OPTIONS) private readonly telegrafOptions: TelegrafModuleOptions, private readonly moduleRef: ModuleRef, @@ -31,8 +35,9 @@ export class ListenersExplorerService } onModuleInit(): void { - const botToken = getBotToken(this.telegrafOptions.name); - this.bot = this.moduleRef.get>(botToken); + this.bot = this.moduleRef.get>(this.botName, { + strict: false, + }); this.bot.use(this.stage.middleware()); this.explore(); diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index eb2e94f..b50e0c4 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -1,21 +1,24 @@ import { DiscoveryModule, ModuleRef } from '@nestjs/core'; import { - Module, DynamicModule, - Provider, - Type, Global, Inject, + Module, OnApplicationShutdown, + Provider, + Type, } from '@nestjs/common'; import { - TelegrafModuleOptions, TelegrafModuleAsyncOptions, + TelegrafModuleOptions, TelegrafOptionsFactory, } from './interfaces'; -import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants'; -import { MetadataAccessorService, ListenersExplorerService } from './services'; -import { getBotToken, createBotFactory } from './utils'; +import { + TELEGRAF_BOT_NAME, + TELEGRAF_MODULE_OPTIONS, +} from './telegraf.constants'; +import { ListenersExplorerService, MetadataAccessorService } from './services'; +import { createBotFactory, getBotToken } from './utils'; @Global() @Module({ @@ -24,14 +27,21 @@ import { getBotToken, createBotFactory } from './utils'; }) export class TelegrafCoreModule implements OnApplicationShutdown { constructor( - @Inject(TELEGRAF_MODULE_OPTIONS) - private readonly options: TelegrafModuleOptions, + @Inject(TELEGRAF_BOT_NAME) + private readonly botName: string, private readonly moduleRef: ModuleRef, ) {} public static forRoot(options: TelegrafModuleOptions): DynamicModule { + const telegrafBotName = getBotToken(options.botName); + + const telegrafBotNameProvider = { + provide: TELEGRAF_BOT_NAME, + useValue: telegrafBotName, + }; + const telegrafBotProvider: Provider = { - provide: getBotToken(options.name), + provide: telegrafBotName, useFactory: async () => await createBotFactory(options), }; @@ -42,6 +52,7 @@ export class TelegrafCoreModule implements OnApplicationShutdown { provide: TELEGRAF_MODULE_OPTIONS, useValue: options, }, + telegrafBotNameProvider, telegrafBotProvider, ], exports: [telegrafBotProvider], @@ -51,7 +62,12 @@ export class TelegrafCoreModule implements OnApplicationShutdown { public static forRootAsync( options: TelegrafModuleAsyncOptions, ): DynamicModule { - const telegrafBotName = getBotToken(options.name); + const telegrafBotName = getBotToken(options.botName); + + const telegrafBotNameProvider = { + provide: TELEGRAF_BOT_NAME, + useValue: telegrafBotName, + }; const telegrafBotProvider: Provider = { provide: telegrafBotName, @@ -64,14 +80,17 @@ export class TelegrafCoreModule implements OnApplicationShutdown { return { module: TelegrafCoreModule, imports: options.imports, - providers: [...asyncProviders, telegrafBotProvider], - exports: [telegrafBotProvider], + providers: [ + ...asyncProviders, + telegrafBotNameProvider, + telegrafBotProvider, + ], + exports: [telegrafBotNameProvider, telegrafBotProvider], }; } async onApplicationShutdown(): Promise { - const botName = getBotToken(this.options.name); - const bot = this.moduleRef.get(botName); + const bot = this.moduleRef.get(this.botName); bot && (await bot.stop()); } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 0ea0d72..3ac37e3 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -1,4 +1,5 @@ export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; +export const TELEGRAF_BOT_NAME = 'TELEGRAF_BOT_NAME'; export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME'; export const UPDATE_METADATA = 'UPDATE_METADATA'; From f8872663d888c818bebf7e9deb9e763cf3585c4a Mon Sep 17 00:00:00 2001 From: Morb0 Date: Tue, 5 Jan 2021 13:37:00 +0300 Subject: [PATCH 3/4] fix(sample): use new options --- sample/01-complete-app/package.json | 4 ++-- sample/01-complete-app/src/app.module.ts | 12 +++++++----- sample/01-complete-app/src/echo/echo.update.ts | 14 +++++++++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/sample/01-complete-app/package.json b/sample/01-complete-app/package.json index 8ccfff6..32a925d 100644 --- a/sample/01-complete-app/package.json +++ b/sample/01-complete-app/package.json @@ -23,12 +23,12 @@ "@nestjs/common": "7.5.1", "@nestjs/core": "7.5.1", "@nestjs/platform-express": "7.5.1", - "dotenv": "^8.2.0", + "dotenv": "8.2.0", "nestjs-telegraf": "*", "reflect-metadata": "0.1.13", "rimraf": "3.0.2", "rxjs": "6.6.3", - "telegraf": "^3.38.0" + "telegraf": "3.38.0" }, "devDependencies": { "@nestjs/cli": "7.5.1", diff --git a/sample/01-complete-app/src/app.module.ts b/sample/01-complete-app/src/app.module.ts index 667774f..408a307 100644 --- a/sample/01-complete-app/src/app.module.ts +++ b/sample/01-complete-app/src/app.module.ts @@ -12,11 +12,13 @@ import { GreeterBotName } from './app.constants'; middlewares: [sessionMiddleware], include: [EchoModule], }), - TelegrafModule.forRoot({ - name: GreeterBotName, - token: process.env.GREETER_BOT_TOKEN, - middlewares: [sessionMiddleware], - include: [GreeterModule], + TelegrafModule.forRootAsync({ + botName: GreeterBotName, + useFactory: () => ({ + token: process.env.GREETER_BOT_TOKEN, + middlewares: [sessionMiddleware], + include: [GreeterModule], + }), }), EchoModule, GreeterModule, diff --git a/sample/01-complete-app/src/echo/echo.update.ts b/sample/01-complete-app/src/echo/echo.update.ts index ad31f76..1d0d6b3 100644 --- a/sample/01-complete-app/src/echo/echo.update.ts +++ b/sample/01-complete-app/src/echo/echo.update.ts @@ -1,13 +1,21 @@ import { Telegraf } from 'telegraf'; -import { Command, Help, InjectBot, On, Start, Update } from 'nestjs-telegraf'; +import { + Command, + getBotToken, + Help, + InjectBot, + On, + Start, + Update, +} from 'nestjs-telegraf'; import { EchoService } from './echo.service'; -import { HELLO_SCENE_ID } from '../app.constants'; +import { GreeterBotName, HELLO_SCENE_ID } from '../app.constants'; import { Context } from '../interfaces/context.interface'; @Update() export class EchoUpdate { constructor( - @InjectBot() + @InjectBot(GreeterBotName) private readonly bot: Telegraf, private readonly echoService: EchoService, ) {} From e3f8b8b43a87580c66d90d013af1cd9883a7e881 Mon Sep 17 00:00:00 2001 From: Morb0 Date: Tue, 5 Jan 2021 15:43:46 +0300 Subject: [PATCH 4/4] chore(gitignore): remove locks from ignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 32f56bd..f9a2902 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -# lock -yarn.lock - # dependencies /node_modules