mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-30 04:18:51 +03:00
initial commit
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
import { Telegraf } from 'telegraf';
|
||||
import { TelegrafModuleOptions } from '../interfaces';
|
||||
|
||||
export async function createBotFactory(
|
||||
options: TelegrafModuleOptions,
|
||||
): Promise<Telegraf<any>> {
|
||||
const bot = new Telegraf<any>(options.token, options.options);
|
||||
|
||||
bot.use(...(options.middlewares ?? []));
|
||||
|
||||
if (options.launchOptions !== false) {
|
||||
await bot.launch(options.launchOptions);
|
||||
}
|
||||
|
||||
return bot;
|
||||
}
|
@@ -1,30 +0,0 @@
|
||||
import { Composer } from 'telegraf';
|
||||
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
||||
import { LISTENERS_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createListenerDecorator<
|
||||
TComposer extends Composer<never>,
|
||||
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>,
|
||||
>(method: TMethod) {
|
||||
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
||||
return (
|
||||
_target: any,
|
||||
_key?: string | symbol,
|
||||
descriptor?: TypedPropertyDescriptor<any>,
|
||||
) => {
|
||||
const metadata = [
|
||||
{
|
||||
method,
|
||||
args,
|
||||
} as ListenerMetadata,
|
||||
];
|
||||
|
||||
const previousValue =
|
||||
Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || [];
|
||||
const value = [...previousValue, ...metadata];
|
||||
Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value);
|
||||
return descriptor;
|
||||
};
|
||||
};
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
import { TELEGRAF_ALL_BOTS } from '../telegraf.constants';
|
||||
|
||||
export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS;
|
@@ -1,5 +0,0 @@
|
||||
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,3 +0,0 @@
|
||||
export * from './get-bot-token.util';
|
||||
export * from './create-bot-factory.util';
|
||||
export * from './create-listener-decorator.util';
|
@@ -1,52 +0,0 @@
|
||||
import { assignMetadata, PipeTransform, Type } from '@nestjs/common';
|
||||
import { isNil, isString } from '@nestjs/common/utils/shared.utils';
|
||||
import { TelegrafParamtype } from '../enums/telegraf-paramtype.enum';
|
||||
import { PARAM_ARGS_METADATA } from '../telegraf.constants';
|
||||
|
||||
export type ParamData = object | string | number;
|
||||
|
||||
export const createTelegrafParamDecorator =
|
||||
(paramtype: TelegrafParamtype) =>
|
||||
(data?: ParamData): ParameterDecorator =>
|
||||
(target, key, index) => {
|
||||
const args =
|
||||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
||||
Reflect.defineMetadata(
|
||||
PARAM_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, data),
|
||||
target.constructor,
|
||||
key,
|
||||
);
|
||||
};
|
||||
|
||||
export const createTelegrafPipesParamDecorator =
|
||||
(paramtype: TelegrafParamtype) =>
|
||||
(
|
||||
data?: any,
|
||||
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
||||
): ParameterDecorator =>
|
||||
(target, key, index) => {
|
||||
addPipesMetadata(paramtype, data, pipes, target, key, index);
|
||||
};
|
||||
|
||||
export const addPipesMetadata = (
|
||||
paramtype: TelegrafParamtype,
|
||||
data: any,
|
||||
pipes: (Type<PipeTransform> | PipeTransform)[],
|
||||
target: Record<string, any>,
|
||||
key: string | symbol,
|
||||
index: number,
|
||||
) => {
|
||||
const args =
|
||||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
||||
const hasParamData = isNil(data) || isString(data);
|
||||
const paramData = hasParamData ? data : undefined;
|
||||
const paramPipes = hasParamData ? pipes : [data, ...pipes];
|
||||
|
||||
Reflect.defineMetadata(
|
||||
PARAM_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, paramData, ...paramPipes),
|
||||
target.constructor,
|
||||
key,
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user