mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +03:00
refactor(): bot token using
This commit is contained in:
parent
d48123ea38
commit
5ada9353e9
@ -8,12 +8,12 @@ import {
|
|||||||
|
|
||||||
export interface TelegrafModuleOptions<C extends Context = Context> {
|
export interface TelegrafModuleOptions<C extends Context = Context> {
|
||||||
token: string;
|
token: string;
|
||||||
|
name?: string;
|
||||||
options?: TelegrafOptions;
|
options?: TelegrafOptions;
|
||||||
launchOptions?: {
|
launchOptions?: {
|
||||||
polling?: LaunchPollingOptions;
|
polling?: LaunchPollingOptions;
|
||||||
webhook?: LaunchWebhookOptions;
|
webhook?: LaunchWebhookOptions;
|
||||||
};
|
};
|
||||||
botName?: string;
|
|
||||||
include?: Function[];
|
include?: Function[];
|
||||||
middlewares?: ReadonlyArray<Middleware<C>>;
|
middlewares?: ReadonlyArray<Middleware<C>>;
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,7 @@ import {
|
|||||||
TelegrafModuleAsyncOptions,
|
TelegrafModuleAsyncOptions,
|
||||||
TelegrafOptionsFactory,
|
TelegrafOptionsFactory,
|
||||||
} from './interfaces';
|
} from './interfaces';
|
||||||
import {
|
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
||||||
TELEGRAF_BOT_NAME,
|
|
||||||
TELEGRAF_MODULE_OPTIONS,
|
|
||||||
} from './telegraf.constants';
|
|
||||||
import {
|
import {
|
||||||
MetadataAccessorService,
|
MetadataAccessorService,
|
||||||
ScenesExplorerService,
|
ScenesExplorerService,
|
||||||
@ -36,15 +33,14 @@ import { createBotFactory } from './utils/create-bot-factory.util';
|
|||||||
})
|
})
|
||||||
export class TelegrafCoreModule implements OnApplicationShutdown {
|
export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
|
@Inject(TELEGRAF_MODULE_OPTIONS)
|
||||||
|
private readonly options: TelegrafModuleOptions,
|
||||||
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 telegrafBotProvider: Provider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: getBotToken(options.name),
|
||||||
useFactory: async () => await createBotFactory(options),
|
useFactory: async () => await createBotFactory(options),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,10 +51,6 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
provide: TELEGRAF_MODULE_OPTIONS,
|
provide: TELEGRAF_MODULE_OPTIONS,
|
||||||
useValue: options,
|
useValue: options,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
provide: TELEGRAF_BOT_NAME,
|
|
||||||
useValue: telegrafBotName,
|
|
||||||
},
|
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
],
|
],
|
||||||
exports: [telegrafBotProvider],
|
exports: [telegrafBotProvider],
|
||||||
@ -81,20 +73,14 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
return {
|
return {
|
||||||
module: TelegrafCoreModule,
|
module: TelegrafCoreModule,
|
||||||
imports: options.imports,
|
imports: options.imports,
|
||||||
providers: [
|
providers: [...asyncProviders, telegrafBotProvider],
|
||||||
...asyncProviders,
|
|
||||||
{
|
|
||||||
provide: TELEGRAF_BOT_NAME,
|
|
||||||
useValue: telegrafBotName,
|
|
||||||
},
|
|
||||||
telegrafBotProvider,
|
|
||||||
],
|
|
||||||
exports: [telegrafBotProvider],
|
exports: [telegrafBotProvider],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async onApplicationShutdown(): Promise<void> {
|
async onApplicationShutdown(): Promise<void> {
|
||||||
const bot = this.moduleRef.get<any>(this.botName);
|
const botName = getBotToken(this.options.name);
|
||||||
|
const bot = this.moduleRef.get<any>(botName);
|
||||||
bot && (await bot.stop());
|
bot && (await bot.stop());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
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';
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import { DEFAULT_BOT_NAME } from '../telegraf.constants';
|
import { DEFAULT_BOT_NAME } from '../telegraf.constants';
|
||||||
|
|
||||||
export function getBotToken(name?: string) {
|
export function getBotToken(name?: string): string {
|
||||||
return name && name !== DEFAULT_BOT_NAME
|
return name && name !== DEFAULT_BOT_NAME ? `${name}Bot` : DEFAULT_BOT_NAME;
|
||||||
? `${name}_BOT_NAME`
|
|
||||||
: DEFAULT_BOT_NAME;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user