mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-11 22:51:06 +03:00
Merge pull request #875 from atassis/feature/all-bots-decorator
feat: created all bots decorator
This commit is contained in:
commit
52c2bff29c
@ -2,3 +2,4 @@ export * from './update.decorator';
|
|||||||
export * from './scene.decorator';
|
export * from './scene.decorator';
|
||||||
export * from './wizard.decorator';
|
export * from './wizard.decorator';
|
||||||
export * from './inject-bot.decorator';
|
export * from './inject-bot.decorator';
|
||||||
|
export * from './inject-all-bots.decorator';
|
||||||
|
9
lib/decorators/core/inject-all-bots.decorator.ts
Normal file
9
lib/decorators/core/inject-all-bots.decorator.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { Inject } from '@nestjs/common';
|
||||||
|
import { Telegraf } from 'telegraf';
|
||||||
|
|
||||||
|
import { getAllBotsToken } from '../../utils/get-all-bots-token.util';
|
||||||
|
|
||||||
|
export type AllBotsMap = Map<string, Telegraf<any>>;
|
||||||
|
|
||||||
|
export const InjectAllBots = (): ParameterDecorator =>
|
||||||
|
Inject(getAllBotsToken());
|
@ -1,6 +1,5 @@
|
|||||||
import { createListenerDecorator } from '../../utils';
|
import { createListenerDecorator } from '../../utils';
|
||||||
import { Scenes } from 'telegraf';
|
import { Scenes } from 'telegraf';
|
||||||
|
|
||||||
export const SceneEnter = createListenerDecorator<Scenes.BaseScene<never>>(
|
export const SceneEnter =
|
||||||
'enter',
|
createListenerDecorator<Scenes.BaseScene<never>>('enter');
|
||||||
);
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { createListenerDecorator } from '../../utils';
|
import { createListenerDecorator } from '../../utils';
|
||||||
import { Scenes } from 'telegraf';
|
import { Scenes } from 'telegraf';
|
||||||
|
|
||||||
export const SceneLeave = createListenerDecorator<Scenes.BaseScene<never>>(
|
export const SceneLeave =
|
||||||
'leave',
|
createListenerDecorator<Scenes.BaseScene<never>>('leave');
|
||||||
);
|
|
||||||
|
@ -4,7 +4,8 @@ import { TgArgumentsHost } from './tg-arguments-host.interface';
|
|||||||
|
|
||||||
export class TelegrafArgumentsHost
|
export class TelegrafArgumentsHost
|
||||||
extends ExecutionContextHost
|
extends ExecutionContextHost
|
||||||
implements TgArgumentsHost {
|
implements TgArgumentsHost
|
||||||
|
{
|
||||||
static create(context: ArgumentsHost): TelegrafArgumentsHost {
|
static create(context: ArgumentsHost): TelegrafArgumentsHost {
|
||||||
const type = context.getType();
|
const type = context.getType();
|
||||||
const tgContext = new TelegrafArgumentsHost(context.getArgs());
|
const tgContext = new TelegrafArgumentsHost(context.getArgs());
|
||||||
|
@ -6,7 +6,8 @@ export type TelegrafContextType = 'telegraf' | ContextType;
|
|||||||
|
|
||||||
export class TelegrafExecutionContext
|
export class TelegrafExecutionContext
|
||||||
extends ExecutionContextHost
|
extends ExecutionContextHost
|
||||||
implements TgArgumentsHost {
|
implements TgArgumentsHost
|
||||||
|
{
|
||||||
static create(context: ExecutionContext): TelegrafExecutionContext {
|
static create(context: ExecutionContext): TelegrafExecutionContext {
|
||||||
const type = context.getType();
|
const type = context.getType();
|
||||||
const tgContext = new TelegrafExecutionContext(
|
const tgContext = new TelegrafExecutionContext(
|
||||||
|
@ -11,7 +11,9 @@ export interface TelegrafModuleOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface TelegrafOptionsFactory {
|
export interface TelegrafOptionsFactory {
|
||||||
createTelegrafOptions(): Promise<TelegrafModuleOptions> | TelegrafModuleOptions;
|
createTelegrafOptions():
|
||||||
|
| Promise<TelegrafModuleOptions>
|
||||||
|
| TelegrafModuleOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TelegrafModuleAsyncOptions
|
export interface TelegrafModuleAsyncOptions
|
||||||
|
@ -10,8 +10,7 @@ export class BaseExplorerService {
|
|||||||
if (!include || isEmpty(include)) {
|
if (!include || isEmpty(include)) {
|
||||||
return [...modulesContainer.values()];
|
return [...modulesContainer.values()];
|
||||||
}
|
}
|
||||||
const whitelisted = this.includeWhitelisted(modulesContainer, include);
|
return this.includeWhitelisted(modulesContainer, include);
|
||||||
return whitelisted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
includeWhitelisted(
|
includeWhitelisted(
|
||||||
|
@ -162,7 +162,7 @@ export class ListenersExplorerService
|
|||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
|
|
||||||
const steps = Object.values(group).map((stepsMetadata) => {
|
wizard.steps = Object.values(group).map((stepsMetadata) => {
|
||||||
const composer = new Composer();
|
const composer = new Composer();
|
||||||
stepsMetadata.forEach((stepMethod) => {
|
stepsMetadata.forEach((stepMethod) => {
|
||||||
this.registerIfListener(
|
this.registerIfListener(
|
||||||
@ -175,8 +175,6 @@ export class ListenersExplorerService
|
|||||||
});
|
});
|
||||||
return composer.middleware();
|
return composer.middleware();
|
||||||
});
|
});
|
||||||
|
|
||||||
wizard.steps = steps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerIfListener(
|
private registerIfListener(
|
||||||
@ -187,7 +185,8 @@ export class ListenersExplorerService
|
|||||||
defaultMetadata?: ListenerMetadata[],
|
defaultMetadata?: ListenerMetadata[],
|
||||||
): void {
|
): void {
|
||||||
const methodRef = prototype[methodName];
|
const methodRef = prototype[methodName];
|
||||||
const metadata = this.metadataAccessor.getListenerMetadata(methodRef) || defaultMetadata;
|
const metadata =
|
||||||
|
this.metadataAccessor.getListenerMetadata(methodRef) || defaultMetadata;
|
||||||
if (!metadata || metadata.length < 1) {
|
if (!metadata || metadata.length < 1) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -222,7 +221,7 @@ export class ListenersExplorerService
|
|||||||
methodName: string,
|
methodName: string,
|
||||||
) {
|
) {
|
||||||
const paramsFactory = this.telegrafParamsFactory;
|
const paramsFactory = this.telegrafParamsFactory;
|
||||||
const resolverCallback = this.externalContextCreator.create<
|
return this.externalContextCreator.create<
|
||||||
Record<number, ParamMetadata>,
|
Record<number, ParamMetadata>,
|
||||||
TelegrafContextType
|
TelegrafContextType
|
||||||
>(
|
>(
|
||||||
@ -236,6 +235,5 @@ export class ListenersExplorerService
|
|||||||
undefined,
|
undefined,
|
||||||
'telegraf',
|
'telegraf',
|
||||||
);
|
);
|
||||||
return resolverCallback;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
lib/telegraf-all-bots.provider.ts
Normal file
10
lib/telegraf-all-bots.provider.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { Provider } from '@nestjs/common';
|
||||||
|
import { Telegraf } from 'telegraf';
|
||||||
|
import { TELEGRAF_ALL_BOTS } from './telegraf.constants';
|
||||||
|
|
||||||
|
export const allBotsMap = new Map<string, Telegraf<any>>();
|
||||||
|
|
||||||
|
export const telegrafAllBotsProvider: Provider = {
|
||||||
|
provide: TELEGRAF_ALL_BOTS,
|
||||||
|
useValue: allBotsMap,
|
||||||
|
};
|
@ -19,6 +19,10 @@ import {
|
|||||||
} from './telegraf.constants';
|
} from './telegraf.constants';
|
||||||
import { ListenersExplorerService, MetadataAccessorService } from './services';
|
import { ListenersExplorerService, MetadataAccessorService } from './services';
|
||||||
import { telegrafStageProvider } from './stage.provider';
|
import { telegrafStageProvider } from './stage.provider';
|
||||||
|
import {
|
||||||
|
allBotsMap,
|
||||||
|
telegrafAllBotsProvider,
|
||||||
|
} from './telegraf-all-bots.provider';
|
||||||
import { createBotFactory, getBotToken } from './utils';
|
import { createBotFactory, getBotToken } from './utils';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
|
|
||||||
const telegrafBotProvider: Provider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async () => await createBotFactory(options),
|
useFactory: async () => {
|
||||||
|
const bot = await createBotFactory(options);
|
||||||
|
allBotsMap.set(telegrafBotName, bot);
|
||||||
|
return bot;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
telegrafStageProvider,
|
telegrafStageProvider,
|
||||||
telegrafBotNameProvider,
|
telegrafBotNameProvider,
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
|
telegrafAllBotsProvider,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
telegrafStageProvider,
|
telegrafStageProvider,
|
||||||
telegrafBotNameProvider,
|
telegrafBotNameProvider,
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
|
telegrafAllBotsProvider,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
|
|
||||||
const telegrafBotProvider: Provider = {
|
const telegrafBotProvider: Provider = {
|
||||||
provide: telegrafBotName,
|
provide: telegrafBotName,
|
||||||
useFactory: async (options: TelegrafModuleOptions) =>
|
useFactory: async (options: TelegrafModuleOptions) => {
|
||||||
await createBotFactory(options),
|
const bot = await createBotFactory(options);
|
||||||
|
allBotsMap.set(telegrafBotName, bot);
|
||||||
|
return bot;
|
||||||
|
},
|
||||||
inject: [TELEGRAF_MODULE_OPTIONS],
|
inject: [TELEGRAF_MODULE_OPTIONS],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
|||||||
telegrafStageProvider,
|
telegrafStageProvider,
|
||||||
telegrafBotNameProvider,
|
telegrafBotNameProvider,
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
|
telegrafAllBotsProvider,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
telegrafStageProvider,
|
telegrafStageProvider,
|
||||||
telegrafBotNameProvider,
|
telegrafBotNameProvider,
|
||||||
telegrafBotProvider,
|
telegrafBotProvider,
|
||||||
|
telegrafAllBotsProvider,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA';
|
|||||||
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
||||||
|
|
||||||
export const TELEGRAF_STAGE = 'TelegrafStage';
|
export const TELEGRAF_STAGE = 'TelegrafStage';
|
||||||
|
|
||||||
|
export const TELEGRAF_ALL_BOTS = 'TELEGRAF_ALL_BOTS';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Composer, Middleware, Telegraf } from 'telegraf';
|
import { Composer, Middleware } from 'telegraf';
|
||||||
|
|
||||||
export type Filter<T extends any[], F> = T extends []
|
export type Filter<T extends any[], F> = T extends []
|
||||||
? []
|
? []
|
||||||
@ -14,5 +14,5 @@ export type OnlyFunctionPropertyNames<T> = {
|
|||||||
|
|
||||||
export type ComposerMethodArgs<
|
export type ComposerMethodArgs<
|
||||||
T extends Composer<never>,
|
T extends Composer<never>,
|
||||||
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
|
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>,
|
||||||
> = Filter<Parameters<T[U]>, Middleware<never>>;
|
> = Filter<Parameters<T[U]>, Middleware<never>>;
|
||||||
|
@ -19,8 +19,9 @@ export function createListenerDecorator<
|
|||||||
args,
|
args,
|
||||||
} as ListenerMetadata,
|
} as ListenerMetadata,
|
||||||
];
|
];
|
||||||
|
|
||||||
const previousValue = Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || [];
|
const previousValue =
|
||||||
|
Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || [];
|
||||||
const value = [...previousValue, ...metadata];
|
const value = [...previousValue, ...metadata];
|
||||||
Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value);
|
Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value);
|
||||||
return descriptor;
|
return descriptor;
|
||||||
|
3
lib/utils/get-all-bots-token.util.ts
Normal file
3
lib/utils/get-all-bots-token.util.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { TELEGRAF_ALL_BOTS } from '../telegraf.constants';
|
||||||
|
|
||||||
|
export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS;
|
@ -5,8 +5,10 @@ import { PARAM_ARGS_METADATA } from '../telegraf.constants';
|
|||||||
|
|
||||||
export type ParamData = object | string | number;
|
export type ParamData = object | string | number;
|
||||||
|
|
||||||
export const createTelegrafParamDecorator = (paramtype: TelegrafParamtype) => {
|
export const createTelegrafParamDecorator =
|
||||||
return (data?: ParamData): ParameterDecorator => (target, key, index) => {
|
(paramtype: TelegrafParamtype) =>
|
||||||
|
(data?: ParamData): ParameterDecorator =>
|
||||||
|
(target, key, index) => {
|
||||||
const args =
|
const args =
|
||||||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
||||||
Reflect.defineMetadata(
|
Reflect.defineMetadata(
|
||||||
@ -16,16 +18,16 @@ export const createTelegrafParamDecorator = (paramtype: TelegrafParamtype) => {
|
|||||||
key,
|
key,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
export const createTelegrafPipesParamDecorator = (
|
export const createTelegrafPipesParamDecorator =
|
||||||
paramtype: TelegrafParamtype,
|
(paramtype: TelegrafParamtype) =>
|
||||||
) => (
|
(
|
||||||
data?: any,
|
data?: any,
|
||||||
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
||||||
): ParameterDecorator => (target, key, index) => {
|
): ParameterDecorator =>
|
||||||
addPipesMetadata(paramtype, data, pipes, target, key, index);
|
(target, key, index) => {
|
||||||
};
|
addPipesMetadata(paramtype, data, pipes, target, key, index);
|
||||||
|
};
|
||||||
|
|
||||||
export const addPipesMetadata = (
|
export const addPipesMetadata = (
|
||||||
paramtype: TelegrafParamtype,
|
paramtype: TelegrafParamtype,
|
||||||
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -5062,8 +5062,7 @@
|
|||||||
"version": "5.3.2",
|
"version": "5.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz",
|
"resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz",
|
||||||
"integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==",
|
"integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"requires": {}
|
|
||||||
},
|
},
|
||||||
"acorn-walk": {
|
"acorn-walk": {
|
||||||
"version": "8.2.0",
|
"version": "8.2.0",
|
||||||
@ -5620,8 +5619,7 @@
|
|||||||
"version": "8.5.0",
|
"version": "8.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz",
|
||||||
"integrity": "sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==",
|
"integrity": "sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"requires": {}
|
|
||||||
},
|
},
|
||||||
"eslint-plugin-prettier": {
|
"eslint-plugin-prettier": {
|
||||||
"version": "4.2.1",
|
"version": "4.2.1",
|
||||||
|
Loading…
Reference in New Issue
Block a user