mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-04-13 03:23:43 +03:00
refactor(): reply switch with dynamic telegraf method call
This commit is contained in:
parent
f10df2e960
commit
36f67ce29d
@ -1,3 +1,2 @@
|
||||
export * from './on.decorator';
|
||||
export * from './update.decorator';
|
||||
export * from './use.decorator';
|
||||
export * from './inject-bot.decorator';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { TelegrafProvider } from '../telegraf.provider';
|
||||
import { TelegrafProvider } from '../../telegraf.provider';
|
||||
|
||||
export const InjectBot = (): ParameterDecorator => Inject(TelegrafProvider);
|
@ -1,12 +0,0 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_TYPE_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
|
||||
/**
|
||||
* Registers a middleware.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=use
|
||||
*/
|
||||
export const Use = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Use);
|
||||
};
|
@ -1,3 +1,2 @@
|
||||
export * from './inject-bot.decorator';
|
||||
export * from './core';
|
||||
export * from './listeners';
|
||||
|
@ -1,14 +1,10 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafActionTriggers } from '../../telegraf.types';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
|
||||
export interface ActionOptions {
|
||||
triggers: TelegrafActionTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling callback_data actions with regular expressions.
|
||||
@ -17,9 +13,7 @@ export interface ActionOptions {
|
||||
*/
|
||||
export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Action),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
triggers,
|
||||
} as ActionOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Action),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
||||
);
|
||||
};
|
||||
|
@ -1,14 +1,10 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafCashtag } from '../../telegraf.types';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
|
||||
export interface CashtagOptions {
|
||||
cashtag: TelegrafCashtag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cashtag handling.
|
||||
@ -17,9 +13,7 @@ export interface CashtagOptions {
|
||||
*/
|
||||
export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Cashtag),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
cashtag,
|
||||
} as CashtagOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Cashtag),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [cashtag]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafCommand } from '../../telegraf.types';
|
||||
|
||||
export interface CommandOptions {
|
||||
command: TelegrafCommand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Command handling.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface CommandOptions {
|
||||
*/
|
||||
export const Command = (command: TelegrafCommand): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Command),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
command: command,
|
||||
} as CommandOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Command),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [command]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafEmail } from '../../telegraf.types';
|
||||
|
||||
export interface EmailOptions {
|
||||
email: TelegrafEmail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with email entity.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface EmailOptions {
|
||||
*/
|
||||
export const Email = (email: TelegrafEmail): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Email),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
email,
|
||||
} as EmailOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Email),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [email]),
|
||||
);
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_TYPE_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
|
||||
/**
|
||||
* Registers middleware for handling callback_data actions with game query.
|
||||
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
|
||||
* @see https://telegraf.js.org/#/?id=inlinequery
|
||||
*/
|
||||
export const GameQuery = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.GameQuery);
|
||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.GameQuery);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafHashtag } from '../../telegraf.types';
|
||||
|
||||
export interface HashtagOptions {
|
||||
hashtag: TelegrafHashtag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashtag handling.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface HashtagOptions {
|
||||
*/
|
||||
export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Hashtag),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
hashtag,
|
||||
} as HashtagOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hashtag),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [hashtag]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafHearsTriggers } from '../../telegraf.types';
|
||||
|
||||
export interface HearsOptions {
|
||||
triggers: TelegrafHearsTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling text messages.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface HearsOptions {
|
||||
*/
|
||||
export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Hears),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
triggers,
|
||||
} as HearsOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Hears),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
||||
);
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_TYPE_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
|
||||
/**
|
||||
* Handler for /help command.
|
||||
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
|
||||
* @see https://telegraf.js.org/#/?id=help
|
||||
*/
|
||||
export const Help = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Help);
|
||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Help);
|
||||
};
|
||||
|
@ -1,3 +1,5 @@
|
||||
export * from './on.decorator';
|
||||
export * from './use.decorator';
|
||||
export * from './action.decorator';
|
||||
export * from './cashtag.decorator';
|
||||
export * from './command.decorator';
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafInlineQueryTriggers } from '../../telegraf.types';
|
||||
|
||||
export interface InlineQueryOptions {
|
||||
triggers: TelegrafInlineQueryTriggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling inline_query actions with regular expressions.
|
||||
*
|
||||
@ -19,9 +15,7 @@ export const InlineQuery = (
|
||||
triggers: TelegrafInlineQueryTriggers,
|
||||
): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.InlineQuery),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
triggers,
|
||||
} as InlineQueryOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.InlineQuery),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [triggers]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafMention } from '../../telegraf.types';
|
||||
|
||||
export interface MentionOptions {
|
||||
mention: TelegrafMention;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mention handling.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface MentionOptions {
|
||||
*/
|
||||
export const Mention = (mention: TelegrafMention): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Mention),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
mention,
|
||||
} as MentionOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Mention),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafUpdateType } from '../../telegraf.types';
|
||||
|
||||
export interface OnOptions {
|
||||
updateTypes: TelegrafUpdateType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for provided update type.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface OnOptions {
|
||||
*/
|
||||
export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.On),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
updateTypes,
|
||||
} as OnOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.On),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [updateTypes]),
|
||||
);
|
||||
};
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafPhone } from '../../telegraf.types';
|
||||
|
||||
export interface PhoneOptions {
|
||||
phone: TelegrafPhone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Phone number handling.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface PhoneOptions {
|
||||
*/
|
||||
export const Phone = (phone: TelegrafPhone): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Phone),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
phone,
|
||||
} as PhoneOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Phone),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [phone]),
|
||||
);
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_TYPE_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
|
||||
/**
|
||||
* Handler for /settings command.
|
||||
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
|
||||
* @see https://telegraf.js.org/#/?id=settings
|
||||
*/
|
||||
export const Settings = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Settings);
|
||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Settings);
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_TYPE_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
|
||||
/**
|
||||
* Handler for /start command.
|
||||
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
|
||||
* @see https://telegraf.js.org/#/?id=start
|
||||
*/
|
||||
export const Start = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Start);
|
||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Start);
|
||||
};
|
||||
|
@ -1,25 +1,19 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafTextLink } from '../../telegraf.types';
|
||||
|
||||
export interface TextLinkOptions {
|
||||
link: TelegrafTextLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with text_link entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||
*/
|
||||
export const TetxLink = (link: TelegrafTextLink): MethodDecorator => {
|
||||
export const TextLink = (link: TelegrafTextLink): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.TextLink),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
link,
|
||||
} as TextLinkOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextLink),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [link]),
|
||||
);
|
||||
};
|
||||
|
@ -1,25 +1,19 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafTextMention } from '../../telegraf.types';
|
||||
|
||||
export interface TextMentionOptions {
|
||||
mention: TelegrafTextMention;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with text_mention entity.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=telegraf-textlink
|
||||
*/
|
||||
export const TetxMention = (mention: TelegrafTextMention): MethodDecorator => {
|
||||
export const TextMention = (mention: TelegrafTextMention): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.TextMention),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
mention,
|
||||
} as TextMentionOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.TextMention),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [mention]),
|
||||
);
|
||||
};
|
||||
|
@ -1,15 +1,11 @@
|
||||
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
||||
import {
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
} from '../../telegraf.constants';
|
||||
import { ListenerType } from '../../enums/listener-type.enum';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
import { TelegrafUrl } from '../../telegraf.types';
|
||||
|
||||
export interface UrlOptions {
|
||||
url: TelegrafUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers middleware for handling messages with url entity.
|
||||
*
|
||||
@ -17,9 +13,7 @@ export interface UrlOptions {
|
||||
*/
|
||||
export const Url = (url: TelegrafUrl): MethodDecorator => {
|
||||
return applyDecorators(
|
||||
SetMetadata(UPDATE_LISTENER_TYPE_METADATA, ListenerType.Url),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, {
|
||||
url,
|
||||
} as UrlOptions),
|
||||
SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Url),
|
||||
SetMetadata(UPDATE_LISTENER_OPTIONS_METADATA, [url]),
|
||||
);
|
||||
};
|
||||
|
12
lib/decorators/listeners/use.decorator.ts
Normal file
12
lib/decorators/listeners/use.decorator.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UPDATE_LISTENER_METHOD_METADATA } from '../../telegraf.constants';
|
||||
import { ListenerMethod } from '../../enums';
|
||||
|
||||
/**
|
||||
* Registers a middleware.
|
||||
*
|
||||
* @see https://telegraf.js.org/#/?id=use
|
||||
*/
|
||||
export const Use = (): MethodDecorator => {
|
||||
return SetMetadata(UPDATE_LISTENER_METHOD_METADATA, ListenerMethod.Use);
|
||||
};
|
@ -1 +1 @@
|
||||
export * from './listener-type.enum';
|
||||
export * from './listener-menthod.enum';
|
||||
|
@ -1,4 +1,4 @@
|
||||
export enum ListenerType {
|
||||
export enum ListenerMethod {
|
||||
Use = 'use',
|
||||
On = 'on',
|
||||
Hears = 'hears',
|
5
lib/enums/update-paramtypes.enum.ts
Normal file
5
lib/enums/update-paramtypes.enum.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export enum UpdateParamtypes {
|
||||
CONTEXT,
|
||||
NEXT,
|
||||
MESSAGE,
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
|
||||
|
||||
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
||||
export const UPDATE_LISTENER_TYPE_METADATA = 'UPDATE_LISTENER_TYPE_METADATA';
|
||||
export const UPDATE_LISTENER_METHOD_METADATA =
|
||||
'UPDATE_LISTENER_METHOD_METADATA';
|
||||
export const UPDATE_LISTENER_OPTIONS_METADATA =
|
||||
'UPDATE_LISTENER_OPTIONS_METADATA';
|
||||
export const UPDATE_ARGS_METADATA = 'UPDATE_ARGS_METADATA';
|
||||
|
@ -1,24 +1,8 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { DiscoveryService, ModuleRef } from '@nestjs/core';
|
||||
import { DiscoveryService } from '@nestjs/core';
|
||||
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
||||
import { TelegrafMetadataAccessor } from './telegraf.metadata-accessor';
|
||||
import { TelegrafProvider } from './telegraf.provider';
|
||||
import { ListenerType } from './enums';
|
||||
import {
|
||||
ActionOptions,
|
||||
CashtagOptions,
|
||||
CommandOptions,
|
||||
EmailOptions,
|
||||
HashtagOptions,
|
||||
HearsOptions,
|
||||
InlineQueryOptions,
|
||||
MentionOptions,
|
||||
OnOptions,
|
||||
PhoneOptions,
|
||||
TextLinkOptions,
|
||||
TextMentionOptions,
|
||||
UrlOptions,
|
||||
} from './decorators';
|
||||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
||||
|
||||
@Injectable()
|
||||
@ -35,7 +19,7 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
}
|
||||
|
||||
explore(): void {
|
||||
const updateClasses = this.filterUpdateClass();
|
||||
const updateClasses = this.filterUpdateClasses();
|
||||
|
||||
updateClasses.forEach((wrapper) => {
|
||||
const { instance } = wrapper;
|
||||
@ -49,7 +33,7 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
});
|
||||
}
|
||||
|
||||
private filterUpdateClass(): InstanceWrapper[] {
|
||||
private filterUpdateClasses(): InstanceWrapper[] {
|
||||
return this.discoveryService
|
||||
.getProviders()
|
||||
.filter((wrapper) => wrapper.instance)
|
||||
@ -65,97 +49,12 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
const methodRef = instance[methodKey];
|
||||
const middlewareFn = methodRef.bind(instance);
|
||||
|
||||
const listenerType = this.metadataAccessor.getListenerType(methodRef);
|
||||
if (!listenerType) return;
|
||||
const listenerMethod = this.metadataAccessor.getListenerMethod(methodRef);
|
||||
if (!listenerMethod) return;
|
||||
|
||||
const listenerOptions = this.metadataAccessor.getListenerOptions(methodRef);
|
||||
|
||||
switch (listenerType) {
|
||||
case ListenerType.On: {
|
||||
const { updateTypes } = listenerOptions as OnOptions;
|
||||
this.telegraf.on(updateTypes, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Use: {
|
||||
this.telegraf.use(middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Start: {
|
||||
this.telegraf.start(middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Help: {
|
||||
this.telegraf.help(middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Settings: {
|
||||
this.telegraf.settings(middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Hears: {
|
||||
const { triggers } = listenerOptions as HearsOptions;
|
||||
this.telegraf.hears(triggers, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Command: {
|
||||
const { command } = listenerOptions as CommandOptions;
|
||||
this.telegraf.command(command, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Action: {
|
||||
const { triggers } = listenerOptions as ActionOptions;
|
||||
this.telegraf.action(triggers, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Mention: {
|
||||
const { mention } = listenerOptions as MentionOptions;
|
||||
this.telegraf.mention(mention, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Phone: {
|
||||
const { phone } = listenerOptions as PhoneOptions;
|
||||
this.telegraf.phone(phone, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Hashtag: {
|
||||
const { hashtag } = listenerOptions as HashtagOptions;
|
||||
this.telegraf.hashtag(hashtag, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Cashtag: {
|
||||
const { cashtag } = listenerOptions as CashtagOptions;
|
||||
this.telegraf.cashtag(cashtag, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Email: {
|
||||
const { email } = listenerOptions as EmailOptions;
|
||||
this.telegraf.email(email, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.Url: {
|
||||
const { url } = listenerOptions as UrlOptions;
|
||||
this.telegraf.url(url, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.TextLink: {
|
||||
const { link } = listenerOptions as TextLinkOptions;
|
||||
this.telegraf.textLink(link, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.TextMention: {
|
||||
const { mention } = listenerOptions as TextMentionOptions;
|
||||
this.telegraf.textMention(mention, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.InlineQuery: {
|
||||
const { triggers } = listenerOptions as InlineQueryOptions;
|
||||
this.telegraf.inlineQuery(triggers, middlewareFn);
|
||||
break;
|
||||
}
|
||||
case ListenerType.GameQuery: {
|
||||
this.telegraf.gameQuery(middlewareFn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// NOTE: Disable spread operator checking because of error: "Expected at least 1 arguments, but got 1 or more."
|
||||
(this.telegraf as any)[listenerMethod](...listenerOptions, middlewareFn);
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,25 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import {
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
UPDATE_LISTENER_METHOD_METADATA,
|
||||
UPDATE_METADATA,
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
} from './telegraf.constants';
|
||||
import { ListenerType } from './enums';
|
||||
import { ListenerMethod } from './enums';
|
||||
|
||||
@Injectable()
|
||||
export class TelegrafMetadataAccessor {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
|
||||
isUpdate(target: Function): boolean {
|
||||
return !!this.reflector.get<true | undefined>(UPDATE_METADATA, target);
|
||||
return !!this.reflector.get(UPDATE_METADATA, target);
|
||||
}
|
||||
|
||||
getListenerType(target: Function): ListenerType | undefined {
|
||||
return this.reflector.get<ListenerType>(
|
||||
UPDATE_LISTENER_TYPE_METADATA,
|
||||
target,
|
||||
);
|
||||
getListenerMethod(target: Function): ListenerMethod | undefined {
|
||||
return this.reflector.get(UPDATE_LISTENER_METHOD_METADATA, target);
|
||||
}
|
||||
|
||||
getListenerOptions(target: Function): unknown | undefined {
|
||||
return this.reflector.get<unknown>(
|
||||
UPDATE_LISTENER_OPTIONS_METADATA,
|
||||
target,
|
||||
);
|
||||
getListenerOptions(target: Function): unknown[] {
|
||||
return this.reflector.get(UPDATE_LISTENER_OPTIONS_METADATA, target) || [];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user