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