mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
merge refactor/v2
This commit is contained in:
13
lib/utils/create-bot-factory.util.ts
Normal file
13
lib/utils/create-bot-factory.util.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafModuleOptions } from '../interfaces';
|
||||
|
||||
export async function createBotFactory(
|
||||
options: TelegrafModuleOptions,
|
||||
): Promise<Telegraf<never>> {
|
||||
const bot = new Telegraf<never>(options.token, options.options);
|
||||
|
||||
bot.use(...(options.middlewares ?? []));
|
||||
await bot.launch(options.launchOptions);
|
||||
|
||||
return bot;
|
||||
}
|
29
lib/utils/create-listener-decorator.util.ts
Normal file
29
lib/utils/create-listener-decorator.util.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { BaseScene as Scene } from 'telegraf';
|
||||
import { ComposerMethodArgs, SceneMethods } from '../types';
|
||||
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createListenerDecorator<TMethod extends SceneMethods>(
|
||||
method: TMethod,
|
||||
) {
|
||||
return (
|
||||
...args: ComposerMethodArgs<Scene<never>, TMethod>
|
||||
): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata);
|
||||
};
|
||||
}
|
||||
|
||||
export function createMissedListenerDecorator<TArgs extends any[]>(
|
||||
method: string,
|
||||
) {
|
||||
return (...args: TArgs): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata);
|
||||
};
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { BaseScene as Scene } from 'telegraf';
|
||||
import { ComposerMethodArgs, SceneMethods } from '../telegraf.types';
|
||||
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createSceneListenerDecorator<Method extends SceneMethods>(
|
||||
method: Method,
|
||||
) {
|
||||
return (
|
||||
...args: ComposerMethodArgs<Scene<never>, Method>
|
||||
): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata);
|
||||
};
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { Composer } from 'telegraf';
|
||||
import { ComposerMethodArgs, UpdateMethods } from '../telegraf.types';
|
||||
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createUpdateListenerDecorator<Method extends UpdateMethods>(
|
||||
method: Method,
|
||||
) {
|
||||
return (
|
||||
...args: ComposerMethodArgs<Composer<never>, Method>
|
||||
): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_METADATA, {
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata);
|
||||
};
|
||||
}
|
5
lib/utils/get-bot-token.util.ts
Normal file
5
lib/utils/get-bot-token.util.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { DEFAULT_BOT_NAME } from '../telegraf.constants';
|
||||
|
||||
export function getBotToken(name?: string): string {
|
||||
return name && name !== DEFAULT_BOT_NAME ? `${name}Bot` : DEFAULT_BOT_NAME;
|
||||
}
|
@@ -1,2 +1,3 @@
|
||||
export * from './create-update-listener-decorator.util';
|
||||
export * from './create-scene-listener-decorator.util';
|
||||
export * from './get-bot-token.util';
|
||||
export * from './create-bot-factory.util';
|
||||
export * from './create-listener-decorator.util';
|
||||
|
Reference in New Issue
Block a user