mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-11 14:48:10 +03:00
merge refactor/v2
This commit is contained in:
commit
197614d90a
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,3 @@
|
|||||||
# lock
|
|
||||||
yarn.lock
|
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
/node_modules
|
/node_modules
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Inject } from '@nestjs/common';
|
import { Inject } from '@nestjs/common';
|
||||||
import { getBotToken } from '../../utils';
|
import { getBotToken } from '../../utils';
|
||||||
|
|
||||||
export const InjectBot = (name?: string): ParameterDecorator =>
|
export const InjectBot = (botName?: string): ParameterDecorator =>
|
||||||
Inject(getBotToken(name));
|
Inject(getBotToken(botName));
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
|
|
||||||
export interface TelegrafModuleOptions<C extends Context = Context> {
|
export interface TelegrafModuleOptions<C extends Context = Context> {
|
||||||
token: string;
|
token: string;
|
||||||
name?: string;
|
botName?: string;
|
||||||
options?: TelegrafOptions;
|
options?: TelegrafOptions;
|
||||||
launchOptions?: {
|
launchOptions?: {
|
||||||
polling?: LaunchPollingOptions;
|
polling?: LaunchPollingOptions;
|
||||||
|
@ -3,20 +3,20 @@ import { DiscoveryService, ModuleRef, ModulesContainer } from '@nestjs/core';
|
|||||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||||
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
||||||
import { Module } from '@nestjs/core/injector/module';
|
import { Module } from '@nestjs/core/injector/module';
|
||||||
|
import { ParamMetadata } from '@nestjs/core/helpers/interfaces';
|
||||||
import { BaseScene, Composer, Stage, Telegraf } from 'telegraf';
|
import { BaseScene, Composer, Stage, Telegraf } from 'telegraf';
|
||||||
|
|
||||||
import { MetadataAccessorService } from './metadata-accessor.service';
|
import { MetadataAccessorService } from './metadata-accessor.service';
|
||||||
import {
|
import {
|
||||||
PARAM_ARGS_METADATA,
|
PARAM_ARGS_METADATA,
|
||||||
|
TELEGRAF_BOT_NAME,
|
||||||
TELEGRAF_MODULE_OPTIONS,
|
TELEGRAF_MODULE_OPTIONS,
|
||||||
} from '../telegraf.constants';
|
} from '../telegraf.constants';
|
||||||
import { ListenerMetadata, TelegrafModuleOptions } from '../interfaces';
|
|
||||||
import { BaseExplorerService } from './base-explorer.service';
|
import { BaseExplorerService } from './base-explorer.service';
|
||||||
import { getBotToken } from '../utils';
|
|
||||||
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
|
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
|
||||||
import { TelegrafParamsFactory } from '../factories/telegraf-params-factory';
|
import { TelegrafParamsFactory } from '../factories/telegraf-params-factory';
|
||||||
import { TelegrafContextType } from '../execution-context/telegraf-execution-context';
|
import { TelegrafContextType } from '../execution-context/telegraf-execution-context';
|
||||||
import { ParamMetadata } from '@nestjs/core/helpers/interfaces';
|
import { TelegrafModuleOptions } from '../interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ListenersExplorerService
|
export class ListenersExplorerService
|
||||||
@ -24,11 +24,13 @@ export class ListenersExplorerService
|
|||||||
implements OnModuleInit {
|
implements OnModuleInit {
|
||||||
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
||||||
private readonly stage = new Stage([]);
|
private readonly stage = new Stage([]);
|
||||||
private bot: Telegraf<never>;
|
private bot: Telegraf<any>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TELEGRAF_MODULE_OPTIONS)
|
@Inject(TELEGRAF_MODULE_OPTIONS)
|
||||||
private readonly telegrafOptions: TelegrafModuleOptions,
|
private readonly telegrafOptions: TelegrafModuleOptions,
|
||||||
|
@Inject(TELEGRAF_BOT_NAME)
|
||||||
|
private readonly botName: string,
|
||||||
private readonly moduleRef: ModuleRef,
|
private readonly moduleRef: ModuleRef,
|
||||||
private readonly discoveryService: DiscoveryService,
|
private readonly discoveryService: DiscoveryService,
|
||||||
private readonly metadataAccessor: MetadataAccessorService,
|
private readonly metadataAccessor: MetadataAccessorService,
|
||||||
@ -40,8 +42,9 @@ export class ListenersExplorerService
|
|||||||
}
|
}
|
||||||
|
|
||||||
onModuleInit(): void {
|
onModuleInit(): void {
|
||||||
const botToken = getBotToken(this.telegrafOptions.name);
|
this.bot = this.moduleRef.get<Telegraf<never>>(this.botName, {
|
||||||
this.bot = this.moduleRef.get<Telegraf<never>>(botToken);
|
strict: false,
|
||||||
|
});
|
||||||
this.bot.use(this.stage.middleware());
|
this.bot.use(this.stage.middleware());
|
||||||
|
|
||||||
this.explore();
|
this.explore();
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
|
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
Module,
|
|
||||||
DynamicModule,
|
DynamicModule,
|
||||||
Provider,
|
|
||||||
Type,
|
|
||||||
Global,
|
Global,
|
||||||
Inject,
|
Inject,
|
||||||
|
Module,
|
||||||
OnApplicationShutdown,
|
OnApplicationShutdown,
|
||||||
|
Provider,
|
||||||
|
Type,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
TelegrafModuleOptions,
|
|
||||||
TelegrafModuleAsyncOptions,
|
TelegrafModuleAsyncOptions,
|
||||||
|
TelegrafModuleOptions,
|
||||||
TelegrafOptionsFactory,
|
TelegrafOptionsFactory,
|
||||||
} from './interfaces';
|
} from './interfaces';
|
||||||
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
import {
|
||||||
import { MetadataAccessorService, ListenersExplorerService } from './services';
|
TELEGRAF_BOT_NAME,
|
||||||
import { getBotToken, createBotFactory } from './utils';
|
TELEGRAF_MODULE_OPTIONS,
|
||||||
|
} from './telegraf.constants';
|
||||||
|
import { ListenersExplorerService, MetadataAccessorService } from './services';
|
||||||
|
import { createBotFactory, getBotToken } from './utils';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
@ -24,14 +27,21 @@ import { getBotToken, createBotFactory } from './utils';
|
|||||||
})
|
})
|
||||||
export class TelegrafCoreModule implements OnApplicationShutdown {
|
export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TELEGRAF_MODULE_OPTIONS)
|
@Inject(TELEGRAF_BOT_NAME)
|
||||||
private readonly options: TelegrafModuleOptions,
|
private readonly botName: string,
|
||||||
private readonly moduleRef: ModuleRef,
|
private readonly moduleRef: ModuleRef,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
|
||||||
|
const telegrafBotName = getBotToken(options.botName);
|
||||||
|
|
||||||
|
const telegrafBotNameProvider = {
|
||||||
|
provide: TELEGRAF_BOT_NAME,
|
||||||
|
useValue: telegrafBotName,
|
||||||
|
};
|
||||||
|
|
||||||
const telegrafBotProvider: Provider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: getBotToken(options.name),
|
provide: telegrafBotName,
|
||||||
useFactory: async () => await createBotFactory(options),
|
useFactory: async () => await createBotFactory(options),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -42,6 +52,7 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
provide: TELEGRAF_MODULE_OPTIONS,
|
provide: TELEGRAF_MODULE_OPTIONS,
|
||||||
useValue: options,
|
useValue: options,
|
||||||
},
|
},
|
||||||
|
telegrafBotNameProvider,
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
],
|
],
|
||||||
exports: [telegrafBotProvider],
|
exports: [telegrafBotProvider],
|
||||||
@ -53,6 +64,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
): DynamicModule {
|
): DynamicModule {
|
||||||
const telegrafBotName = getBotToken(options.botName);
|
const telegrafBotName = getBotToken(options.botName);
|
||||||
|
|
||||||
|
const telegrafBotNameProvider = {
|
||||||
|
provide: TELEGRAF_BOT_NAME,
|
||||||
|
useValue: telegrafBotName,
|
||||||
|
};
|
||||||
|
|
||||||
const telegrafBotProvider: Provider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async (options: TelegrafModuleOptions) =>
|
useFactory: async (options: TelegrafModuleOptions) =>
|
||||||
@ -64,14 +80,17 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
return {
|
return {
|
||||||
module: TelegrafCoreModule,
|
module: TelegrafCoreModule,
|
||||||
imports: options.imports,
|
imports: options.imports,
|
||||||
providers: [...asyncProviders, telegrafBotProvider],
|
providers: [
|
||||||
exports: [telegrafBotProvider],
|
...asyncProviders,
|
||||||
|
telegrafBotNameProvider,
|
||||||
|
telegrafBotProvider,
|
||||||
|
],
|
||||||
|
exports: [telegrafBotNameProvider, telegrafBotProvider],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async onApplicationShutdown(): Promise<void> {
|
async onApplicationShutdown(): Promise<void> {
|
||||||
const botName = getBotToken(this.options.name);
|
const bot = this.moduleRef.get<any>(this.botName);
|
||||||
const bot = this.moduleRef.get<any>(botName);
|
|
||||||
bot && (await bot.stop());
|
bot && (await bot.stop());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';
|
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';
|
||||||
|
|
||||||
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
|
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 DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME';
|
||||||
|
|
||||||
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
||||||
|
@ -23,12 +23,12 @@
|
|||||||
"@nestjs/common": "7.5.1",
|
"@nestjs/common": "7.5.1",
|
||||||
"@nestjs/core": "7.5.1",
|
"@nestjs/core": "7.5.1",
|
||||||
"@nestjs/platform-express": "7.5.1",
|
"@nestjs/platform-express": "7.5.1",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "8.2.0",
|
||||||
"nestjs-telegraf": "*",
|
"nestjs-telegraf": "*",
|
||||||
"reflect-metadata": "0.1.13",
|
"reflect-metadata": "0.1.13",
|
||||||
"rimraf": "3.0.2",
|
"rimraf": "3.0.2",
|
||||||
"rxjs": "6.6.3",
|
"rxjs": "6.6.3",
|
||||||
"telegraf": "^3.38.0"
|
"telegraf": "3.38.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nestjs/cli": "7.5.1",
|
"@nestjs/cli": "7.5.1",
|
||||||
|
@ -12,11 +12,13 @@ import { GreeterBotName } from './app.constants';
|
|||||||
middlewares: [sessionMiddleware],
|
middlewares: [sessionMiddleware],
|
||||||
include: [EchoModule],
|
include: [EchoModule],
|
||||||
}),
|
}),
|
||||||
TelegrafModule.forRoot({
|
TelegrafModule.forRootAsync({
|
||||||
name: GreeterBotName,
|
botName: GreeterBotName,
|
||||||
token: process.env.GREETER_BOT_TOKEN,
|
useFactory: () => ({
|
||||||
middlewares: [sessionMiddleware],
|
token: process.env.GREETER_BOT_TOKEN,
|
||||||
include: [GreeterModule],
|
middlewares: [sessionMiddleware],
|
||||||
|
include: [GreeterModule],
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
EchoModule,
|
EchoModule,
|
||||||
GreeterModule,
|
GreeterModule,
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import { Telegraf } from 'telegraf';
|
import { Telegraf } from 'telegraf';
|
||||||
import { Command, Help, InjectBot, On, Start, Update } from 'nestjs-telegraf';
|
import { Command, Help, InjectBot, On, Start, Update } from 'nestjs-telegraf';
|
||||||
import { EchoService } from './echo.service';
|
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';
|
import { Context } from '../interfaces/context.interface';
|
||||||
|
|
||||||
@Update()
|
@Update()
|
||||||
export class EchoUpdate {
|
export class EchoUpdate {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectBot()
|
@InjectBot(GreeterBotName)
|
||||||
private readonly bot: Telegraf<Context>,
|
private readonly bot: Telegraf<Context>,
|
||||||
private readonly echoService: EchoService,
|
private readonly echoService: EchoService,
|
||||||
) {}
|
) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user