mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-11 14:48:10 +03:00
fix(): missing decorators & refactor
This commit is contained in:
parent
90cb548551
commit
f13eebe685
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling callback_data actions with regular expressions.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=action
|
||||
*/
|
||||
export const Action = createUpdateListenerDecorator('action');
|
||||
export const Action = createListenerDecorator('action');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Cashtag handling.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=cashtag
|
||||
*/
|
||||
export const Cashtag = createUpdateListenerDecorator('cashtag');
|
||||
export const Cashtag = createMissedListenerDecorator<[string | string[]]>('cashtag');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Command handling.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=command
|
||||
*/
|
||||
export const Command = createUpdateListenerDecorator('command');
|
||||
export const Command = createListenerDecorator('command');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with email entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-email
|
||||
*/
|
||||
export const Email = createUpdateListenerDecorator('email');
|
||||
export const Email = createMissedListenerDecorator<[string | string[]]>('email');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling callback_data actions with game query.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||
*/
|
||||
export const GameQuery = createUpdateListenerDecorator('gameQuery');
|
||||
export const GameQuery = createListenerDecorator('gameQuery');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Hashtag handling.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=hashtag
|
||||
*/
|
||||
export const Hashtag = createUpdateListenerDecorator('hashtag');
|
||||
export const Hashtag = createMissedListenerDecorator<[string | string[]]>('hashtag');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling text messages.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=hears
|
||||
*/
|
||||
export const Hears = createUpdateListenerDecorator('hears');
|
||||
export const Hears = createListenerDecorator('hears');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Handler for /help command.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=help
|
||||
*/
|
||||
export const Help = createUpdateListenerDecorator('help');
|
||||
export const Help = createListenerDecorator('help');
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
import { HearsTriggers } from 'telegraf/typings/composer';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling inline_query actions with regular expressions.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||
*/
|
||||
export const InlineQuery = createUpdateListenerDecorator('inlineQuery');
|
||||
export const InlineQuery = createMissedListenerDecorator<[HearsTriggers<unknown>]>('inlineQuery');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Mention handling.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=mention
|
||||
*/
|
||||
export const Mention = createUpdateListenerDecorator('mention');
|
||||
export const Mention = createMissedListenerDecorator<[string | string[]]>('mention');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for provided update type.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=on
|
||||
*/
|
||||
export const On = createUpdateListenerDecorator('on');
|
||||
export const On = createListenerDecorator('on');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Phone number handling.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=phone
|
||||
*/
|
||||
export const Phone = createUpdateListenerDecorator('phone');
|
||||
export const Phone = createMissedListenerDecorator<[string | string[]]>('phone');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Handler for /settings command.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=settings
|
||||
*/
|
||||
export const Settings = createUpdateListenerDecorator('settings');
|
||||
export const Settings = createMissedListenerDecorator<[]>('settings');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Handler for /start command.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=start
|
||||
*/
|
||||
export const Start = createUpdateListenerDecorator('start');
|
||||
export const Start = createListenerDecorator('start');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with text_link entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||
*/
|
||||
export const TextLink = createUpdateListenerDecorator('textLink');
|
||||
export const TextLink = createMissedListenerDecorator<[string | string[]]>('textLink');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with text_mention entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||
*/
|
||||
export const TextMention = createUpdateListenerDecorator('textMention');
|
||||
export const TextMention = createMissedListenerDecorator<[string | string[]]>('textMention');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createMissedListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with url entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-url
|
||||
*/
|
||||
export const Url = createUpdateListenerDecorator('url');
|
||||
export const Url = createMissedListenerDecorator<[string | string[]]>('url');
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { createUpdateListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
/**
|
||||
* Registers a middleware.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=use
|
||||
*/
|
||||
export const Use = createUpdateListenerDecorator('use');
|
||||
export const Use = createListenerDecorator('use');
|
||||
|
@ -1,3 +1,3 @@
|
||||
import { createSceneListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
export const SceneEnter = createSceneListenerDecorator('enter');
|
||||
export const SceneEnter = createListenerDecorator('enter');
|
||||
|
@ -1,3 +1,3 @@
|
||||
import { createSceneListenerDecorator } from '../../helpers';
|
||||
import { createListenerDecorator } from '../../utils';
|
||||
|
||||
export const SceneLeave = createSceneListenerDecorator('leave');
|
||||
export const SceneLeave = createListenerDecorator('leave');
|
||||
|
13
lib/index.ts
13
lib/index.ts
@ -1,15 +1,6 @@
|
||||
export * as Composer from 'telegraf/composer';
|
||||
export * as Markup from 'telegraf/markup';
|
||||
export * as BaseScene from 'telegraf/scenes/base';
|
||||
export * as session from 'telegraf/session';
|
||||
export * as Stage from 'telegraf/stage';
|
||||
export * as WizardScene from 'telegraf/scenes/wizard';
|
||||
export * as Extra from 'telegraf/extra';
|
||||
|
||||
export * from './decorators';
|
||||
export * from './interfaces';
|
||||
export * from './helpers';
|
||||
export * from './utils';
|
||||
export * from './telegraf.module';
|
||||
export * from './types';
|
||||
export { Telegraf } from 'telegraf';
|
||||
export * from './telegraf.constants';
|
||||
export * from './telegraf.module';
|
||||
|
@ -1,3 +1,2 @@
|
||||
export * from './telegraf-options.interface';
|
||||
export * from './listener-metadata.interface';
|
||||
export * from './update-metadata.interface';
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||
import { Module } from '@nestjs/core/injector/module';
|
||||
import { flattenDeep, groupBy, identity, isEmpty, mapValues } from 'lodash';
|
||||
import { UpdateMetadata } from '../interfaces';
|
||||
import { flattenDeep, identity, isEmpty } from 'lodash';
|
||||
|
||||
export class BaseExplorerService {
|
||||
getModules(
|
||||
@ -21,11 +20,11 @@ export class BaseExplorerService {
|
||||
): Module[] {
|
||||
const modules = [...modulesContainer.values()];
|
||||
return modules.filter(({ metatype }) =>
|
||||
include.some((item) => item === metatype),
|
||||
include.includes(metatype),
|
||||
);
|
||||
}
|
||||
|
||||
flatMap<T = UpdateMetadata>(
|
||||
flatMap<T>(
|
||||
modules: Module[],
|
||||
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
||||
): T[] {
|
||||
@ -37,23 +36,4 @@ export class BaseExplorerService {
|
||||
};
|
||||
return flattenDeep(invokeMap()).filter(identity);
|
||||
}
|
||||
|
||||
groupMetadata(resolvers: UpdateMetadata[]) {
|
||||
const groupByType = groupBy(
|
||||
resolvers,
|
||||
(metadata: UpdateMetadata) => metadata.type,
|
||||
);
|
||||
const groupedMetadata = mapValues(
|
||||
groupByType,
|
||||
(resolversArr: UpdateMetadata[]) =>
|
||||
resolversArr.reduce(
|
||||
(prev, curr) => ({
|
||||
...prev,
|
||||
[curr.name]: curr.callback,
|
||||
}),
|
||||
{},
|
||||
),
|
||||
);
|
||||
return groupedMetadata;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
export * from './updates-explorer.service';
|
||||
export * from './listeners-explorer.service';
|
||||
export * from './metadata-accessor.service';
|
||||
export * from './scenes-explorer.service';
|
||||
export * from './updates-explorer.service';
|
||||
export * from './listeners-explorer.service';
|
||||
|
@ -12,7 +12,7 @@ import { BaseExplorerService } from './base-explorer.service';
|
||||
import { getBotToken } from '../utils';
|
||||
|
||||
@Injectable()
|
||||
export class TelegrafExplorerService
|
||||
export class ListenersExplorerService
|
||||
extends BaseExplorerService
|
||||
implements OnModuleInit {
|
||||
private readonly bot: Telegraf<any>;
|
||||
|
@ -18,13 +18,6 @@ export class MetadataAccessorService {
|
||||
return !!this.reflector.get(UPDATE_METADATA, target);
|
||||
}
|
||||
|
||||
isUpdateListener(target: Function) {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(UPDATE_LISTENER_METADATA, target);
|
||||
}
|
||||
|
||||
isScene(target: Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
|
@ -16,18 +16,15 @@ import {
|
||||
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
|
||||
import {
|
||||
MetadataAccessorService,
|
||||
ScenesExplorerService,
|
||||
UpdatesExplorerService,
|
||||
ListenersExplorerService,
|
||||
} from './services';
|
||||
import { getBotToken } from './utils';
|
||||
import { createBotFactory } from './utils/create-bot-factory.util';
|
||||
import { getBotToken, createBotFactory } from './utils';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [DiscoveryModule],
|
||||
providers: [
|
||||
UpdatesExplorerService,
|
||||
ScenesExplorerService,
|
||||
ListenersExplorerService,
|
||||
MetadataAccessorService,
|
||||
],
|
||||
})
|
||||
|
@ -17,5 +17,5 @@ export type ComposerMethodArgs<
|
||||
U extends OnlyFunctionPropertyNames<T> = OnlyFunctionPropertyNames<T>
|
||||
> = Filter<Parameters<T[U]>, Middleware<never>>;
|
||||
|
||||
export type UpdateMethods = OnlyFunctionPropertyNames<Composer<never>>;
|
||||
export type ComposerMethods = OnlyFunctionPropertyNames<Composer<never>>;
|
||||
export type SceneMethods = OnlyFunctionPropertyNames<BaseScene<never>>;
|
||||
|
@ -4,11 +4,24 @@ import { ComposerMethodArgs, SceneMethods } from '../types';
|
||||
import { UPDATE_LISTENER_METADATA } from '../telegraf.constants';
|
||||
import { ListenerMetadata } from '../interfaces';
|
||||
|
||||
export function createListenerDecorator<Method extends SceneMethods>(
|
||||
method: Method,
|
||||
export function createListenerDecorator<TMethod extends SceneMethods>(
|
||||
method: TMethod,
|
||||
) {
|
||||
return (
|
||||
...args: ComposerMethodArgs<Scene<never>, Method>
|
||||
...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,
|
||||
|
@ -1 +1,3 @@
|
||||
export * from './get-bot-token.util';
|
||||
export * from './create-bot-factory.util';
|
||||
export * from './create-listener-decorator.util';
|
||||
|
Loading…
Reference in New Issue
Block a user