2020-12-29 22:41:06 +03:00
|
|
|
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
|
|
|
|
import {
|
|
|
|
DynamicModule,
|
|
|
|
Inject,
|
|
|
|
Module,
|
|
|
|
OnApplicationBootstrap,
|
|
|
|
OnApplicationShutdown,
|
|
|
|
Provider,
|
|
|
|
} from '@nestjs/common';
|
|
|
|
import { Telegraf } from 'telegraf';
|
2020-01-12 14:18:48 +03:00
|
|
|
import {
|
|
|
|
TelegrafModuleAsyncOptions,
|
2020-12-29 22:41:06 +03:00
|
|
|
TelegrafModuleOptions,
|
2020-12-26 14:54:10 +03:00
|
|
|
TelegrafOptionsFactory,
|
2020-01-12 14:18:48 +03:00
|
|
|
} from './interfaces';
|
2020-12-26 14:54:10 +03:00
|
|
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
2020-12-26 16:11:09 +03:00
|
|
|
import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor';
|
2020-12-30 01:49:09 +03:00
|
|
|
import { TelegrafUpdateExplorer } from './explorers/telegraf-update.explorer';
|
|
|
|
import { TelegrafSceneExplorer } from './explorers/telegraf-scene.explorer';
|
2020-12-29 22:41:06 +03:00
|
|
|
import { createProviders, TelegrafProvider } from './telegraf.providers';
|
2019-02-28 11:29:26 +03:00
|
|
|
|
2020-12-26 14:54:10 +03:00
|
|
|
@Module({
|
|
|
|
imports: [DiscoveryModule],
|
2020-12-29 22:41:06 +03:00
|
|
|
providers: [
|
|
|
|
TelegrafMetadataAccessor,
|
|
|
|
TelegrafSceneExplorer,
|
|
|
|
TelegrafUpdateExplorer,
|
|
|
|
],
|
2020-12-26 14:54:10 +03:00
|
|
|
})
|
2020-12-29 22:41:06 +03:00
|
|
|
export class TelegrafModule
|
|
|
|
implements OnApplicationBootstrap, OnApplicationShutdown {
|
|
|
|
constructor(
|
|
|
|
@Inject(TELEGRAF_MODULE_OPTIONS)
|
|
|
|
private readonly options: TelegrafModuleOptions,
|
|
|
|
private readonly moduleRef: ModuleRef,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async onApplicationBootstrap(): Promise<void> {
|
|
|
|
const { launchOptions } = this.options;
|
|
|
|
const telegraf = this.moduleRef.get(Telegraf);
|
|
|
|
await telegraf.launch(launchOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onApplicationShutdown(): Promise<void> {
|
|
|
|
const telegraf = this.moduleRef.get(Telegraf);
|
|
|
|
await telegraf.stop();
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:06:25 +03:00
|
|
|
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
2020-12-29 22:41:06 +03:00
|
|
|
const providers = [...createProviders(options), TelegrafProvider];
|
2020-12-26 14:54:10 +03:00
|
|
|
|
2019-02-28 11:29:26 +03:00
|
|
|
return {
|
2020-01-12 01:15:32 +03:00
|
|
|
module: TelegrafModule,
|
2020-12-26 14:54:10 +03:00
|
|
|
providers,
|
|
|
|
exports: providers,
|
2020-01-12 14:18:48 +03:00
|
|
|
};
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|
2020-01-12 01:36:53 +03:00
|
|
|
|
2020-03-19 16:21:35 +03:00
|
|
|
public static forRootAsync(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): DynamicModule {
|
2020-12-26 14:54:10 +03:00
|
|
|
const providers = [...this.createAsyncProviders(options), TelegrafProvider];
|
|
|
|
|
2020-01-12 01:36:53 +03:00
|
|
|
return {
|
2020-03-19 16:21:35 +03:00
|
|
|
module: TelegrafModule,
|
2020-12-26 14:54:10 +03:00
|
|
|
imports: options.imports || [],
|
|
|
|
providers,
|
|
|
|
exports: providers,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createAsyncProviders(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): Provider[] {
|
|
|
|
if (options.useExisting || options.useFactory) {
|
|
|
|
return [this.createAsyncOptionsProvider(options)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
this.createAsyncOptionsProvider(options),
|
|
|
|
{
|
|
|
|
provide: options.useClass,
|
|
|
|
useClass: options.useClass,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createAsyncOptionsProvider(
|
|
|
|
options: TelegrafModuleAsyncOptions,
|
|
|
|
): Provider {
|
|
|
|
if (options.useFactory) {
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: options.useFactory,
|
|
|
|
inject: options.inject || [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
provide: TELEGRAF_MODULE_OPTIONS,
|
|
|
|
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
|
|
|
|
await optionsFactory.createTelegrafOptions(),
|
|
|
|
inject: [options.useExisting || options.useClass],
|
2020-01-12 14:18:48 +03:00
|
|
|
};
|
2020-01-12 01:36:53 +03:00
|
|
|
}
|
2019-02-28 11:29:26 +03:00
|
|
|
}
|